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 case StringConstants.SERVICE_TYPE_100GE_S:
144 pceResult.setRC(ResponseCodes.RESPONSE_OK);
145 pceResult.setServiceType(serviceType);
146 LOG.info("In PostAlgoPathValidator: ODU4/ODUC4 path found {}", path);
149 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
150 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
158 private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
161 for (PceGraphEdge edge : path.getEdgeList()) {
163 latency += edge.link().getLatency();
164 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
165 } catch (NullPointerException e) {
166 LOG.warn("- In checkLatency: the link {} does not contain latency field",
167 edge.link().getLinkId().getValue());
170 return (latency < maxLatency);
173 // Check the inclusion if it is defined in the hard constraints
174 private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
175 List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
176 if (listToInclude.isEmpty()) {
180 List<PceGraphEdge> pathEdges = path.getEdgeList();
181 LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
183 List<String> listOfElementsSubNode = new ArrayList<>();
184 listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
185 listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
186 pceHardConstraintsInput));
188 List<String> listOfElementsCLLI = new ArrayList<>();
189 listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
190 listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
191 pceHardConstraintsInput));
193 List<String> listOfElementsSRLG = new ArrayList<>();
194 // first link is XPONDEROUTPUT, no SRLG for it
195 listOfElementsSRLG.add("NONE");
196 listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
197 pceHardConstraintsInput));
199 // validation: check each type for each element
200 for (ResourcePair next : listToInclude) {
202 switch (next.getType()) {
204 if (listOfElementsSubNode.contains(next.getName())) {
205 indx = listOfElementsSubNode.indexOf(next.getName());
209 if (listOfElementsSRLG.contains(next.getName())) {
210 indx = listOfElementsSRLG.indexOf(next.getName());
214 if (listOfElementsCLLI.contains(next.getName())) {
215 indx = listOfElementsCLLI.indexOf(next.getName());
219 LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
223 LOG.debug(" in checkInclude stopped : {} ", next.getName());
227 LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
229 listOfElementsSubNode.subList(0, indx).clear();
230 listOfElementsCLLI.subList(0, indx).clear();
231 listOfElementsSRLG.subList(0, indx).clear();
234 LOG.info(" in checkInclude passed : {} ", path.getVertexList());
238 private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
239 PceConstraints pceHardConstraints) {
241 List<String> listOfElements = new ArrayList<>();
242 for (PceGraphEdge link : pathEdges) {
245 listOfElements.add(link.link().getdestNetworkSupNodeId());
248 listOfElements.add(link.link().getdestCLLI());
251 if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
252 listOfElements.add("NONE");
255 // srlg of link is List<Long>. But in this algo we need string representation of
257 // this should be any SRLG mentioned in include constraints if any of them if
259 boolean found = false;
260 for (Long srlg : link.link().getsrlgList()) {
261 String srlgStr = String.valueOf(srlg);
262 if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
263 listOfElements.add(srlgStr);
264 LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
269 // there is no specific srlg to include. thus add to list just the first one
270 listOfElements.add("NONE");
274 LOG.debug("listOfElementsBuild unsupported resource type");
277 return listOfElements;
280 private Map<String, Uint16> chooseTribPort(GraphPath<String,
281 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
282 LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
283 Map<String, Uint16> tribPortMap = new HashMap<>();
285 for (PceGraphEdge edge : path.getEdgeList()) {
286 NodeId linkSrcNode = edge.link().getSourceId();
287 String linkSrcTp = edge.link().getSourceTP().getValue();
288 NodeId linkDestNode = edge.link().getDestId();
289 String linkDestTp = edge.link().getDestTP().getValue();
290 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
291 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
292 List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
293 List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
294 List<Uint16> commonEdgeTpnPool = new ArrayList<>();
295 for (Uint16 srcTpn : srcTpnPool) {
296 if (destTpnPool.contains(srcTpn)) {
297 commonEdgeTpnPool.add(srcTpn);
300 Collections.sort(commonEdgeTpnPool);
301 if (!commonEdgeTpnPool.isEmpty()) {
302 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
303 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
304 for (Uint16 commonTribPort : commonEdgeTpnPool) {
305 if (tribPort.equals(commonTribPort.toJava())) {
306 tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
311 tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
315 private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
316 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
317 LOG.info("In choosetribSlot: edgeList = {} ", path.getEdgeList());
318 Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
320 for (PceGraphEdge edge : path.getEdgeList()) {
321 NodeId linkSrcNode = edge.link().getSourceId();
322 String linkSrcTp = edge.link().getSourceTP().getValue();
323 NodeId linkDestNode = edge.link().getDestId();
324 String linkDestTp = edge.link().getDestTP().getValue();
325 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
326 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
327 List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
328 List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
329 List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
330 List<Uint16> tribSlotList = new ArrayList<>();
331 for (Uint16 integer : srcTsPool) {
332 if (destTsPool.contains(integer)) {
333 commonEdgeTsPoolList.add(integer);
336 Collections.sort(commonEdgeTsPoolList);
337 List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
338 for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
339 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
341 commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
344 Collections.sort(commonGoodStartEdgeTsPoolList);
345 boolean goodTsList = false;
346 for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
347 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
348 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
349 for (int i = 0; i < nbSlot; i++) {
350 if (!commonEdgeTsPoolList.get(goodStartIndex + i)
351 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
353 tribSlotList.clear();
356 tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
361 tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
363 tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
367 private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
368 String tribport = tribPort.values().toArray()[0].toString();
369 @SuppressWarnings("unchecked")
370 List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
371 OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
372 .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
373 OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
374 .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
375 List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
376 minmaxTpTsList.add(minOpucnTs);
377 minmaxTpTsList.add(maxOpucnTs);
378 return minmaxTpTsList;
381 // Check the path OSNR
382 private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
385 LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
386 LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
387 double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
388 for (PceGraphEdge edge : path.getEdgeList()) {
389 if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
391 linkOsnrDb = edge.link().getosnr();
392 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
393 // 1 over the local OSNR, in linear units
394 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
398 osnrDb = getOsnrDb(1 / inverseLocalOsnr);
399 } catch (ArithmeticException e) {
400 LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
403 LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
404 return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
407 private double getOsnrDb(double osnrLu) {
408 return (10 * Math.log10(osnrLu));
411 private double getInverseOsnrLinkLu(double linkOsnrDb) {
412 // 1 over the link OSNR, in linear units
413 double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
414 LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
415 return (CONST_OSNR / linkOsnrLu);
419 * Get spectrum assignment for path.
421 * @param path the path for which we get spectrum assignment.
422 * @param allPceNodes all optical nodes.
423 * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
425 * @return a spectrum assignment object which contains begin and end index. If
426 * no spectrum assignment found, beginIndex = stopIndex = 0
428 private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
429 Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
430 byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
431 Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
432 BitSet result = BitSet.valueOf(freqMap);
433 boolean isFlexGrid = true;
434 LOG.info("Processing path {} with length {}", path, path.getLength());
435 BitSet pceNodeFreqMap;
436 for (PceGraphEdge edge : path.getEdgeList()) {
437 LOG.info("Processing source {} ", edge.link().getSourceId());
438 if (allPceNodes.containsKey(edge.link().getSourceId())) {
439 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
440 LOG.info("Processing PCE node {}", pceNode);
441 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
442 LOG.info("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
443 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
446 if ((pceNode.getSlotWidthGranularity().equals(GridConstant.SLOT_WIDTH_50))
447 && (pceNode.getCentralFreqGranularity().equals(GridConstant.SLOT_WIDTH_50))) {
448 LOG.info("Node {}: version is {} with slot width granularity {} and central "
449 + "frequency granularity is {} -> fixed grid mode",
450 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
451 pceNode.getCentralFreqGranularity());
454 pceNodeFreqMap = pceNode.getBitSetData();
455 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
456 if (pceNodeFreqMap != null) {
457 result.and(pceNodeFreqMap);
458 LOG.debug("intermediate bitset {}", result);
462 LOG.debug("Bitset result {}", result);
463 return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
467 * Compute spectrum assignment from spectrum occupation for spectral width.
469 * @param spectrumOccupation the spectrum occupation BitSet.
470 * @param spectralWidthSlotNumber the nb slots for spectral width.
471 * @param isFlexGrid true if flexible grid, false otherwise.
472 * @return a spectrum assignment object which contains begin and stop index. If
473 * no spectrum assignment found, beginIndex = stopIndex = 0
475 private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
476 boolean isFlexGrid) {
477 SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
478 .setBeginIndex(Uint16.valueOf(0))
479 .setStopIndex(Uint16.valueOf(0))
480 .setFlexGrid(isFlexGrid);
481 BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
482 referenceBitSet.set(0, spectralWidthSlotNumber);
483 int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
484 //higher is the frequency, smallest is the wavelength number
485 //in operational, the allocation is done through wavelength starting from the smallest
486 //so we have to loop from the last element of the spectrum occupation
487 for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
488 if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
489 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
490 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
494 return spectrumAssignmentBldr.build();