KeyMapping is immutable
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / app / BmpMonitoringStationImpl.java
index b82579e95c03263618027e51d2a4eb3e4ea3be82..b79568c61099f134c215910a09ea526ec3a18ef5 100644 (file)
@@ -8,7 +8,6 @@
 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;
@@ -19,6 +18,8 @@ import io.netty.channel.ChannelFutureListener;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 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;
@@ -34,6 +35,7 @@ import org.opendaylight.protocol.bmp.api.BmpDispatcher;
 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.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;
@@ -71,55 +73,73 @@ public final class BmpMonitoringStationImpl implements BmpMonitoringStation, Clu
         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, extensions, codecTree);
+        sessionManager = new RouterSessionManager(yangMonitorId, this.domDataBroker, extensions, codecTree);
 
         LOG.info("BMP Monitor Singleton Service {} registered, Monitor Id {}",
                 getIdentifier().getName(), this.monitorId.getValue());
-        this.singletonServiceRegistration = singletonProvider.registerClusterSingletonService(this);
+        singletonServiceRegistration = singletonProvider.registerClusterSingletonService(this);
     }
 
     @Override
     public synchronized void instantiateServiceInstance() {
         LOG.info("BMP Monitor Singleton Service {} instantiated, Monitor Id {}",
-                getIdentifier().getName(), 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.INSTANCE.inetAddressForNoZone(mr.getAddress()), password.getValue());
+                }
+            }
+        }
+
+        return KeyMapping.of(passwords);
     }
 
     @Override
     public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
         LOG.info("BMP Monitor Singleton Service {} instance closed, Monitor Id {}",
-                getIdentifier().getName(), 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 DOMDataTreeWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
-        wTx.delete(LogicalDatastoreType.OPERATIONAL, this.yangMonitorId);
-        LOG.info("BMP monitoring station {} closed.", this.monitorId.getValue());
+        final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
+        wTx.delete(LogicalDatastoreType.OPERATIONAL, yangMonitorId);
+        LOG.info("BMP monitoring station {} closed.", monitorId.getValue());
         return wTx.commit();
     }
 
@@ -129,8 +149,8 @@ public final class BmpMonitoringStationImpl implements BmpMonitoringStation, Clu
     }
 
     private void connectMonitoredRouters(final BmpDispatcher pdispatcher) {
-        if (this.monitoredRouters != null) {
-            for (final MonitoredRouter mr : this.monitoredRouters) {
+        if (monitoredRouters != null) {
+            for (final MonitoredRouter mr : monitoredRouters) {
                 if (mr.getActive()) {
                     requireNonNull(mr.getAddress());
                     requireNonNull(mr.getPort());
@@ -138,35 +158,35 @@ public final class BmpMonitoringStationImpl implements BmpMonitoringStation, Clu
                     final InetAddress addr = InetAddresses.forString(s);
                     final KeyMapping ret;
                     final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
-                    ret = KeyMapping.getKeyMapping(addr, rfc2385KeyPassword.getValue());
+                    ret = KeyMapping.of(addr, rfc2385KeyPassword.getValue());
                     pdispatcher.createClient(Ipv4Util.toInetSocketAddress(mr.getAddress(), mr.getPort()),
-                            this.sessionManager, ret);
+                            sessionManager, ret);
                 }
             }
         }
     }
 
     private synchronized void createEmptyMonitor() {
-        final DOMDataTreeWriteTransaction 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.commit().get();
         } catch (final ExecutionException | InterruptedException e) {
-            LOG.error("Failed to initiate BMP Monitor {}.", this.monitorId.getValue(), 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;
         }
     }
 }