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