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