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