Andrea Campanella
Committed by Gerrit Code Review

ONOS-3839 Fixing errors in sending requests and hanging on future.join

Change-Id: I6da5bf1ff728efeb0d531cf7f04f6bf49f11a0a9
...@@ -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 }
......
...@@ -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) {
......