Adapt PCE to compute a 100GE path on OTN Switch
[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.Arrays;
14 import java.util.BitSet;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
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;
36
37 public class PostAlgoPathValidator {
38     /* Logging. */
39     private static final Logger LOG = LoggerFactory.getLogger(PostAlgoPathValidator.class);
40
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;
46
47     @SuppressWarnings("fallthrough")
48     @SuppressFBWarnings(
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) {
53
54         // check if the path is empty
55         if (path.getEdgeList().isEmpty()) {
56             pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
57             return pceResult;
58         }
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);
68             //fallthrough
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);
77                     return pceResult;
78                 }
79                 if (spectrumAssignment.getFlexGrid()) {
80                     LOG.info("Spectrum assignment flexgrid mode");
81                     pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
82                 } else {
83                     LOG.info("Spectrum assignment fixedgrid mode");
84                     pceResult.setResultWavelength(
85                             GridUtils.getWaveLengthIndexFromSpectrumAssigment(spectrumAssignment.getBeginIndex()
86                                 .toJava()));
87                 }
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);
91
92                 // Check the OSNR
93                 if (!checkOSNR(path)) {
94                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
95                     pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
96                     return pceResult;
97                 }
98
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);
104                     return pceResult;
105                 }
106
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);
111                     return pceResult;
112                 }
113
114                 // TODO here other post algo validations can be added
115                 // more data can be sent to PceGraph module via PceResult structure if required
116
117                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
118                 pceResult.setLocalCause(PceResult.LocalCause.NONE);
119                 break;
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);
133
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);
139                 }
140                 break;
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);
147                 break;
148             default:
149                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
150                 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
151                     serviceType, path);
152                 break;
153         }
154         return pceResult;
155     }
156
157     // Check the latency
158     private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
159         double latency = 0;
160
161         for (PceGraphEdge edge : path.getEdgeList()) {
162             try {
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());
168             }
169         }
170         return (latency < maxLatency);
171     }
172
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()) {
177             return true;
178         }
179
180         List<PceGraphEdge> pathEdges = path.getEdgeList();
181         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
182
183         List<String> listOfElementsSubNode = new ArrayList<>();
184         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
185         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
186             pceHardConstraintsInput));
187
188         List<String> listOfElementsCLLI = new ArrayList<>();
189         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
190         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
191             pceHardConstraintsInput));
192
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));
198
199         // validation: check each type for each element
200         for (ResourcePair next : listToInclude) {
201             int indx = -1;
202             switch (next.getType()) {
203                 case NODE:
204                     if (listOfElementsSubNode.contains(next.getName())) {
205                         indx = listOfElementsSubNode.indexOf(next.getName());
206                     }
207                     break;
208                 case SRLG:
209                     if (listOfElementsSRLG.contains(next.getName())) {
210                         indx = listOfElementsSRLG.indexOf(next.getName());
211                     }
212                     break;
213                 case CLLI:
214                     if (listOfElementsCLLI.contains(next.getName())) {
215                         indx = listOfElementsCLLI.indexOf(next.getName());
216                     }
217                     break;
218                 default:
219                     LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
220             }
221
222             if (indx < 0) {
223                 LOG.debug(" in checkInclude stopped : {} ", next.getName());
224                 return false;
225             }
226
227             LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
228
229             listOfElementsSubNode.subList(0, indx).clear();
230             listOfElementsCLLI.subList(0, indx).clear();
231             listOfElementsSRLG.subList(0, indx).clear();
232         }
233
234         LOG.info(" in checkInclude passed : {} ", path.getVertexList());
235         return true;
236     }
237
238     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
239         PceConstraints pceHardConstraints) {
240
241         List<String> listOfElements = new ArrayList<>();
242         for (PceGraphEdge link : pathEdges) {
243             switch (type) {
244                 case NODE:
245                     listOfElements.add(link.link().getdestNetworkSupNodeId());
246                     break;
247                 case CLLI:
248                     listOfElements.add(link.link().getdestCLLI());
249                     break;
250                 case SRLG:
251                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
252                         listOfElements.add("NONE");
253                         break;
254                     }
255                     // srlg of link is List<Long>. But in this algo we need string representation of
256                     // one SRLG
257                     // this should be any SRLG mentioned in include constraints if any of them if
258                     // mentioned
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());
265                             found = true;
266                         }
267                     }
268                     if (!found) {
269                         // there is no specific srlg to include. thus add to list just the first one
270                         listOfElements.add("NONE");
271                     }
272                     break;
273                 default:
274                     LOG.debug("listOfElementsBuild unsupported resource type");
275             }
276         }
277         return listOfElements;
278     }
279
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<>();
284
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);
298                 }
299             }
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);
307                     }
308                 }
309             }
310         }
311         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
312         return tribPortMap;
313     }
314
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<>();
319
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);
334                 }
335             }
336             Collections.sort(commonEdgeTsPoolList);
337             List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
338             for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
339                 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
340                         || nbSlot == 1) {
341                     commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
342                 }
343             }
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))) {
352                             goodTsList = false;
353                             tribSlotList.clear();
354                             break;
355                         }
356                         tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
357                         goodTsList = true;
358                     }
359                 }
360             }
361             tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
362         }
363         tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
364         return tribSlotMap;
365     }
366
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;
379     }
380
381     // Check the path OSNR
382     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
383         double linkOsnrDb;
384         double osnrDb = 0;
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) {
390                 // link OSNR in dB
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);
395             }
396         }
397         try {
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());
401             return false;
402         }
403         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
404         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
405     }
406
407     private double getOsnrDb(double osnrLu) {
408         return (10 * Math.log10(osnrLu));
409     }
410
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);
416     }
417
418     /**
419      * Get spectrum assignment for path.
420      *
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
424      *                                service type.
425      * @return a spectrum assignment object which contains begin and end index. If
426      *         no spectrum assignment found, beginIndex = stopIndex = 0
427      */
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());
444                     isFlexGrid = false;
445                 }
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());
452                     isFlexGrid = false;
453                 }
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);
459                 }
460             }
461         }
462         LOG.debug("Bitset result {}", result);
463         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
464     }
465
466     /**
467      * Compute spectrum assignment from spectrum occupation for spectral width.
468      *
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
474      */
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));
491                 break;
492             }
493         }
494         return spectrumAssignmentBldr.build();
495     }
496 }