2 * Copyright © 2017 AT&T, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce.graph;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.BitSet;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
19 import org.jgrapht.GraphPath;
20 import org.opendaylight.transportpce.common.ResponseCodes;
21 import org.opendaylight.transportpce.common.StringConstants;
22 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
23 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
24 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
25 import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
26 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
27 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.SpectrumAssignment;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.SpectrumAssignmentBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmLinkType;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev181130.OpucnTribSlotDef;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
33 import org.opendaylight.yangtools.yang.common.Uint16;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class PostAlgoPathValidator {
39 private static final Logger LOG = LoggerFactory.getLogger(PostAlgoPathValidator.class);
41 private static final double MIN_OSNR_W100G = 17;
42 private static final double TRX_OSNR = 33;
43 private static final double ADD_OSNR = 30;
44 public static final Long CONST_OSNR = 1L;
45 public static final double SYS_MARGIN = 0;
47 @SuppressWarnings("fallthrough")
49 value = "SF_SWITCH_FALLTHROUGH",
50 justification = "intentional fallthrough")
51 public PceResult checkPath(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes,
52 PceResult pceResult, PceConstraints pceHardConstraints, String serviceType) {
54 // check if the path is empty
55 if (path.getEdgeList().isEmpty()) {
56 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
59 int spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
60 .getOrDefault(serviceType, GridConstant.NB_SLOTS_100G);
61 SpectrumAssignment spectrumAssignment = null;
62 //variable to deal with 1GE (Nb=1) and 10GE (Nb=10) cases
63 switch (serviceType) {
64 case StringConstants.SERVICE_TYPE_OTUC4:
65 case StringConstants.SERVICE_TYPE_400GE:
66 spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
67 .getOrDefault(serviceType, GridConstant.NB_SLOTS_400G);
69 case StringConstants.SERVICE_TYPE_100GE_T:
70 case StringConstants.SERVICE_TYPE_OTU4:
71 spectrumAssignment = getSpectrumAssignment(path, allPceNodes, spectralWidthSlotNumber);
72 pceResult.setServiceType(serviceType);
73 if (spectrumAssignment.getBeginIndex().equals(Uint16.valueOf(0))
74 && spectrumAssignment.getStopIndex().equals(Uint16.valueOf(0))) {
75 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
76 pceResult.setLocalCause(PceResult.LocalCause.NO_PATH_EXISTS);
79 if (spectrumAssignment.getFlexGrid()) {
80 LOG.info("Spectrum assignment flexgrid mode");
81 pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
83 LOG.info("Spectrum assignment fixedgrid mode");
84 pceResult.setResultWavelength(
85 GridUtils.getWaveLengthIndexFromSpectrumAssigment(spectrumAssignment.getBeginIndex()
88 pceResult.setMinFreq(GridUtils.getStartFrequencyFromIndex(spectrumAssignment.getBeginIndex().toJava()));
89 pceResult.setMaxFreq(GridUtils.getStopFrequencyFromIndex(spectrumAssignment.getStopIndex().toJava()));
90 LOG.info("In PostAlgoPathValidator: spectrum assignment found {} {}", spectrumAssignment, path);
93 if (!checkOSNR(path)) {
94 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
95 pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
99 // Check if MaxLatency is defined in the hard constraints
100 if ((pceHardConstraints.getMaxLatency() != -1)
101 && (!checkLatency(pceHardConstraints.getMaxLatency(), path))) {
102 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
103 pceResult.setLocalCause(PceResult.LocalCause.TOO_HIGH_LATENCY);
107 // Check if nodes are included in the hard constraints
108 if (!checkInclude(path, pceHardConstraints)) {
109 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
110 pceResult.setLocalCause(PceResult.LocalCause.HD_NODE_INCLUDE);
114 // TODO here other post algo validations can be added
115 // more data can be sent to PceGraph module via PceResult structure if required
117 pceResult.setRC(ResponseCodes.RESPONSE_OK);
118 pceResult.setLocalCause(PceResult.LocalCause.NONE);
120 case StringConstants.SERVICE_TYPE_100GE_M:
121 case StringConstants.SERVICE_TYPE_10GE:
122 case StringConstants.SERVICE_TYPE_1GE:
123 Map<String, Integer> tribSlotNbMap = Map.of(
124 StringConstants.SERVICE_TYPE_100GE_M, 20,
125 StringConstants.SERVICE_TYPE_10GE, 8,
126 StringConstants.SERVICE_TYPE_1GE, 1);
127 int tribSlotNb = tribSlotNbMap.get(serviceType);
128 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
129 pceResult.setServiceType(serviceType);
130 Map<String, List<Uint16>> tribSlot = chooseTribSlot(path, allPceNodes, tribSlotNb);
131 Map<String, Uint16> tribPort = chooseTribPort(path, allPceNodes, tribSlot, tribSlotNb);
132 List<OpucnTribSlotDef> resultTribPortTribSlot = getMinMaxTpTs(tribPort, tribSlot);
134 if (resultTribPortTribSlot.get(0) != null && resultTribPortTribSlot.get(1) != null) {
135 pceResult.setResultTribPortTribSlot(resultTribPortTribSlot);
136 pceResult.setRC(ResponseCodes.RESPONSE_OK);
137 LOG.info("In PostAlgoPathValidator: found TribPort {} - tribSlot {} - tribSlotNb {}",
138 tribPort, tribSlot, tribSlotNb);
141 case StringConstants.SERVICE_TYPE_ODU4:
142 case StringConstants.SERVICE_TYPE_ODUC4:
143 pceResult.setRC(ResponseCodes.RESPONSE_OK);
144 LOG.info("In PostAlgoPathValidator: ODU4/ODUC4 path found {}", path);
147 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
148 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
156 private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
159 for (PceGraphEdge edge : path.getEdgeList()) {
161 latency += edge.link().getLatency();
162 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
163 } catch (NullPointerException e) {
164 LOG.warn("- In checkLatency: the link {} does not contain latency field",
165 edge.link().getLinkId().getValue());
168 return (latency < maxLatency);
171 // Check the inclusion if it is defined in the hard constraints
172 private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
173 List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
174 if (listToInclude.isEmpty()) {
178 List<PceGraphEdge> pathEdges = path.getEdgeList();
179 LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
181 List<String> listOfElementsSubNode = new ArrayList<>();
182 listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
183 listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
184 pceHardConstraintsInput));
186 List<String> listOfElementsCLLI = new ArrayList<>();
187 listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
188 listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
189 pceHardConstraintsInput));
191 List<String> listOfElementsSRLG = new ArrayList<>();
192 // first link is XPONDEROUTPUT, no SRLG for it
193 listOfElementsSRLG.add("NONE");
194 listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
195 pceHardConstraintsInput));
197 // validation: check each type for each element
198 for (ResourcePair next : listToInclude) {
200 switch (next.getType()) {
202 if (listOfElementsSubNode.contains(next.getName())) {
203 indx = listOfElementsSubNode.indexOf(next.getName());
207 if (listOfElementsSRLG.contains(next.getName())) {
208 indx = listOfElementsSRLG.indexOf(next.getName());
212 if (listOfElementsCLLI.contains(next.getName())) {
213 indx = listOfElementsCLLI.indexOf(next.getName());
217 LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
221 LOG.debug(" in checkInclude stopped : {} ", next.getName());
225 LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
227 listOfElementsSubNode.subList(0, indx).clear();
228 listOfElementsCLLI.subList(0, indx).clear();
229 listOfElementsSRLG.subList(0, indx).clear();
232 LOG.info(" in checkInclude passed : {} ", path.getVertexList());
236 private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
237 PceConstraints pceHardConstraints) {
239 List<String> listOfElements = new ArrayList<>();
240 for (PceGraphEdge link : pathEdges) {
243 listOfElements.add(link.link().getdestNetworkSupNodeId());
246 listOfElements.add(link.link().getdestCLLI());
249 if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
250 listOfElements.add("NONE");
253 // srlg of link is List<Long>. But in this algo we need string representation of
255 // this should be any SRLG mentioned in include constraints if any of them if
257 boolean found = false;
258 for (Long srlg : link.link().getsrlgList()) {
259 String srlgStr = String.valueOf(srlg);
260 if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
261 listOfElements.add(srlgStr);
262 LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
267 // there is no specific srlg to include. thus add to list just the first one
268 listOfElements.add("NONE");
272 LOG.debug("listOfElementsBuild unsupported resource type");
275 return listOfElements;
278 private Map<String, Uint16> chooseTribPort(GraphPath<String,
279 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
280 LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
281 Map<String, Uint16> tribPortMap = new HashMap<>();
283 for (PceGraphEdge edge : path.getEdgeList()) {
284 NodeId linkSrcNode = edge.link().getSourceId();
285 String linkSrcTp = edge.link().getSourceTP().getValue();
286 NodeId linkDestNode = edge.link().getDestId();
287 String linkDestTp = edge.link().getDestTP().getValue();
288 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
289 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
290 List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
291 List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
292 List<Uint16> commonEdgeTpnPool = new ArrayList<>();
293 for (Uint16 srcTpn : srcTpnPool) {
294 if (destTpnPool.contains(srcTpn)) {
295 commonEdgeTpnPool.add(srcTpn);
298 Collections.sort(commonEdgeTpnPool);
299 if (!commonEdgeTpnPool.isEmpty()) {
300 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
301 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
302 for (Uint16 commonTribPort : commonEdgeTpnPool) {
303 if (tribPort.equals(commonTribPort.toJava())) {
304 tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
309 tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
313 private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
314 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
315 LOG.info("In choosetribSlot: edgeList = {} ", path.getEdgeList());
316 Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
318 for (PceGraphEdge edge : path.getEdgeList()) {
319 NodeId linkSrcNode = edge.link().getSourceId();
320 String linkSrcTp = edge.link().getSourceTP().getValue();
321 NodeId linkDestNode = edge.link().getDestId();
322 String linkDestTp = edge.link().getDestTP().getValue();
323 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
324 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
325 List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
326 List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
327 List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
328 List<Uint16> tribSlotList = new ArrayList<>();
329 for (Uint16 integer : srcTsPool) {
330 if (destTsPool.contains(integer)) {
331 commonEdgeTsPoolList.add(integer);
334 Collections.sort(commonEdgeTsPoolList);
335 List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
336 for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
337 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
339 commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
342 Collections.sort(commonGoodStartEdgeTsPoolList);
343 boolean goodTsList = false;
344 for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
345 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
346 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
347 for (int i = 0; i < nbSlot; i++) {
348 if (!commonEdgeTsPoolList.get(goodStartIndex + i)
349 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
351 tribSlotList.clear();
354 tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
359 tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
361 tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
365 private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
366 String tribport = tribPort.values().toArray()[0].toString();
367 @SuppressWarnings("unchecked")
368 List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
369 OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
370 .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
371 OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
372 .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
373 List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
374 minmaxTpTsList.add(minOpucnTs);
375 minmaxTpTsList.add(maxOpucnTs);
376 return minmaxTpTsList;
379 // Check the path OSNR
380 private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
383 LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
384 LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
385 double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
386 for (PceGraphEdge edge : path.getEdgeList()) {
387 if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
389 linkOsnrDb = edge.link().getosnr();
390 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
391 // 1 over the local OSNR, in linear units
392 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
396 osnrDb = getOsnrDb(1 / inverseLocalOsnr);
397 } catch (ArithmeticException e) {
398 LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
401 LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
402 return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
405 private double getOsnrDb(double osnrLu) {
406 return (10 * Math.log10(osnrLu));
409 private double getInverseOsnrLinkLu(double linkOsnrDb) {
410 // 1 over the link OSNR, in linear units
411 double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
412 LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
413 return (CONST_OSNR / linkOsnrLu);
417 * Get spectrum assignment for path.
419 * @param path the path for which we get spectrum assignment.
420 * @param allPceNodes all optical nodes.
421 * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
423 * @return a spectrum assignment object which contains begin and end index. If
424 * no spectrum assignment found, beginIndex = stopIndex = 0
426 private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
427 Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
428 byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
429 Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
430 BitSet result = BitSet.valueOf(freqMap);
431 boolean isFlexGrid = true;
432 LOG.info("Processing path {} with length {}", path, path.getLength());
433 BitSet pceNodeFreqMap;
434 for (PceGraphEdge edge : path.getEdgeList()) {
435 LOG.info("Processing source {} ", edge.link().getSourceId());
436 if (allPceNodes.containsKey(edge.link().getSourceId())) {
437 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
438 LOG.info("Processing PCE node {}", pceNode);
439 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
440 LOG.info("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
441 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
444 if ((pceNode.getSlotWidthGranularity().equals(GridConstant.SLOT_WIDTH_50))
445 && (pceNode.getCentralFreqGranularity().equals(GridConstant.SLOT_WIDTH_50))) {
446 LOG.info("Node {}: version is {} with slot width granularity {} and central "
447 + "frequency granularity is {} -> fixed grid mode",
448 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
449 pceNode.getCentralFreqGranularity());
452 pceNodeFreqMap = pceNode.getBitSetData();
453 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
454 if (pceNodeFreqMap != null) {
455 result.and(pceNodeFreqMap);
456 LOG.debug("intermediate bitset {}", result);
460 LOG.debug("Bitset result {}", result);
461 return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
465 * Compute spectrum assignment from spectrum occupation for spectral width.
467 * @param spectrumOccupation the spectrum occupation BitSet.
468 * @param spectralWidthSlotNumber the nb slots for spectral width.
469 * @param isFlexGrid true if flexible grid, false otherwise.
470 * @return a spectrum assignment object which contains begin and stop index. If
471 * no spectrum assignment found, beginIndex = stopIndex = 0
473 private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
474 boolean isFlexGrid) {
475 SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
476 .setBeginIndex(Uint16.valueOf(0))
477 .setStopIndex(Uint16.valueOf(0))
478 .setFlexGrid(isFlexGrid);
479 BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
480 referenceBitSet.set(0, spectralWidthSlotNumber);
481 int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
482 //higher is the frequency, smallest is the wavelength number
483 //in operational, the allocation is done through wavelength starting from the smallest
484 //so we have to loop from the last element of the spectrum occupation
485 for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
486 if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
487 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
488 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
492 return spectrumAssignmentBldr.build();