Committed by
Gerrit Code Review
ONOS-3839 Fixing errors in sending requests and hanging on future.join
Change-Id: I6da5bf1ff728efeb0d531cf7f04f6bf49f11a0a9
Showing
4 changed files
with
143 additions
and
73 deletions
... | @@ -52,10 +52,11 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour | ... | @@ -52,10 +52,11 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour |
52 | Preconditions.checkNotNull(controller, "Netconf controller is null"); | 52 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
53 | List<ControllerInfo> controllers = new ArrayList<>(); | 53 | List<ControllerInfo> controllers = new ArrayList<>(); |
54 | try { | 54 | try { |
55 | + String reply = controller.getDevicesMap().get(ofDeviceId).getSession(). | ||
56 | + getConfig("running"); | ||
57 | + log.debug("Reply XML {}", reply); | ||
55 | controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser. | 58 | controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser. |
56 | - loadXml(new ByteArrayInputStream(controller. | 59 | + loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); |
57 | - getDevicesMap().get(ofDeviceId).getSession(). | ||
58 | - getConfig("running").getBytes(StandardCharsets.UTF_8))))); | ||
59 | } catch (IOException e) { | 60 | } catch (IOException e) { |
60 | log.error("Cannot comunicate to device {} ", ofDeviceId); | 61 | log.error("Cannot comunicate to device {} ", ofDeviceId); |
61 | } | 62 | } |
... | @@ -70,16 +71,15 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour | ... | @@ -70,16 +71,15 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour |
70 | Preconditions.checkNotNull(controller, "Netconf controller is null"); | 71 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
71 | try { | 72 | try { |
72 | NetconfDevice device = controller.getNetconfDevice(deviceId); | 73 | NetconfDevice device = controller.getNetconfDevice(deviceId); |
73 | - log.warn("provider map {}", controller.getDevicesMap()); | ||
74 | String config = null; | 74 | String config = null; |
75 | + | ||
75 | try { | 76 | try { |
77 | + String reply = device.getSession().getConfig("running"); | ||
78 | + log.info("reply XML {}", reply); | ||
76 | config = XmlConfigParser.createControllersConfig( | 79 | config = XmlConfigParser.createControllersConfig( |
77 | XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")), | 80 | XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")), |
78 | XmlConfigParser.loadXml( | 81 | XmlConfigParser.loadXml( |
79 | - new ByteArrayInputStream(device.getSession() | 82 | + new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))), |
80 | - .getConfig("running") | ||
81 | - .getBytes( | ||
82 | - StandardCharsets.UTF_8))), | ||
83 | "running", "merge", "create", controllers | 83 | "running", "merge", "create", controllers |
84 | ); | 84 | ); |
85 | } catch (IOException e) { | 85 | } catch (IOException e) { | ... | ... |
... | @@ -91,14 +91,26 @@ public final class NetconfDeviceOutputEvent extends | ... | @@ -91,14 +91,26 @@ public final class NetconfDeviceOutputEvent extends |
91 | this.messageID = msgID; | 91 | this.messageID = msgID; |
92 | } | 92 | } |
93 | 93 | ||
94 | + /** | ||
95 | + * return the message payload of the reply form the device. | ||
96 | + * @return reply | ||
97 | + */ | ||
94 | public String getMessagePayload() { | 98 | public String getMessagePayload() { |
95 | return messagePayload; | 99 | return messagePayload; |
96 | } | 100 | } |
97 | 101 | ||
102 | + /** | ||
103 | + * Event-related device information. | ||
104 | + * @return information about the device | ||
105 | + */ | ||
98 | public NetconfDeviceInfo getDeviceInfo() { | 106 | public NetconfDeviceInfo getDeviceInfo() { |
99 | return deviceInfo; | 107 | return deviceInfo; |
100 | } | 108 | } |
101 | 109 | ||
110 | + /** | ||
111 | + * Reply messageId. | ||
112 | + * @return messageId | ||
113 | + */ | ||
102 | public Integer getMessageID() { | 114 | public Integer getMessageID() { |
103 | return messageID; | 115 | return messageID; |
104 | } | 116 | } | ... | ... |
... | @@ -28,12 +28,14 @@ import org.slf4j.Logger; | ... | @@ -28,12 +28,14 @@ import org.slf4j.Logger; |
28 | import org.slf4j.LoggerFactory; | 28 | import org.slf4j.LoggerFactory; |
29 | 29 | ||
30 | import java.io.IOException; | 30 | import java.io.IOException; |
31 | -import java.io.PrintWriter; | ||
32 | import java.util.Collections; | 31 | import java.util.Collections; |
33 | import java.util.HashMap; | 32 | import java.util.HashMap; |
34 | import java.util.List; | 33 | import java.util.List; |
35 | import java.util.Map; | 34 | import java.util.Map; |
36 | import java.util.concurrent.CompletableFuture; | 35 | import java.util.concurrent.CompletableFuture; |
36 | +import java.util.concurrent.ExecutionException; | ||
37 | +import java.util.concurrent.TimeUnit; | ||
38 | +import java.util.concurrent.TimeoutException; | ||
37 | import java.util.concurrent.atomic.AtomicInteger; | 39 | import java.util.concurrent.atomic.AtomicInteger; |
38 | 40 | ||
39 | 41 | ||
... | @@ -48,17 +50,22 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -48,17 +50,22 @@ public class NetconfSessionImpl implements NetconfSession { |
48 | 50 | ||
49 | private static final int CONNECTION_TIMEOUT = 0; | 51 | private static final int CONNECTION_TIMEOUT = 0; |
50 | private static final String ENDPATTERN = "]]>]]>"; | 52 | private static final String ENDPATTERN = "]]>]]>"; |
51 | - private static final AtomicInteger MESSAGE_ID_INTEGER = new AtomicInteger(0); | ||
52 | private static final String MESSAGE_ID_STRING = "message-id"; | 53 | private static final String MESSAGE_ID_STRING = "message-id"; |
53 | private static final String HELLO = "hello"; | 54 | private static final String HELLO = "hello"; |
54 | private static final String NEW_LINE = "\n"; | 55 | private static final String NEW_LINE = "\n"; |
55 | - | 56 | + private static final int FUTURE_REPLY_TIMEOUT = 5000; |
56 | - | 57 | + private static final String ERROR = "ERROR "; |
58 | + private static final String END_OF_RPC_OPEN_TAG = "\">"; | ||
59 | + private static final String EQUAL = "="; | ||
60 | + private static final String NUMBER_BETWEEN_QUOTES_MATCHER = "\"+([0-9]+)+\""; | ||
61 | + private static final String XML_HEADER = | ||
62 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | ||
63 | + | ||
64 | + private final AtomicInteger messageIdInteger = new AtomicInteger(0); | ||
57 | private Connection netconfConnection; | 65 | private Connection netconfConnection; |
58 | private NetconfDeviceInfo deviceInfo; | 66 | private NetconfDeviceInfo deviceInfo; |
59 | private Session sshSession; | 67 | private Session sshSession; |
60 | private boolean connectionActive; | 68 | private boolean connectionActive; |
61 | - private PrintWriter out = null; | ||
62 | private List<String> deviceCapabilities = | 69 | private List<String> deviceCapabilities = |
63 | Collections.singletonList("urn:ietf:params:netconf:base:1.0"); | 70 | Collections.singletonList("urn:ietf:params:netconf:base:1.0"); |
64 | private String serverCapabilities; | 71 | private String serverCapabilities; |
... | @@ -116,7 +123,6 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -116,7 +123,6 @@ public class NetconfSessionImpl implements NetconfSession { |
116 | try { | 123 | try { |
117 | sshSession = netconfConnection.openSession(); | 124 | sshSession = netconfConnection.openSession(); |
118 | sshSession.startSubSystem("netconf"); | 125 | sshSession.startSubSystem("netconf"); |
119 | - out = new PrintWriter(sshSession.getStdin()); | ||
120 | t = new NetconfStreamThread(sshSession.getStdout(), sshSession.getStdin(), | 126 | t = new NetconfStreamThread(sshSession.getStdout(), sshSession.getStdin(), |
121 | sshSession.getStderr(), deviceInfo, | 127 | sshSession.getStderr(), deviceInfo, |
122 | new NetconfSessionDelegateImpl()); | 128 | new NetconfSessionDelegateImpl()); |
... | @@ -130,17 +136,20 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -130,17 +136,20 @@ public class NetconfSessionImpl implements NetconfSession { |
130 | } | 136 | } |
131 | } | 137 | } |
132 | 138 | ||
133 | - private void sendHello() throws IOException { | 139 | + private void sendHello() throws NetconfException { |
134 | serverCapabilities = sendRequest(createHelloString()); | 140 | serverCapabilities = sendRequest(createHelloString()); |
135 | } | 141 | } |
136 | 142 | ||
137 | private String createHelloString() { | 143 | private String createHelloString() { |
138 | StringBuilder hellobuffer = new StringBuilder(); | 144 | StringBuilder hellobuffer = new StringBuilder(); |
139 | - hellobuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | 145 | + hellobuffer.append(XML_HEADER); |
146 | + hellobuffer.append("\n"); | ||
140 | hellobuffer.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); | 147 | hellobuffer.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
141 | hellobuffer.append(" <capabilities>\n"); | 148 | hellobuffer.append(" <capabilities>\n"); |
142 | deviceCapabilities.forEach( | 149 | deviceCapabilities.forEach( |
143 | - cap -> hellobuffer.append(" <capability>" + cap + "</capability>\n")); | 150 | + cap -> hellobuffer.append(" <capability>") |
151 | + .append(cap) | ||
152 | + .append("</capability>\n")); | ||
144 | hellobuffer.append(" </capabilities>\n"); | 153 | hellobuffer.append(" </capabilities>\n"); |
145 | hellobuffer.append("</hello>\n"); | 154 | hellobuffer.append("</hello>\n"); |
146 | hellobuffer.append(ENDPATTERN); | 155 | hellobuffer.append(ENDPATTERN); |
... | @@ -166,31 +175,62 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -166,31 +175,62 @@ public class NetconfSessionImpl implements NetconfSession { |
166 | 175 | ||
167 | @Override | 176 | @Override |
168 | public String requestSync(String request) throws NetconfException { | 177 | public String requestSync(String request) throws NetconfException { |
169 | - String reply = sendRequest(request + NEW_LINE + ENDPATTERN); | 178 | + if (!request.contains(ENDPATTERN)) { |
170 | - return checkReply(reply) ? reply : "ERROR " + reply; | 179 | + request = request + NEW_LINE + ENDPATTERN; |
180 | + } | ||
181 | + String reply = sendRequest(request); | ||
182 | + return checkReply(reply) ? reply : ERROR + reply; | ||
171 | } | 183 | } |
172 | 184 | ||
173 | @Override | 185 | @Override |
174 | public CompletableFuture<String> request(String request) { | 186 | public CompletableFuture<String> request(String request) { |
175 | CompletableFuture<String> ftrep = t.sendMessage(request); | 187 | CompletableFuture<String> ftrep = t.sendMessage(request); |
176 | - replies.put(MESSAGE_ID_INTEGER.get(), ftrep); | 188 | + replies.put(messageIdInteger.get(), ftrep); |
177 | return ftrep; | 189 | return ftrep; |
178 | } | 190 | } |
179 | 191 | ||
180 | private String sendRequest(String request) throws NetconfException { | 192 | private String sendRequest(String request) throws NetconfException { |
181 | checkAndRestablishSession(); | 193 | checkAndRestablishSession(); |
182 | - //FIXME find out a better way to enforce the presence of message-id | 194 | + request = formatRequestMessageId(request); |
183 | - if (!request.contains(MESSAGE_ID_STRING) && !request.contains(HELLO)) { | 195 | + request = formatXmlHeader(request); |
184 | - request = request.replaceFirst("\">", "\" message-id=\"" | ||
185 | - + MESSAGE_ID_INTEGER.get() + "\"" + ">"); | ||
186 | - } | ||
187 | CompletableFuture<String> futureReply = request(request); | 196 | CompletableFuture<String> futureReply = request(request); |
188 | - MESSAGE_ID_INTEGER.incrementAndGet(); | 197 | + String rp; |
189 | - String rp = futureReply.join(); | 198 | + try { |
190 | - log.debug("Reply from device {}", rp); | 199 | + rp = futureReply.get(FUTURE_REPLY_TIMEOUT, TimeUnit.MILLISECONDS); |
200 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { | ||
201 | + //replies.remove(messageIdInteger.get()); | ||
202 | + throw new NetconfException("Can't get the reply for request" + request, e); | ||
203 | + } | ||
204 | +// String rp = Tools.futureGetOrElse(futureReply, FUTURE_REPLY_TIMEOUT, TimeUnit.MILLISECONDS, | ||
205 | +// "Error in completing the request with message-id " + | ||
206 | +// messageIdInteger.get() + | ||
207 | +// ": future timed out."); | ||
208 | + messageIdInteger.incrementAndGet(); | ||
209 | + log.debug("Result {} from request {} to device {}", rp, request, deviceInfo); | ||
191 | return rp; | 210 | return rp; |
192 | } | 211 | } |
193 | 212 | ||
213 | + private String formatRequestMessageId(String request) { | ||
214 | + if (request.contains(MESSAGE_ID_STRING)) { | ||
215 | + //FIXME if application provieds his own counting of messages this fails that count | ||
216 | + request = request.replaceFirst(MESSAGE_ID_STRING + EQUAL + NUMBER_BETWEEN_QUOTES_MATCHER, | ||
217 | + MESSAGE_ID_STRING + EQUAL + "\"" + messageIdInteger.get() + "\""); | ||
218 | + } else if (!request.contains(MESSAGE_ID_STRING) && !request.contains(HELLO)) { | ||
219 | + //FIXME find out a better way to enforce the presence of message-id | ||
220 | + request = request.replaceFirst(END_OF_RPC_OPEN_TAG, "\" " + MESSAGE_ID_STRING + EQUAL + "\"" | ||
221 | + + messageIdInteger.get() + "\"" + ">"); | ||
222 | + } | ||
223 | + return request; | ||
224 | + } | ||
225 | + | ||
226 | + private String formatXmlHeader(String request) { | ||
227 | + if (!request.contains(XML_HEADER)) { | ||
228 | + //FIXME if application provieds his own XML header of different type there is a clash | ||
229 | + request = XML_HEADER + "\n" + request; | ||
230 | + } | ||
231 | + return request; | ||
232 | + } | ||
233 | + | ||
194 | @Override | 234 | @Override |
195 | public String get(String request) throws NetconfException { | 235 | public String get(String request) throws NetconfException { |
196 | return requestSync(request); | 236 | return requestSync(request); |
... | @@ -203,16 +243,21 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -203,16 +243,21 @@ public class NetconfSessionImpl implements NetconfSession { |
203 | 243 | ||
204 | @Override | 244 | @Override |
205 | public String getConfig(String targetConfiguration, String configurationSchema) throws NetconfException { | 245 | public String getConfig(String targetConfiguration, String configurationSchema) throws NetconfException { |
206 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); | 246 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
207 | - rpc.append("<rpc message-id=\"" + MESSAGE_ID_INTEGER.get() + "\" " | 247 | + rpc.append("<rpc "); |
208 | - + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); | 248 | + rpc.append(MESSAGE_ID_STRING); |
249 | + rpc.append(EQUAL); | ||
250 | + rpc.append("\""); | ||
251 | + rpc.append(messageIdInteger.get()); | ||
252 | + rpc.append("\" "); | ||
253 | + rpc.append("xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); | ||
209 | rpc.append("<get-config>\n"); | 254 | rpc.append("<get-config>\n"); |
210 | rpc.append("<source>\n"); | 255 | rpc.append("<source>\n"); |
211 | - rpc.append("<" + targetConfiguration + "/>"); | 256 | + rpc.append("<").append(targetConfiguration).append("/>"); |
212 | rpc.append("</source>"); | 257 | rpc.append("</source>"); |
213 | if (configurationSchema != null) { | 258 | if (configurationSchema != null) { |
214 | rpc.append("<filter type=\"subtree\">\n"); | 259 | rpc.append("<filter type=\"subtree\">\n"); |
215 | - rpc.append(configurationSchema + "\n"); | 260 | + rpc.append(configurationSchema).append("\n"); |
216 | rpc.append("</filter>\n"); | 261 | rpc.append("</filter>\n"); |
217 | } | 262 | } |
218 | rpc.append("</get-config>\n"); | 263 | rpc.append("</get-config>\n"); |
... | @@ -232,12 +277,17 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -232,12 +277,17 @@ public class NetconfSessionImpl implements NetconfSession { |
232 | public boolean editConfig(String targetConfiguration, String mode, String newConfiguration) | 277 | public boolean editConfig(String targetConfiguration, String mode, String newConfiguration) |
233 | throws NetconfException { | 278 | throws NetconfException { |
234 | newConfiguration = newConfiguration.trim(); | 279 | newConfiguration = newConfiguration.trim(); |
235 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); | 280 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
236 | - rpc.append("<rpc message-id=\"" + MESSAGE_ID_INTEGER.get() + "\" " | 281 | + rpc.append("<rpc "); |
237 | - + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); | 282 | + rpc.append(MESSAGE_ID_STRING); |
283 | + rpc.append(EQUAL); | ||
284 | + rpc.append("\""); | ||
285 | + rpc.append(messageIdInteger.get()); | ||
286 | + rpc.append("\" "); | ||
287 | + rpc.append("xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); | ||
238 | rpc.append("<edit-config>"); | 288 | rpc.append("<edit-config>"); |
239 | rpc.append("<target>"); | 289 | rpc.append("<target>"); |
240 | - rpc.append("<" + targetConfiguration + "/>"); | 290 | + rpc.append("<").append(targetConfiguration).append("/>"); |
241 | rpc.append("</target>"); | 291 | rpc.append("</target>"); |
242 | rpc.append("<default-operation>"); | 292 | rpc.append("<default-operation>"); |
243 | rpc.append(mode); | 293 | rpc.append(mode); |
... | @@ -259,15 +309,14 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -259,15 +309,14 @@ public class NetconfSessionImpl implements NetconfSession { |
259 | newConfiguration = "<configuration>" + newConfiguration | 309 | newConfiguration = "<configuration>" + newConfiguration |
260 | + "</configuration>"; | 310 | + "</configuration>"; |
261 | } | 311 | } |
262 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" " + | 312 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
263 | - "encoding=\"UTF-8\"?>"); | ||
264 | rpc.append("<rpc>"); | 313 | rpc.append("<rpc>"); |
265 | rpc.append("<copy-config>"); | 314 | rpc.append("<copy-config>"); |
266 | rpc.append("<target>"); | 315 | rpc.append("<target>"); |
267 | - rpc.append("<" + targetConfiguration + "/>"); | 316 | + rpc.append("<").append(targetConfiguration).append("/>"); |
268 | rpc.append("</target>"); | 317 | rpc.append("</target>"); |
269 | rpc.append("<source>"); | 318 | rpc.append("<source>"); |
270 | - rpc.append("<" + newConfiguration + "/>"); | 319 | + rpc.append("<").append(newConfiguration).append("/>"); |
271 | rpc.append("</source>"); | 320 | rpc.append("</source>"); |
272 | rpc.append("</copy-config>"); | 321 | rpc.append("</copy-config>"); |
273 | rpc.append("</rpc>"); | 322 | rpc.append("</rpc>"); |
... | @@ -282,12 +331,11 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -282,12 +331,11 @@ public class NetconfSessionImpl implements NetconfSession { |
282 | targetConfiguration); | 331 | targetConfiguration); |
283 | return false; | 332 | return false; |
284 | } | 333 | } |
285 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" " + | 334 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
286 | - "encoding=\"UTF-8\"?>"); | ||
287 | rpc.append("<rpc>"); | 335 | rpc.append("<rpc>"); |
288 | rpc.append("<delete-config>"); | 336 | rpc.append("<delete-config>"); |
289 | rpc.append("<target>"); | 337 | rpc.append("<target>"); |
290 | - rpc.append("<" + targetConfiguration + "/>"); | 338 | + rpc.append("<").append(targetConfiguration).append("/>"); |
291 | rpc.append("</target>"); | 339 | rpc.append("</target>"); |
292 | rpc.append("</delete-config>"); | 340 | rpc.append("</delete-config>"); |
293 | rpc.append("</rpc>"); | 341 | rpc.append("</rpc>"); |
... | @@ -297,8 +345,7 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -297,8 +345,7 @@ public class NetconfSessionImpl implements NetconfSession { |
297 | 345 | ||
298 | @Override | 346 | @Override |
299 | public boolean lock() throws NetconfException { | 347 | public boolean lock() throws NetconfException { |
300 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" " + | 348 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
301 | - "encoding=\"UTF-8\"?>"); | ||
302 | rpc.append("<rpc>"); | 349 | rpc.append("<rpc>"); |
303 | rpc.append("<lock>"); | 350 | rpc.append("<lock>"); |
304 | rpc.append("<target>"); | 351 | rpc.append("<target>"); |
... | @@ -312,8 +359,7 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -312,8 +359,7 @@ public class NetconfSessionImpl implements NetconfSession { |
312 | 359 | ||
313 | @Override | 360 | @Override |
314 | public boolean unlock() throws NetconfException { | 361 | public boolean unlock() throws NetconfException { |
315 | - StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" " + | 362 | + StringBuilder rpc = new StringBuilder(XML_HEADER); |
316 | - "encoding=\"UTF-8\"?>"); | ||
317 | rpc.append("<rpc>"); | 363 | rpc.append("<rpc>"); |
318 | rpc.append("<unlock>"); | 364 | rpc.append("<unlock>"); |
319 | rpc.append("<target>"); | 365 | rpc.append("<target>"); | ... | ... |
... | @@ -49,17 +49,20 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -49,17 +49,20 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
49 | private static final String RPC_ERROR = "rpc-error"; | 49 | private static final String RPC_ERROR = "rpc-error"; |
50 | private static final String NOTIFICATION_LABEL = "<notification>"; | 50 | private static final String NOTIFICATION_LABEL = "<notification>"; |
51 | 51 | ||
52 | - private static PrintWriter outputStream; | 52 | + private PrintWriter outputStream; |
53 | - private static NetconfDeviceInfo netconfDeviceInfo; | 53 | + private final InputStream err; |
54 | - private static NetconfSessionDelegate sessionDelegate; | 54 | + private final InputStream in; |
55 | - private static NetconfMessageState state; | 55 | + private NetconfDeviceInfo netconfDeviceInfo; |
56 | - private static List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners | 56 | + private NetconfSessionDelegate sessionDelegate; |
57 | + private NetconfMessageState state; | ||
58 | + private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners | ||
57 | = Lists.newArrayList(); | 59 | = Lists.newArrayList(); |
58 | 60 | ||
59 | public NetconfStreamThread(final InputStream in, final OutputStream out, | 61 | public NetconfStreamThread(final InputStream in, final OutputStream out, |
60 | final InputStream err, NetconfDeviceInfo deviceInfo, | 62 | final InputStream err, NetconfDeviceInfo deviceInfo, |
61 | NetconfSessionDelegate delegate) { | 63 | NetconfSessionDelegate delegate) { |
62 | - super(handler(in, err)); | 64 | + this.in = in; |
65 | + this.err = err; | ||
63 | outputStream = new PrintWriter(out); | 66 | outputStream = new PrintWriter(out); |
64 | netconfDeviceInfo = deviceInfo; | 67 | netconfDeviceInfo = deviceInfo; |
65 | state = NetconfMessageState.NO_MATCHING_PATTERN; | 68 | state = NetconfMessageState.NO_MATCHING_PATTERN; |
... | @@ -70,6 +73,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -70,6 +73,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
70 | 73 | ||
71 | @Override | 74 | @Override |
72 | public CompletableFuture<String> sendMessage(String request) { | 75 | public CompletableFuture<String> sendMessage(String request) { |
76 | + log.debug("Sending message {} to device {}", request, netconfDeviceInfo); | ||
73 | outputStream.print(request); | 77 | outputStream.print(request); |
74 | outputStream.flush(); | 78 | outputStream.flush(); |
75 | return new CompletableFuture<>(); | 79 | return new CompletableFuture<>(); |
... | @@ -147,9 +151,8 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -147,9 +151,8 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
147 | abstract NetconfMessageState evaluateChar(char c); | 151 | abstract NetconfMessageState evaluateChar(char c); |
148 | } | 152 | } |
149 | 153 | ||
150 | - private static Runnable handler(final InputStream in, final InputStream err) { | 154 | + public void run() { |
151 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); | 155 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); |
152 | - return () -> { | ||
153 | try { | 156 | try { |
154 | boolean socketClosed = false; | 157 | boolean socketClosed = false; |
155 | StringBuilder deviceReplyBuilder = new StringBuilder(); | 158 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
... | @@ -157,6 +160,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -157,6 +160,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
157 | int cInt = bufferReader.read(); | 160 | int cInt = bufferReader.read(); |
158 | if (cInt == -1) { | 161 | if (cInt == -1) { |
159 | socketClosed = true; | 162 | socketClosed = true; |
163 | + log.debug("char {} " + bufferReader.read()); | ||
160 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( | 164 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
161 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, | 165 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
162 | null, null, -1, netconfDeviceInfo); | 166 | null, null, -1, netconfDeviceInfo); |
... | @@ -167,27 +171,36 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -167,27 +171,36 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
167 | state = state.evaluateChar(c); | 171 | state = state.evaluateChar(c); |
168 | deviceReplyBuilder.append(c); | 172 | deviceReplyBuilder.append(c); |
169 | if (state == NetconfMessageState.END_PATTERN) { | 173 | if (state == NetconfMessageState.END_PATTERN) { |
170 | - String deviceReply = deviceReplyBuilder.toString() | 174 | + String deviceReply = deviceReplyBuilder.toString(); |
171 | - .replace(END_PATTERN, ""); | 175 | + if (deviceReply.equals(END_PATTERN)) { |
172 | - if (deviceReply.contains(RPC_REPLY) || | ||
173 | - deviceReply.contains(RPC_ERROR) || | ||
174 | - deviceReply.contains(HELLO)) { | ||
175 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( | 176 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
176 | - NetconfDeviceOutputEvent.Type.DEVICE_REPLY, | 177 | + NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
177 | - null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); | 178 | + null, null, -1, netconfDeviceInfo); |
178 | - sessionDelegate.notify(event); | ||
179 | netconfDeviceEventListeners.forEach( | 179 | netconfDeviceEventListeners.forEach( |
180 | listener -> listener.event(event)); | 180 | listener -> listener.event(event)); |
181 | - } else if (deviceReply.contains(NOTIFICATION_LABEL)) { | ||
182 | - final String finalDeviceReply = deviceReply; | ||
183 | - netconfDeviceEventListeners.forEach( | ||
184 | - listener -> listener.event(new NetconfDeviceOutputEvent( | ||
185 | - NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, | ||
186 | - null, finalDeviceReply, getMsgId(finalDeviceReply), netconfDeviceInfo))); | ||
187 | } else { | 181 | } else { |
188 | - log.info("Error on replay from device {} ", deviceReply); | 182 | + deviceReply = deviceReply.replace(END_PATTERN, ""); |
183 | + if (deviceReply.contains(RPC_REPLY) || | ||
184 | + deviceReply.contains(RPC_ERROR) || | ||
185 | + deviceReply.contains(HELLO)) { | ||
186 | + NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( | ||
187 | + NetconfDeviceOutputEvent.Type.DEVICE_REPLY, | ||
188 | + null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); | ||
189 | + sessionDelegate.notify(event); | ||
190 | + netconfDeviceEventListeners.forEach( | ||
191 | + listener -> listener.event(event)); | ||
192 | + } else if (deviceReply.contains(NOTIFICATION_LABEL)) { | ||
193 | + final String finalDeviceReply = deviceReply; | ||
194 | + netconfDeviceEventListeners.forEach( | ||
195 | + listener -> listener.event(new NetconfDeviceOutputEvent( | ||
196 | + NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, | ||
197 | + null, finalDeviceReply, getMsgId(finalDeviceReply), | ||
198 | + netconfDeviceInfo))); | ||
199 | + } else { | ||
200 | + log.info("Error on replay from device {} ", deviceReply); | ||
201 | + } | ||
202 | + deviceReplyBuilder.setLength(0); | ||
189 | } | 203 | } |
190 | - deviceReplyBuilder.setLength(0); | ||
191 | } | 204 | } |
192 | } | 205 | } |
193 | } catch (IOException e) { | 206 | } catch (IOException e) { |
... | @@ -196,7 +209,6 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler | ... | @@ -196,7 +209,6 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler |
196 | netconfDeviceInfo, e)); | 209 | netconfDeviceInfo, e)); |
197 | //TODO should we send a socket closed message to listeners ? | 210 | //TODO should we send a socket closed message to listeners ? |
198 | } | 211 | } |
199 | - }; | ||
200 | } | 212 | } |
201 | 213 | ||
202 | private static int getMsgId(String reply) { | 214 | private static int getMsgId(String reply) { | ... | ... |
-
Please register or login to post a comment