Migrate topology-manager to OSGi DS
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyProvider.java
index 6d418c8e2f580949a89275478b7c934bea8902e5..e8b1adf7fe6d8466fad59e9c4a66648a77cb7975 100644 (file)
@@ -7,16 +7,15 @@
  */
 package org.opendaylight.openflowplugin.applications.topology.manager;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-import javax.annotation.Nonnull;
-import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.apache.aries.blueprint.annotation.service.Reference;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.NotificationService;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
@@ -30,72 +29,65 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.NotificationListener;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class FlowCapableTopologyProvider implements ClusterSingletonService, AutoCloseable {
+@Component(service = { })
+public final class FlowCapableTopologyProvider implements ClusterSingletonService, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
     private static final String TOPOLOGY_PROVIDER = "ofp-topology-manager";
     static final String TOPOLOGY_ID = "flow:1";
 
-    private final DataBroker dataBroker;
-    private final NotificationService notificationService;
+    private final InstanceIdentifier<Topology> topologyPathIID;
+    private final TransactionChainManager transactionChainManager;
     private final OperationProcessor processor;
-    private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
-    private InstanceIdentifier<Topology> topologyPathIID;
-    private TransactionChainManager transactionChainManager;
-    private ListenerRegistration<NotificationListener> listenerRegistration;
+
+    private Registration listenerRegistration;
     private ClusterSingletonServiceRegistration singletonServiceRegistration;
 
     @Inject
+    @Activate
     public FlowCapableTopologyProvider(@Reference final DataBroker dataBroker,
-                                       @Reference final NotificationService notificationService,
-                                       final OperationProcessor processor,
-                                       @Reference final ClusterSingletonServiceProvider
-                                               clusterSingletonServiceProvider) {
-        this.dataBroker = dataBroker;
-        this.notificationService = notificationService;
-        this.processor = processor;
-        this.clusterSingletonServiceProvider = clusterSingletonServiceProvider;
-    }
-
-    /**
-     * Gets called on start of a bundle.
-     */
-    @PostConstruct
-    public void start() {
+            @Reference final NotificationService notificationService,
+            @Reference final ClusterSingletonServiceProvider clusterSingletonServiceProvider,
+            @Reference final OperationProcessor processor) {
+        this.processor = requireNonNull(processor);
         final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
-        this.topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
+        topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
 
-        final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, topologyPathIID);
-        this.listenerRegistration = notificationService.registerNotificationListener(listener);
-        this.transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
-        this.transactionChainManager.activateTransactionManager();
-        this.transactionChainManager.initialSubmitWriteTransaction();
-        this.singletonServiceRegistration = this.clusterSingletonServiceProvider.registerClusterSingletonService(this);
+        listenerRegistration = notificationService.registerCompositeListener(
+            new FlowCapableTopologyExporter(processor, topologyPathIID).toListener());
+        transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
+        transactionChainManager.activateTransactionManager();
+        transactionChainManager.initialSubmitWriteTransaction();
+        singletonServiceRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);
         LOG.info("Topology Manager service started.");
     }
 
-    @Override
     @PreDestroy
+    @Deactivate
+    @Override
     public void close() {
-        this.transactionChainManager.close();
-        if (this.listenerRegistration != null) {
+        transactionChainManager.close();
+        if (listenerRegistration != null) {
             LOG.info("Closing notification listener registration.");
-            this.listenerRegistration.close();
-            this.listenerRegistration = null;
+            listenerRegistration.close();
+            listenerRegistration = null;
         }
 
-        if (this.singletonServiceRegistration != null) {
+        if (singletonServiceRegistration != null) {
             LOG.info("Closing clustering singleton service registration.");
-            this.singletonServiceRegistration.close();
-            this.singletonServiceRegistration = null;
+            singletonServiceRegistration.close();
+            singletonServiceRegistration = null;
         }
-        LOG.debug("Topology Manager instance is stopped.");
+        LOG.info("Topology Manager instance is stopped.");
     }
 
     @Override
@@ -114,7 +106,6 @@ public class FlowCapableTopologyProvider implements ClusterSingletonService, Aut
         return Futures.immediateFuture(null);
     }
 
-    @Nonnull
     @Override
     public ServiceGroupIdentifier getIdentifier() {
         return ServiceGroupIdentifier.create(TOPOLOGY_PROVIDER);
@@ -122,7 +113,7 @@ public class FlowCapableTopologyProvider implements ClusterSingletonService, Aut
 
     private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
         try {
-            Optional<Topology> ofTopology = this.transactionChainManager
+            Optional<Topology> ofTopology = transactionChainManager
                     .readFromTransaction(LogicalDatastoreType.OPERATIONAL, path).get();
             LOG.debug("OpenFlow topology exist in the operational data store at {}", path);
             if (ofTopology.isPresent()) {