KeyMapping is immutable
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / config / PCEPTopologyConfiguration.java
index cef1cab4c98795ac0b7adbc565f444f724309040..698c7d05842f107e400eaf5425849128578d632d 100644 (file)
@@ -13,11 +13,15 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.net.InetAddresses;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.util.HashMap;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.protocol.concepts.KeyMapping;
 import org.opendaylight.protocol.pcep.SpeakerIdMapping;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.config.rev200120.pcep.config.SessionConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.config.rev181109.PcepNodeConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.sync.optimizations.config.rev181109.PcepNodeSyncConfig;
@@ -27,33 +31,30 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 
-public final class PCEPTopologyConfiguration {
-    private final InetSocketAddress address;
-    private final KeyMapping keys;
-    private final TopologyId topologyId;
+public final class PCEPTopologyConfiguration implements Immutable {
+    private final @NonNull InetSocketAddress address;
+    private final @NonNull KeyMapping keys;
     private final short rpcTimeout;
     private final @NonNull SpeakerIdMapping speakerIds;
-    private final InstanceIdentifier<Topology> topology;
+    private final @NonNull KeyedInstanceIdentifier<Topology, TopologyKey> topology;
 
     public PCEPTopologyConfiguration(final @NonNull SessionConfig config, final @NonNull Topology topology) {
-        requireNonNull(topology);
-        address = PCEPTopologyProviderUtil.getInetSocketAddress(requireNonNull(config.getListenAddress()),
-                requireNonNull(config.getListenPort()));
-        keys = requireNonNull(PCEPTopologyProviderUtil.contructKeys(topology));
+        this.topology = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology.key());
+        address = getInetSocketAddress(config.getListenAddress(), config.getListenPort());
+        keys = constructKeys(topology.getNode());
         speakerIds = contructSpeakersId(topology.getNode());
-        topologyId = requireNonNull(topology.getTopologyId());
         rpcTimeout = config.getRpcTimeout();
-        this.topology = InstanceIdentifier.builder(NetworkTopology.class)
-                .child(Topology.class, new TopologyKey(topologyId)).build();
     }
 
     public @NonNull TopologyId getTopologyId() {
-        return topologyId;
+        return topology.getKey().getTopologyId();
     }
 
-    public @NonNull InstanceIdentifier<Topology> getTopology() {
+    public @NonNull KeyedInstanceIdentifier<Topology, TopologyKey> getTopology() {
         return topology;
     }
 
@@ -73,6 +74,33 @@ public final class PCEPTopologyConfiguration {
         return speakerIds;
     }
 
+    private static @NonNull KeyMapping constructKeys(final @Nullable Map<NodeKey, Node> nodes) {
+        if (nodes == null) {
+            return KeyMapping.of();
+        }
+
+        final var passwords = new HashMap<InetAddress, String>();
+        for (var node : nodes.values()) {
+            if (node != null) {
+                final var nodeConfig = node.augmentation(PcepNodeConfig.class);
+                if (nodeConfig != null) {
+                    final var sessionConfig = nodeConfig.getSessionConfig();
+                    if (sessionConfig != null) {
+                        final var rfc2385KeyPassword = sessionConfig.getPassword();
+                        if (rfc2385KeyPassword != null) {
+                            final String password = rfc2385KeyPassword.getValue();
+                            if (!password.isEmpty()) {
+                                passwords.put(nodeAddress(node), password);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        return KeyMapping.of(passwords);
+    }
+
     private static @NonNull SpeakerIdMapping contructSpeakersId(final @Nullable Map<NodeKey, Node> nodes) {
         if (nodes == null) {
             return SpeakerIdMapping.of();
@@ -89,7 +117,7 @@ public final class PCEPTopologyConfiguration {
                         if (nodeSyncConfig != null) {
                             final var speakerEntityId = nodeSyncConfig.getSpeakerEntityIdValue();
                             if (speakerEntityId != null) {
-                                builder.put(InetAddresses.forString(node.getNodeId().getValue()), speakerEntityId);
+                                builder.put(nodeAddress(node), speakerEntityId);
                             }
                         }
                     }
@@ -99,4 +127,14 @@ public final class PCEPTopologyConfiguration {
 
         return SpeakerIdMapping.copyOf(builder.build());
     }
+
+    private static InetAddress nodeAddress(final Node node) {
+        return InetAddresses.forString(node.getNodeId().getValue());
+    }
+
+    private static @NonNull InetSocketAddress getInetSocketAddress(final IpAddressNoZone address,
+            final PortNumber port) {
+        return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressForNoZone(requireNonNull(address)),
+            port.getValue().toJava());
+    }
 }