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