Technical debt - Fix PCE sonar issues
[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.rev181130.Link1;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenationKey;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.link.oms.attributes.Span;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
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(Arrays.asList(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.rev181130.Link1 linkC = link
107                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.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 (org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130
114                 .networks.network.link.LinkConcatenation lc : linkC.nonnullLinkConcatenation().values()) {
115             if (lc != null && lc.getSRLGId() != null) {
116                 srlgList.add(lc.getSRLGId().toJava());
117             } else {
118                 LOG.debug("No concatenation or SLRG id for this link");
119             }
120         }
121         return srlgList;
122     }
123
124     public static String getSupNetworkNode(Node node) {
125         for (SupportingNode snode : node.nonnullSupportingNode().values()) {
126             if (NetworkUtils.UNDERLAY_NETWORK_ID.equals(snode.getNetworkRef().getValue())) {
127                 return snode.getNodeRef().getValue();
128             }
129         }
130         return null;
131     }
132
133     public static String getSupClliNode(Node node) {
134         for (SupportingNode snode : node.nonnullSupportingNode().values()) {
135             if (NetworkUtils.CLLI_NETWORK_ID.equals(snode.getNetworkRef().getValue())) {
136                 return snode.getNodeRef().getValue();
137             }
138         }
139         return null;
140     }
141
142     public static SortedMap<String, String> getAllSupNode(Node node) {
143         TreeMap<String, String> allSupNodes = new TreeMap<>();
144         for (SupportingNode supnode : node.nonnullSupportingNode().values()) {
145             allSupNodes.put(supnode.getNetworkRef().getValue(),
146                     supnode.getNodeRef().getValue());
147         }
148         return allSupNodes;
149     }
150
151     public static String getSupLink(Link link) {
152         Iterator<SupportingLink> supportingLinkIterator = link.nonnullSupportingLink().values().iterator();
153         if (!supportingLinkIterator.hasNext()) {
154             return "";
155         }
156         SupportingLink first = supportingLinkIterator.next();
157         if (first == null || first.getLinkRef() == null) {
158             return "";
159         }
160         return first.getLinkRef().toString();
161     }
162
163
164     public static Long getAvailableBandwidth(Link link) {
165         if (link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
166             .Link1.class) != null
167             && link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
168                 .Link1.class).getAvailableBandwidth() != null) {
169             return link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
170                 .Link1.class).getAvailableBandwidth().toJava();
171         } else {
172             LOG.warn("MapUtils: no Available Bandwidth available for link {}", link.getLinkId());
173             return 0L;
174         }
175     }
176
177     public static Long getUsedBandwidth(Link link) {
178         if (link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
179             .Link1.class) != null
180             && link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
181                 .Link1.class).getUsedBandwidth() != null) {
182             return link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130
183                 .Link1.class).getUsedBandwidth().toJava();
184         } else {
185             LOG.warn("MapUtils: no Available Bandwidth available for link {}", link.getLinkId());
186             return 0L;
187         }
188     }
189
190     public static OpenroadmLinkType calcType(Link link) {
191         Link1 link1 = null;
192         OpenroadmLinkType tmplType = null;
193         // ID and type
194         link1 = link.augmentation(Link1.class);
195         if (link1 == null) {
196             LOG.error(MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG, link.getLinkId().getValue());
197             return null;
198         }
199
200         tmplType = link1.getLinkType();
201
202         if (tmplType == null) {
203             LOG.error("MapUtils: No Link type available. {}", link.getLinkId().getValue());
204             return null;
205         }
206         return tmplType;
207     }
208
209     public static Span getOmsAttributesSpan(Link link) {
210         org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Link1 link1 = null;
211         link1 =
212             link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Link1.class);
213
214         if (link1 == null) {
215             LOG.error(MAP_UTILS_NO_LINK_AUGMENTATION_AVAILABLE_MSG, link.getLinkId().getValue());
216             return null;
217         }
218         if (link1.getOMSAttributes() == null) {
219             LOG.error("MapUtils: No Link getOMSAttributes available. {}", link.getLinkId().getValue());
220             return null;
221         }
222         return link1.getOMSAttributes().getSpan();
223     }
224
225     public static LinkId extractOppositeLink(Link link) {
226         LinkId tmpoppositeLink = null;
227         org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1 linkOpposite
228             = link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1.class);
229         tmpoppositeLink = linkOpposite.getOppositeLink();
230         LOG.debug("PceLink: reading oppositeLink.  {}", linkOpposite);
231         if (tmpoppositeLink == null) {
232             LOG.error("PceLink: Error reading oppositeLink. Link is ignored {}", link.getLinkId().getValue());
233             return null;
234         }
235         return tmpoppositeLink;
236     }
237
238
239 }