Clustering support for existing design (FRM,IM,SM) - add retry mechanisms to avoid...
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyProvider.java
index 58ab7ff09d56b6aff65e0497034f898342714bc8..66168949ff76176ea6b4720a82905c4efd1f1328 100644 (file)
@@ -8,11 +8,15 @@
 package org.opendaylight.openflowplugin.applications.topology.manager;
 
 import java.util.concurrent.ExecutionException;
+
+import com.google.common.base.Optional;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
+import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
@@ -22,12 +26,11 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
-import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider implements AutoCloseable {
-    private final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
+public class FlowCapableTopologyProvider implements BindingAwareProvider, AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
     private ListenerRegistration<NotificationListener> listenerRegistration;
     private Thread thread;
     private TerminationPointChangeListenerImpl terminationPointChangeListener;
@@ -55,12 +58,14 @@ public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider im
         this.terminationPointChangeListener = new TerminationPointChangeListenerImpl(dataBroker, processor);
         nodeChangeListener = new NodeChangeListenerImpl(dataBroker, processor);
 
-        final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
-        tx.put(LogicalDatastoreType.OPERATIONAL, path, new TopologyBuilder().setKey(key).build(), true);
-        try {
-            tx.submit().get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Initial topology export failed, continuing anyway", e);
+        if(!isFlowTopologyExist(dataBroker, path)){
+            final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
+            tx.put(LogicalDatastoreType.OPERATIONAL, path, new TopologyBuilder().setKey(key).build(), true);
+            try {
+                tx.submit().get();
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.warn("Initial topology export failed, continuing anyway", e);
+            }
         }
 
         thread = new Thread(processor);
@@ -90,7 +95,7 @@ public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider im
         }
     }
 
-    private void unregisterListener(final AutoCloseable listenerToClose) {
+    private static void unregisterListener(final AutoCloseable listenerToClose) {
         if (listenerToClose != null) {
             try {
                 listenerToClose.close();
@@ -101,18 +106,18 @@ public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider im
         }
     }
 
-    /**
-     * Gets called during stop bundle
-     *
-     * @param context The execution context of the bundle being stopped.
-     */
-    @Override
-    public void stopImpl(final BundleContext context) {
+    private boolean isFlowTopologyExist(final DataBroker dataBroker,
+                                        final InstanceIdentifier<Topology> path) {
+        final ReadTransaction tx = dataBroker.newReadOnlyTransaction();
         try {
-            this.close();
-        } catch (InterruptedException e) {
-            LOG.warn("Failed to stop provider: {}", e.getMessage());
-            LOG.debug("Failed to stop provider.. ", e);
+            Optional<Topology> ofTopology = tx.read(LogicalDatastoreType.OPERATIONAL, path).checkedGet();
+            LOG.debug("OpenFlow topology exist in the operational data store at {}",path);
+            if(ofTopology.isPresent()){
+                return true;
+            }
+        } catch (ReadFailedException e) {
+            LOG.warn("OpenFlow topology read operation failed!", e);
         }
+        return false;
     }
 }