use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class NetconfStreamThread method run.
@Override
public void run() {
BufferedReader bufferReader = null;
while (bufferReader == null) {
try {
bufferReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
try {
boolean socketClosed = false;
StringBuilder deviceReplyBuilder = new StringBuilder();
while (!socketClosed && !this.isInterrupted()) {
int cInt = bufferReader.read();
if (cInt == -1) {
log.debug("Netconf device {} sent error char in session," + " will need to be reopened", netconfDeviceInfo);
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.SESSION_CLOSED, null, null, Optional.of(-1), netconfDeviceInfo);
netconfDeviceEventListeners.forEach(listener -> listener.event(event));
socketClosed = true;
log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo);
}
char c = (char) cInt;
state = state.evaluateChar(c);
deviceReplyBuilder.append(c);
if (state == NetconfMessageState.END_PATTERN) {
String deviceReply = deviceReplyBuilder.toString();
if (deviceReply.equals(END_PATTERN)) {
socketClosed = true;
close(deviceReply);
} else {
deviceReply = deviceReply.replace(END_PATTERN, "");
dealWithReply(deviceReply);
deviceReplyBuilder.setLength(0);
}
} else if (state == NetconfMessageState.END_CHUNKED_PATTERN) {
String deviceReply = deviceReplyBuilder.toString();
if (!validateChunkedFraming(deviceReply)) {
log.debug("Netconf device {} send badly framed message {}", netconfDeviceInfo, deviceReply);
socketClosed = true;
close(deviceReply);
} else {
deviceReply = deviceReply.replaceAll(MSGLEN_REGEX_PATTERN, "");
deviceReply = deviceReply.replaceAll(CHUNKED_END_REGEX_PATTERN, "");
dealWithReply(deviceReply);
deviceReplyBuilder.setLength(0);
}
}
}
} catch (ClosedByInterruptException i) {
log.debug("Connection to device {} was terminated on request", netconfDeviceInfo.toString());
} catch (IOException e) {
log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
throw new IllegalStateException(new NetconfException("Error in reading from the session for device {}" + netconfDeviceInfo, e));
// TODO should we send a socket closed message to listeners ?
}
}
use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class NetconfStreamThread method dealWithReply.
private void dealWithReply(String deviceReply) {
if (deviceReply.contains(RPC_REPLY) || deviceReply.contains(RPC_ERROR) || deviceReply.contains(HELLO)) {
log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
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)) {
log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", netconfDeviceInfo, enableNotifications, getMsgId(deviceReply), deviceReply);
if (enableNotifications) {
log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size());
final String finalDeviceReply = deviceReply;
netconfDeviceEventListeners.forEach(listener -> listener.event(new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, finalDeviceReply, getMsgId(finalDeviceReply), netconfDeviceInfo)));
}
} else {
log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
}
}
Aggregations