Andrea Campanella
Committed by Gerrit Code Review

ONOS-4121 Restricting hello searching in replies and fixing closed connection bug

Change-Id: I1459ae1e0f4489e74c3bb52f80622690bbdba533
...@@ -53,7 +53,7 @@ public class NetconfSessionImpl implements NetconfSession { ...@@ -53,7 +53,7 @@ public class NetconfSessionImpl implements NetconfSession {
53 private static final int CONNECTION_TIMEOUT = 0; 53 private static final int CONNECTION_TIMEOUT = 0;
54 private static final String ENDPATTERN = "]]>]]>"; 54 private static final String ENDPATTERN = "]]>]]>";
55 private static final String MESSAGE_ID_STRING = "message-id"; 55 private static final String MESSAGE_ID_STRING = "message-id";
56 - private static final String HELLO = "hello"; 56 + private static final String HELLO = "<hello";
57 private static final String NEW_LINE = "\n"; 57 private static final String NEW_LINE = "\n";
58 private static final int FUTURE_REPLY_TIMEOUT = 5000; 58 private static final int FUTURE_REPLY_TIMEOUT = 5000;
59 private static final String ERROR = "ERROR "; 59 private static final String ERROR = "ERROR ";
...@@ -131,7 +131,7 @@ public class NetconfSessionImpl implements NetconfSession { ...@@ -131,7 +131,7 @@ public class NetconfSessionImpl implements NetconfSession {
131 this.addDeviceOutputListener(new NetconfDeviceOutputEventListenerImpl(deviceInfo)); 131 this.addDeviceOutputListener(new NetconfDeviceOutputEventListenerImpl(deviceInfo));
132 sendHello(); 132 sendHello();
133 } catch (IOException e) { 133 } catch (IOException e) {
134 - log.error("Failed to create ch.ethz.ssh2.Session session:", e); 134 + log.error("Failed to create ch.ethz.ssh2.Session session." + e.getMessage());
135 throw new NetconfException("Failed to create ch.ethz.ssh2.Session session with device" + 135 throw new NetconfException("Failed to create ch.ethz.ssh2.Session session with device" +
136 deviceInfo, e); 136 deviceInfo, e);
137 } 137 }
......
...@@ -44,7 +44,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -44,7 +44,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
44 44
45 private static final Logger log = LoggerFactory 45 private static final Logger log = LoggerFactory
46 .getLogger(NetconfStreamThread.class); 46 .getLogger(NetconfStreamThread.class);
47 - private static final String HELLO = "hello"; 47 + private static final String HELLO = "<hello";
48 private static final String END_PATTERN = "]]>]]>"; 48 private static final String END_PATTERN = "]]>]]>";
49 private static final String RPC_REPLY = "rpc-reply"; 49 private static final String RPC_REPLY = "rpc-reply";
50 private static final String RPC_ERROR = "rpc-error"; 50 private static final String RPC_ERROR = "rpc-error";
...@@ -87,23 +87,23 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -87,23 +87,23 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
87 @Override 87 @Override
88 NetconfMessageState evaluateChar(char c) { 88 NetconfMessageState evaluateChar(char c) {
89 if (c == ']') { 89 if (c == ']') {
90 - return FIRST_BRAKET; 90 + return FIRST_BRACKET;
91 } else { 91 } else {
92 return this; 92 return this;
93 } 93 }
94 } 94 }
95 }, 95 },
96 - FIRST_BRAKET { 96 + FIRST_BRACKET {
97 @Override 97 @Override
98 NetconfMessageState evaluateChar(char c) { 98 NetconfMessageState evaluateChar(char c) {
99 if (c == ']') { 99 if (c == ']') {
100 - return SECOND_BRAKET; 100 + return SECOND_BRACKET;
101 } else { 101 } else {
102 return NO_MATCHING_PATTERN; 102 return NO_MATCHING_PATTERN;
103 } 103 }
104 } 104 }
105 }, 105 },
106 - SECOND_BRAKET { 106 + SECOND_BRACKET {
107 @Override 107 @Override
108 NetconfMessageState evaluateChar(char c) { 108 NetconfMessageState evaluateChar(char c) {
109 if (c == '>') { 109 if (c == '>') {
...@@ -117,13 +117,13 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -117,13 +117,13 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
117 @Override 117 @Override
118 NetconfMessageState evaluateChar(char c) { 118 NetconfMessageState evaluateChar(char c) {
119 if (c == ']') { 119 if (c == ']') {
120 - return THIRD_BRAKET; 120 + return THIRD_BRACKET;
121 } else { 121 } else {
122 return NO_MATCHING_PATTERN; 122 return NO_MATCHING_PATTERN;
123 } 123 }
124 } 124 }
125 }, 125 },
126 - THIRD_BRAKET { 126 + THIRD_BRACKET {
127 @Override 127 @Override
128 NetconfMessageState evaluateChar(char c) { 128 NetconfMessageState evaluateChar(char c) {
129 if (c == ']') { 129 if (c == ']') {
...@@ -163,6 +163,12 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -163,6 +163,12 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
163 if (cInt == -1) { 163 if (cInt == -1) {
164 log.debug("Netconf device {} sent error char in session," + 164 log.debug("Netconf device {} sent error char in session," +
165 " will need to be reopend", netconfDeviceInfo); 165 " will need to be reopend", netconfDeviceInfo);
166 + NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
167 + NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
168 + null, null, Optional.of(-1), netconfDeviceInfo);
169 + netconfDeviceEventListeners.forEach(
170 + listener -> listener.event(event));
171 + socketClosed = true;
166 } 172 }
167 char c = (char) cInt; 173 char c = (char) cInt;
168 state = state.evaluateChar(c); 174 state = state.evaluateChar(c);
...@@ -210,12 +216,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -210,12 +216,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
210 } 216 }
211 217
212 private static Optional<Integer> getMsgId(String reply) { 218 private static Optional<Integer> getMsgId(String reply) {
213 - if (reply.contains(HELLO)) { 219 + if (reply.contains(MESSAGE_ID)) {
214 - return Optional.of(0);
215 - }
216 - if (reply.contains(RPC_ERROR) && !reply.contains(MESSAGE_ID)) {
217 - return Optional.empty();
218 - }
219 String[] outer = reply.split(MESSAGE_ID); 220 String[] outer = reply.split(MESSAGE_ID);
220 Preconditions.checkArgument(outer.length != 1, 221 Preconditions.checkArgument(outer.length != 1,
221 "Error in retrieving the message id"); 222 "Error in retrieving the message id");
...@@ -223,6 +224,10 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -223,6 +224,10 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
223 Preconditions.checkNotNull(Integer.parseInt(messageID), 224 Preconditions.checkNotNull(Integer.parseInt(messageID),
224 "Error in retrieving the message id"); 225 "Error in retrieving the message id");
225 return Optional.of(Integer.parseInt(messageID)); 226 return Optional.of(Integer.parseInt(messageID));
227 + } else if (reply.contains(HELLO)) {
228 + return Optional.of(0);
229 + }
230 + return Optional.empty();
226 } 231 }
227 232
228 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) { 233 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
......