Search in sources :

Example 6 with NetconfDeviceOutputEvent

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 ?
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NetconfException(org.onosproject.netconf.NetconfException) BufferedReader(java.io.BufferedReader)

Example 7 with NetconfDeviceOutputEvent

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);
    }
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) LoggerFactory(org.slf4j.LoggerFactory) MatchResult(java.util.regex.MatchResult) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) NetconfDeviceOutputEventListener(org.onosproject.netconf.NetconfDeviceOutputEventListener) ArrayList(java.util.ArrayList) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) List(java.util.List) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) InputStream(java.io.InputStream) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent)

Aggregations

NetconfDeviceOutputEvent (org.onosproject.netconf.NetconfDeviceOutputEvent)7 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 NetconfException (org.onosproject.netconf.NetconfException)3 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)2 HashSet (java.util.HashSet)2 Alarm (org.onosproject.alarm.Alarm)2 DefaultAlarm (org.onosproject.alarm.DefaultAlarm)2 NetconfDeviceInfo (org.onosproject.netconf.NetconfDeviceInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Field (java.lang.reflect.Field)1 StandardCharsets (java.nio.charset.StandardCharsets)1