Update MRI projects for Aluminium
[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 org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.protocol.concepts.KeyMapping;
18 import org.opendaylight.protocol.pcep.SpeakerIdMapping;
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.IpAddressNoZone;
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.rev181109.PcepNodeConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TopologyTypes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.sync.optimizations.config.rev181109.PcepNodeSyncConfig;
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
29 final class PCEPTopologyProviderUtil {
30     private PCEPTopologyProviderUtil() {
31         // Hidden on purpose
32     }
33
34     static KeyMapping contructKeys(final @NonNull Topology topology) {
35         final KeyMapping ret = KeyMapping.getKeyMapping();
36         if (topology.getNode() == null) {
37             return ret;
38         }
39         topology.nonnullNode().values().stream()
40                 .filter(Objects::nonNull)
41                 .filter(node -> node.augmentation(PcepNodeConfig.class) != null)
42                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig() != null)
43                 .filter(node -> node.augmentation(PcepNodeConfig.class)
44                         .getSessionConfig().getPassword() != null)
45                 .filter(node -> !node.augmentation(PcepNodeConfig.class)
46                         .getSessionConfig().getPassword().getValue().isEmpty())
47                 .forEach(node -> {
48                     final PcepNodeConfig config = node.augmentation(PcepNodeConfig.class);
49                     final Rfc2385Key rfc2385KeyPassword = config.getSessionConfig().getPassword();
50                     final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
51                     ret.put(address, rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
52                 });
53
54         return ret;
55     }
56
57     static InetSocketAddress getInetSocketAddress(final @NonNull IpAddressNoZone address,
58             final @NonNull PortNumber port) {
59         return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressForNoZone(address), port.getValue().toJava());
60     }
61
62     static boolean filterPcepTopologies(final @Nullable TopologyTypes topologyTypes) {
63         if (topologyTypes == null) {
64             return false;
65         }
66         final TopologyTypes1 aug = topologyTypes.augmentation(TopologyTypes1.class);
67
68         return aug != null && aug.getTopologyPcep() != null;
69     }
70
71
72     static SpeakerIdMapping contructSpeakersId(final Topology topology) {
73         final SpeakerIdMapping ret = SpeakerIdMapping.getSpeakerIdMap();
74         if (topology.getNode() == null) {
75             return ret;
76         }
77         topology.nonnullNode().values().stream()
78                 .filter(Objects::nonNull)
79                 .filter(node -> node.augmentation(PcepNodeConfig.class) != null)
80                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig() != null)
81                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig()
82                         .augmentation(PcepNodeSyncConfig.class) != null)
83                 .forEach(node -> {
84                     final PcepNodeConfig config = node.augmentation(PcepNodeConfig.class);
85                     final PcepNodeSyncConfig nodeSyncConfig = config.getSessionConfig()
86                             .augmentation(PcepNodeSyncConfig.class);
87                     final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
88                     ret.put(address, nodeSyncConfig.getSpeakerEntityIdValue());
89                 });
90
91         return ret;
92     }
93 }