Bug 8902 - Changes in FRM for Reconciliation Framework
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastership.java
index 54360a005dee373773f0d9fc2ff5f98da867413a..ebf9f3686d4f5e01d9f2eb60317873c328c0e8cb 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
+ * 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,
@@ -10,11 +10,15 @@ package org.opendaylight.openflowplugin.applications.frm.impl;
 
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+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.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -25,26 +29,27 @@ public class DeviceMastership implements ClusterSingletonService, AutoCloseable
     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
     private final NodeId nodeId;
     private final ServiceGroupIdentifier identifier;
-    private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
-    private boolean deviceMastered;
+    private final AtomicBoolean deviceMastered = new AtomicBoolean(false);
+    private final AtomicBoolean isDeviceInOperDS = new AtomicBoolean(false);
+    private final InstanceIdentifier<FlowCapableNode> fcnIID;
 
-    public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
+    public DeviceMastership(final NodeId nodeId) {
         this.nodeId = nodeId;
         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
-        this.deviceMastered = false;
-        clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
+        fcnIID = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId)).augmentation
+                (FlowCapableNode.class);
     }
 
     @Override
     public void instantiateServiceInstance() {
-        LOG.debug("FRM started for: {}", nodeId.getValue());
-        deviceMastered = true;
+        LOG.info("FRM started for: {}", nodeId.getValue());
+        deviceMastered.set(true);
     }
 
     @Override
     public ListenableFuture<Void> closeServiceInstance() {
-        LOG.debug("FRM stopped for: {}", nodeId.getValue());
-        deviceMastered = false;
+        LOG.info("FRM stopped for: {}", nodeId.getValue());
+        deviceMastered.set(false);
         return Futures.immediateFuture(null);
     }
 
@@ -55,17 +60,16 @@ public class DeviceMastership implements ClusterSingletonService, AutoCloseable
 
     @Override
     public void close() {
-        if (clusterSingletonServiceRegistration != null) {
-            try {
-                clusterSingletonServiceRegistration.close();
-            } catch (Exception e) {
-                LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
-            }
-        }
     }
 
     public boolean isDeviceMastered() {
-        return deviceMastered;
+        return deviceMastered.get();
     }
 
+    public void setDeviceOperationalStatus(boolean inOperDS) {
+        isDeviceInOperDS.set(inOperDS);
+    }
+    public void reconcile() {
+        deviceMastered.set(true);
+    }
 }