pce(gnpy) for Sulfur
[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.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;
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.info("Spectrum assignment flexgrid mode");
85                     pceResult.setResultWavelength(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER);
86                 } else {
87                     LOG.info("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.info("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         if (listToInclude.isEmpty()) {
183             return true;
184         }
185
186         List<PceGraphEdge> pathEdges = path.getEdgeList();
187         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
188
189         List<String> listOfElementsSubNode = new ArrayList<>();
190         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
191         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
192             pceHardConstraintsInput));
193
194         List<String> listOfElementsCLLI = new ArrayList<>();
195         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
196         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
197             pceHardConstraintsInput));
198
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));
204
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());
215
216         return listOfElementsSubNode.containsAll(listNodeToInclude)
217                 && listOfElementsSRLG.containsAll(listSrlgToInclude)
218                 && listOfElementsCLLI.containsAll(listClliToInclude);
219     }
220
221     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
222         PceConstraints pceHardConstraints) {
223
224         List<String> listOfElements = new ArrayList<>();
225         for (PceGraphEdge link : pathEdges) {
226             switch (type) {
227                 case NODE:
228                     listOfElements.add(link.link().getdestNetworkSupNodeId());
229                     break;
230                 case CLLI:
231                     listOfElements.add(link.link().getdestCLLI());
232                     break;
233                 case SRLG:
234                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
235                         listOfElements.add("NONE");
236                         break;
237                     }
238                     // srlg of link is List<Long>. But in this algo we need string representation of
239                     // one SRLG
240                     // this should be any SRLG mentioned in include constraints if any of them if
241                     // mentioned
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());
248                             found = true;
249                         }
250                     }
251                     if (!found) {
252                         // there is no specific srlg to include. thus add to list just the first one
253                         listOfElements.add("NONE");
254                     }
255                     break;
256                 default:
257                     LOG.debug("listOfElementsBuild unsupported resource type");
258             }
259         }
260         return listOfElements;
261     }
262
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<>();
267
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);
281                 }
282             }
283
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);
290                     }
291                 }
292             }
293         }
294         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
295         return tribPortMap;
296     }
297
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<>();
302
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);
317                 }
318             }
319             Collections.sort(commonEdgeTsPoolList);
320             List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
321             for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
322                 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
323                         || nbSlot == 1) {
324                     commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
325                 }
326             }
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))) {
335                             goodTsList = false;
336                             tribSlotList.clear();
337                             break;
338                         }
339                         tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
340                         goodTsList = true;
341                     }
342                 }
343             }
344             tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
345         }
346         tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
347         return tribSlotMap;
348     }
349
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;
362     }
363
364     // Check the path OSNR
365     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
366         double linkOsnrDb;
367         double osnrDb = 0;
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) {
373                 // link OSNR in dB
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);
378             }
379         }
380         try {
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());
384             return false;
385         }
386         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
387         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
388     }
389
390     private double getOsnrDb(double osnrLu) {
391         return (10 * Math.log10(osnrLu));
392     }
393
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);
399     }
400
401     /**
402      * Get spectrum assignment for path.
403      *
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
407      *                                service type.
408      * @return a spectrum assignment object which contains begin and end index. If
409      *         no spectrum assignment found, beginIndex = stopIndex = 0
410      */
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());
427                     isFlexGrid = false;
428                 }
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());
437                     isFlexGrid = false;
438                 }
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);
444                 }
445             }
446         }
447         LOG.debug("Bitset result {}", result);
448         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
449     }
450
451     /**
452      * Compute spectrum assignment from spectrum occupation for spectral width.
453      *
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
459      */
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));
476                 break;
477             }
478         }
479         return spectrumAssignmentBldr.build();
480     }
481 }