Introduce the 100GE_S service type in PCE
[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                 LOG.info("In PostAlgoPathValidator: ODU4/ODUC4 path found {}", path);
146                 break;
147             default:
148                 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
149                 LOG.warn("In PostAlgoPathValidator checkPath: unsupported serviceType {} found {}",
150                     serviceType, path);
151                 break;
152         }
153         return pceResult;
154     }
155
156     // Check the latency
157     private boolean checkLatency(Long maxLatency, GraphPath<String, PceGraphEdge> path) {
158         double latency = 0;
159
160         for (PceGraphEdge edge : path.getEdgeList()) {
161             try {
162                 latency += edge.link().getLatency();
163                 LOG.debug("- In checkLatency: latency of {} = {} units", edge.link().getLinkId().getValue(), latency);
164             } catch (NullPointerException e) {
165                 LOG.warn("- In checkLatency: the link {} does not contain latency field",
166                     edge.link().getLinkId().getValue());
167             }
168         }
169         return (latency < maxLatency);
170     }
171
172     // Check the inclusion if it is defined in the hard constraints
173     private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
174         List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
175         if (listToInclude.isEmpty()) {
176             return true;
177         }
178
179         List<PceGraphEdge> pathEdges = path.getEdgeList();
180         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
181
182         List<String> listOfElementsSubNode = new ArrayList<>();
183         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
184         listOfElementsSubNode.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.NODE,
185             pceHardConstraintsInput));
186
187         List<String> listOfElementsCLLI = new ArrayList<>();
188         listOfElementsCLLI.add(pathEdges.get(0).link().getsourceCLLI());
189         listOfElementsCLLI.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.CLLI,
190             pceHardConstraintsInput));
191
192         List<String> listOfElementsSRLG = new ArrayList<>();
193         // first link is XPONDEROUTPUT, no SRLG for it
194         listOfElementsSRLG.add("NONE");
195         listOfElementsSRLG.addAll(listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG,
196             pceHardConstraintsInput));
197
198         // validation: check each type for each element
199         for (ResourcePair next : listToInclude) {
200             int indx = -1;
201             switch (next.getType()) {
202                 case NODE:
203                     if (listOfElementsSubNode.contains(next.getName())) {
204                         indx = listOfElementsSubNode.indexOf(next.getName());
205                     }
206                     break;
207                 case SRLG:
208                     if (listOfElementsSRLG.contains(next.getName())) {
209                         indx = listOfElementsSRLG.indexOf(next.getName());
210                     }
211                     break;
212                 case CLLI:
213                     if (listOfElementsCLLI.contains(next.getName())) {
214                         indx = listOfElementsCLLI.indexOf(next.getName());
215                     }
216                     break;
217                 default:
218                     LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType());
219             }
220
221             if (indx < 0) {
222                 LOG.debug(" in checkInclude stopped : {} ", next.getName());
223                 return false;
224             }
225
226             LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList());
227
228             listOfElementsSubNode.subList(0, indx).clear();
229             listOfElementsCLLI.subList(0, indx).clear();
230             listOfElementsSRLG.subList(0, indx).clear();
231         }
232
233         LOG.info(" in checkInclude passed : {} ", path.getVertexList());
234         return true;
235     }
236
237     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
238         PceConstraints pceHardConstraints) {
239
240         List<String> listOfElements = new ArrayList<>();
241         for (PceGraphEdge link : pathEdges) {
242             switch (type) {
243                 case NODE:
244                     listOfElements.add(link.link().getdestNetworkSupNodeId());
245                     break;
246                 case CLLI:
247                     listOfElements.add(link.link().getdestCLLI());
248                     break;
249                 case SRLG:
250                     if (link.link().getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
251                         listOfElements.add("NONE");
252                         break;
253                     }
254                     // srlg of link is List<Long>. But in this algo we need string representation of
255                     // one SRLG
256                     // this should be any SRLG mentioned in include constraints if any of them if
257                     // mentioned
258                     boolean found = false;
259                     for (Long srlg : link.link().getsrlgList()) {
260                         String srlgStr = String.valueOf(srlg);
261                         if (pceHardConstraints.getSRLGnames().contains(srlgStr)) {
262                             listOfElements.add(srlgStr);
263                             LOG.info("listOfElementsBuild. FOUND SRLG {} in link {}", srlgStr, link.link());
264                             found = true;
265                         }
266                     }
267                     if (!found) {
268                         // there is no specific srlg to include. thus add to list just the first one
269                         listOfElements.add("NONE");
270                     }
271                     break;
272                 default:
273                     LOG.debug("listOfElementsBuild unsupported resource type");
274             }
275         }
276         return listOfElements;
277     }
278
279     private Map<String, Uint16> chooseTribPort(GraphPath<String,
280         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, Map<String, List<Uint16>> tribSlotMap, int nbSlot) {
281         LOG.info("In choosetribPort: edgeList = {} ", path.getEdgeList());
282         Map<String, Uint16> tribPortMap = new HashMap<>();
283
284         for (PceGraphEdge edge : path.getEdgeList()) {
285             NodeId linkSrcNode = edge.link().getSourceId();
286             String linkSrcTp = edge.link().getSourceTP().getValue();
287             NodeId linkDestNode = edge.link().getDestId();
288             String linkDestTp = edge.link().getDestTP().getValue();
289             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
290             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
291             List<Uint16> srcTpnPool = pceOtnNodeSrc.getAvailableTribPorts().get(linkSrcTp);
292             List<Uint16> destTpnPool = pceOtnNodeDest.getAvailableTribPorts().get(linkDestTp);
293             List<Uint16> commonEdgeTpnPool = new ArrayList<>();
294             for (Uint16 srcTpn : srcTpnPool) {
295                 if (destTpnPool.contains(srcTpn)) {
296                     commonEdgeTpnPool.add(srcTpn);
297                 }
298             }
299             Collections.sort(commonEdgeTpnPool);
300             if (!commonEdgeTpnPool.isEmpty()) {
301                 Integer startTribSlot = tribSlotMap.values().stream().findFirst().get().get(0).toJava();
302                 Integer tribPort = (int) Math.ceil((double)startTribSlot / nbSlot);
303                 for (Uint16 commonTribPort : commonEdgeTpnPool) {
304                     if (tribPort.equals(commonTribPort.toJava())) {
305                         tribPortMap.put(edge.link().getLinkId().getValue(), commonTribPort);
306                     }
307                 }
308             }
309         }
310         tribPortMap.forEach((k,v) -> LOG.info("TribPortMap : k = {}, v = {}", k, v));
311         return tribPortMap;
312     }
313
314     private Map<String, List<Uint16>> chooseTribSlot(GraphPath<String,
315         PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes, int nbSlot) {
316         LOG.info("In choosetribSlot: edgeList = {} ", path.getEdgeList());
317         Map<String, List<Uint16>> tribSlotMap = new HashMap<>();
318
319         for (PceGraphEdge edge : path.getEdgeList()) {
320             NodeId linkSrcNode = edge.link().getSourceId();
321             String linkSrcTp = edge.link().getSourceTP().getValue();
322             NodeId linkDestNode = edge.link().getDestId();
323             String linkDestTp = edge.link().getDestTP().getValue();
324             PceNode pceOtnNodeSrc = allPceNodes.get(linkSrcNode);
325             PceNode pceOtnNodeDest = allPceNodes.get(linkDestNode);
326             List<Uint16> srcTsPool = pceOtnNodeSrc.getAvailableTribSlots().get(linkSrcTp);
327             List<Uint16> destTsPool = pceOtnNodeDest.getAvailableTribSlots().get(linkDestTp);
328             List<Uint16> commonEdgeTsPoolList = new ArrayList<>();
329             List<Uint16> tribSlotList = new ArrayList<>();
330             for (Uint16 integer : srcTsPool) {
331                 if (destTsPool.contains(integer)) {
332                     commonEdgeTsPoolList.add(integer);
333                 }
334             }
335             Collections.sort(commonEdgeTsPoolList);
336             List<Uint16> commonGoodStartEdgeTsPoolList = new ArrayList<>();
337             for (Uint16 startEdgeTsPool : commonEdgeTsPoolList) {
338                 if (Integer.valueOf(1).equals(startEdgeTsPool.toJava() % nbSlot)
339                         || nbSlot == 1) {
340                     commonGoodStartEdgeTsPoolList.add(startEdgeTsPool);
341                 }
342             }
343             Collections.sort(commonGoodStartEdgeTsPoolList);
344             boolean goodTsList = false;
345             for (Uint16 goodStartTsPool : commonGoodStartEdgeTsPoolList) {
346                 int goodStartIndex = commonEdgeTsPoolList.indexOf(Uint16.valueOf(goodStartTsPool.intValue()));
347                 if (!goodTsList && commonEdgeTsPoolList.size() - goodStartIndex >= nbSlot) {
348                     for (int i = 0; i < nbSlot; i++) {
349                         if (!commonEdgeTsPoolList.get(goodStartIndex + i)
350                                 .equals(Uint16.valueOf(goodStartTsPool.toJava() + i))) {
351                             goodTsList = false;
352                             tribSlotList.clear();
353                             break;
354                         }
355                         tribSlotList.add(commonEdgeTsPoolList.get(goodStartIndex + i));
356                         goodTsList = true;
357                     }
358                 }
359             }
360             tribSlotMap.put(edge.link().getLinkId().getValue(), tribSlotList);
361         }
362         tribSlotMap.forEach((k,v) -> LOG.info("TribSlotMap : k = {}, v = {}", k, v));
363         return tribSlotMap;
364     }
365
366     private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
367         String tribport = tribPort.values().toArray()[0].toString();
368         @SuppressWarnings("unchecked")
369         List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
370         OpucnTribSlotDef minOpucnTs = OpucnTribSlotDef
371             .getDefaultInstance(String.join(".", tribport, tsList.get(0).toString()));
372         OpucnTribSlotDef maxOpucnTs = OpucnTribSlotDef
373             .getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()));
374         List<OpucnTribSlotDef> minmaxTpTsList = new ArrayList<>();
375         minmaxTpTsList.add(minOpucnTs);
376         minmaxTpTsList.add(maxOpucnTs);
377         return minmaxTpTsList;
378     }
379
380     // Check the path OSNR
381     private boolean checkOSNR(GraphPath<String, PceGraphEdge> path) {
382         double linkOsnrDb;
383         double osnrDb = 0;
384         LOG.info("- In checkOSNR: OSNR of the transmitter = {} dB", TRX_OSNR);
385         LOG.info("- In checkOSNR: add-path incremental OSNR = {} dB", ADD_OSNR);
386         double inverseLocalOsnr = getInverseOsnrLinkLu(TRX_OSNR) + getInverseOsnrLinkLu(ADD_OSNR);
387         for (PceGraphEdge edge : path.getEdgeList()) {
388             if (edge.link().getlinkType() == OpenroadmLinkType.ROADMTOROADM) {
389                 // link OSNR in dB
390                 linkOsnrDb = edge.link().getosnr();
391                 LOG.info("- In checkOSNR: OSNR of {} = {} dB", edge.link().getLinkId().getValue(), linkOsnrDb);
392                 // 1 over the local OSNR, in linear units
393                 inverseLocalOsnr += getInverseOsnrLinkLu(linkOsnrDb);
394             }
395         }
396         try {
397             osnrDb = getOsnrDb(1 / inverseLocalOsnr);
398         } catch (ArithmeticException e) {
399             LOG.debug("In checkOSNR: OSNR is equal to 0 and the number of links is: {}", path.getEdgeList().size());
400             return false;
401         }
402         LOG.info("In checkOSNR: OSNR of the path is {} dB", osnrDb);
403         return ((osnrDb + SYS_MARGIN) > MIN_OSNR_W100G);
404     }
405
406     private double getOsnrDb(double osnrLu) {
407         return (10 * Math.log10(osnrLu));
408     }
409
410     private double getInverseOsnrLinkLu(double linkOsnrDb) {
411         // 1 over the link OSNR, in linear units
412         double linkOsnrLu = Math.pow(10, (linkOsnrDb / 10.0));
413         LOG.debug("In retrieveosnr: the inverse of link osnr is {} (Linear Unit)", linkOsnrLu);
414         return (CONST_OSNR / linkOsnrLu);
415     }
416
417     /**
418      * Get spectrum assignment for path.
419      *
420      * @param path                    the path for which we get spectrum assignment.
421      * @param allPceNodes             all optical nodes.
422      * @param spectralWidthSlotNumber number of slot for spectral width. Depends on
423      *                                service type.
424      * @return a spectrum assignment object which contains begin and end index. If
425      *         no spectrum assignment found, beginIndex = stopIndex = 0
426      */
427     private SpectrumAssignment getSpectrumAssignment(GraphPath<String, PceGraphEdge> path,
428             Map<NodeId, PceNode> allPceNodes, int spectralWidthSlotNumber) {
429         byte[] freqMap = new byte[GridConstant.NB_OCTECTS];
430         Arrays.fill(freqMap, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
431         BitSet result = BitSet.valueOf(freqMap);
432         boolean isFlexGrid = true;
433         LOG.info("Processing path {} with length {}", path, path.getLength());
434         BitSet pceNodeFreqMap;
435         for (PceGraphEdge edge : path.getEdgeList()) {
436             LOG.info("Processing source {} ", edge.link().getSourceId());
437             if (allPceNodes.containsKey(edge.link().getSourceId())) {
438                 PceNode pceNode = allPceNodes.get(edge.link().getSourceId());
439                 LOG.info("Processing PCE node {}", pceNode);
440                 if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNode.getVersion())) {
441                     LOG.info("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
442                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity());
443                     isFlexGrid = false;
444                 }
445                 if ((pceNode.getSlotWidthGranularity().equals(GridConstant.SLOT_WIDTH_50))
446                     && (pceNode.getCentralFreqGranularity().equals(GridConstant.SLOT_WIDTH_50))) {
447                     LOG.info("Node {}: version is {} with slot width granularity  {} and central "
448                             + "frequency granularity is {} -> fixed grid mode",
449                         pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(),
450                         pceNode.getCentralFreqGranularity());
451                     isFlexGrid = false;
452                 }
453                 pceNodeFreqMap = pceNode.getBitSetData();
454                 LOG.debug("Pce node bitset {}", pceNodeFreqMap);
455                 if (pceNodeFreqMap != null) {
456                     result.and(pceNodeFreqMap);
457                     LOG.debug("intermediate bitset {}", result);
458                 }
459             }
460         }
461         LOG.debug("Bitset result {}", result);
462         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
463     }
464
465     /**
466      * Compute spectrum assignment from spectrum occupation for spectral width.
467      *
468      * @param spectrumOccupation      the spectrum occupation BitSet.
469      * @param spectralWidthSlotNumber the nb slots for spectral width.
470      * @param isFlexGrid              true if flexible grid, false otherwise.
471      * @return a spectrum assignment object which contains begin and stop index. If
472      *         no spectrum assignment found, beginIndex = stopIndex = 0
473      */
474     private SpectrumAssignment computeBestSpectrumAssignment(BitSet spectrumOccupation, int spectralWidthSlotNumber,
475             boolean isFlexGrid) {
476         SpectrumAssignmentBuilder spectrumAssignmentBldr = new SpectrumAssignmentBuilder()
477             .setBeginIndex(Uint16.valueOf(0))
478             .setStopIndex(Uint16.valueOf(0))
479             .setFlexGrid(isFlexGrid);
480         BitSet referenceBitSet = new BitSet(spectralWidthSlotNumber);
481         referenceBitSet.set(0, spectralWidthSlotNumber);
482         int nbSteps = isFlexGrid ? spectralWidthSlotNumber : 1;
483         //higher is the frequency, smallest is the wavelength number
484         //in operational, the allocation is done through wavelength starting from the smallest
485         //so we have to loop from the last element of the spectrum occupation
486         for (int i = spectrumOccupation.size(); i >= spectralWidthSlotNumber; i -= nbSteps) {
487             if (spectrumOccupation.get(i - spectralWidthSlotNumber, i).equals(referenceBitSet)) {
488                 spectrumAssignmentBldr.setBeginIndex(Uint16.valueOf(i - spectralWidthSlotNumber));
489                 spectrumAssignmentBldr.setStopIndex(Uint16.valueOf(i - 1));
490                 break;
491             }
492         }
493         return spectrumAssignmentBldr.build();
494     }
495 }