SDN-R Sodium compliant netconfnode-state-service and devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / DeviceManagerNetconfConnectHandler.java
index fbcded8..bb61a82 100644 (file)
 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
 
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
+import javax.annotation.concurrent.GuardedBy;
 import org.eclipse.jdt.annotation.NonNull;
-import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
+import org.eclipse.jdt.annotation.Nullable;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitor;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.ODLEventListenerHandler;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.factory.NetworkElementFactory;
@@ -30,8 +32,8 @@ import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener;
-import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
@@ -39,34 +41,30 @@ import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DeviceManagerNetconfConnectHandler implements NetconfNodeConnectListener, NetconfNodeStateListener {
+public class DeviceManagerNetconfConnectHandler extends DeviceManagerNetconfNotConnectHandler
+        implements NetconfNodeConnectListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerNetconfConnectHandler.class);
 
-    private final @NonNull ListenerRegistration<DeviceManagerNetconfConnectHandler> registerNetconfNodeConnectListener;
-    private final @NonNull ListenerRegistration<NetconfNodeStateListener> registerNetconfNodeStateListener;
-
-    private final @NonNull ODLEventListenerHandler odlEventListenerHandler;
-    private final @NonNull DeviceMonitor deviceMonitor;
-    private final @NonNull List<NetworkElementFactory> factoryList;
-    private final @NonNull DeviceManagerServiceProvider serviceProvider;
-
     private final Object networkelementLock;
-    private final ConcurrentHashMap<String, NetworkElement> networkElementRepresentations;
+    /** Contains all connected devices */
+    @GuardedBy("networkelementLock")
+    private final ConcurrentHashMap<String, NetworkElement> connectedNetworkElementRepresentations;
+
+    private final @NonNull ListenerRegistration<DeviceManagerNetconfConnectHandler> registerNetconfNodeConnectListener;
 
     public DeviceManagerNetconfConnectHandler(@NonNull NetconfNodeStateService netconfNodeStateService,
+            @NonNull ClusterSingletonServiceProvider clusterSingletonServiceProvider,
             @NonNull ODLEventListenerHandler odlEventListenerHandler, @NonNull DeviceMonitor deviceMonitor,
             @NonNull DeviceManagerServiceProvider serviceProvider, @NonNull List<NetworkElementFactory> factoryList) {
 
-        HtAssert.nonnull(netconfNodeStateService, this.odlEventListenerHandler = odlEventListenerHandler,
-                this.deviceMonitor = deviceMonitor, this.serviceProvider = serviceProvider,
-                this.factoryList = factoryList);
+        super(netconfNodeStateService, clusterSingletonServiceProvider, odlEventListenerHandler, deviceMonitor,
+                serviceProvider, factoryList);
 
         this.networkelementLock = new Object();
-        this.networkElementRepresentations = new ConcurrentHashMap<>();
+        this.connectedNetworkElementRepresentations = new ConcurrentHashMap<>();
 
         this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
-        this.registerNetconfNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
     }
 
     @Override
@@ -84,26 +82,19 @@ public class DeviceManagerNetconfConnectHandler implements NetconfNodeConnectLis
         // times for same mountPointNodeName.
         // networkElementRepresentations contains handled NEs at master node.
 
-        synchronized (networkelementLock) {
-            if (networkElementRepresentations.containsKey(mountPointNodeName)) {
-                LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
-                return;
-            }
+        if (isInNetworkElementRepresentations(mountPointNodeName)) {
+            LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
+            return;
         }
         // update db with connect status
         NetconfNode netconfNode = acessor.getNetconfNode();
         sendUpdateNotification(mountPointNodeName, netconfNode.getConnectionStatus(), netconfNode);
 
-        for (NetworkElementFactory f : factoryList) {
-            Optional<NetworkElement> optionalNe = f.create(acessor, serviceProvider);
+        for (NetworkElementFactory f : getFactoryList()) {
+            Optional<NetworkElement> optionalNe = f.create(acessor, getServiceProvider());
             if (optionalNe.isPresent()) {
                 // sendUpdateNotification(mountPointNodeName, nNode.getConnectionStatus(), nNode);
-                NetworkElement inNe = optionalNe.get();
-                LOG.info("NE Management for {} with {}", mountPointNodeName, inNe.getClass().getName());
-                putToNetworkElementRepresentations(mountPointNodeName, inNe);
-                deviceMonitor.deviceConnectMasterIndication(mountPointNodeName, inNe);
-
-                inNe.register();
+                handleNeStartup(mountPointNodeName, optionalNe.get());
                 break; // Use the first provided
             }
         }
@@ -123,40 +114,21 @@ public class DeviceManagerNetconfConnectHandler implements NetconfNodeConnectLis
 
         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
         stopListenerOnNodeForConnectedState(mountPointNodeName);
-        deviceMonitor.deviceDisconnectIndication(mountPointNodeName);
-    }
-
-    @Override
-    public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
-        LOG.info("onCreated {}", nNodeId);
-        odlEventListenerHandler.mountpointCreatedIndication(nNodeId.getValue(), netconfNode);
-
-    }
-
-    @Override
-    public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
-        LOG.info("onStateChange {}", nNodeId);
-        odlEventListenerHandler.onStateChangeIndication(nNodeId.getValue(), netconfNode);
-    }
-
-    @Override
-    public void onRemoved(NodeId nNodeId) {
-        String mountPointNodeName = nNodeId.getValue();
-        LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
-
-        stopListenerOnNodeForConnectedState(mountPointNodeName);
-        deviceMonitor.removeMountpointIndication(mountPointNodeName);
-        odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
+        if (isDeviceMonitorEnabled()) {
+            getDeviceMonitor().deviceDisconnectIndication(mountPointNodeName);
+        }
     }
 
     @Override
     public void close() {
-        if (registerNetconfNodeConnectListener != null) {
+        if (Objects.nonNull(registerNetconfNodeConnectListener)) {
             registerNetconfNodeConnectListener.close();
         }
-        if (registerNetconfNodeStateListener != null) {
-            registerNetconfNodeStateListener.close();
-        }
+        super.close();
+    }
+
+    public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
+        return this.connectedNetworkElementRepresentations.get(mountpoint);
     }
 
     /*--------------------------------------------
@@ -167,32 +139,49 @@ public class DeviceManagerNetconfConnectHandler implements NetconfNodeConnectLis
      * Do all tasks necessary to move from mountpoint state connected -> connecting
      * 
      * @param mountPointNodeName provided
-     * @param ne representing the device connected to mountpoint
      */
     private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
-        NetworkElement ne = networkElementRepresentations.remove(mountPointNodeName);
+        NetworkElement ne = connectedNetworkElementRepresentations.remove(mountPointNodeName);
         if (ne != null) {
             ne.deregister();
         }
     }
 
-    private void putToNetworkElementRepresentations(String mountPointNodeName, NetworkElement ne) {
+    private boolean isInNetworkElementRepresentations(String mountPointNodeName) {
+        synchronized (networkelementLock) {
+            return connectedNetworkElementRepresentations.contains(mountPointNodeName);
+        }
+    }
+
+
+    private void handleNeStartup(String mountPointNodeName, NetworkElement inNe) {
+
+        LOG.info("NE Management for {} with {}", mountPointNodeName, inNe.getClass().getName());
         NetworkElement result;
         synchronized (networkelementLock) {
-            result = networkElementRepresentations.put(mountPointNodeName, ne);
+            result = connectedNetworkElementRepresentations.put(mountPointNodeName, inNe);
         }
         if (result != null) {
             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
         } else {
-            LOG.debug("refresh necon entry for {} with type {}", mountPointNodeName, ne.getDeviceType());
-            odlEventListenerHandler.connectIndication(mountPointNodeName, ne.getDeviceType());
+            LOG.debug("refresh necon entry for {} with type {}", mountPointNodeName, inNe.getDeviceType());
+            if (isOdlEventListenerHandlerEnabled()) {
+                getOdlEventListenerHandler().connectIndication(mountPointNodeName, inNe.getDeviceType());
+            }
+        }
+        if (isDeviceMonitorEnabled()) {
+            getDeviceMonitor().deviceConnectMasterIndication(mountPointNodeName, inNe);
         }
+
+        inNe.register();
     }
 
     private void sendUpdateNotification(String mountPointNodeName, ConnectionStatus csts, NetconfNode nNode) {
         LOG.info("update ConnectedState for device :: Name : {} ConnectionStatus {}", mountPointNodeName, csts);
-        odlEventListenerHandler.updateRegistration(mountPointNodeName, ConnectionStatus.class.getSimpleName(),
-                csts != null ? csts.getName() : "null", nNode);
+        if (isOdlEventListenerHandlerEnabled()) {
+            getOdlEventListenerHandler().updateRegistration(mountPointNodeName, ConnectionStatus.class.getSimpleName(),
+                    csts != null ? csts.getName() : "null", nNode);
+        }
     }
 
 }