Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManager.java
index 2627cf6344dc99d9d353e44761387337ee19a10f..2286338b07934acbd655416532676b33995e72f9 100644 (file)
@@ -10,35 +10,41 @@ package org.opendaylight.openflowplugin.applications.frm.impl;
 
 import com.google.common.annotations.VisibleForTesting;
 import java.util.concurrent.ConcurrentHashMap;
+
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Manager for clustering service registrations of {@link DeviceMastership}.
  */
-public class DeviceMastershipManager {
+public class DeviceMastershipManager implements OpendaylightInventoryListener, AutoCloseable{
     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
     private final ClusterSingletonServiceProvider clusterSingletonService;
+    private final ListenerRegistration<?> notifListenerRegistration;
     private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
 
-    public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService) {
+    public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
+                                   final NotificationProviderService notificationService) {
         this.clusterSingletonService = clusterSingletonService;
+        this.notifListenerRegistration = notificationService.registerNotificationListener(this);
     }
 
     public void onDeviceConnected(final NodeId nodeId) {
-        final DeviceMastership mastership = new DeviceMastership(nodeId, clusterSingletonService);
-        deviceMasterships.put(nodeId, mastership);
-        LOG.debug("FRM service registered for: {}", nodeId.getValue());
+        //No-op
     }
 
     public void onDeviceDisconnected(final NodeId nodeId) {
-        final DeviceMastership mastership = deviceMasterships.remove(nodeId);
-        if (mastership != null) {
-            mastership.close();
-        }
-        LOG.debug("FRM service unregistered for: {}", nodeId.getValue());
+        //No-op
     }
 
     public boolean isDeviceMastered(final NodeId nodeId) {
@@ -50,4 +56,39 @@ public class DeviceMastershipManager {
         return deviceMasterships;
     }
 
+    @Override
+    public void onNodeUpdated(NodeUpdated notification) {
+        LOG.debug("NodeUpdate notification received : {}", notification);
+        DeviceMastership membership = deviceMasterships.computeIfAbsent(notification.getId(), device ->
+                new DeviceMastership(notification.getId(), clusterSingletonService));
+        membership.registerClusterSingletonService();
+    }
+
+    @Override
+    public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
+        //Not published by plugin
+    }
+
+    @Override
+    public void onNodeRemoved(NodeRemoved notification) {
+        LOG.debug("NodeRemoved notification received : {}", notification);
+        NodeId nodeId = notification.getNodeRef().getValue().firstKeyOf(Node.class).getId();
+        final DeviceMastership mastership = deviceMasterships.remove(nodeId);
+        if (mastership != null) {
+            mastership.close();
+            LOG.info("Unregistered FRM cluster singleton service for service id : {}", nodeId.getValue());
+        }
+    }
+
+    @Override
+    public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
+        //Not published by plugin
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (notifListenerRegistration != null) {
+            notifListenerRegistration.close();
+        }
+    }
 }