activate deleteMapping function in Renderer
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / RendererNotificationsImpl.java
index 90b12a93e6b6a3f1c85b3ca839fc45ec8dde737a..c0dd38ebdb2f94345dc0506976c125c991c30158 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.transportpce.renderer;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
@@ -15,9 +14,8 @@ import com.google.common.cache.LoadingCache;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
 import java.util.stream.Collectors;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -31,16 +29,17 @@ import org.opendaylight.controller.md.sal.binding.api.MountPointService;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.renderer.listeners.AlarmNotificationListener;
 import org.opendaylight.transportpce.renderer.listeners.DeOperationsListener;
 import org.opendaylight.transportpce.renderer.listeners.DeviceListener;
 import org.opendaylight.transportpce.renderer.listeners.LldpListener;
 import org.opendaylight.transportpce.renderer.listeners.TcaListener;
-import org.opendaylight.transportpce.renderer.mapping.PortMapping;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.AlarmNotification;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.OrgOpenroadmDeOperationsListener;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev161014.OrgOpenroadmDeviceListener;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.OrgOpenroadmLldpListener;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev161014.OrgOpenroadmTcaListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
@@ -59,17 +58,16 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
-
     private final DataBroker dataBroker;
     private final MountPointService mountService;
     private static final Logger LOG = LoggerFactory.getLogger(RendererNotificationsImpl.class);
     private ListenerRegistration<RendererNotificationsImpl> dataTreeChangeListenerRegistration;
-
+    private final PortMapping portMapping;
+    private final DeviceTransactionManager deviceTransactionManager;
     private final Set<String> currentMountedDevice;
     public static final InstanceIdentifier<Topology> NETCONF_TOPO_IID = InstanceIdentifier.create(NetworkTopology.class)
         .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
@@ -83,13 +81,20 @@ public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
         });
 
     public RendererNotificationsImpl(final DataBroker dataBroker, final MountPointService mountService,
-        Set<String> currentMountedDevice) {
+        Set<String> currentMountedDevice, PortMapping portMapping,DeviceTransactionManager deviceTransactionManager) {
         this.dataBroker = dataBroker;
         this.mountService = mountService;
         this.currentMountedDevice = currentMountedDevice;
+        this.portMapping = portMapping;
+        this.deviceTransactionManager = deviceTransactionManager;
+        if (portMapping == null) {
+            LOG.error("Portmapping is null !");
+        }
+        if (deviceTransactionManager == null) {
+            LOG.error("deviceTransactionManager is null");
+        }
         if (mountService == null) {
             LOG.error("Mount service is null");
-
         }
         if (dataBroker != null) {
             this.dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
@@ -97,82 +102,69 @@ public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
         }
     }
 
-    private void registerNotificationListener(final NodeId nodeId) {
-        final Optional<MountPoint> mountPoint;
-        try {
-            // Get mount point for specified device
-            mountPoint = mountService.getMountPoint(mountIds.get(nodeId.getValue()));
-            if (!mountPoint.isPresent()) {
-                LOG.error("Mount point for node {} doesn't exist", nodeId.getValue());
-            }
-        } catch (ExecutionException e) {
-            throw new IllegalArgumentException(e);
+    private void registerNotificationListener(final String nodeId) {
+
+        LOG.info("onDeviceConnected: {}", nodeId);
+        Optional<MountPoint> mountPointOpt = this.deviceTransactionManager.getDeviceMountPoint(nodeId);
+        MountPoint mountPoint;
+        if (mountPointOpt.isPresent()) {
+            mountPoint = mountPointOpt.get();
+        } else {
+            LOG.error("Failed to get mount point for node {}", nodeId);
+            return;
         }
 
-        // Register notification service
-        final Optional<NotificationService> notificationService = mountPoint.get().getService(
-            NotificationService.class);
+        final Optional<NotificationService> notificationService =
+                mountPoint.getService(NotificationService.class).toJavaUtil();
         if (!notificationService.isPresent()) {
-            LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
+            LOG.error("Failed to get RpcService for node {}", nodeId);
+            return;
         }
 
         final OrgOpenroadmAlarmListener alarmListener;
         alarmListener = new AlarmNotificationListener();
         LOG.info("Registering notification listener on {} for node: {}", AlarmNotification.QNAME, nodeId);
         // Register notification listener
-        final ListenerRegistration<OrgOpenroadmAlarmListener>
-            accessAlarmNotificationListenerRegistration =
-                notificationService.get().registerNotificationListener(alarmListener);
 
         final OrgOpenroadmDeOperationsListener deOperationsListener;
         deOperationsListener = new DeOperationsListener();
         LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
         // Register notification listener
-        final ListenerRegistration<OrgOpenroadmDeOperationsListener>
-            accessDeOperationasNotificationListenerRegistration =
-                notificationService.get().registerNotificationListener(deOperationsListener);
 
         final OrgOpenroadmDeviceListener deviceListener;
         deviceListener = new DeviceListener();
         LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
         // Register notification listener
-        final ListenerRegistration<OrgOpenroadmDeviceListener>
-            accessDeviceNotificationListenerRegistration = notificationService.get()
-                .registerNotificationListener(deviceListener);
 
         final OrgOpenroadmLldpListener lldpListener;
         lldpListener = new LldpListener();
         LOG.info("Registering notification listener on OrgOpenroadmLldpListener for node: {}", nodeId);
         // Register notification listener
-        final ListenerRegistration<OrgOpenroadmLldpListener> accessLldpNotificationListenerRegistration =
-            notificationService.get().registerNotificationListener(lldpListener);
 
         final OrgOpenroadmTcaListener tcaListener;
         tcaListener = new TcaListener();
         LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
         // Register notification listener
-        final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
-            notificationService.get().registerNotificationListener(tcaListener);
 
         // Listening to NETCONF datastream
         final String streamName = "NETCONF";
-        final Optional<RpcConsumerRegistry> service = mountPoint.get().getService(RpcConsumerRegistry.class);
+        final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
         if (!service.isPresent()) {
-            LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
+            LOG.error("Failed to get RpcService for node {}", nodeId);
         }
+
         final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
         final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
         createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
         LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
-        final Future<RpcResult<Void>> subscription = rpcService.createSubscription(createSubscriptionInputBuilder
-            .build());
+
     }
 
     public void close() {
         LOG.info("RenderernotificationsImpl Closed");
         // Clean up the Data Change Listener registration
-        if (dataTreeChangeListenerRegistration != null) {
-            dataTreeChangeListenerRegistration.close();
+        if (this.dataTreeChangeListenerRegistration != null) {
+            this.dataTreeChangeListenerRegistration.close();
         }
     }
 
@@ -182,17 +174,29 @@ public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
         for (DataTreeModification<Node> change : changes) {
 
             DataObjectModification<Node> rootNode = change.getRootNode();
-            String nodeId = rootNode.getDataAfter().getKey().getNodeId().getValue();
-            NetconfNode nnode = Preconditions.checkNotNull(rootNode.getDataAfter().getAugmentation(NetconfNode.class),
-                "Node not connected via Netconf protocol");
-            if (nnode != null) {
-
-                if (rootNode.getModificationType() == ModificationType.WRITE) {
-                    LOG.info("Node added " + nodeId);
+            NetconfNode nnode = null;
+            String nodeId = new String();
+            if (rootNode.getDataAfter() != null) {
+                nnode = Preconditions.checkNotNull(rootNode.getDataAfter().getAugmentation(NetconfNode.class),
+                    "Node not connected via Netconf protocol");
+                nodeId = rootNode.getDataAfter().getKey().getNodeId().getValue();
+            }
 
-                } else if (rootNode.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
+            if (rootNode.getModificationType() == ModificationType.DELETE) {
+                String nodeid = rootNode.getDataBefore().getKey().getNodeId().getValue();
+                LOG.info("Node {} removed...", nodeid);
+                this.portMapping.deleteMappingData(nodeid);
+            }
 
-                    LOG.info("Node modified " + nodeId);
+            if (nnode != null) {
+                if (nodeId.equals("controller-config")) {
+                    // We shouldn't process controller-config as an OpenROAM device
+                    LOG.info("{} ignored: org-openroadm-device advertised but not a real ROADM device", nodeId);
+                    return;
+                }
+                if ((rootNode.getModificationType() == ModificationType.WRITE) ||
+                        (rootNode.getModificationType() == ModificationType.SUBTREE_MODIFIED)) {
+                    LOG.info("Node added or modified {}", nodeId);
                     ConnectionStatus csts = nnode.getConnectionStatus();
 
                     switch (csts) {
@@ -202,17 +206,16 @@ public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
                                 .stream().map(cp -> cp.getCapability()).collect(Collectors.toList());
                             LOG.info("Capabilities: {}", capabilities);
                             /*
-                             * TODO: check for required
-                             * capabilities to listen for notifications
-                             * registerNotificationListener(rootNode.
-                             * getDataAfter(). getNodeId());
+                             * TODO: check for required capabilities to listen
+                             * for notifications
                              */
-                            currentMountedDevice.add(nodeId);
-                            new PortMapping(dataBroker, mountService, nodeId).createMappingData();
+                            registerNotificationListener(nodeId);
+                            this.currentMountedDevice.add(nodeId);
+                            this.portMapping.createMappingData(nodeId);
                             break;
                         }
                         case Connecting: {
-                            LOG.info("NETCONF Node: {} was disconnected", nodeId);
+                            LOG.info("NETCONF Node: {} is (dis)connecting", nodeId);
                             break;
                         }
                         case UnableToConnect: {
@@ -220,13 +223,11 @@ public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
                             break;
                         }
                         default:
-                            LOG.warn("Unexpected connection status " + csts.getName());
+                            LOG.warn("Unexpected connection status {}", csts.getName());
                     }
-                } else if (rootNode.getModificationType() ==  ModificationType.DELETE) {
-                    LOG.info("Node removed " + nodeId);
-                    currentMountedDevice.remove(nodeId);
                 }
             }
         }
+        LOG.info("Netconf devices currently mounted are : {}", this.currentMountedDevice.toString());
     }
-}
\ No newline at end of file
+}