Switch to MD-SAL APIs
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManager.java
index 87c3ba638da6ab04849e63ade189668187063623..d7fd461d46f3dbcb12ddbc2df086e77533d54d14 100644 (file)
@@ -1,11 +1,10 @@
-/**
+/*
  * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.applications.frm.impl;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -17,26 +16,26 @@ import java.util.Collections;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-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.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
+import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService;
+import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-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.Nodes;
-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.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -45,33 +44,37 @@ import org.slf4j.LoggerFactory;
 /**
  * Manager for clustering service registrations of {@link DeviceMastership}.
  */
-public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<FlowCapableNode>,
-        OpendaylightInventoryListener, AutoCloseable{
+public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable,
+        MastershipChangeService {
     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
-    private static final InstanceIdentifier<FlowCapableNode> II_TO_FLOW_CAPABLE_NODE
-            = InstanceIdentifier.builder(Nodes.class)
-            .child(Node.class)
-            .augmentation(FlowCapableNode.class)
-            .build();
+    private static final InstanceIdentifier<FlowCapableNode> II_TO_FLOW_CAPABLE_NODE = InstanceIdentifier
+            .builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build();
 
     private final ClusterSingletonServiceProvider clusterSingletonService;
-    private final ListenerRegistration<?> notifListenerRegistration;
     private final FlowNodeReconciliation reconcliationAgent;
     private final DataBroker dataBroker;
-    private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
+    private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap<>();
     private final Object lockObj = new Object();
+    private final RpcProviderService rpcProviderService;
+    private final FrmReconciliationService reconcliationService;
+
     private ListenerRegistration<DeviceMastershipManager> listenerRegistration;
     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
+    private MastershipChangeRegistration mastershipChangeServiceRegistration;
 
     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
-                                   final NotificationProviderService notificationService,
                                    final FlowNodeReconciliation reconcliationAgent,
-                                   final DataBroker dataBroker) {
+                                   final DataBroker dataBroker,
+                                   final MastershipChangeServiceManager mastershipChangeServiceManager,
+                                   final RpcProviderService rpcProviderService,
+                                   final FrmReconciliationService reconciliationService) {
         this.clusterSingletonService = clusterSingletonService;
-        this.notifListenerRegistration = notificationService.registerNotificationListener(this);
         this.reconcliationAgent = reconcliationAgent;
+        this.rpcProviderService = rpcProviderService;
+        this.reconcliationService = reconciliationService;
         this.dataBroker = dataBroker;
         registerNodeListener();
+        this.mastershipChangeServiceRegistration = mastershipChangeServiceManager.register(this);
     }
 
     public boolean isDeviceMastered(final NodeId nodeId) {
@@ -91,43 +94,13 @@ public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<
     }
 
     @Override
-    public void onNodeUpdated(NodeUpdated notification) {
-        LOG.debug("NodeUpdate notification received : {}", notification);
-        DeviceMastership membership = deviceMasterships.computeIfAbsent(notification.getId(), device ->
-                new DeviceMastership(notification.getId(), clusterSingletonService, reconcliationAgent));
-        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 onDataTreeChanged(@Nonnull Collection<DataTreeModification<FlowCapableNode>> changes) {
+    public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> changes) {
         Preconditions.checkNotNull(changes, "Changes may not be null!");
 
         for (DataTreeModification<FlowCapableNode> change : changes) {
             final InstanceIdentifier<FlowCapableNode> key = change.getRootPath().getRootIdentifier();
             final DataObjectModification<FlowCapableNode> mod = change.getRootNode();
-            final InstanceIdentifier<FlowCapableNode> nodeIdent =
-                    key.firstIdentifierOf(FlowCapableNode.class);
+            final InstanceIdentifier<FlowCapableNode> nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
 
             switch (mod.getModificationType()) {
                 case DELETE:
@@ -136,7 +109,7 @@ public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<
                     }
                     break;
                 case SUBTREE_MODIFIED:
-                    //NO-OP since we do not need to reconcile on Node-updated
+                    // NO-OP since we do not need to reconcile on Node-updated
                     break;
                 case WRITE:
                     if (mod.getDataBefore() == null) {
@@ -149,46 +122,40 @@ public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<
         }
     }
 
-    public void remove(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode del,
-                       InstanceIdentifier<FlowCapableNode> nodeIdent) {
-        if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE)){
+    public void remove(final InstanceIdentifier<FlowCapableNode> identifier, final FlowCapableNode del,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+        if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE)) {
             if (LOG.isDebugEnabled()) {
-                LOG.debug("Node removed: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
+                LOG.debug("Node removed: {}", nodeIdent.firstKeyOf(Node.class).getId().getValue());
             }
 
-            if ( ! nodeIdent.isWildcarded()) {
-                if (activeNodes.contains(nodeIdent)) {
-                    synchronized (lockObj) {
-                        if (activeNodes.contains(nodeIdent)) {
-                            Set<InstanceIdentifier<FlowCapableNode>> set =
-                                    Sets.newHashSet(activeNodes);
-                            set.remove(nodeIdent);
-                            activeNodes = Collections.unmodifiableSet(set);
-                            setNodeOperationalStatus(nodeIdent,false);
-                        }
+            if (!nodeIdent.isWildcarded() && activeNodes.contains(nodeIdent)) {
+                synchronized (lockObj) {
+                    if (activeNodes.contains(nodeIdent)) {
+                        Set<InstanceIdentifier<FlowCapableNode>> set = Sets.newHashSet(activeNodes);
+                        set.remove(nodeIdent);
+                        activeNodes = Collections.unmodifiableSet(set);
+                        setNodeOperationalStatus(nodeIdent, false);
                     }
                 }
             }
-
         }
     }
 
-    public void add(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode add,
-                    InstanceIdentifier<FlowCapableNode> nodeIdent) {
-        if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE)){
+    public void add(final InstanceIdentifier<FlowCapableNode> identifier, final FlowCapableNode add,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+        if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE)) {
             if (LOG.isDebugEnabled()) {
-                LOG.debug("Node added: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
+                LOG.debug("Node added: {}", nodeIdent.firstKeyOf(Node.class).getId().getValue());
             }
 
-            if ( ! nodeIdent.isWildcarded()) {
-                if (!activeNodes.contains(nodeIdent)) {
-                    synchronized (lockObj) {
-                        if (!activeNodes.contains(nodeIdent)) {
-                            Set<InstanceIdentifier<FlowCapableNode>> set = Sets.newHashSet(activeNodes);
-                            set.add(nodeIdent);
-                            activeNodes = Collections.unmodifiableSet(set);
-                            setNodeOperationalStatus(nodeIdent,true);
-                        }
+            if (!nodeIdent.isWildcarded() && !activeNodes.contains(nodeIdent)) {
+                synchronized (lockObj) {
+                    if (!activeNodes.contains(nodeIdent)) {
+                        Set<InstanceIdentifier<FlowCapableNode>> set = Sets.newHashSet(activeNodes);
+                        set.add(nodeIdent);
+                        activeNodes = Collections.unmodifiableSet(set);
+                        setNodeOperationalStatus(nodeIdent, true);
                     }
                 }
             }
@@ -196,54 +163,69 @@ public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<
     }
 
     @Override
-    public void close() {
+    public void close() throws Exception {
         if (listenerRegistration != null) {
-            try {
-                listenerRegistration.close();
-            } catch (Exception e) {
-                LOG.warn("Error occurred while closing operational Node listener: {}", e.getMessage());
-                LOG.debug("Error occurred while closing operational Node listener", e);
-            }
+            listenerRegistration.close();
             listenerRegistration = null;
         }
-        if (notifListenerRegistration != null) {
-            notifListenerRegistration.close();
+        if (mastershipChangeServiceRegistration != null) {
+            mastershipChangeServiceRegistration.close();
+            mastershipChangeServiceRegistration = null;
         }
     }
 
-
-    private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
-                                                  InstanceIdentifier<?> identifier2) {
-        return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
+    private boolean compareInstanceIdentifierTail(final InstanceIdentifier<?> identifier1,
+            final InstanceIdentifier<?> identifier2) {
+        return Iterables.getLast(identifier1.getPathArguments())
+                .equals(Iterables.getLast(identifier2.getPathArguments()));
     }
 
-    private void setNodeOperationalStatus(InstanceIdentifier<FlowCapableNode> nodeIid, boolean status) {
+    private void setNodeOperationalStatus(final InstanceIdentifier<FlowCapableNode> nodeIid, final boolean status) {
         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getId();
-        if (nodeId != null ) {
-            if (deviceMasterships.containsKey(nodeId) ) {
-                deviceMasterships.get(nodeId).setDeviceOperationalStatus(status);
-                LOG.debug("Operational status of device {} is set to {}",nodeId, status);
-            }
+        if (nodeId != null && deviceMasterships.containsKey(nodeId)) {
+            deviceMasterships.get(nodeId).setDeviceOperationalStatus(status);
+            LOG.debug("Operational status of device {} is set to {}", nodeId, status);
         }
     }
-    private void registerNodeListener(){
+
+    @SuppressWarnings("IllegalCatch")
+    private void registerNodeListener() {
 
         final InstanceIdentifier<FlowCapableNode> flowNodeWildCardIdentifier = InstanceIdentifier.create(Nodes.class)
                 .child(Node.class).augmentation(FlowCapableNode.class);
 
-        final DataTreeIdentifier<FlowCapableNode> treeId =
-                new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, flowNodeWildCardIdentifier);
+        final DataTreeIdentifier<FlowCapableNode> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+                flowNodeWildCardIdentifier);
 
         try {
             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
 
-            listenerRegistration = looper.loopUntilNoException(() ->
-                    dataBroker.registerDataTreeChangeListener(treeId, DeviceMastershipManager.this));
+            listenerRegistration = looper.loopUntilNoException(
+                () -> dataBroker.registerDataTreeChangeListener(treeId, DeviceMastershipManager.this));
         } catch (Exception e) {
             LOG.warn("Data listener registration failed: {}", e.getMessage());
             LOG.debug("Data listener registration failed ", e);
             throw new IllegalStateException("Node listener registration failed!", e);
         }
     }
+
+    @Override
+    public void onBecomeOwner(@Nonnull final DeviceInfo deviceInfo) {
+        LOG.debug("Mastership role notification received for device : {}", deviceInfo.getDatapathId());
+        DeviceMastership membership = deviceMasterships.computeIfAbsent(deviceInfo.getNodeId(),
+            device -> new DeviceMastership(deviceInfo.getNodeId()));
+        membership.reconcile();
+        membership.registerReconciliationRpc(rpcProviderService, reconcliationService);
+    }
+
+    @Override
+    public void onLoseOwnership(@Nonnull final DeviceInfo deviceInfo) {
+        final DeviceMastership mastership = deviceMasterships.remove(deviceInfo.getNodeId());
+        if (mastership != null) {
+            mastership.deregisterReconciliationRpc();
+            mastership.close();
+            LOG.debug("Unregistered deviceMastership for device : {}", deviceInfo.getNodeId());
+        }
+    }
 }