BGPCEP-710: Create Network Topology Loader
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / config / BmpDeployerImpl.java
index 0e452698915779056c5919b4276b3eba6d5ad4e8..5744621d7d8a2b98e4e59ed9204d297f7b5d95c6 100644 (file)
@@ -14,24 +14,21 @@ import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.HashMap;
 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.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
-import org.opendaylight.protocol.bmp.impl.api.BmpDeployer;
 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.odl.bmp.monitors.BmpMonitorConfigKey;
 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;
@@ -39,7 +36,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.moni
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -48,15 +44,16 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeListener<OdlBmpMonitors>, AutoCloseable {
+public 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);
+            InstanceIdentifier.create(OdlBmpMonitors.class);
     private static final YangInstanceIdentifier BMP_MONITOR_YII =
-        YangInstanceIdentifier.of(BmpMonitor.QNAME);
+            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();
+            new NodeIdentifier(BmpMonitor.QNAME)).addChild(ImmutableNodes.mapNodeBuilder(Monitor.QNAME).build()).build();
     private final BmpDispatcher dispatcher;
     @GuardedBy("this")
     private final Map<MonitorId, BmpMonitoringStationImpl> bmpMonitorServices = new HashMap<>();
@@ -69,7 +66,7 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
         this.bmpDeployerDependencies = requireNonNull(bmpDeployerDependencies);
     }
 
-    public synchronized void register() {
+    public synchronized void init() {
         final DOMDataWriteTransaction wTx = this.bmpDeployerDependencies.getDomDataBroker().newWriteOnlyTransaction();
         wTx.merge(LogicalDatastoreType.OPERATIONAL, BMP_MONITOR_YII, EMPTY_PARENT_NODE);
         wTx.submit();
@@ -81,14 +78,14 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
     public synchronized void onDataTreeChanged(final Collection<DataTreeModification<OdlBmpMonitors>> changes) {
         final DataTreeModification<OdlBmpMonitors> dataTreeModification = Iterables.getOnlyElement(changes);
         final Collection<DataObjectModification<? extends DataObject>> rootNode = dataTreeModification.getRootNode()
-            .getModifiedChildren();
+                .getModifiedChildren();
         if (rootNode.isEmpty()) {
             return;
         }
         rootNode.forEach(dto -> handleModification((DataObjectModification<BmpMonitorConfig>) dto));
     }
 
-    private void handleModification(final DataObjectModification<BmpMonitorConfig> config) {
+    private synchronized void handleModification(final DataObjectModification<BmpMonitorConfig> config) {
         final ModificationType modificationType = config.getModificationType();
         LOG.trace("Bmp Monitor configuration has changed: {}, type modification {}", config, modificationType);
         switch (modificationType) {
@@ -104,12 +101,12 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
         }
     }
 
-    private void updateBmpMonitor(final BmpMonitorConfig bmpConfig) {
+    private synchronized void updateBmpMonitor(final BmpMonitorConfig bmpConfig) {
         final MonitorId monitorId = bmpConfig.getMonitorId();
         final BmpMonitoringStationImpl oldService = this.bmpMonitorServices.remove(monitorId);
         try {
             if (oldService != null) {
-                oldService.closeServiceInstance().get();
+                oldService.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
                 oldService.close();
             }
 
@@ -117,7 +114,7 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
             final InetSocketAddress inetAddress =
                     Ipv4Util.toInetSocketAddress(server.getBindingAddress(), server.getBindingPort());
             final BmpMonitoringStationImpl monitor = new BmpMonitoringStationImpl(this.bmpDeployerDependencies,
-                this.dispatcher, monitorId, inetAddress, bmpConfig.getMonitoredRouter());
+                    this.dispatcher, monitorId, inetAddress, bmpConfig.getMonitoredRouter());
             this.bmpMonitorServices.put(monitorId, monitor);
         } catch (final Exception e) {
             LOG.error("Failed to create Bmp Monitor {}.", monitorId, e);
@@ -125,7 +122,7 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
 
     }
 
-    private void removeBmpMonitor(final MonitorId monitorId) {
+    private synchronized void removeBmpMonitor(final MonitorId monitorId) {
         final BmpMonitoringStation service = this.bmpMonitorServices.remove(monitorId);
         if (service != null) {
             LOG.debug("Closing Bmp Monitor {}.", monitorId);
@@ -138,28 +135,7 @@ public class BmpDeployerImpl implements BmpDeployer, ClusteredDataTreeChangeList
     }
 
     @Override
-    public synchronized void writeBmpMonitor(final BmpMonitorConfig bmpConfig) throws TransactionCommitFailedException {
-        final KeyedInstanceIdentifier<BmpMonitorConfig, BmpMonitorConfigKey> iid = ODL_BMP_MONITORS_IID
-            .child(BmpMonitorConfig.class, bmpConfig.getKey());
-
-        final WriteTransaction wTx = this.bmpDeployerDependencies.getDataBroker().newWriteOnlyTransaction();
-        wTx.put(LogicalDatastoreType.CONFIGURATION, iid, bmpConfig, true);
-        wTx.submit().checkedGet();
-    }
-
-    @Override
-    public synchronized void deleteBmpMonitor(final MonitorId monitorId)
-            throws TransactionCommitFailedException {
-        final KeyedInstanceIdentifier<BmpMonitorConfig, BmpMonitorConfigKey> iid = ODL_BMP_MONITORS_IID
-            .child(BmpMonitorConfig.class, new BmpMonitorConfigKey(monitorId));
-
-        final WriteTransaction wTx = this.bmpDeployerDependencies.getDataBroker().newWriteOnlyTransaction();
-        wTx.delete(LogicalDatastoreType.CONFIGURATION, iid);
-        wTx.submit().checkedGet();
-    }
-
-    @Override
-    public synchronized void close() throws Exception {
+    public synchronized void close() {
         if (this.registration != null) {
             this.registration.close();
         }