Merge "Debug tool for openconfig proprietary extensions"
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / NetConfTopologyListener.java
index dedc387a0d7c400eb80054f59246a68c34caa8f7..367388f8d7ea5515324901041b24ff5a61b0ba81 100644 (file)
@@ -9,33 +9,38 @@ package org.opendaylight.transportpce.networkmodel;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.Collection;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
-import javax.annotation.Nonnull;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataObjectModification;
 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
 import org.opendaylight.mdsal.binding.api.MountPoint;
 import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.mdsal.binding.api.RpcService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.StringConstants;
+import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.networkmodel.dto.NodeRegistration;
 import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscription;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.ConnectionOper.ConnectionStatus;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.connection.oper.available.capabilities.AvailableCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -62,16 +67,17 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
         this.portMapping = portMapping;
     }
 
-    public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
+    @Override
+    public void onDataTreeChanged(List<DataTreeModification<Node>> changes) {
         LOG.info("onDataTreeChanged - {}", this.getClass().getSimpleName());
         for (DataTreeModification<Node> change : changes) {
             DataObjectModification<Node> rootNode = change.getRootNode();
-            if (rootNode.getDataBefore() == null) {
+            if (rootNode.dataBefore() == null) {
                 continue;
             }
-            String nodeId = rootNode.getDataBefore().key().getNodeId().getValue();
-            NetconfNode netconfNodeBefore = rootNode.getDataBefore().augmentation(NetconfNode.class);
-            switch (rootNode.getModificationType()) {
+            String nodeId = rootNode.dataBefore().key().getNodeId().getValue();
+            NetconfNode netconfNodeBefore = rootNode.dataBefore().augmentation(NetconfNode.class);
+            switch (rootNode.modificationType()) {
                 case DELETE:
                     if (this.networkModelService.deleteOpenRoadmnode(nodeId)) {
                         onDeviceDisConnected(nodeId);
@@ -79,7 +85,7 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
                     }
                     break;
                 case WRITE:
-                    NetconfNode netconfNodeAfter = rootNode.getDataAfter().augmentation(NetconfNode.class);
+                    NetconfNode netconfNodeAfter = rootNode.dataAfter().augmentation(NetconfNode.class);
                     if (ConnectionStatus.Connecting.equals(netconfNodeBefore.getConnectionStatus())
                             && ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
                         LOG.info("Connecting Node: {}", nodeId);
@@ -93,8 +99,8 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
                             return;
                         }
                         this.networkModelService
-                            .createOpenRoadmNode(nodeId, deviceCapabilityOpt.get().getCapability());
-                        onDeviceConnected(nodeId, deviceCapabilityOpt.get().getCapability());
+                            .createOpenRoadmNode(nodeId, deviceCapabilityOpt.orElseThrow().getCapability());
+                        onDeviceConnected(nodeId, deviceCapabilityOpt.orElseThrow().getCapability());
                         LOG.info("Device {} correctly connected to controller", nodeId);
                     }
                     if (ConnectionStatus.Connected.equals(netconfNodeBefore.getConnectionStatus())
@@ -103,7 +109,7 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
                     }
                     break;
                 default:
-                    LOG.debug("Unknown modification type {}", rootNode.getModificationType().name());
+                    LOG.debug("Unknown modification type {}", rootNode.modificationType().name());
                     break;
             }
         }
@@ -116,7 +122,7 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
             LOG.error("Failed to get mount point for node {}", nodeId);
             return;
         }
-        MountPoint mountPoint = mountPointOpt.get();
+        MountPoint mountPoint = mountPointOpt.orElseThrow();
         final Optional<NotificationService> notificationService = mountPoint.getService(NotificationService.class);
         if (notificationService.isEmpty()) {
             LOG.error(RPC_SERVICE_FAILED, nodeId);
@@ -124,7 +130,7 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
         }
         NodeRegistration nodeRegistration =
             new NodeRegistration(
-                nodeId, openRoadmVersion, notificationService.get(), this.dataBroker, this.portMapping);
+                nodeId, openRoadmVersion, notificationService.orElseThrow(), this.dataBroker, this.portMapping);
         nodeRegistration.registerListeners();
         registrations.put(nodeId, nodeRegistration);
 
@@ -137,23 +143,20 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
     }
 
     private boolean subscribeStream(MountPoint mountPoint, String nodeId) {
-        final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
+        final Optional<RpcService> service = mountPoint.getService(RpcService.class);
         if (service.isEmpty()) {
             return false;
         }
-        final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
+        final CreateSubscription rpcService = service.orElseThrow().getRpc(CreateSubscription.class);
         if (rpcService == null) {
             LOG.error(RPC_SERVICE_FAILED, nodeId);
             return false;
         }
         // Set the default stream as OPENROADM
-        for (String streamName : List.of("OPENROADM", "NETCONF")) {
+        for (String streamName : getSupportedStream(nodeId)) {
             LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
-            ListenableFuture<RpcResult<CreateSubscriptionOutput>> subscription =
-                rpcService.createSubscription(
+            ListenableFuture<RpcResult<CreateSubscriptionOutput>> subscription = rpcService.invoke(
                     new CreateSubscriptionInputBuilder().setStream(new StreamNameType(streamName)).build());
-            // If OpenROADM stream is not supported
-            // Try NETCONF subscription
             if (checkSupportedStream(streamName, subscription)) {
                 return true;
             }
@@ -189,4 +192,31 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
         return subscriptionSuccessful;
     }
 
+    private List<String> getSupportedStream(String nodeId) {
+        InstanceIdentifier<Streams> streamsIID = InstanceIdentifier.create(Netconf.class).child(Streams.class);
+        Optional<Streams> ordmInfoObject =
+                deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, streamsIID,
+                        Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (ordmInfoObject == null || ordmInfoObject.isEmpty() || ordmInfoObject.orElseThrow().getStream().isEmpty()) {
+            LOG.error("List of streams supports by device is not present");
+            return List.of("OPENROADM","NETCONF");
+        }
+        List<String> streams = new ArrayList<>();
+        List<String> netconfStreams = new ArrayList<>();
+        for (Stream strm : ordmInfoObject.orElseThrow().getStream().values()) {
+            LOG.debug("Streams are {}", strm);
+            if ("OPENROADM".equalsIgnoreCase(strm.getName().getValue())) {
+                streams.add(strm.getName().getValue());
+            } else if ("NETCONF".equalsIgnoreCase(strm.getName().getValue())) {
+                netconfStreams.add(strm.getName().getValue());
+            }
+        }
+        // If OpenROADM streams are not supported, try NETCONF streams subscription
+        streams.addAll(netconfStreams);
+        return
+            streams.isEmpty()
+                ? List.of("OPENROADM","NETCONF")
+                : streams;
+    }
+
 }