Search in sources :

Example 1 with NetconfDeviceOutputEvent

use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.

the class PolatisAlarmConfig method translateAlarms.

@Override
public <T> Set<Alarm> translateAlarms(List<T> unparsedAlarms) {
    deviceId = handler().data().deviceId();
    Set<Alarm> alarms = new HashSet<>();
    for (T alarm : unparsedAlarms) {
        if (alarm instanceof NetconfDeviceOutputEvent) {
            NetconfDeviceOutputEvent event = (NetconfDeviceOutputEvent) alarm;
            if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) {
                String message = event.getMessagePayload();
                InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
                try {
                    Document doc = XmlEventParser.createDocFromMessage(in);
                    long timeStamp = XmlEventParser.getEventTime(doc);
                    Node descriptionNode = XmlEventParser.getDescriptionNode(doc);
                    while (descriptionNode != null) {
                        if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) {
                            String nodeName = descriptionNode.getNodeName();
                            if (nodeName.equals(ALARM_TYPE_LOS)) {
                                Node portIdNode = descriptionNode.getChildNodes().item(1);
                                String portId = portIdNode.getTextContent();
                                String description = "Loss of Service alarm raised for fibre " + portId;
                                alarms.add(new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, description), deviceId, description, Alarm.SeverityLevel.MAJOR, timeStamp).build());
                                descriptionNode = null;
                            }
                        } else {
                            descriptionNode = descriptionNode.getNextSibling();
                        }
                    }
                } catch (SAXException | IOException | ParserConfigurationException | UnsupportedOperationException | IllegalArgumentException e) {
                    log.error("Exception thrown translating message from {}.", deviceId, e);
                }
            }
        }
    }
    return alarms;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) Alarm(org.onosproject.alarm.Alarm) DefaultAlarm(org.onosproject.alarm.DefaultAlarm) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Example 2 with NetconfDeviceOutputEvent

use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.

the class NetconfControllerImplTest method setUp.

@Before
public void setUp() throws Exception {
    ctrl = new NetconfControllerImpl();
    ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
    ctrl.cfgService = cfgService;
    ctrl.deviceService = deviceService;
    ctrl.deviceKeyService = deviceKeyService;
    ctrl.netCfgService = netCfgService;
    ctrl.mastershipService = mastershipService;
    NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
    NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
    NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
    ctrl.clusterCommunicator = clusterCommunicationService;
    ctrl.clusterService = mockClusterService;
    // Creating mock devices
    deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
    deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
    deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
    badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
    deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
    deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
    // Create a JSON entry just like Network Config accepts
    ObjectMapper mapper = new ObjectMapper();
    String jsonMessage = "{\n" + "  \"ip\":\"" + DEVICE_10_IP + "\",\n" + "  \"port\":" + DEVICE_10_PORT + ",\n" + "  \"username\":\"" + DEVICE_10_USERNAME + "\",\n" + "  \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" + "  \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" + "}";
    InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
    JsonNode jsonNode = mapper.readTree(jsonStream);
    jsonStream.close();
    ConfigApplyDelegate delegate = new MockDelegate();
    deviceConfig10 = new NetconfDeviceConfig();
    deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
    device1 = new TestNetconfDevice(deviceInfo1);
    deviceId1 = deviceInfo1.getDeviceId();
    device2 = new TestNetconfDevice(deviceInfo2);
    deviceId2 = deviceInfo2.getDeviceId();
    // Adding to the map for testing get device calls.
    Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
    field1.setAccessible(true);
    reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
    reflectedDeviceMap.put(deviceId1, device1);
    reflectedDeviceMap.put(deviceId2, device2);
    // Creating mock events for testing NetconfDeviceOutputEventListener
    Field field2 = ctrl.getClass().getDeclaredField("downListener");
    field2.setAccessible(true);
    reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
    eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, null, Optional.of(1), deviceInfo1);
    eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, null, Optional.of(2), deviceInfo2);
}
Also used : NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DeviceId(org.onosproject.net.DeviceId) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConfigApplyDelegate(org.onosproject.net.config.ConfigApplyDelegate) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) Field(java.lang.reflect.Field) NetconfDevice(org.onosproject.netconf.NetconfDevice) ByteArrayInputStream(java.io.ByteArrayInputStream) NetconfDeviceConfig(org.onosproject.netconf.config.NetconfDeviceConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 3 with NetconfDeviceOutputEvent

use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.

the class NetconfSessionMinaImpl method sendRequest.

private String sendRequest(String request, boolean isHello, int timeout) throws NetconfException {
    checkAndReestablish();
    int messageId = -1;
    if (!isHello) {
        messageId = messageIdInteger.getAndIncrement();
    }
    // FIXME potentially re-writing chunked encoded String?
    request = formatXmlHeader(request);
    request = formatRequestMessageId(request, messageId);
    int useTimeout = timeout > 0 ? timeout : replyTimeout;
    log.debug("Sending request to NETCONF with timeout {} for {}", useTimeout, deviceInfo.name());
    CompletableFuture<String> futureReply = request(request, messageId);
    String rp;
    try {
        rp = futureReply.get(useTimeout, TimeUnit.SECONDS);
        // Why here???
        replies.remove(messageId);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new NetconfException("Interrupted waiting for reply for request" + request, e);
    } catch (TimeoutException e) {
        throw new NetconfException("Timed out waiting for reply for request " + request + " after " + useTimeout + " sec.", e);
    } catch (ExecutionException e) {
        log.warn("Closing session {} for {} due to unexpected Error", sessionID, deviceInfo, e);
        stopClient();
        NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.SESSION_CLOSED, null, "Closed due to unexpected error " + e.getCause(), Optional.of(-1), deviceInfo);
        publishEvent(event);
        // move to cleanUp()?
        errorReplies.clear();
        cleanUp();
        throw new NetconfException("Closing session " + sessionID + " for " + deviceInfo + " for request " + request, e);
    }
    log.debug("Result {} from request {} to device {}", rp, request, deviceInfo);
    return rp.trim();
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent)

Example 4 with NetconfDeviceOutputEvent

use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.

the class NetconfStreamThread method close.

private void close(String deviceReply) {
    log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", netconfDeviceInfo, deviceReply);
    if (!deviceReply.equals(ON_REQUEST)) {
        NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, null, Optional.of(-1), netconfDeviceInfo);
        netconfDeviceEventListeners.forEach(listener -> listener.event(event));
    }
    this.interrupt();
}
Also used : NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent)

Example 5 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