Merge changes I74e9bee3,Ia1ad6f9c,Ic47363f6,I2bba2b18,I507097db, ...
[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     @SuppressFBWarnings(
48         value = "SF_SWITCH_FALLTHROUGH",
49         justification = "intentional fallthrough")
50     public PceResult checkPath(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes,
51         PceResult pceResult, PceConstraints pceHardConstraints, String serviceType) {
52
53         // check if the path is empty
54         if (path.getEdgeList().isEmpty()) {
55             pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
56             return pceResult;
57         }
58         int spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
59             .getOrDefault(serviceType, GridConstant.NB_SLOTS_100G);
60         SpectrumAssignment spectrumAssignment = null;
61         //variable to deal with 1GE (Nb=1) and 10GE (Nb=10) cases
62         switch (serviceType) {
63             case StringConstants.SERVICE_TYPE_OTUC4:
64             case StringConstants.SERVICE_TYPE_400GE:
65                 spectralWidthSlotNumber = GridConstant.SPECTRAL_WIDTH_SLOT_NUMBER_MAP
66                     .getOrDefault(serviceType, GridConstant.NB_SLOTS_400G);
67             //fallthrough
68             case StringConstants.SERVICE_TYPE_100GE_T:
69             case StringConstants.SERVICE_TYPE_OTU4:
70                 spectrumAssignment = getSpectrumAssignment(path, allPceNodes, spectralWidthSlotNumber);
71                 pceResult.setServiceType(serviceType);
72                 if (spectrumAssignment.getBeginIndex().equals(Uint16.valueOf(0))
73                         && spectrumAssignment.getStopIndex().equals(Uint16.valueOf(0))) {
74                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
75                     pceResult.setLocalCause(PceResult.LocalCause.NO_PATH_EXISTS);
76                     return pceResult;
77                 }
78                 if (spectrumAssignment.getFlexGrid()) {
79                     LOG.info("Spectrum assignment flexgrid mode");
80                     pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
81                 } else {
82                     LOG.info("Spectrum assignment fixedgrid mode");
83                     pceResult.setResultWavelength(
84                             GridUtils.getWaveLengthIndexFromSpectrumAssigment(spectrumAssignment.getBeginIndex()
85                                 .toJava()));
86                 }
87                 pceResult.setMinFreq(GridUtils.getStartFrequencyFromIndex(spectrumAssignment.getBeginIndex().toJava()));
88                 pceResult.setMaxFreq(GridUtils.getStopFrequencyFromIndex(spectrumAssignment.getStopIndex().toJava()));
89                 LOG.info("In PostAlgoPathValidator: spectrum assignment found {} {}", spectrumAssignment, path);
90
91                 // Check the OSNR
92                 if (!checkOSNR(path)) {
93                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
94                     pceResult.setLocalCause(PceResult.LocalCause.OUT_OF_SPEC_OSNR);
95                     return pceResult;
96                 }
97
98                 // Check if MaxLatency is defined in the hard constraints
99                 if ((pceHardConstraints.getMaxLatency() != -1)
100                         && (!checkLatency(pceHardConstraints.getMaxLatency(), path))) {
101                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
102                     pceResult.setLocalCause(PceResult.LocalCause.TOO_HIGH_LATENCY);
103                     return pceResult;
104                 }
105
106                 // Check if nodes are included in the hard constraints
107                 if (!checkInclude(path, pceHardConstraints)) {
108                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
109                     pceResult.setLocalCause(PceResult.LocalCause.HD_NODE_INCLUDE);
110                     return pceResult;
111                 }
112
113                 // TODO here other post algo validations can be added
114                 // more data can be sent to PceGraph module via PceResult structure if required
115
116                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
117                 pceResult.setLocalCause(PceResult.LocalCause.NONE);
118                 break;
119             case StringConstants.SERVICE_TYPE_100GE_M:
120             case StringConstants.SERVICE_TYPE_10GE:
121             case StringConstants.SERVICE_TYPE_1GE:
122                 Map<String, Integer> tribSlotNbMap = Map.of(
123                     StringConstants.SERVICE_TYPE_100GE_M, 20,
124                     StringConstants.SERVICE_TYPE_10GE, 8,
125                     StringConstants.SERVICE_TYPE_1GE, 1);
126                 int tribSlotNb = tribSlotNbMap.get(serviceType);
127                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
128                 pceResult.setServiceType(serviceType);
129                 Map<String, List<Uint16>> tribSlot = chooseTribSlot(path, allPceNodes, tribSlotNb);
130                 Map<String, Uint16> tribPort = chooseTribPort(path, allPceNodes, tribSlot, tribSlotNb);
131                 List<OpucnTribSlotDef> resultTribPortTribSlot = getMinMaxTpTs(tribPort, tribSlot);
132
133                 if (resultTribPortTribSlot.get(0) != null && resultTribPortTribSlot.get(1) != null) {
134                     pceResult.setResultTribPortTribSlot(resultTribPortTribSlot);
135                     pceResult.setRC(ResponseCodes.RESPONSE_OK);
136                     LOG.info("In PostAlgoPathValidator: found TribPort {} - tribSlot {} - tribSlotNb {}",
137                         tribPort, tribSlot, tribSlotNb);
138                 }
139                 break;
140             case StringConstants.SERVICE_TYPE_ODU4:
141             case StringConstants.SERVICE_TYPE_ODUC4:
142                 pceResult.setRC(ResponseCodes.RESPONSE_OK);
143                 LOG.info("In PostAlgoPathValidator: ODU4/ODUC4 path found {}", path);
144                 break;
145             default:
146                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
147                 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
148                     serviceType, path);
149                 break;
150         }
151         return pceResult;
152     }
153
154     // Check the latency
155     private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
156         double latency = 0;
157
158         for (PceGraphEdge edge : path.getEdgeList()) {
159             try {
160                 latency += edge.link().getLatency();
161                 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
162             } catch (NullPointerException e) {
163                 LOG.warn("- In checkLatency: the link {} does not contain latency field",
164                     edge.link().getLinkId().getValue());
165             }
166         }
167         return (latency < maxLatency);
168     }
169
170     // Check the inclusion if it is defined in the hard constraints
171     private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
172         List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
173         if (listToInclude.isEmpty()) {
174             return true;
175         }
176
177         List<PceGraphEdge> pathEdges = path.getEdgeList();
178         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
179
180         List<String> listOfElementsSubNode = new ArrayList<>();
181         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
182         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
183             pceHardConstraintsInput));
184
185         List<String> listOfElementsCLLI = new ArrayList<>();
186         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
187         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
188             pceHardConstraintsInput));
189
190         List<String> listOfElementsSRLG = new ArrayList<>();
191         // first link is XPONDEROUTPUT, no SRLG for it
192         listOfElementsSRLG.add("NONE");
193         listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
194             pceHardConstraintsInput));
195
196         // validation: check each type for each element
197         for (ResourcePair next : listToInclude) {
198             int indx = -1;
199             switch (next.getType()) {
200                 case NODE:
201                     if (listOfElementsSubNode.contains(next.getName())) {
202                         indx = listOfElementsSubNode.indexOf(next.getName());
203                     }
204                     break;
205                 case SRLG:
206                     if (listOfElementsSRLG.contains(next.getName())) {
207                         indx = listOfElementsSRLG.indexOf(next.getName());
208                     }
209                     break;
210                 case CLLI:
211                     if (listOfElementsCLLI.contains(next.getName())) {
212                         indx = listOfElementsCLLI.indexOf(next.getName());
213                     }
214                     break;
215                 default:
216                     LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
217             }
218
219             if (indx < 0) {
220                 LOG.debug(" in checkInclude stopped : {} ", next.getName());
221                 return false;
222             }
223
224             LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
225
226             listOfElementsSubNode.subList(0, indx).clear();
227             listOfElementsCLLI.subList(0, indx).clear();
228             listOfElementsSRLG.subList(0, indx).clear();
229         }
230
231         LOG.info(" in checkInclude passed : {} ", path.getVertexList());
232         return true;
233     }
234
235     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
236         PceConstraints pceHardConstraints) {
237
238         List<String> listOfElements = new ArrayList<>();
239         for (PceGraphEdge link : pathEdges) {
240             switch (type) {
241                 case NODE:
242                     listOfElements.add(link.link().getdestNetworkSupNodeId());
243                     break;
244                 case CLLI:
245                     listOfElements.add(link.link().getdestCLLI());
246                     break;
247                 case SRLG:
248                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
249                         listOfElements.add("NONE");
250                         break;
251                     }
252                     // srlg of link is List<Long>. But in this algo we need string representation of
253                     // one SRLG
254                     // this should be any SRLG mentioned in include constraints if any of them if
255                     // mentioned
256                     boolean found = false;
257                     for (Long srlg : link.link().getsrlgList()) {
258                         String srlgStr = String.valueOf(srlg);
259                         if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
260                             listOfElements.add(srlgStr);
261                             LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
262                             found = true;
263                         }
264                     }
265                     if (!found) {
266                         // there is no specific srlg to include. thus add to list just the first one
267                         listOfElements.add("NONE");
268                     }
269                     break;
270                 default:
271                     LOG.debug("listOfElementsBuild unsupported resource type");
272             }
273         }
274         return listOfElements;
275     }
276
277     private Map<String, Uint16> chooseTribPort(GraphPath<String,
278         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
279         LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
280         Map<String, Uint16> tribPortMap = new HashMap<>();
281
282         for (PceGraphEdge edge : path.getEdgeList()) {
283             NodeId linkSrcNode = edge.link().getSourceId();
284             String linkSrcTp = edge.link().getSourceTP().toString();
285             NodeId linkDestNode = edge.link().getDestId();
286             String linkDestTp = edge.link().getDestTP().toString();
287             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
288             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
289             List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
290             List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
291             List<Uint16> commonEdgeTpnPool = new ArrayList<>();
292             for (Uint16 srcTpn : srcTpnPool) {
293                 if (destTpnPool.contains(srcTpn)) {
294                     commonEdgeTpnPool.add(srcTpn);
295                 }
296             }
297             Collections.sort(commonEdgeTpnPool);
298             if (!commonEdgeTpnPool.isEmpty()) {
299                 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
300                 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
301                 for (Uint16 commonTribPort : commonEdgeTpnPool) {
302                     if (tribPort.equals(commonTribPort.toJava())) {
303                         tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
304                     }
305                 }
306             }
307         }
308         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
309         return tribPortMap;
310     }
311
312     private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
313         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
314         LOG.info("In choosetribSlot: edgeList = {} ", path.getEdgeList());
315         Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
316
317         for (PceGraphEdge edge : path.getEdgeList()) {
318             NodeId linkSrcNode = edge.link().getSourceId();
319             String linkSrcTp = edge.link().getSourceTP().toString();
320             NodeId linkDestNode = edge.link().getDestId();
321             String linkDestTp = edge.link().getDestTP().toString();
322             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
323             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
324             List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
325             List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
326             List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
327             List<Uint16> tribSlotList = new ArrayList<>();
328             for (Uint16 integer : srcTsPool) {
329                 if (destTsPool.contains(integer)) {
330                     commonEdgeTsPoolList.add(integer);
331                 }
332             }
333             Collections.sort(commonEdgeTsPoolList);
334             List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
335             for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
336                 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
337                         || nbSlot == 1) {
338                     commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
339                 }
340             }
341             Collections.sort(commonGoodStartEdgeTsPoolList);
342             boolean goodTsList = false;
343             for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
344                 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
345                 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
346                     for (int i = 0; i < nbSlot; i++) {
347                         if (!commonEdgeTsPoolList.get(goodStartIndex + i)
348                                 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
349                             goodTsList = false;
350                             tribSlotList.clear();
351                             break;
352                         }
353                         tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
354                         goodTsList = true;
355                     }
356                 }
357             }
358             tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
359         }
360         tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
361         return tribSlotMap;
362     }
363
364     private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
365         String tribport = tribPort.values().toArray()[0].toString();
366         @SuppressWarnings("unchecked")
367         List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
368         OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
369             .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
370         OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
371             .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
372         List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
373         minmaxTpTsList.add(minOpucnTs);
374         minmaxTpTsList.add(maxOpucnTs);
375         return minmaxTpTsList;
376     }
377
378     // Check the path OSNR
379     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
380         double linkOsnrDb;
381         double osnrDb = 0;
382         LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
383         LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
384         double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
385         for (PceGraphEdge edge : path.getEdgeList()) {
386             if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
387                 // link OSNR in dB
388                 linkOsnrDb = edge.link().getosnr();
389                 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
390                 // 1 over the local OSNR, in linear units
391                 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
392             }
393         }
394         try {
395             osnrDb = getOsnrDb(1 / inverseLocalOsnr);
396         } catch (ArithmeticException e) {
397             LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
398             return false;
399         }
400         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
401         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
402     }
403
404     private double getOsnrDb(double osnrLu) {
405         return (10 * Math.log10(osnrLu));
406     }
407
408     private double getInverseOsnrLinkLu(double linkOsnrDb) {
409         // 1 over the link OSNR, in linear units
410         double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
411         LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
412         return (CONST_OSNR / linkOsnrLu);
413     }
414
415     /**
416      * Get spectrum assignment for path.
417      *
418      * @param path                    the path for which we get spectrum assignment.
419      * @param allPceNodes             all optical nodes.
420      * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
421      *                                service type.
422      * @return a spectrum assignment object which contains begin and end index. If
423      *         no spectrum assignment found, beginIndex = stopIndex = 0
424      */
425     private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
426             Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
427         byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
428         Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
429         BitSet result = BitSet.valueOf(freqMap);
430         boolean isFlexGrid = true;
431         LOG.info("Processing path {} with length {}", path, path.getLength());
432         BitSet pceNodeFreqMap;
433         for (PceGraphEdge edge : path.getEdgeList()) {
434             LOG.info("Processing source {} ", edge.link().getSourceId());
435             if (allPceNodes.containsKey(edge.link().getSourceId())) {
436                 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
437                 LOG.info("Processing PCE node {}", pceNode);
438                 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
439                     LOG.info("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
440                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
441                     isFlexGrid = false;
442                 }
443                 if ((pceNode.getSlotWidthGranularity().equals(GridConstant.SLOT_WIDTH_50))
444                     && (pceNode.getCentralFreqGranularity().equals(GridConstant.SLOT_WIDTH_50))) {
445                     LOG.info("Node {}: version is {} with slot width granularity  {} and central "
446                             + "frequency granularity is {} -> fixed grid mode",
447                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
448                         pceNode.getCentralFreqGranularity());
449                     isFlexGrid = false;
450                 }
451                 pceNodeFreqMap = pceNode.getBitSetData();
452                 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
453                 if (pceNodeFreqMap != null) {
454                     result.and(pceNodeFreqMap);
455                     LOG.debug("intermediate bitset {}", result);
456                 }
457             }
458         }
459         LOG.debug("Bitset result {}", result);
460         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
461     }
462
463     /**
464      * Compute spectrum assignment from spectrum occupation for spectral width.
465      *
466      * @param spectrumOccupation      the spectrum occupation BitSet.
467      * @param spectralWidthSlotNumber the nb slots for spectral width.
468      * @param isFlexGrid              true if flexible grid, false otherwise.
469      * @return a spectrum assignment object which contains begin and stop index. If
470      *         no spectrum assignment found, beginIndex = stopIndex = 0
471      */
472     private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
473             boolean isFlexGrid) {
474         SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
475             .setBeginIndex(Uint16.valueOf(0))
476             .setStopIndex(Uint16.valueOf(0))
477             .setFlexGrid(isFlexGrid);
478         BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
479         referenceBitSet.set(0, spectralWidthSlotNumber);
480         int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
481         //higher is the frequency, smallest is the wavelength number
482         //in operational, the allocation is done through wavelength starting from the smallest
483         //so we have to loop from the last element of the spectrum occupation
484         for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
485             if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
486                 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
487                 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
488                 break;
489             }
490         }
491         return spectrumAssignmentBldr.build();
492     }
493 }