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