d89242977d87e63faa16ea70a2379cc48aeb5102
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / config / PCEPTopologyProviderUtil.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.pcep.topology.provider.config;
9
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.config.rev171025.PcepNodeConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.TopologyTypes1;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
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.TopologyTypes;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 final class PCEPTopologyProviderUtil {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderUtil.class);
34
35     private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
36
37     private PCEPTopologyProviderUtil() {
38         throw new UnsupportedOperationException();
39     }
40
41     static KeyMapping contructKeys(@Nonnull final Topology topology) {
42         final KeyMapping ret = KeyMapping.getKeyMapping();
43         topology.getNode().stream()
44                 .filter(Objects::nonNull)
45                 .filter(node -> node.getAugmentation(PcepNodeConfig.class) != null)
46                 .filter(node -> node.getAugmentation(PcepNodeConfig.class).getSessionConfig() != null)
47                 .filter(node -> !node.getAugmentation(PcepNodeConfig.class)
48                         .getSessionConfig().getPassword().getValue().isEmpty())
49                 .forEach(node -> {
50                     final PcepNodeConfig config = node.getAugmentation(PcepNodeConfig.class);
51                     final Rfc2385Key rfc2385KeyPassword = config.getSessionConfig().getPassword();
52                     final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
53                     ret.put(address, rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
54                 });
55
56         return ret;
57     }
58
59     static InetSocketAddress getInetSocketAddress(@Nonnull final IpAddress address, @Nonnull final PortNumber port) {
60         return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressFor(address), port.getValue());
61     }
62
63     static boolean filterPcepTopologies(@Nullable final TopologyTypes topologyTypes) {
64         if (topologyTypes == null) {
65             return false;
66         }
67         final TopologyTypes1 aug = topologyTypes.getAugmentation(TopologyTypes1.class);
68
69         return aug != null && aug.getTopologyPcep() != null;
70     }
71
72     static void closeTopology(final PCEPTopologyProviderBean topology, final TopologyId topologyId) {
73         if (topology != null) {
74             try {
75                 topology.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
76             } catch (final Exception e) {
77                 LOG.error("Topology {} instance failed to close service instance", topologyId, e);
78             }
79             topology.close();
80         }
81     }
82 }