Add new revision for pcep types model
[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 javax.annotation.Nonnull;
16 import javax.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.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.rev181109.PcepNodeConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.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 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 PCEPTopologyProviderUtil() {
36         throw new UnsupportedOperationException();
37     }
38
39     static KeyMapping contructKeys(@Nonnull final Topology topology) {
40         final KeyMapping ret = KeyMapping.getKeyMapping();
41         topology.getNode().stream()
42                 .filter(Objects::nonNull)
43                 .filter(node -> node.augmentation(PcepNodeConfig.class) != null)
44                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig() != null)
45                 .filter(node -> node.augmentation(PcepNodeConfig.class)
46                         .getSessionConfig().getPassword() != null)
47                 .filter(node -> !node.augmentation(PcepNodeConfig.class)
48                         .getSessionConfig().getPassword().getValue().isEmpty())
49                 .forEach(node -> {
50                     final PcepNodeConfig config = node.augmentation(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.augmentation(TopologyTypes1.class);
68
69         return aug != null && aug.getTopologyPcep() != null;
70     }
71
72
73     static SpeakerIdMapping contructSpeakersId(final Topology topology) {
74         final SpeakerIdMapping ret = SpeakerIdMapping.getSpeakerIdMap();
75         topology.getNode().stream()
76                 .filter(Objects::nonNull)
77                 .filter(node -> node.augmentation(PcepNodeConfig.class) != null)
78                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig() != null)
79                 .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig()
80                         .augmentation(PcepNodeSyncConfig.class) != null)
81                 .forEach(node -> {
82                     final PcepNodeConfig config = node.augmentation(PcepNodeConfig.class);
83                     final PcepNodeSyncConfig nodeSyncConfig = config.getSessionConfig()
84                             .augmentation(PcepNodeSyncConfig.class);
85                     final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
86                     ret.put(address, nodeSyncConfig.getSpeakerEntityIdValue());
87                 });
88
89         return ret;
90     }
91 }