Update transportpce-pce YANG
[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.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;
19 import java.util.Map;
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.rev220808.SpectrumAssignment;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.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;
38
39 public class PostAlgoPathValidator {
40     /* Logging. */
41     private static final Logger LOG = LoggerFactory.getLogger(PostAlgoPathValidator.class);
42
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;
48
49     @SuppressWarnings("fallthrough")
50     @SuppressFBWarnings(
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) {
55
56         // check if the path is empty
57         if (path.getEdgeList().isEmpty()) {
58             pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
59             return pceResult;
60         }
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);
72             //fallthrough
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);
81                     return pceResult;
82                 }
83                 if (spectrumAssignment.getFlexGrid()) {
84                     LOG.debug("Spectrum assignment flexgrid mode");
85                     pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
86                 } else {
87                     LOG.debug("Spectrum assignment fixedgrid mode");
88                     pceResult.setResultWavelength(
89                             GridUtils.getWaveLengthIndexFromSpectrumAssigment(spectrumAssignment.getBeginIndex()
90                                 .toJava()));
91                 }
92                 pceResult.setMinFreq(GridUtils.getStartFrequencyFromIndex(spectrumAssignment.getBeginIndex().toJava()));
93                 pceResult.setMaxFreq(GridUtils.getStopFrequencyFromIndex(spectrumAssignment.getStopIndex().toJava()));
94                 LOG.debug("In PostAlgoPathValidator: spectrum assignment found {} {}", spectrumAssignment, path);
95
96                 // Check the OSNR
97                 if (!checkOSNR(path)) {
98                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
99                     pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
100                     return pceResult;
101                 }
102
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);
108                     return pceResult;
109                 }
110
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);
115                     return pceResult;
116                 }
117
118                 // TODO here other post algo validations can be added
119                 // more data can be sent to PceGraph module via PceResult structure if required
120
121                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
122                 pceResult.setLocalCause(PceResult.LocalCause.NONE);
123                 break;
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);
137
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);
143                 }
144                 break;
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);
153                 break;
154             default:
155                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
156                 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
157                     serviceType, path);
158                 break;
159         }
160         return pceResult;
161     }
162
163     // Check the latency
164     private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
165         double latency = 0;
166
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());
171                 return false;
172             }
173             latency += edge.link().getLatency();
174             LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
175         }
176         return (latency < maxLatency);
177     }
178
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             .stream().sorted((rp1, rp2) -> rp1.getName().compareTo(rp2.getName()))
183             .collect(Collectors.toList());
184         if (listToInclude.isEmpty()) {
185             return true;
186         }
187
188         List<PceGraphEdge> pathEdges = path.getEdgeList();
189         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
190
191         List<String> listOfElementsSubNode = new ArrayList<>();
192         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
193         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
194             pceHardConstraintsInput));
195
196         List<String> listOfElementsCLLI = new ArrayList<>();
197         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
198         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
199             pceHardConstraintsInput));
200
201         List<String> listOfElementsSRLG = new ArrayList<>();
202         // first link is XPONDEROUTPUT, no SRLG for it
203         listOfElementsSRLG.add("NONE");
204         listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
205             pceHardConstraintsInput));
206
207         // validation: check each type for each element
208         List<String> listNodeToInclude = listToInclude
209                 .stream().filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
210                 .map(ResourcePair::getName).collect(Collectors.toList());
211         List<String> listSrlgToInclude = listToInclude
212                 .stream().filter(rp -> PceConstraints.ResourceType.SRLG.equals(rp.getType()))
213                 .map(ResourcePair::getName).collect(Collectors.toList());
214         List<String> listClliToInclude = listToInclude
215                 .stream().filter(rp -> PceConstraints.ResourceType.CLLI.equals(rp.getType()))
216                 .map(ResourcePair::getName).collect(Collectors.toList());
217
218         return listOfElementsSubNode.containsAll(listNodeToInclude)
219                 && listOfElementsSRLG.containsAll(listSrlgToInclude)
220                 && listOfElementsCLLI.containsAll(listClliToInclude);
221     }
222
223     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
224         PceConstraints pceHardConstraints) {
225
226         List<String> listOfElements = new ArrayList<>();
227         for (PceGraphEdge link : pathEdges) {
228             switch (type) {
229                 case NODE:
230                     listOfElements.add(link.link().getdestNetworkSupNodeId());
231                     break;
232                 case CLLI:
233                     listOfElements.add(link.link().getdestCLLI());
234                     break;
235                 case SRLG:
236                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
237                         listOfElements.add("NONE");
238                         break;
239                     }
240                     // srlg of link is List<Long>. But in this algo we need string representation of
241                     // one SRLG
242                     // this should be any SRLG mentioned in include constraints if any of them if
243                     // mentioned
244                     boolean found = false;
245                     for (Long srlg : link.link().getsrlgList()) {
246                         String srlgStr = String.valueOf(srlg);
247                         if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
248                             listOfElements.add(srlgStr);
249                             LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
250                             found = true;
251                         }
252                     }
253                     if (!found) {
254                         // there is no specific srlg to include. thus add to list just the first one
255                         listOfElements.add("NONE");
256                     }
257                     break;
258                 default:
259                     LOG.debug("listOfElementsBuild unsupported resource type");
260             }
261         }
262         return listOfElements;
263     }
264
265     private Map<String, Uint16> chooseTribPort(GraphPath<String,
266         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
267         LOG.debug("In choosetribPort: edgeList = {} ", path.getEdgeList());
268         Map<String, Uint16> tribPortMap = new HashMap<>();
269
270         for (PceGraphEdge edge : path.getEdgeList()) {
271             NodeId linkSrcNode = edge.link().getSourceId();
272             String linkSrcTp = edge.link().getSourceTP().getValue();
273             NodeId linkDestNode = edge.link().getDestId();
274             String linkDestTp = edge.link().getDestTP().getValue();
275             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
276             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
277             List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
278             List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
279             List<Uint16> commonEdgeTpnPool = new ArrayList<>();
280             for (Uint16 srcTpn : srcTpnPool) {
281                 if (destTpnPool.contains(srcTpn)) {
282                     commonEdgeTpnPool.add(srcTpn);
283                 }
284             }
285
286             if (!commonEdgeTpnPool.isEmpty()) {
287                 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
288                 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
289                 for (Uint16 commonTribPort : commonEdgeTpnPool) {
290                     if (tribPort.equals(commonTribPort.toJava())) {
291                         tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
292                     }
293                 }
294             }
295         }
296         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
297         return tribPortMap;
298     }
299
300     private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
301         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
302         LOG.debug("In choosetribSlot: edgeList = {} ", path.getEdgeList());
303         Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
304
305         for (PceGraphEdge edge : path.getEdgeList()) {
306             NodeId linkSrcNode = edge.link().getSourceId();
307             String linkSrcTp = edge.link().getSourceTP().getValue();
308             NodeId linkDestNode = edge.link().getDestId();
309             String linkDestTp = edge.link().getDestTP().getValue();
310             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
311             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
312             List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
313             List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
314             List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
315             List<Uint16> tribSlotList = new ArrayList<>();
316             for (Uint16 integer : srcTsPool) {
317                 if (destTsPool.contains(integer)) {
318                     commonEdgeTsPoolList.add(integer);
319                 }
320             }
321             Collections.sort(commonEdgeTsPoolList);
322             List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
323             for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
324                 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
325                         || nbSlot == 1) {
326                     commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
327                 }
328             }
329             Collections.sort(commonGoodStartEdgeTsPoolList);
330             boolean goodTsList = false;
331             for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
332                 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
333                 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
334                     for (int i = 0; i < nbSlot; i++) {
335                         if (!commonEdgeTsPoolList.get(goodStartIndex + i)
336                                 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
337                             goodTsList = false;
338                             tribSlotList.clear();
339                             break;
340                         }
341                         tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
342                         goodTsList = true;
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     private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
353         String tribport = tribPort.values().toArray()[0].toString();
354         @SuppressWarnings("unchecked")
355         List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
356         OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
357             .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
358         OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
359             .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
360         List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
361         minmaxTpTsList.add(minOpucnTs);
362         minmaxTpTsList.add(maxOpucnTs);
363         return minmaxTpTsList;
364     }
365
366     // Check the path OSNR
367     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
368         double linkOsnrDb;
369         double osnrDb = 0;
370         LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
371         LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
372         double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
373         for (PceGraphEdge edge : path.getEdgeList()) {
374             if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
375                 // link OSNR in dB
376                 linkOsnrDb = edge.link().getosnr();
377                 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
378                 // 1 over the local OSNR, in linear units
379                 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
380             }
381         }
382         try {
383             osnrDb = getOsnrDb(1 / inverseLocalOsnr);
384         } catch (ArithmeticException e) {
385             LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
386             return false;
387         }
388         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
389         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
390     }
391
392     private double getOsnrDb(double osnrLu) {
393         return (10 * Math.log10(osnrLu));
394     }
395
396     private double getInverseOsnrLinkLu(double linkOsnrDb) {
397         // 1 over the link OSNR, in linear units
398         double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
399         LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
400         return (CONST_OSNR / linkOsnrLu);
401     }
402
403     /**
404      * Get spectrum assignment for path.
405      *
406      * @param path                    the path for which we get spectrum assignment.
407      * @param allPceNodes             all optical nodes.
408      * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
409      *                                service type.
410      * @return a spectrum assignment object which contains begin and end index. If
411      *         no spectrum assignment found, beginIndex = stopIndex = 0
412      */
413     private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
414             Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
415         byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
416         Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
417         BitSet result = BitSet.valueOf(freqMap);
418         boolean isFlexGrid = true;
419         LOG.debug("Processing path {} with length {}", path, path.getLength());
420         BitSet pceNodeFreqMap;
421         for (PceGraphEdge edge : path.getEdgeList()) {
422             LOG.debug("Processing source {} ", edge.link().getSourceId());
423             if (allPceNodes.containsKey(edge.link().getSourceId())) {
424                 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
425                 LOG.debug("Processing PCE node {}", pceNode);
426                 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
427                     LOG.debug("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
428                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
429                     isFlexGrid = false;
430                 }
431                 if ((pceNode.getSlotWidthGranularity().setScale(0, RoundingMode.CEILING)
432                         .equals(GridConstant.SLOT_WIDTH_50))
433                         && (pceNode.getCentralFreqGranularity().setScale(0, RoundingMode.CEILING)
434                         .equals(GridConstant.SLOT_WIDTH_50))) {
435                     LOG.debug("Node {}: version is {} with slot width granularity  {} and central "
436                             + "frequency granularity is {} -> fixed grid mode",
437                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
438                         pceNode.getCentralFreqGranularity());
439                     isFlexGrid = false;
440                 }
441                 pceNodeFreqMap = pceNode.getBitSetData();
442                 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
443                 if (pceNodeFreqMap != null) {
444                     result.and(pceNodeFreqMap);
445                     LOG.debug("intermediate bitset {}", result);
446                 }
447             }
448         }
449         LOG.debug("Bitset result {}", result);
450         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
451     }
452
453     /**
454      * Compute spectrum assignment from spectrum occupation for spectral width.
455      *
456      * @param spectrumOccupation      the spectrum occupation BitSet.
457      * @param spectralWidthSlotNumber the nb slots for spectral width.
458      * @param isFlexGrid              true if flexible grid, false otherwise.
459      * @return a spectrum assignment object which contains begin and stop index. If
460      *         no spectrum assignment found, beginIndex = stopIndex = 0
461      */
462     private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
463             boolean isFlexGrid) {
464         SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
465             .setBeginIndex(Uint16.valueOf(0))
466             .setStopIndex(Uint16.valueOf(0))
467             .setFlexGrid(isFlexGrid);
468         BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
469         referenceBitSet.set(0, spectralWidthSlotNumber);
470         int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
471         //higher is the frequency, smallest is the wavelength number
472         //in operational, the allocation is done through wavelength starting from the smallest
473         //so we have to loop from the last element of the spectrum occupation
474         for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
475             if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
476                 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
477                 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
478                 break;
479             }
480         }
481         return spectrumAssignmentBldr.build();
482     }
483 }