Fix transaction manager closing.
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyProvider.java
index 66168949ff76176ea6b4720a82905c4efd1f1328..83942007b8f9a2503db44d32adf31014509e5ddc 100644 (file)
@@ -7,17 +7,12 @@
  */
 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.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.openflowplugin.common.txchain.TransactionChainManager;
 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;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
@@ -29,54 +24,56 @@ import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class FlowCapableTopologyProvider implements BindingAwareProvider, AutoCloseable {
+public class FlowCapableTopologyProvider implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
-    private ListenerRegistration<NotificationListener> listenerRegistration;
-    private Thread thread;
-    private TerminationPointChangeListenerImpl terminationPointChangeListener;
-    private NodeChangeListenerImpl nodeChangeListener;
+    private static final String TOPOLOGY_PROVIDER = "topology-provider";
     static final String TOPOLOGY_ID = "flow:1";
 
+
+    private final DataBroker dataBroker;
+    private final NotificationProviderService notificationService;
+    private final OperationProcessor processor;
+    private TransactionChainManager transactionChainManager;
+    private ListenerRegistration<NotificationListener> listenerRegistration;
+
+    public FlowCapableTopologyProvider(DataBroker dataBroker, NotificationProviderService notificationService,
+            OperationProcessor processor) {
+        this.dataBroker = dataBroker;
+        this.notificationService = notificationService;
+        this.processor = processor;
+    }
+
     /**
      * Gets called on start of a bundle.
-     *
-     * @param session
      */
-    @Override
-    public synchronized void onSessionInitiated(final ProviderContext session) {
-        final DataBroker dataBroker = session.getSALService(DataBroker.class);
-        final NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class);
-
+    public void start() {
         final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
         final InstanceIdentifier<Topology> path = InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, key);
 
-        final OperationProcessor processor = new OperationProcessor(dataBroker);
         final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, path);
         this.listenerRegistration = notificationService.registerNotificationListener(listener);
-        this.terminationPointChangeListener = new TerminationPointChangeListenerImpl(dataBroker, processor);
-        nodeChangeListener = new NodeChangeListenerImpl(dataBroker, processor);
+        this.transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
+        this.transactionChainManager.activateTransactionManager();
+        this.transactionChainManager.initialSubmitWriteTransaction();
 
-        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);
-            }
+        if(!isFlowTopologyExist(path)){
+            transactionChainManager.writeToTransaction(
+                    LogicalDatastoreType.OPERATIONAL,
+                    path,
+                    new TopologyBuilder().setKey(key).build(),
+                    true);
+            transactionChainManager.submitTransaction();
         }
 
-        thread = new Thread(processor);
-        thread.setDaemon(true);
-        thread.setName("FlowCapableTopologyExporter-" + TOPOLOGY_ID);
-        thread.start();
+        LOG.info("FlowCapableTopologyProvider started");
     }
 
     @Override
-    public synchronized void close() throws InterruptedException {
+    public void close() {
         LOG.info("FlowCapableTopologyProvider stopped.");
+        this.transactionChainManager.close();
         if (this.listenerRegistration != null) {
             try {
                 this.listenerRegistration.close();
@@ -86,31 +83,13 @@ public class FlowCapableTopologyProvider implements BindingAwareProvider, AutoCl
             }
             listenerRegistration = null;
         }
-        unregisterListener(terminationPointChangeListener);
-        unregisterListener(nodeChangeListener);
-        if (thread != null) {
-            thread.interrupt();
-            thread.join();
-            thread = null;
-        }
-    }
-
-    private static void unregisterListener(final AutoCloseable listenerToClose) {
-        if (listenerToClose != null) {
-            try {
-                listenerToClose.close();
-            } catch (Exception e) {
-                LOG.warn("Failed to close listener registration: {}", e.getMessage());
-                LOG.debug("Failed to close listener registration.. ", e);
-            }
-        }
     }
 
-    private boolean isFlowTopologyExist(final DataBroker dataBroker,
-                                        final InstanceIdentifier<Topology> path) {
-        final ReadTransaction tx = dataBroker.newReadOnlyTransaction();
+    private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
         try {
-            Optional<Topology> ofTopology = tx.read(LogicalDatastoreType.OPERATIONAL, path).checkedGet();
+            Optional<Topology> ofTopology = this.transactionChainManager
+                    .readFromTransaction(LogicalDatastoreType.OPERATIONAL, path)
+                    .checkedGet();
             LOG.debug("OpenFlow topology exist in the operational data store at {}",path);
             if(ofTopology.isPresent()){
                 return true;