Bump upstreams
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / app / BmpMonitoringStationImpl.java
index 82f5477b87ee4612f0e5b0d3898a85e0136b4c64..10e01044fd6ad47fdd5d7cf0e829aa4c33cdaeb7 100644 (file)
@@ -5,39 +5,42 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bmp.impl.app;
 
 import static java.util.Objects.requireNonNull;
-import static org.opendaylight.protocol.bmp.impl.app.KeyConstructorUtil.constructKeys;
 
 import com.google.common.base.Preconditions;
 import com.google.common.net.InetAddresses;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.FluentFuture;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.util.List;
-import javax.annotation.Nonnull;
-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.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+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.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
-import org.opendaylight.protocol.bmp.impl.config.BmpDeployerDependencies;
 import org.opendaylight.protocol.bmp.impl.spi.BmpMonitoringStation;
 import org.opendaylight.protocol.concepts.KeyMapping;
 import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev170517.odl.bmp.monitors.bmp.monitor.config.MonitoredRouter;
-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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.routers.Router;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev200120.odl.bmp.monitors.bmp.monitor.config.MonitoredRouter;
+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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.routers.Router;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -56,118 +59,134 @@ public final class BmpMonitoringStationImpl implements BmpMonitoringStation, Clu
     private final DOMDataBroker domDataBroker;
     private final InetSocketAddress address;
     private final MonitorId monitorId;
-    private final List<MonitoredRouter> monitoredRouters;
+    private final Collection<MonitoredRouter> monitoredRouters;
     private final BmpDispatcher dispatcher;
     private final RouterSessionManager sessionManager;
     private final YangInstanceIdentifier yangMonitorId;
     private Channel channel;
     private ClusterSingletonServiceRegistration singletonServiceRegistration;
 
-    public BmpMonitoringStationImpl(final BmpDeployerDependencies bmpDeployerDependencies,
-            final BmpDispatcher dispatcher, final MonitorId monitorId, final InetSocketAddress address,
-            final List<MonitoredRouter> mrs) {
-        this.domDataBroker = requireNonNull(bmpDeployerDependencies.getDomDataBroker());
+    public BmpMonitoringStationImpl(final DOMDataBroker domDataBroker, final BmpDispatcher dispatcher,
+            final RIBExtensionConsumerContext extensions, final BindingCodecTree codecTree,
+            final ClusterSingletonServiceProvider singletonProvider, final MonitorId monitorId,
+            final InetSocketAddress address, final Collection<MonitoredRouter> mrs) {
+        this.domDataBroker = requireNonNull(domDataBroker);
         this.dispatcher = requireNonNull(dispatcher);
         this.monitorId = monitorId;
-        this.monitoredRouters = mrs;
+        monitoredRouters = mrs;
         this.address = requireNonNull(address);
 
-        this.yangMonitorId = YangInstanceIdentifier.builder()
+        yangMonitorId = YangInstanceIdentifier.builder()
                 .node(BmpMonitor.QNAME).node(Monitor.QNAME)
                 .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, monitorId.getValue()).build();
 
-        this.sessionManager = new RouterSessionManager(this.yangMonitorId, this.domDataBroker,
-                bmpDeployerDependencies.getExtensions(), bmpDeployerDependencies.getTree());
+        sessionManager = new RouterSessionManager(yangMonitorId, this.domDataBroker, extensions, codecTree);
 
         LOG.info("BMP Monitor Singleton Service {} registered, Monitor Id {}",
-                getIdentifier().getValue(), this.monitorId.getValue());
-        this.singletonServiceRegistration = bmpDeployerDependencies.getClusterSingletonProvider()
-                .registerClusterSingletonService(this);
+                getIdentifier().getName(), this.monitorId.getValue());
+        singletonServiceRegistration = singletonProvider.registerClusterSingletonService(this);
     }
 
     @Override
     public synchronized void instantiateServiceInstance() {
         LOG.info("BMP Monitor Singleton Service {} instantiated, Monitor Id {}",
-                getIdentifier().getValue(), this.monitorId.getValue());
+                getIdentifier().getName(), monitorId.getValue());
 
-        final ChannelFuture channelFuture = this.dispatcher.createServer(this.address, this.sessionManager,
-                constructKeys(this.monitoredRouters));
+        final ChannelFuture channelFuture = dispatcher.createServer(address, sessionManager,
+                constructKeys(monitoredRouters));
         try {
-            this.channel = channelFuture.sync().channel();
+            channel = channelFuture.sync().channel();
             createEmptyMonitor();
-            LOG.info("BMP Monitoring station {} started", this.monitorId.getValue());
+            LOG.info("BMP Monitoring station {} started", monitorId.getValue());
 
-            connectMonitoredRouters(this.dispatcher);
+            connectMonitoredRouters(dispatcher);
             LOG.info("Connecting to monitored routers completed.");
         } catch (final InterruptedException e) {
-            LOG.error("Failed to instantiate BMP Monitor Singleton {}", this.monitorId.getValue(), e);
+            LOG.error("Failed to instantiate BMP Monitor Singleton {}", monitorId.getValue(), e);
+        }
+
+    }
+
+    private static KeyMapping constructKeys(final Collection<MonitoredRouter> mrs) {
+        if (mrs == null || mrs.isEmpty()) {
+            return KeyMapping.of();
+        }
+
+        final Map<InetAddress, String> passwords = new HashMap<>();
+        for (MonitoredRouter mr : mrs) {
+            if (mr != null) {
+                final Rfc2385Key password = mr.getPassword();
+                if (password != null && !password.getValue().isEmpty()) {
+                    passwords.put(IetfInetUtil.inetAddressForNoZone(mr.getAddress()), password.getValue());
+                }
+            }
         }
 
+        return KeyMapping.of(passwords);
     }
 
     @Override
-    public synchronized ListenableFuture<Void> closeServiceInstance() {
+    public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
         LOG.info("BMP Monitor Singleton Service {} instance closed, Monitor Id {}",
-                getIdentifier().getValue(), this.monitorId.getValue());
-        if (this.channel != null) {
-            this.channel.close().addListener((ChannelFutureListener) future -> {
+                getIdentifier().getName(), monitorId.getValue());
+        if (channel != null) {
+            channel.close().addListener((ChannelFutureListener) future -> {
                 Preconditions.checkArgument(future.isSuccess(),
                         "Channel failed to close: %s", future.cause());
                 BmpMonitoringStationImpl.this.sessionManager.close();
             });
         }
 
-        final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
-        wTx.delete(LogicalDatastoreType.OPERATIONAL, this.yangMonitorId);
-        LOG.info("BMP monitoring station {} closed.", this.monitorId.getValue());
-        return wTx.submit();
+        final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
+        wTx.delete(LogicalDatastoreType.OPERATIONAL, yangMonitorId);
+        LOG.info("BMP monitoring station {} closed.", monitorId.getValue());
+        return wTx.commit();
     }
 
-    @Nonnull
     @Override
     public ServiceGroupIdentifier getIdentifier() {
         return SERVICE_GROUP_IDENTIFIER;
     }
 
-    private void connectMonitoredRouters(final BmpDispatcher dispatcher) {
-        if (this.monitoredRouters != null) {
-            for (final MonitoredRouter mr : this.monitoredRouters) {
-                if (mr.isActive()) {
+    private void connectMonitoredRouters(final BmpDispatcher pdispatcher) {
+        if (monitoredRouters != null) {
+            for (final MonitoredRouter mr : monitoredRouters) {
+                if (mr.getActive()) {
                     requireNonNull(mr.getAddress());
                     requireNonNull(mr.getPort());
-                    final String s = mr.getAddress().getIpv4Address().getValue();
+                    final String s = mr.getAddress().getIpv4AddressNoZone().getValue();
                     final InetAddress addr = InetAddresses.forString(s);
                     final KeyMapping ret;
                     final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
-                    ret = KeyMapping.getKeyMapping(addr, rfc2385KeyPassword.getValue());
-                    dispatcher.createClient(Ipv4Util.toInetSocketAddress(mr.getAddress(), mr.getPort()),
-                            this.sessionManager, ret);
+                    ret = KeyMapping.of(addr, rfc2385KeyPassword.getValue());
+                    pdispatcher.createClient(Ipv4Util.toInetSocketAddress(mr.getAddress(), mr.getPort()),
+                            sessionManager, ret);
                 }
             }
         }
     }
 
     private synchronized void createEmptyMonitor() {
-        final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
         wTx.put(LogicalDatastoreType.OPERATIONAL,
                 YangInstanceIdentifier.builder().node(BmpMonitor.QNAME).node(Monitor.QNAME)
-                        .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue()).build(),
-                ImmutableNodes.mapEntryBuilder(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue())
-                        .addChild(ImmutableNodes.leafNode(MONITOR_ID_QNAME, this.monitorId.getValue()))
+                        .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, monitorId.getValue()).build(),
+                ImmutableNodes.mapEntryBuilder(Monitor.QNAME, MONITOR_ID_QNAME, monitorId.getValue())
+                        .addChild(ImmutableNodes.leafNode(MONITOR_ID_QNAME, monitorId.getValue()))
                         .addChild(ImmutableNodes.mapNodeBuilder(Router.QNAME).build())
                         .build());
         try {
-            wTx.submit().checkedGet();
-        } catch (final TransactionCommitFailedException e) {
-            LOG.error("Failed to initiate BMP Monitor {}.", this.monitorId.getValue(), e);
+            wTx.commit().get();
+        } catch (final ExecutionException | InterruptedException e) {
+            LOG.error("Failed to initiate BMP Monitor {}.", monitorId.getValue(), e);
         }
     }
 
     @Override
     public synchronized void close() throws Exception {
-        if (this.singletonServiceRegistration != null) {
-            this.singletonServiceRegistration.close();
-            this.singletonServiceRegistration = null;
+        if (singletonServiceRegistration != null) {
+            singletonServiceRegistration.close();
+            singletonServiceRegistration = null;
         }
     }
 }