clean some compilation warnings
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / NetConfTopologyListener.java
index 0fc982ec54852c72b763b9708f0c89416ef05c86..9772212e00775c6b836753ce8d8f4039b7e1deeb 100644 (file)
@@ -11,17 +11,15 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
-
 import javax.annotation.Nonnull;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
 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.StringConstants;
 import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
@@ -37,9 +35,6 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.O
 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;
-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.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.opendaylight.netconf.node.topology.rev150114.NetconfNode;
@@ -113,31 +108,6 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
         final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
                 notificationService.get().registerNotificationListener(tcaListener);
 
-
-
-        String streamName = getSupportedStream(nodeId);
-        if (streamName == null) {
-            streamName = "OPENROADM";
-        }
-        final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
-        if (service.isPresent()) {
-            final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
-            if (rpcService == null) {
-                LOG.error("Failed to get RpcService for node {}", nodeId);
-            } else {
-                final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
-                    new CreateSubscriptionInputBuilder();
-                createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
-                LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
-                rpcService.createSubscription(createSubscriptionInputBuilder.build());
-            }
-        } else {
-            LOG.error("Failed to get RpcService for node {}", nodeId);
-        }
-        NodeRegistration nodeRegistration = new NodeRegistration(nodeId, accessAlarmNotificationListenerRegistration,
-                accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
-                accessLldpNotificationListenerRegistration, accessTcaNotificationListenerRegistration);
-        this.registrations.put(nodeId, nodeRegistration);
     }
 
     private void onDeviceDisConnected(final String nodeId) {
@@ -153,48 +123,63 @@ public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
     }
 
     @Override
+    @SuppressWarnings("checkstyle:FallThrough")
     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
         LOG.info("onDataTreeChanged");
         for (DataTreeModification<Node> change : changes) {
             DataObjectModification<Node> rootNode = change.getRootNode();
-            if (rootNode.getDataAfter() == null) {
+            if ((rootNode.getDataAfter() == null) && (rootNode.getModificationType() != ModificationType.DELETE)) {
+                LOG.error("rootNode.getDataAfter is null : Node not connected via Netconf protocol");
+                continue;
+            }
+            if (rootNode.getModificationType() == ModificationType.DELETE) {
+                if (rootNode.getDataBefore() != null) {
+                    String nodeId = rootNode.getDataBefore().key().getNodeId().getValue();
+                    LOG.info("Node {} deleted", nodeId);
+                    this.networkModelService.deleteOpenROADMnode(nodeId);
+                    onDeviceDisConnected(nodeId);
+                } else {
+                    LOG.error("rootNode.getDataBefore is null !");
+                }
                 continue;
             }
-            String nodeId = rootNode.getDataAfter().getKey().getNodeId().getValue();
-            NetconfNode netconfNode = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
+            String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
+            NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
+
             if ((netconfNode != null) && !StringConstants.DEFAULT_NETCONF_NODEID.equals(nodeId)) {
                 switch (rootNode.getModificationType()) {
                     case WRITE:
                         LOG.info("Node added: {}", nodeId);
-                        break;
                     case SUBTREE_MODIFIED:
                         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
                                 netconfNode.getConnectionStatus();
-                        long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
+                        try {
+                            long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
                                 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
                                 .count();
-                        if (count > 0) {
-                            LOG.info("OpenROADM node detected: {} {}", nodeId, connectionStatus.name());
-                            switch (connectionStatus) {
-                                case Connected:
-                                    this.networkModelService.createOpenROADMnode(nodeId);
-                                    onDeviceConnected(nodeId);
-                                    break;
-                                case Connecting:
-                                case UnableToConnect:
-                                    this.networkModelService.setOpenROADMnodeStatus(nodeId, connectionStatus);
-                                    onDeviceDisConnected(nodeId);
-                                    break;
-                                default:
-                                    LOG.warn("Unsupported device state {}", connectionStatus.getName());
-                                    break;
+                            if (count > 0) {
+                                LOG.info("OpenROADM node detected: {} {}", nodeId, connectionStatus.name());
+                                switch (connectionStatus) {
+                                    case Connected:
+                                        this.networkModelService.createOpenROADMnode(nodeId);
+                                        onDeviceConnected(nodeId);
+                                        break;
+                                    case Connecting:
+                                    case UnableToConnect:
+                                        this.networkModelService.setOpenROADMnodeStatus(nodeId, connectionStatus);
+                                        onDeviceDisConnected(nodeId);
+                                        break;
+                                    default:
+                                        LOG.warn("Unsupported device state {}", connectionStatus.getName());
+                                        break;
+                                }
                             }
+                        } catch (NullPointerException e) {
+                            LOG.error("Cannot get available Capabilities");
                         }
                         break;
-                    case DELETE:
-                        LOG.info("Node deleted: {}", nodeId);
-                        break;
                     default:
+                        LOG.warn("Unexpected connection status : {}", rootNode.getModificationType());
                         break;
                 }
             }