2 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.pcep.topology.provider;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.net.InetAddresses;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
15 import java.util.HashMap;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.protocol.concepts.KeyMapping;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev220720.graph.topology.GraphKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.Node1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TopologyTypes1;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
29 import org.opendaylight.yangtools.concepts.Immutable;
31 final class PCEPTopologyConfiguration implements Immutable {
32 private final @NonNull InetSocketAddress address;
33 private final @NonNull KeyMapping keys;
34 private final short rpcTimeout;
35 private final GraphKey graphKey;
37 PCEPTopologyConfiguration(final @NonNull InetSocketAddress address, final short rpcTimeout,
38 final @NonNull KeyMapping keys, final @NonNull GraphKey graphKey) {
39 this.address = requireNonNull(address);
40 this.keys = requireNonNull(keys);
41 this.rpcTimeout = rpcTimeout;
42 this.graphKey = requireNonNull(graphKey);
45 static @Nullable PCEPTopologyConfiguration of(final @NonNull Topology topology) {
46 final var types = topology.getTopologyTypes();
50 final var typesAug = types.augmentation(TopologyTypes1.class);
51 if (typesAug == null) {
54 final var topologyPcep = typesAug.getTopologyPcep();
55 if (topologyPcep == null) {
58 final var sessionConfig = topologyPcep.getSessionConfig();
59 if (sessionConfig == null) {
63 return new PCEPTopologyConfiguration(
64 getInetSocketAddress(sessionConfig.getListenAddress(), sessionConfig.getListenPort()),
65 sessionConfig.getRpcTimeout(), constructKeys(topology.getNode()),
66 constructGraphKey(sessionConfig.getTedName()));
69 short getRpcTimeout() {
73 @NonNull InetSocketAddress getAddress() {
77 @NonNull KeyMapping getKeys() {
81 @NonNull GraphKey getGraphKey() {
85 private static @NonNull KeyMapping constructKeys(final @Nullable Map<NodeKey, Node> nodes) {
87 return KeyMapping.of();
90 final var passwords = new HashMap<InetAddress, String>();
91 for (var node : nodes.values()) {
93 final var nodeConfig = node.augmentation(Node1.class);
94 if (nodeConfig != null) {
95 final var sessionConfig = nodeConfig.getSessionConfig();
96 if (sessionConfig != null) {
97 final var rfc2385KeyPassword = sessionConfig.getPassword();
98 if (rfc2385KeyPassword != null) {
99 final String password = rfc2385KeyPassword.getValue();
100 if (!password.isEmpty()) {
101 passwords.put(nodeAddress(node), password);
109 return KeyMapping.of(passwords);
112 private static @NonNull GraphKey constructGraphKey(final String name) {
113 return new GraphKey(name.startsWith("ted://") ? name : "ted://" + name);
116 private static InetAddress nodeAddress(final Node node) {
117 return InetAddresses.forString(node.getNodeId().getValue());
120 private static @NonNull InetSocketAddress getInetSocketAddress(final IpAddressNoZone address,
121 final PortNumber port) {
122 return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressForNoZone(requireNonNull(address)),
123 port.getValue().toJava());