Bump upstreams
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / config / BmpDeployerImpl.java
index 5744621d7d8a2b98e4e59ed9204d297f7b5d95c6..499d3f0cdf613d0726e87c6236ef6b658eb5c03a 100644 (file)
@@ -10,30 +10,42 @@ package org.opendaylight.protocol.bmp.impl.config;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.FutureCallback;
+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.concurrent.GuardedBy;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+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;
 import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev170517.OdlBmpMonitors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev170517.odl.bmp.monitors.BmpMonitorConfig;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev170517.server.config.Server;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.BmpMonitor;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.MonitorId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.bmp.monitor.Monitor;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev200120.OdlBmpMonitors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev200120.odl.bmp.monitors.BmpMonitorConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev200120.server.config.Server;
+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.Registration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -41,44 +53,72 @@ 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;
 
-public class BmpDeployerImpl implements ClusteredDataTreeChangeListener<OdlBmpMonitors>, AutoCloseable {
+@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 final static 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 DOMDataWriteTransaction wTx = this.bmpDeployerDependencies.getDomDataBroker().newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
         wTx.merge(LogicalDatastoreType.OPERATIONAL, BMP_MONITOR_YII, EMPTY_PARENT_NODE);
-        wTx.submit();
-        this.registration = this.bmpDeployerDependencies.getDataBroker().registerDataTreeChangeListener(
-                new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, ODL_BMP_MONITORS_IID), this);
+        wTx.commit().addCallback(new FutureCallback<CommitInfo>() {
+            @Override
+            public void onSuccess(final CommitInfo result) {
+                LOG.trace("Successful commit");
+            }
+
+            @Override
+            public void onFailure(final Throwable trw) {
+                LOG.error("Failed commit", trw);
+            }
+        }, MoreExecutors.directExecutor());
+        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<DataObjectModification<? extends DataObject>> rootNode = dataTreeModification.getRootNode()
-                .getModifiedChildren();
+        final Collection<? extends DataObjectModification<? extends DataObject>> rootNode =
+            dataTreeModification.getRootNode().getModifiedChildren();
         if (rootNode.isEmpty()) {
             return;
         }
@@ -101,9 +141,10 @@ public class BmpDeployerImpl implements ClusteredDataTreeChangeListener<OdlBmpMo
         }
     }
 
+    @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);
@@ -113,17 +154,19 @@ public class BmpDeployerImpl implements ClusteredDataTreeChangeListener<OdlBmpMo
             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);
         }
 
     }
 
+    @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 {
@@ -135,9 +178,12 @@ public class BmpDeployerImpl implements ClusteredDataTreeChangeListener<OdlBmpMo
     }
 
     @Override
+    @Deactivate
+    @PreDestroy
     public synchronized void close() {
-        if (this.registration != null) {
-            this.registration.close();
+        if (registration != null) {
+            registration.close();
+            registration = null;
         }
     }
 }