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.math.RoundingMode;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.BitSet;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.stream.Collectors;
21 import org.jgrapht.GraphPath;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.transportpce.common.StringConstants;
24 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
25 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
26 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
27 import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
28 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
29 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220615.SpectrumAssignment;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220615.SpectrumAssignmentBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.OpenroadmLinkType;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev181130.OpucnTribSlotDef;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
35 import org.opendaylight.yangtools.yang.common.Uint16;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class PostAlgoPathValidator {
41 private static final Logger LOG = LoggerFactory.getLogger(PostAlgoPathValidator.class);
43 private static final double MIN_OSNR_W100G = 17;
44 private static final double TRX_OSNR = 33;
45 private static final double ADD_OSNR = 30;
46 public static final Long CONST_OSNR = 1L;
47 public static final double SYS_MARGIN = 0;
49 @SuppressWarnings("fallthrough")
51 value = "SF_SWITCH_FALLTHROUGH",
52 justification = "intentional fallthrough")
53 public PceResult checkPath(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes,
54 PceResult pceResult, PceConstraints pceHardConstraints, String serviceType) {
56 // check if the path is empty
57 if (path.getEdgeList().isEmpty()) {
58 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
61 int spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
62 .getOrDefault(serviceType, GridConstant.NB_SLOTS_100G);
63 SpectrumAssignment spectrumAssignment = null;
64 //variable to deal with 1GE (Nb=1) and 10GE (Nb=10) cases
65 switch (serviceType) {
66 case StringConstants.SERVICE_TYPE_OTUC2:
67 case StringConstants.SERVICE_TYPE_OTUC3:
68 case StringConstants.SERVICE_TYPE_OTUC4:
69 case StringConstants.SERVICE_TYPE_400GE:
70 spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
71 .getOrDefault(serviceType, GridConstant.NB_SLOTS_400G);
73 case StringConstants.SERVICE_TYPE_100GE_T:
74 case StringConstants.SERVICE_TYPE_OTU4:
75 spectrumAssignment = getSpectrumAssignment(path, allPceNodes, spectralWidthSlotNumber);
76 pceResult.setServiceType(serviceType);
77 if (spectrumAssignment.getBeginIndex().equals(Uint16.valueOf(0))
78 && spectrumAssignment.getStopIndex().equals(Uint16.valueOf(0))) {
79 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
80 pceResult.setLocalCause(PceResult.LocalCause.NO_PATH_EXISTS);
83 if (spectrumAssignment.getFlexGrid()) {
84 LOG.info("Spectrum assignment flexgrid mode");
85 pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
87 LOG.info("Spectrum assignment fixedgrid mode");
88 pceResult.setResultWavelength(
89 GridUtils.getWaveLengthIndexFromSpectrumAssigment(spectrumAssignment.getBeginIndex()
92 pceResult.setMinFreq(GridUtils.getStartFrequencyFromIndex(spectrumAssignment.getBeginIndex().toJava()));
93 pceResult.setMaxFreq(GridUtils.getStopFrequencyFromIndex(spectrumAssignment.getStopIndex().toJava()));
94 LOG.info("In PostAlgoPathValidator: spectrum assignment found {} {}", spectrumAssignment, path);
97 if (!checkOSNR(path)) {
98 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
99 pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
103 // Check if MaxLatency is defined in the hard constraints
104 if ((pceHardConstraints.getMaxLatency() != -1)
105 && (!checkLatency(pceHardConstraints.getMaxLatency(), path))) {
106 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
107 pceResult.setLocalCause(PceResult.LocalCause.TOO_HIGH_LATENCY);
111 // Check if nodes are included in the hard constraints
112 if (!checkInclude(path, pceHardConstraints)) {
113 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
114 pceResult.setLocalCause(PceResult.LocalCause.HD_NODE_INCLUDE);
118 // TODO here other post algo validations can be added
119 // more data can be sent to PceGraph module via PceResult structure if required
121 pceResult.setRC(ResponseCodes.RESPONSE_OK);
122 pceResult.setLocalCause(PceResult.LocalCause.NONE);
124 case StringConstants.SERVICE_TYPE_100GE_M:
125 case StringConstants.SERVICE_TYPE_10GE:
126 case StringConstants.SERVICE_TYPE_1GE:
127 Map<String, Integer> tribSlotNbMap = Map.of(
128 StringConstants.SERVICE_TYPE_100GE_M, 20,
129 StringConstants.SERVICE_TYPE_10GE, 8,
130 StringConstants.SERVICE_TYPE_1GE, 1);
131 int tribSlotNb = tribSlotNbMap.get(serviceType);
132 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
133 pceResult.setServiceType(serviceType);
134 Map<String, List<Uint16>> tribSlot = chooseTribSlot(path, allPceNodes, tribSlotNb);
135 Map<String, Uint16> tribPort = chooseTribPort(path, allPceNodes, tribSlot, tribSlotNb);
136 List<OpucnTribSlotDef> resultTribPortTribSlot = getMinMaxTpTs(tribPort, tribSlot);
138 if (resultTribPortTribSlot.get(0) != null && resultTribPortTribSlot.get(1) != null) {
139 pceResult.setResultTribPortTribSlot(resultTribPortTribSlot);
140 pceResult.setRC(ResponseCodes.RESPONSE_OK);
141 LOG.info("In PostAlgoPathValidator: found TribPort {} - tribSlot {} - tribSlotNb {}",
142 tribPort, tribSlot, tribSlotNb);
145 case StringConstants.SERVICE_TYPE_ODU4:
146 case StringConstants.SERVICE_TYPE_ODUC2:
147 case StringConstants.SERVICE_TYPE_ODUC3:
148 case StringConstants.SERVICE_TYPE_ODUC4:
149 case StringConstants.SERVICE_TYPE_100GE_S:
150 pceResult.setRC(ResponseCodes.RESPONSE_OK);
151 pceResult.setServiceType(serviceType);
152 LOG.info("In PostAlgoPathValidator: ODU4/ODUCn path found {}", path);
155 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
156 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
164 private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
167 for (PceGraphEdge edge : path.getEdgeList()) {
168 if (edge.link() == null || edge.link().getLatency() == null) {
169 LOG.warn("- In checkLatency: the link {} does not contain latency field",
170 edge.link().getLinkId().getValue());
173 latency += edge.link().getLatency();
174 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
176 return (latency < maxLatency);
179 // Check the inclusion if it is defined in the hard constraints
180 private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
181 List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
182 if (listToInclude.isEmpty()) {
186 List<PceGraphEdge> pathEdges = path.getEdgeList();
187 LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
189 List<String> listOfElementsSubNode = new ArrayList<>();
190 listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
191 listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
192 pceHardConstraintsInput));
194 List<String> listOfElementsCLLI = new ArrayList<>();
195 listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
196 listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
197 pceHardConstraintsInput));
199 List<String> listOfElementsSRLG = new ArrayList<>();
200 // first link is XPONDEROUTPUT, no SRLG for it
201 listOfElementsSRLG.add("NONE");
202 listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
203 pceHardConstraintsInput));
205 // validation: check each type for each element
206 List<String> listNodeToInclude = listToInclude
207 .stream().filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
208 .map(ResourcePair::getName).collect(Collectors.toList());
209 List<String> listSrlgToInclude = listToInclude
210 .stream().filter(rp -> PceConstraints.ResourceType.SRLG.equals(rp.getType()))
211 .map(ResourcePair::getName).collect(Collectors.toList());
212 List<String> listClliToInclude = listToInclude
213 .stream().filter(rp -> PceConstraints.ResourceType.CLLI.equals(rp.getType()))
214 .map(ResourcePair::getName).collect(Collectors.toList());
216 return listOfElementsSubNode.containsAll(listNodeToInclude)
217 && listOfElementsSRLG.containsAll(listSrlgToInclude)
218 && listOfElementsCLLI.containsAll(listClliToInclude);
221 private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
222 PceConstraints pceHardConstraints) {
224 List<String> listOfElements = new ArrayList<>();
225 for (PceGraphEdge link : pathEdges) {
228 listOfElements.add(link.link().getdestNetworkSupNodeId());
231 listOfElements.add(link.link().getdestCLLI());
234 if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
235 listOfElements.add("NONE");
238 // srlg of link is List<Long>. But in this algo we need string representation of
240 // this should be any SRLG mentioned in include constraints if any of them if
242 boolean found = false;
243 for (Long srlg : link.link().getsrlgList()) {
244 String srlgStr = String.valueOf(srlg);
245 if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
246 listOfElements.add(srlgStr);
247 LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
252 // there is no specific srlg to include. thus add to list just the first one
253 listOfElements.add("NONE");
257 LOG.debug("listOfElementsBuild unsupported resource type");
260 return listOfElements;
263 private Map<String, Uint16> chooseTribPort(GraphPath<String,
264 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
265 LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
266 Map<String, Uint16> tribPortMap = new HashMap<>();
268 for (PceGraphEdge edge : path.getEdgeList()) {
269 NodeId linkSrcNode = edge.link().getSourceId();
270 String linkSrcTp = edge.link().getSourceTP().getValue();
271 NodeId linkDestNode = edge.link().getDestId();
272 String linkDestTp = edge.link().getDestTP().getValue();
273 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
274 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
275 List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
276 List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
277 List<Uint16> commonEdgeTpnPool = new ArrayList<>();
278 for (Uint16 srcTpn : srcTpnPool) {
279 if (destTpnPool.contains(srcTpn)) {
280 commonEdgeTpnPool.add(srcTpn);
284 if (!commonEdgeTpnPool.isEmpty()) {
285 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
286 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
287 for (Uint16 commonTribPort : commonEdgeTpnPool) {
288 if (tribPort.equals(commonTribPort.toJava())) {
289 tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
294 tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
298 private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
299 PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
300 LOG.info("In choosetribSlot: edgeList = {} ", path.getEdgeList());
301 Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
303 for (PceGraphEdge edge : path.getEdgeList()) {
304 NodeId linkSrcNode = edge.link().getSourceId();
305 String linkSrcTp = edge.link().getSourceTP().getValue();
306 NodeId linkDestNode = edge.link().getDestId();
307 String linkDestTp = edge.link().getDestTP().getValue();
308 PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
309 PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
310 List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
311 List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
312 List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
313 List<Uint16> tribSlotList = new ArrayList<>();
314 for (Uint16 integer : srcTsPool) {
315 if (destTsPool.contains(integer)) {
316 commonEdgeTsPoolList.add(integer);
319 Collections.sort(commonEdgeTsPoolList);
320 List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
321 for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
322 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
324 commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
327 Collections.sort(commonGoodStartEdgeTsPoolList);
328 boolean goodTsList = false;
329 for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
330 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
331 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
332 for (int i = 0; i < nbSlot; i++) {
333 if (!commonEdgeTsPoolList.get(goodStartIndex + i)
334 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
336 tribSlotList.clear();
339 tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
344 tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
346 tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
350 private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
351 String tribport = tribPort.values().toArray()[0].toString();
352 @SuppressWarnings("unchecked")
353 List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
354 OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
355 .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
356 OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
357 .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
358 List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
359 minmaxTpTsList.add(minOpucnTs);
360 minmaxTpTsList.add(maxOpucnTs);
361 return minmaxTpTsList;
364 // Check the path OSNR
365 private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
368 LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
369 LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
370 double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
371 for (PceGraphEdge edge : path.getEdgeList()) {
372 if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
374 linkOsnrDb = edge.link().getosnr();
375 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
376 // 1 over the local OSNR, in linear units
377 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
381 osnrDb = getOsnrDb(1 / inverseLocalOsnr);
382 } catch (ArithmeticException e) {
383 LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
386 LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
387 return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
390 private double getOsnrDb(double osnrLu) {
391 return (10 * Math.log10(osnrLu));
394 private double getInverseOsnrLinkLu(double linkOsnrDb) {
395 // 1 over the link OSNR, in linear units
396 double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
397 LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
398 return (CONST_OSNR / linkOsnrLu);
402 * Get spectrum assignment for path.
404 * @param path the path for which we get spectrum assignment.
405 * @param allPceNodes all optical nodes.
406 * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
408 * @return a spectrum assignment object which contains begin and end index. If
409 * no spectrum assignment found, beginIndex = stopIndex = 0
411 private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
412 Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
413 byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
414 Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
415 BitSet result = BitSet.valueOf(freqMap);
416 boolean isFlexGrid = true;
417 LOG.info("Processing path {} with length {}", path, path.getLength());
418 BitSet pceNodeFreqMap;
419 for (PceGraphEdge edge : path.getEdgeList()) {
420 LOG.info("Processing source {} ", edge.link().getSourceId());
421 if (allPceNodes.containsKey(edge.link().getSourceId())) {
422 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
423 LOG.info("Processing PCE node {}", pceNode);
424 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
425 LOG.info("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
426 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
429 if ((pceNode.getSlotWidthGranularity().setScale(0, RoundingMode.CEILING)
430 .equals(GridConstant.SLOT_WIDTH_50))
431 && (pceNode.getCentralFreqGranularity().setScale(0, RoundingMode.CEILING)
432 .equals(GridConstant.SLOT_WIDTH_50))) {
433 LOG.debug("Node {}: version is {} with slot width granularity {} and central "
434 + "frequency granularity is {} -> fixed grid mode",
435 pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
436 pceNode.getCentralFreqGranularity());
439 pceNodeFreqMap = pceNode.getBitSetData();
440 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
441 if (pceNodeFreqMap != null) {
442 result.and(pceNodeFreqMap);
443 LOG.debug("intermediate bitset {}", result);
447 LOG.debug("Bitset result {}", result);
448 return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
452 * Compute spectrum assignment from spectrum occupation for spectral width.
454 * @param spectrumOccupation the spectrum occupation BitSet.
455 * @param spectralWidthSlotNumber the nb slots for spectral width.
456 * @param isFlexGrid true if flexible grid, false otherwise.
457 * @return a spectrum assignment object which contains begin and stop index. If
458 * no spectrum assignment found, beginIndex = stopIndex = 0
460 private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
461 boolean isFlexGrid) {
462 SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
463 .setBeginIndex(Uint16.valueOf(0))
464 .setStopIndex(Uint16.valueOf(0))
465 .setFlexGrid(isFlexGrid);
466 BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
467 referenceBitSet.set(0, spectralWidthSlotNumber);
468 int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
469 //higher is the frequency, smallest is the wavelength number
470 //in operational, the allocation is done through wavelength starting from the smallest
471 //so we have to loop from the last element of the spectrum occupation
472 for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
473 if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
474 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
475 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
479 return spectrumAssignmentBldr.build();