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