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
Preconditions.checkNotNull(controller, "Netconf controller is null");
List<ControllerInfo> controllers = new ArrayList<>();
try {
String reply = controller.getDevicesMap().get(ofDeviceId).getSession().
getConfig("running");
log.debug("Reply XML {}", reply);
controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
loadXml(new ByteArrayInputStream(controller.
getDevicesMap().get(ofDeviceId).getSession().
getConfig("running").getBytes(StandardCharsets.UTF_8)))));
loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
} catch (IOException e) {
log.error("Cannot comunicate to device {} ", ofDeviceId);
}
......@@ -70,16 +71,15 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour
Preconditions.checkNotNull(controller, "Netconf controller is null");
try {
NetconfDevice device = controller.getNetconfDevice(deviceId);
log.warn("provider map {}", controller.getDevicesMap());
String config = null;
try {
String reply = device.getSession().getConfig("running");
log.info("reply XML {}", reply);
config = XmlConfigParser.createControllersConfig(
XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
XmlConfigParser.loadXml(
new ByteArrayInputStream(device.getSession()
.getConfig("running")
.getBytes(
StandardCharsets.UTF_8))),
new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))),
"running", "merge", "create", controllers
);
} catch (IOException e) {
......
......@@ -91,14 +91,26 @@ public final class NetconfDeviceOutputEvent extends
this.messageID = msgID;
}
/**
* return the message payload of the reply form the device.
* @return reply
*/
public String getMessagePayload() {
return messagePayload;
}
/**
* Event-related device information.
* @return information about the device
*/
public NetconfDeviceInfo getDeviceInfo() {
return deviceInfo;
}
/**
* Reply messageId.
* @return messageId
*/
public Integer getMessageID() {
return messageID;
}
......
......@@ -49,17 +49,20 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
private static final String RPC_ERROR = "rpc-error";
private static final String NOTIFICATION_LABEL = "<notification>";
private static PrintWriter outputStream;
private static NetconfDeviceInfo netconfDeviceInfo;
private static NetconfSessionDelegate sessionDelegate;
private static NetconfMessageState state;
private static List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
private PrintWriter outputStream;
private final InputStream err;
private final InputStream in;
private NetconfDeviceInfo netconfDeviceInfo;
private NetconfSessionDelegate sessionDelegate;
private NetconfMessageState state;
private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
= Lists.newArrayList();
public NetconfStreamThread(final InputStream in, final OutputStream out,
final InputStream err, NetconfDeviceInfo deviceInfo,
NetconfSessionDelegate delegate) {
super(handler(in, err));
this.in = in;
this.err = err;
outputStream = new PrintWriter(out);
netconfDeviceInfo = deviceInfo;
state = NetconfMessageState.NO_MATCHING_PATTERN;
......@@ -70,6 +73,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
@Override
public CompletableFuture<String> sendMessage(String request) {
log.debug("Sending message {} to device {}", request, netconfDeviceInfo);
outputStream.print(request);
outputStream.flush();
return new CompletableFuture<>();
......@@ -147,9 +151,8 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
abstract NetconfMessageState evaluateChar(char c);
}
private static Runnable handler(final InputStream in, final InputStream err) {
public void run() {
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
return () -> {
try {
boolean socketClosed = false;
StringBuilder deviceReplyBuilder = new StringBuilder();
......@@ -157,6 +160,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
int cInt = bufferReader.read();
if (cInt == -1) {
socketClosed = true;
log.debug("char {} " + bufferReader.read());
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
null, null, -1, netconfDeviceInfo);
......@@ -167,27 +171,36 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
state = state.evaluateChar(c);
deviceReplyBuilder.append(c);
if (state == NetconfMessageState.END_PATTERN) {
String deviceReply = deviceReplyBuilder.toString()
.replace(END_PATTERN, "");
if (deviceReply.contains(RPC_REPLY) ||
deviceReply.contains(RPC_ERROR) ||
deviceReply.contains(HELLO)) {
String deviceReply = deviceReplyBuilder.toString();
if (deviceReply.equals(END_PATTERN)) {
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
sessionDelegate.notify(event);
NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
null, null, -1, netconfDeviceInfo);
netconfDeviceEventListeners.forEach(
listener -> listener.event(event));
} else if (deviceReply.contains(NOTIFICATION_LABEL)) {
final String finalDeviceReply = deviceReply;
netconfDeviceEventListeners.forEach(
listener -> listener.event(new NetconfDeviceOutputEvent(
NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
null, finalDeviceReply, getMsgId(finalDeviceReply), netconfDeviceInfo)));
} else {
log.info("Error on replay from device {} ", deviceReply);
deviceReply = deviceReply.replace(END_PATTERN, "");
if (deviceReply.contains(RPC_REPLY) ||
deviceReply.contains(RPC_ERROR) ||
deviceReply.contains(HELLO)) {
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
sessionDelegate.notify(event);
netconfDeviceEventListeners.forEach(
listener -> listener.event(event));
} else if (deviceReply.contains(NOTIFICATION_LABEL)) {
final String finalDeviceReply = deviceReply;
netconfDeviceEventListeners.forEach(
listener -> listener.event(new NetconfDeviceOutputEvent(
NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
null, finalDeviceReply, getMsgId(finalDeviceReply),
netconfDeviceInfo)));
} else {
log.info("Error on replay from device {} ", deviceReply);
}
deviceReplyBuilder.setLength(0);
}
deviceReplyBuilder.setLength(0);
}
}
} catch (IOException e) {
......@@ -196,7 +209,6 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
netconfDeviceInfo, e));
//TODO should we send a socket closed message to listeners ?
}
};
}
private static int getMsgId(String reply) {
......