2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. 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.config;
10 import com.google.common.net.InetAddresses;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Objects;
15 import java.util.concurrent.TimeUnit;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.protocol.concepts.KeyMapping;
19 import org.opendaylight.protocol.pcep.SpeakerIdMapping;
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.IpAddress;
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.controller.rfc2385.cfg.rev160324.Rfc2385Key;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.config.rev171025.PcepNodeConfig;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.TopologyTypes1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.sync.optimizations.config.rev171025.PcepNodeSyncConfig;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 final class PCEPTopologyProviderUtil {
35 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderUtil.class);
37 private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
39 private PCEPTopologyProviderUtil() {
40 throw new UnsupportedOperationException();
43 static KeyMapping contructKeys(@Nonnull final Topology topology) {
44 final KeyMapping ret = KeyMapping.getKeyMapping();
45 topology.getNode().stream()
46 .filter(Objects::nonNull)
47 .filter(node -> node.getAugmentation(PcepNodeConfig.class) != null)
48 .filter(node -> node.getAugmentation(PcepNodeConfig.class).getSessionConfig() != null)
49 .filter(node -> !node.getAugmentation(PcepNodeConfig.class)
50 .getSessionConfig().getPassword().getValue().isEmpty())
52 final PcepNodeConfig config = node.getAugmentation(PcepNodeConfig.class);
53 final Rfc2385Key rfc2385KeyPassword = config.getSessionConfig().getPassword();
54 final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
55 ret.put(address, rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
61 static InetSocketAddress getInetSocketAddress(@Nonnull final IpAddress address, @Nonnull final PortNumber port) {
62 return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressFor(address), port.getValue());
65 static boolean filterPcepTopologies(@Nullable final TopologyTypes topologyTypes) {
66 if (topologyTypes == null) {
69 final TopologyTypes1 aug = topologyTypes.getAugmentation(TopologyTypes1.class);
71 return aug != null && aug.getTopologyPcep() != null;
74 static void closeTopology(@Nullable final PCEPTopologyProviderBean topology, @Nonnull final TopologyId topologyId) {
75 if (topology != null) {
77 topology.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
78 } catch (final Exception e) {
79 LOG.error("Topology {} instance failed to close service instance", topologyId, e);
85 static SpeakerIdMapping contructSpeakersId(final Topology topology) {
86 final SpeakerIdMapping ret = SpeakerIdMapping.getSpeakerIdMap();
87 topology.getNode().stream()
88 .filter(Objects::nonNull)
89 .filter(node -> node.getAugmentation(PcepNodeConfig.class) != null)
90 .filter(node -> node.getAugmentation(PcepNodeConfig.class).getSessionConfig() != null)
91 .filter(node -> node.getAugmentation(PcepNodeConfig.class).getSessionConfig()
92 .getAugmentation(PcepNodeSyncConfig.class) != null)
94 final PcepNodeConfig config = node.getAugmentation(PcepNodeConfig.class);
95 final PcepNodeSyncConfig nodeSyncConfig = config.getSessionConfig()
96 .getAugmentation(PcepNodeSyncConfig.class);
97 final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
98 ret.put(address, nodeSyncConfig.getSpeakerEntityIdValue());