Adapt TransportPCE code to Sulfur
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / MapUtils.java
1 /*
2  * Copyright © 2017 AT&T, Inc. 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.transportpce.pce.networkanalyzer;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.SortedMap;
16 import java.util.TreeMap;
17 import org.opendaylight.transportpce.common.NetworkUtils;
18 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210.networks.network.link.oms.attributes.Span;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.OpenroadmLinkType;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.link.concatenation.LinkConcatenation;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.link.concatenation.LinkConcatenationKey;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.node.SupportingNode;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.link.SupportingLink;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public final class MapUtils {
33     private static final String MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG =
34             "MapUtils: No Link augmentation available. {}";
35     /* Logging. */
36     private static final Logger LOG = LoggerFactory.getLogger(MapUtils.class);
37
38     private MapUtils() {
39     }
40
41     public static void mapDiversityConstraints(List<Node> allNodes, List<Link> allLinks,
42             PceConstraints pceHardConstraints) {
43         List<String> excClliNodes = pceHardConstraints.getExcludeClliNodes();
44         List<String> excNodes = pceHardConstraints.getExcludeNodes();
45         List<String> excSrlgLinks = pceHardConstraints.getExcludeSrlgLinks();
46
47         LOG.info("mapDiversityConstraints before : ExcludeClliNodes {} \n ExcludeNodes {} \n ExcludeSrlgLinks {}",
48                 excClliNodes, excNodes, excSrlgLinks);
49
50         for (Node node : allNodes) {
51             if (excClliNodes.contains(node.getNodeId().getValue())) {
52                 LOG.debug("mapDiversityConstraints setExcludeCLLI for node {}", node.getNodeId().getValue());
53                 pceHardConstraints.setExcludeCLLI(List.of(getCLLI(node)));
54             }
55
56             if (excNodes.contains(node.getNodeId().getValue())) {
57                 LOG.debug("mapDiversityConstraints setExcludeSupNodes for node {}", node.getNodeId().getValue());
58                 pceHardConstraints.setExcludeSupNodes(Arrays.asList(getSupNetworkNode(node)));
59             }
60         }
61
62         for (Link link : allLinks) {
63             if (excSrlgLinks.contains(link.getLinkId().getValue())) {
64                 // zero SRLG means not populated as not OMS link
65                 List<Long> srlg = null;
66                 if (calcType(link) == OpenroadmLinkType.ROADMTOROADM) {
67                     srlg = getSRLG(link);
68                     if (!srlg.isEmpty()) {
69                         pceHardConstraints.setExcludeSRLG(srlg);
70                         LOG.debug("mapDiversityConstraints setExcludeSRLG {} for link {}",
71                                 srlg, link.getLinkId().getValue());
72                     }
73                 }
74             }
75         }
76
77         LOG.info("mapDiversityConstraints after : ExcludeCLLI {} \n ExcludeSupNodes {} \n ExcludeSRLG {}",
78                 pceHardConstraints.getExcludeCLLI(),
79                 pceHardConstraints.getExcludeSupNodes(),
80                 pceHardConstraints.getExcludeSRLG());
81
82     }
83
84     public static String getCLLI(Node node) {
85         // TODO STUB retrieve CLLI from node. for now it is supporting node ID of the first supp node
86         return node.nonnullSupportingNode().values().iterator().next().getNodeRef().getValue();
87     }
88
89     public static List<Long> getSRLG(Link link) {
90         Span omsAttributesSpan = getOmsAttributesSpan(link);
91         if (omsAttributesSpan == null) {
92             LOG.debug("No concatenation for this link");
93             return new ArrayList<>();
94         }
95         List<Long> srlgList = new ArrayList<>();
96         Map<LinkConcatenationKey, LinkConcatenation> linkList = omsAttributesSpan.nonnullLinkConcatenation();
97         for (LinkConcatenation lc : linkList.values()) {
98             if (lc != null && lc.getSRLGId() != null) {
99                 srlgList.add(lc.getSRLGId().toJava());
100             }
101         }
102         return srlgList;
103     }
104
105     public static List<Long> getSRLGfromLink(Link link) {
106         org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1 linkC = link
107                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1.class);
108         if (linkC == null) {
109             LOG.error(MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG, link.getLinkId().getValue());
110             return new ArrayList<>();
111         }
112         List<Long> srlgList = new ArrayList<>();
113         for (LinkConcatenation lc : linkC.nonnullLinkConcatenation().values()) {
114             if (lc != null && lc.getSRLGId() != null) {
115                 srlgList.add(lc.getSRLGId().toJava());
116             } else {
117                 LOG.debug("No concatenation or SLRG id for this link");
118             }
119         }
120         return srlgList;
121     }
122
123     public static String getSupNetworkNode(Node node) {
124         for (SupportingNode snode : node.nonnullSupportingNode().values()) {
125             if (NetworkUtils.UNDERLAY_NETWORK_ID.equals(snode.getNetworkRef().getValue())) {
126                 return snode.getNodeRef().getValue();
127             }
128         }
129         return null;
130     }
131
132     public static String getSupClliNode(Node node) {
133         for (SupportingNode snode : node.nonnullSupportingNode().values()) {
134             if (NetworkUtils.CLLI_NETWORK_ID.equals(snode.getNetworkRef().getValue())) {
135                 return snode.getNodeRef().getValue();
136             }
137         }
138         return null;
139     }
140
141     public static SortedMap<String, String> getAllSupNode(Node node) {
142         TreeMap<String, String> allSupNodes = new TreeMap<>();
143         for (SupportingNode supnode : node.nonnullSupportingNode().values()) {
144             allSupNodes.put(supnode.getNetworkRef().getValue(),
145                     supnode.getNodeRef().getValue());
146         }
147         return allSupNodes;
148     }
149
150     public static String getSupLink(Link link) {
151         Iterator<SupportingLink> supportingLinkIterator = link.nonnullSupportingLink().values().iterator();
152         if (!supportingLinkIterator.hasNext()) {
153             return "";
154         }
155         SupportingLink first = supportingLinkIterator.next();
156         if (first == null || first.getLinkRef() == null) {
157             return "";
158         }
159         return first.getLinkRef().toString();
160     }
161
162
163     public static Long getAvailableBandwidth(Link link) {
164         if (link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
165             .Link1.class) != null
166             && link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
167                 .Link1.class).getAvailableBandwidth() != null) {
168             return link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
169                 .Link1.class).getAvailableBandwidth().toJava();
170         } else {
171             LOG.warn("MapUtils: no Available Bandwidth available for link {}", link.getLinkId());
172             return 0L;
173         }
174     }
175
176     public static Long getUsedBandwidth(Link link) {
177         if (link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
178             .Link1.class) != null
179             && link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
180                 .Link1.class).getUsedBandwidth() != null) {
181             return link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev211210
182                 .Link1.class).getUsedBandwidth().toJava();
183         } else {
184             LOG.warn("MapUtils: no Available Bandwidth available for link {}", link.getLinkId());
185             return 0L;
186         }
187     }
188
189     public static OpenroadmLinkType calcType(Link link) {
190         Link1 link1 = null;
191         OpenroadmLinkType tmplType = null;
192         // ID and type
193         link1 = link.augmentation(Link1.class);
194         if (link1 == null) {
195             LOG.error(MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG, link.getLinkId().getValue());
196             return null;
197         }
198
199         tmplType = link1.getLinkType();
200
201         if (tmplType == null) {
202             LOG.error("MapUtils: No Link type available. {}", link.getLinkId().getValue());
203             return null;
204         }
205         return tmplType;
206     }
207
208     public static Span getOmsAttributesSpan(Link link) {
209         org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210.Link1 link1 = null;
210         link1 =
211             link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210.Link1.class);
212
213         if (link1 == null) {
214             LOG.error(MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG, link.getLinkId().getValue());
215             return null;
216         }
217         if (link1.getOMSAttributes() == null) {
218             LOG.error("MapUtils: No Link getOMSAttributes available. {}", link.getLinkId().getValue());
219             return null;
220         }
221         return link1.getOMSAttributes().getSpan();
222     }
223
224     public static LinkId extractOppositeLink(Link link) {
225         LinkId tmpoppositeLink = null;
226         org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1 linkOpposite
227             = link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1.class);
228         tmpoppositeLink = linkOpposite.getOppositeLink();
229         LOG.debug("PceLink: reading oppositeLink.  {}", linkOpposite);
230         if (tmpoppositeLink == null) {
231             LOG.error("PceLink: Error reading oppositeLink. Link is ignored {}", link.getLinkId().getValue());
232             return null;
233         }
234         return tmpoppositeLink;
235     }
236
237
238 }