Merge "Fix spotbugs issues of the PCE module"
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / graph / PostAlgoPathValidator.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
9 package org.opendaylight.transportpce.pce.graph;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.jgrapht.GraphPath;
18 import org.opendaylight.transportpce.common.ResponseCodes;
19 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
20 import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
21 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
22 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
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.NodeId;
25 import org.opendaylight.yangtools.yang.common.Uint16;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class PostAlgoPathValidator {
30     /* Logging. */
31     private static final Logger LOG = LoggerFactory.getLogger(PostAlgoPathValidator.class);
32
33     private static final int MAX_WAWELENGTH = 96;
34     private static final double MIN_OSNR_W100G = 17;
35     private static final double TRX_OSNR = 33;
36     private static final double ADD_OSNR = 30;
37     public static final Long CONST_OSNR = 1L;
38     public static final double SYS_MARGIN = 0;
39
40     public PceResult checkPath(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes,
41         PceResult pceResult, PceConstraints pceHardConstraints, String serviceType) {
42
43         // check if the path is empty
44         if (path.getEdgeList().isEmpty()) {
45             pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
46             return pceResult;
47         }
48
49         int tribSlotNb = 1;
50         //variable to deal with 1GE (Nb=1) and 10GE (Nb=10) cases
51         switch (serviceType) {
52
53             case "100GE":
54             case "OTU4":
55                 // choose wavelength available in all nodes of the path
56                 Long waveL = chooseWavelength(path, allPceNodes);
57                 pceResult.setServiceType(serviceType);
58                 if (waveL < 0) {
59                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
60                     pceResult.setLocalCause(PceResult.LocalCause.NO_PATH_EXISTS);
61                     return pceResult;
62                 }
63                 pceResult.setResultWavelength(waveL);
64                 LOG.info("In PostAlgoPathValidator: chooseWavelength WL found {} {}", waveL, path);
65
66                 // Check the OSNR
67                 if (!checkOSNR(path)) {
68                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
69                     pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
70                     return pceResult;
71                 }
72
73                 // Check if MaxLatency is defined in the hard constraints
74                 if ((pceHardConstraints.getMaxLatency() != -1)
75                         && (!checkLatency(pceHardConstraints.getMaxLatency(), path))) {
76                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
77                     pceResult.setLocalCause(PceResult.LocalCause.TOO_HIGH_LATENCY);
78                     return pceResult;
79                 }
80
81                 // Check if nodes are included in the hard constraints
82                 if (!checkInclude(path, pceHardConstraints)) {
83                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
84                     pceResult.setLocalCause(PceResult.LocalCause.HD_NODE_INCLUDE);
85                     return pceResult;
86                 }
87
88                 // TODO here other post algo validations can be added
89                 // more data can be sent to PceGraph module via PceResult structure if required
90
91                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
92                 pceResult.setLocalCause(PceResult.LocalCause.NONE);
93
94                 break;
95
96             case "10GE":
97                 tribSlotNb = 10;
98             //fallthrough
99             case "1GE":
100                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
101                 pceResult.setServiceType(serviceType);
102                 Map<String, Uint16> tribPort = chooseTribPort(path, allPceNodes);
103                 Map<String, List<Uint16>> tribSlot = chooseTribSlot(path, allPceNodes, tribSlotNb);
104
105                 if (tribPort != null && tribSlot != null) {
106                     pceResult.setResultTribPort(tribPort);
107                     pceResult.setResultTribSlot(tribSlot);
108                     pceResult.setResultTribSlotNb(tribSlotNb);
109                     pceResult.setRC(ResponseCodes.RESPONSE_OK);
110                     LOG.info("In PostAlgoPathValidator: found TribPort {} - tribSlot {} - tribSlotNb {}",
111                         tribPort, tribSlot, tribSlotNb);
112                 }
113                 break;
114
115             case "ODU4":
116                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
117                 LOG.info("In PostAlgoPathValidator: ODU4 path found {}", path);
118                 break;
119
120             default:
121                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
122                 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
123                     serviceType, path);
124                 break;
125         }
126
127         return pceResult;
128     }
129
130     // Choose the first available wavelength from the source to the destination
131     private Long chooseWavelength(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes) {
132         Long wavelength = -1L;
133         for (long i = 1; i <= MAX_WAWELENGTH; i++) {
134             boolean completed = true;
135             LOG.debug("In chooseWavelength: {} {}", path.getLength(), path);
136             for (PceGraphEdge edge : path.getEdgeList()) {
137                 LOG.debug("In chooseWavelength: source {} ", edge.link().getSourceId());
138                 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
139                 if (!pceNode.checkWL(i)) {
140                     completed = false;
141                     break;
142                 }
143             }
144             if (completed) {
145                 wavelength = i;
146                 break;
147             }
148         }
149         return wavelength;
150     }
151
152     // Check the latency
153     private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
154         double latency = 0;
155
156         for (PceGraphEdge edge : path.getEdgeList()) {
157             try {
158                 latency += edge.link().getLatency();
159                 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
160             } catch (NullPointerException e) {
161                 LOG.warn("- In checkLatency: the link {} does not contain latency field",
162                     edge.link().getLinkId().getValue());
163             }
164         }
165         return (latency < maxLatency);
166     }
167
168     // Check the inclusion if it is defined in the hard constraints
169     private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
170         List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
171         if (listToInclude.isEmpty()) {
172             return true;
173         }
174
175         List<PceGraphEdge> pathEdges = path.getEdgeList();
176         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
177
178         List<String> listOfElementsSubNode = new ArrayList<>();
179         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
180         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
181             pceHardConstraintsInput));
182
183         List<String> listOfElementsCLLI = new ArrayList<>();
184         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
185         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
186             pceHardConstraintsInput));
187
188         List<String> listOfElementsSRLG = new ArrayList<>();
189         // first link is XPONDEROUTPUT, no SRLG for it
190         listOfElementsSRLG.add("NONE");
191         listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
192             pceHardConstraintsInput));
193
194         // validation: check each type for each element
195         for (ResourcePair next : listToInclude) {
196             int indx = -1;
197             switch (next.getType()) {
198                 case NODE:
199                     if (listOfElementsSubNode.contains(next.getName())) {
200                         indx = listOfElementsSubNode.indexOf(next.getName());
201                     }
202                     break;
203                 case SRLG:
204                     if (listOfElementsSRLG.contains(next.getName())) {
205                         indx = listOfElementsSRLG.indexOf(next.getName());
206                     }
207                     break;
208                 case CLLI:
209                     if (listOfElementsCLLI.contains(next.getName())) {
210                         indx = listOfElementsCLLI.indexOf(next.getName());
211                     }
212                     break;
213                 default:
214                     LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
215             }
216
217             if (indx < 0) {
218                 LOG.debug(" in checkInclude stopped : {} ", next.getName());
219                 return false;
220             }
221
222             LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
223
224             listOfElementsSubNode.subList(0, indx).clear();
225             listOfElementsCLLI.subList(0, indx).clear();
226             listOfElementsSRLG.subList(0, indx).clear();
227         }
228
229         LOG.info(" in checkInclude passed : {} ", path.getVertexList());
230         return true;
231     }
232
233     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
234         PceConstraints pceHardConstraints) {
235
236         List<String> listOfElements = new ArrayList<>();
237         for (PceGraphEdge link : pathEdges) {
238             switch (type) {
239                 case NODE:
240                     listOfElements.add(link.link().getdestNetworkSupNodeId());
241                     break;
242                 case CLLI:
243                     listOfElements.add(link.link().getdestCLLI());
244                     break;
245                 case SRLG:
246                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
247                         listOfElements.add("NONE");
248                         break;
249                     }
250                     // srlg of link is List<Long>. But in this algo we need string representation of
251                     // one SRLG
252                     // this should be any SRLG mentioned in include constraints if any of them if
253                     // mentioned
254                     boolean found = false;
255                     for (Long srlg : link.link().getsrlgList()) {
256                         String srlgStr = String.valueOf(srlg);
257                         if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
258                             listOfElements.add(srlgStr);
259                             LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
260                             found = true;
261                         }
262                     }
263                     if (!found) {
264                         // there is no specific srlg to include. thus add to list just the first one
265                         listOfElements.add("NONE");
266                     }
267                     break;
268                 default:
269                     LOG.debug("listOfElementsBuild unsupported resource type");
270             }
271         }
272         return listOfElements;
273     }
274
275     private Map<String, Uint16> chooseTribPort(GraphPath<String,
276         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes) {
277         LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
278         Map<String, Uint16> tribPortMap = new HashMap<>();
279
280         for (PceGraphEdge edge : path.getEdgeList()) {
281             NodeId linkSrcNode = edge.link().getSourceId();
282             String linkSrcTp = edge.link().getSourceTP().toString();
283             NodeId linkDestNode = edge.link().getDestId();
284             String linkDestTp = edge.link().getDestTP().toString();
285             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
286             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
287             List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
288             List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
289             List<Uint16> commonEdgeTpnPool = new ArrayList<>();
290             for (Uint16 integer : srcTpnPool) {
291                 if (destTpnPool.contains(integer)) {
292                     commonEdgeTpnPool.add(integer);
293                 }
294             }
295             Collections.sort(commonEdgeTpnPool);
296             if (!commonEdgeTpnPool.isEmpty()) {
297                 tribPortMap.put(edge.link().getLinkId().getValue(), commonEdgeTpnPool.get(0));
298             }
299         }
300         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
301         return tribPortMap;
302     }
303
304     private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
305         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
306         LOG.info("In choosetribSlot2: edgeList = {} ", path.getEdgeList());
307         Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
308
309         for (PceGraphEdge edge : path.getEdgeList()) {
310             NodeId linkSrcNode = edge.link().getSourceId();
311             String linkSrcTp = edge.link().getSourceTP().toString();
312             NodeId linkDestNode = edge.link().getDestId();
313             String linkDestTp = edge.link().getDestTP().toString();
314             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
315             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
316             List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
317             List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
318             List<Uint16> commonEdgeTsPool = new ArrayList<>();
319             List<Uint16> tribSlotList = new ArrayList<>();
320             for (Uint16 integer : srcTsPool) {
321                 if (destTsPool.contains(integer)) {
322                     commonEdgeTsPool.add(integer);
323                 }
324             }
325             Collections.sort(commonEdgeTsPool);
326             boolean discontinue = true;
327             int index = 0;
328             while (discontinue && (commonEdgeTsPool.size() - index >= nbSlot)) {
329                 discontinue = false;
330                 Integer val = commonEdgeTsPool.get(index).toJava();
331                 for (int i = 0; i < nbSlot; i++) {
332                     if (commonEdgeTsPool.get(index + i).equals(Uint16.valueOf(val + i))) {
333                         tribSlotList.add(commonEdgeTsPool.get(index + i));
334                     } else {
335                         discontinue = true;
336                         tribSlotList.clear();
337                         index += i;
338                         break;
339                     }
340                 }
341             }
342             tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
343         }
344         tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
345         return tribSlotMap;
346     }
347
348     private List<List<Uint16>> chooseTribSlot3(GraphPath<String, PceGraphEdge> path,
349         Map<NodeId, PceNode> allPceNodes) {
350         List<List<Uint16>> tribSlot = new ArrayList<>();
351         boolean statusOK = true;
352         boolean check = false;
353         Object nodeClass = allPceNodes.getClass();
354         if (nodeClass.getClass().isInstance(PceNode.class)) {
355             LOG.debug("In choosetribSlot: AllPceNodes contains PceNode instance, no trib port search");
356             return tribSlot;
357         } else if (nodeClass.getClass().isInstance(PceNode.class)) {
358             LOG.debug("In choosetribPort: {} {}", path.getLength(), path);
359         }
360         for (PceGraphEdge edge : path.getEdgeList()) {
361             LOG.debug("In chooseTribSlot: source {} ", edge.link().getSourceId());
362             PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
363             Object tps = allPceNodes.get(edge.link().getSourceTP());
364             Object tpd = allPceNodes.get(edge.link().getDestTP());
365             if ((pceNode.getAvailableTribSlots().containsKey(tps.toString()))
366                 && (pceNode.getAvailableTribSlots().containsKey(tpd.toString()))) {
367                 List<Uint16> tribSlotEdgeSourceN = pceNode.getAvailableTribSlots().get(tps.toString());
368                 List<Uint16> tribSlotEdgeDestN = pceNode.getAvailableTribSlots().get(tps.toString());
369                 check = false;
370                 for (int i = 0; i <= 79; i++) {
371                     if (tribSlotEdgeSourceN.get(i) == null) {
372                         break;
373                     }
374                     // TODO This will need to be modified as soon as the trib-slots allocation per
375                     // trib-port
376                     // policy applied by the different manufacturer is known
377                     if (tribSlotEdgeSourceN.get(i).equals(tribSlotEdgeDestN.get(i))) {
378                         check = true;
379                     } else {
380                         check = false;
381                         LOG.debug("In chooseTribSlot: Misalignement of trib slots between source {} and dest {}",
382                             edge.link().getSourceId(), edge.link().getDestId());
383                         break;
384                     }
385                 }
386                 if (check) {
387                     tribSlot.add(tribSlotEdgeSourceN);
388                 }
389             } else {
390                 LOG.debug("In chooseTribSlot: source {} does not have provisonned hosting HO interface ",
391                     edge.link().getSourceId());
392                 statusOK = false;
393             }
394         }
395         if (statusOK && check) {
396             return tribSlot;
397         } else {
398             tribSlot.clear();
399             return tribSlot;
400         }
401     }
402
403     // Check the path OSNR
404     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
405         double linkOsnrDb;
406         double osnrDb = 0;
407         LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
408         LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
409         double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
410         for (PceGraphEdge edge : path.getEdgeList()) {
411             if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
412                 // link OSNR in dB
413                 linkOsnrDb = edge.link().getosnr();
414                 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
415                 // 1 over the local OSNR, in linear units
416                 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
417             }
418         }
419         try {
420             osnrDb = getOsnrDb(1 / inverseLocalOsnr);
421         } catch (ArithmeticException e) {
422             LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
423             return false;
424         }
425         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
426         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
427     }
428
429     private double getOsnrDb(double osnrLu) {
430         double osnrDb;
431         osnrDb = 10 * Math.log10(osnrLu);
432         return osnrDb;
433     }
434
435     private double getInverseOsnrLinkLu(double linkOsnrDb) {
436         // 1 over the link OSNR, in linear units
437         double linkOsnrLu;
438         linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
439         LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
440         return (CONST_OSNR / linkOsnrLu);
441     }
442
443 }