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