Bump upstreams
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / config / BmpDeployerImpl.java
index 8bd4c83886ada9eb94e3ba9c6e47dd0c53cc0f22..499d3f0cdf613d0726e87c6236ef6b658eb5c03a 100644 (file)
@@ -15,17 +15,26 @@ import com.google.common.util.concurrent.MoreExecutors;
 import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataObjectModification;
 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
+import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
 import org.opendaylight.protocol.bmp.impl.app.BmpMonitoringStationImpl;
 import org.opendaylight.protocol.bmp.impl.spi.BmpMonitoringStation;
@@ -36,7 +45,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.moni
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.BmpMonitor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.MonitorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.bmp.monitor.Monitor;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -44,35 +53,51 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+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
+@Component(service = {})
 public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<OdlBmpMonitors>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(BmpDeployerImpl.class);
 
     private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
     private static final InstanceIdentifier<OdlBmpMonitors> ODL_BMP_MONITORS_IID =
             InstanceIdentifier.create(OdlBmpMonitors.class);
-    private static final YangInstanceIdentifier BMP_MONITOR_YII =
-            YangInstanceIdentifier.of(BmpMonitor.QNAME);
-    private static final ContainerNode EMPTY_PARENT_NODE = Builders.containerBuilder().withNodeIdentifier(
-            new NodeIdentifier(BmpMonitor.QNAME)).addChild(ImmutableNodes.mapNodeBuilder(Monitor.QNAME)
-            .build()).build();
+    private static final YangInstanceIdentifier BMP_MONITOR_YII = YangInstanceIdentifier.of(BmpMonitor.QNAME);
+    private static final ContainerNode EMPTY_PARENT_NODE = Builders.containerBuilder()
+        .withNodeIdentifier(new NodeIdentifier(BmpMonitor.QNAME))
+        .addChild(ImmutableNodes.mapNodeBuilder(Monitor.QNAME).build())
+        .build();
+
     private final BmpDispatcher dispatcher;
+    private final DOMDataBroker domDataBroker;
+    private final RIBExtensionConsumerContext extensions;
+    private final BindingCodecTree codecTree;
+    private final ClusterSingletonServiceProvider singletonProvider;
+
     @GuardedBy("this")
     private final Map<MonitorId, BmpMonitoringStationImpl> bmpMonitorServices = new HashMap<>();
-    private final BmpDeployerDependencies bmpDeployerDependencies;
     @GuardedBy("this")
-    private ListenerRegistration<BmpDeployerImpl> registration;
+    private Registration registration;
 
-    public BmpDeployerImpl(final BmpDispatcher dispatcher, final BmpDeployerDependencies bmpDeployerDependencies) {
+    @Activate
+    @Inject
+    public BmpDeployerImpl(@Reference final BmpDispatcher dispatcher, @Reference final DataBroker dataBroker,
+            @Reference final DOMDataBroker domDataBroker, @Reference final RIBExtensionConsumerContext extensions,
+            @Reference final BindingCodecTree codecTree,
+            @Reference final ClusterSingletonServiceProvider singletonProvider) {
         this.dispatcher = requireNonNull(dispatcher);
-        this.bmpDeployerDependencies = requireNonNull(bmpDeployerDependencies);
-    }
+        this.domDataBroker = requireNonNull(domDataBroker);
+        this.extensions = requireNonNull(extensions);
+        this.codecTree = requireNonNull(codecTree);
+        this.singletonProvider = requireNonNull(singletonProvider);
 
-    public synchronized void init() {
-        final DOMDataTreeWriteTransaction wTx = this.bmpDeployerDependencies
-            .getDomDataBroker().newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
         wTx.merge(LogicalDatastoreType.OPERATIONAL, BMP_MONITOR_YII, EMPTY_PARENT_NODE);
         wTx.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
@@ -85,12 +110,12 @@ public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<Od
                 LOG.error("Failed commit", trw);
             }
         }, MoreExecutors.directExecutor());
-        this.registration = this.bmpDeployerDependencies.getDataBroker().registerDataTreeChangeListener(
+        registration = dataBroker.registerDataTreeChangeListener(
             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ODL_BMP_MONITORS_IID), this);
     }
 
     @Override
-    public synchronized void onDataTreeChanged(final Collection<DataTreeModification<OdlBmpMonitors>> changes) {
+    public synchronized void onDataTreeChanged(final List<DataTreeModification<OdlBmpMonitors>> changes) {
         final DataTreeModification<OdlBmpMonitors> dataTreeModification = Iterables.getOnlyElement(changes);
         final Collection<? extends DataObjectModification<? extends DataObject>> rootNode =
             dataTreeModification.getRootNode().getModifiedChildren();
@@ -119,7 +144,7 @@ public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<Od
     @SuppressWarnings("checkstyle:IllegalCatch")
     private synchronized void updateBmpMonitor(final BmpMonitorConfig bmpConfig) {
         final MonitorId monitorId = bmpConfig.getMonitorId();
-        final BmpMonitoringStationImpl oldService = this.bmpMonitorServices.remove(monitorId);
+        final BmpMonitoringStationImpl oldService = bmpMonitorServices.remove(monitorId);
         try {
             if (oldService != null) {
                 oldService.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
@@ -129,9 +154,10 @@ public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<Od
             final Server server = bmpConfig.getServer();
             final InetSocketAddress inetAddress =
                     Ipv4Util.toInetSocketAddress(server.getBindingAddress(), server.getBindingPort());
-            final BmpMonitoringStationImpl monitor = new BmpMonitoringStationImpl(this.bmpDeployerDependencies,
-                    this.dispatcher, monitorId, inetAddress, bmpConfig.getMonitoredRouter());
-            this.bmpMonitorServices.put(monitorId, monitor);
+            final BmpMonitoringStationImpl monitor = new BmpMonitoringStationImpl(domDataBroker, dispatcher,
+                extensions, codecTree, singletonProvider, monitorId, inetAddress,
+                bmpConfig.nonnullMonitoredRouter().values());
+            bmpMonitorServices.put(monitorId, monitor);
         } catch (final Exception e) {
             LOG.error("Failed to create Bmp Monitor {}.", monitorId, e);
         }
@@ -140,7 +166,7 @@ public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<Od
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     private synchronized void removeBmpMonitor(final MonitorId monitorId) {
-        final BmpMonitoringStation service = this.bmpMonitorServices.remove(monitorId);
+        final BmpMonitoringStation service = bmpMonitorServices.remove(monitorId);
         if (service != null) {
             LOG.debug("Closing Bmp Monitor {}.", monitorId);
             try {
@@ -152,10 +178,12 @@ public final class BmpDeployerImpl implements ClusteredDataTreeChangeListener<Od
     }
 
     @Override
+    @Deactivate
+    @PreDestroy
     public synchronized void close() {
-        if (this.registration != null) {
-            this.registration.close();
-            this.registration = null;
+        if (registration != null) {
+            registration.close();
+            registration = null;
         }
     }
 }