Activation status handling mechanism for ForwardingConstruct provisioning
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / ForwardingConstructChangeListener.java
index 729d847ffc871df3836349a675628a3c12a92cdd..44af5d31198feca4759506a45add2ed6f3fba873 100644 (file)
@@ -9,11 +9,7 @@ package org.opendaylight.unimgr.impl;
 
 import java.util.Collection;
 
-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.DataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.binding.api.*;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriverRepoService;
 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.ForwardingConstructs;
@@ -29,16 +25,21 @@ import org.slf4j.LoggerFactory;
  */
 public class ForwardingConstructChangeListener implements DataTreeChangeListener<ForwardingConstruct>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(ForwardingConstructChangeListener.class);
+
     private final ListenerRegistration<ForwardingConstructChangeListener> listener;
+
     private final ForwardingConstructActivatorService routeActivator;
 
     private final ActivationDriverRepoService activationRepoService;
 
+    private final DataBroker dataBroker;
+
     public ForwardingConstructChangeListener(DataBroker dataBroker, ActivationDriverRepoService activationRepoService) {
+        this.dataBroker  = dataBroker;
         this.activationRepoService = activationRepoService;
         routeActivator = new ForwardingConstructActivatorService(activationRepoService);
 
-        final InstanceIdentifier<ForwardingConstruct> fwPath = getFwConstructsPath();
+        final InstanceIdentifier<ForwardingConstruct> fwPath = getFcPath();
         final DataTreeIdentifier<ForwardingConstruct> dataTreeIid =
                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fwPath);
         listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
@@ -59,6 +60,7 @@ public class ForwardingConstructChangeListener implements DataTreeChangeListener
         }
         for (final DataTreeModification<ForwardingConstruct> change : collection) {
             final DataObjectModification<ForwardingConstruct> root = change.getRootNode();
+
             switch (root.getModificationType()) {
                 case SUBTREE_MODIFIED:
                     update(change);
@@ -86,14 +88,19 @@ public class ForwardingConstructChangeListener implements DataTreeChangeListener
     protected void add(DataTreeModification<ForwardingConstruct> newDataObject) {
         //TODO: Refine the logged addition
         LOG.debug("FcRoute add event received {}", newDataObject);
-        routeActivator.activate(newDataObject.getRootNode().getDataAfter());
-
+        routeActivator.activate(
+            getFcForActivation(newDataObject),
+            getFcActivationStateTracker(newDataObject)
+        );
     }
 
     protected void remove(DataTreeModification<ForwardingConstruct> removedDataObject) {
         //TODO: Refine the logged removal
         LOG.debug("FcRoute remove event received {}", removedDataObject);
-        routeActivator.deactivate(removedDataObject.getRootNode().getDataBefore());
+        routeActivator.deactivate(
+            getFcForDeactivation(removedDataObject),
+            getFcActivationStateTracker(removedDataObject)
+        );
 
     }
 
@@ -102,8 +109,9 @@ public class ForwardingConstructChangeListener implements DataTreeChangeListener
         LOG.debug("FcRoute update event received {}", modifiedDataObject);
 
         //TODO for the moment transactional nature of this action is ignored :P
-        routeActivator.deactivate(modifiedDataObject.getRootNode().getDataBefore());
-        routeActivator.activate(modifiedDataObject.getRootNode().getDataAfter());
+        ForwardingConstructActivationStateTracker stateTracker = getFcActivationStateTracker(modifiedDataObject);
+        routeActivator.deactivate(getFcForDeactivation(modifiedDataObject), stateTracker);
+        routeActivator.activate(getFcForActivation(modifiedDataObject), stateTracker);
     }
 
     @Override
@@ -111,9 +119,26 @@ public class ForwardingConstructChangeListener implements DataTreeChangeListener
         listener.close();
     }
 
-
-    private InstanceIdentifier<ForwardingConstruct> getFwConstructsPath() {
+    private InstanceIdentifier<ForwardingConstruct> getFcPath() {
         return InstanceIdentifier
                 .builder(ForwardingConstructs.class).child(ForwardingConstruct.class).build();
     }
+
+    private InstanceIdentifier<ForwardingConstruct> getFcIid(DataTreeModification<ForwardingConstruct> fcModification) {
+        return fcModification.getRootPath().getRootIdentifier();
+    }
+
+    private ForwardingConstruct getFcForActivation(DataTreeModification<ForwardingConstruct> fcModification) {
+        return fcModification.getRootNode().getDataAfter();
+    }
+
+    private ForwardingConstruct getFcForDeactivation(DataTreeModification<ForwardingConstruct> fcModification) {
+        return fcModification.getRootNode().getDataBefore();
+    }
+
+
+    private ForwardingConstructActivationStateTracker getFcActivationStateTracker(
+            DataTreeModification<ForwardingConstruct> fcModification) {
+        return new ForwardingConstructActivationStateTracker(dataBroker, getFcIid(fcModification));
+    }
 }