Update release in docs/conf.yaml
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / graph / PostAlgoPathValidator.java
index beba206825d9f5d16e25fae0294956981b67b839..53353aeae0415acfb29aa9867e4f4d2de734be67 100644 (file)
@@ -16,8 +16,10 @@ import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 import org.jgrapht.GraphPath;
@@ -36,8 +38,9 @@ import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair
 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.SpectrumAssignment;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.SpectrumAssignmentBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.PceConstraintMode;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.SpectrumAssignment;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.SpectrumAssignmentBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev230526.TerminationPoint1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmLinkType;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmNodeType;
@@ -55,11 +58,14 @@ public class PostAlgoPathValidator {
 
     public static final Long CONST_OSNR = 1L;
     public static final double SYS_MARGIN = 0;
+
     private Double tpceCalculatedMargin = 0.0;
     private final NetworkTransactionService networkTransactionService;
+    private final BitSet spectrumConstraint;
 
-    public PostAlgoPathValidator(NetworkTransactionService networkTransactionService) {
+    public PostAlgoPathValidator(NetworkTransactionService networkTransactionService, BitSet spectrumConstraint) {
         this.networkTransactionService = networkTransactionService;
+        this.spectrumConstraint = spectrumConstraint;
     }
 
     @SuppressWarnings("fallthrough")
@@ -68,7 +74,7 @@ public class PostAlgoPathValidator {
         justification = "intentional fallthrough")
     public PceResult checkPath(GraphPath<String, PceGraphEdge> path, Map<NodeId, PceNode> allPceNodes,
             Map<LinkId, PceLink> allPceLinks, PceResult pceResult, PceConstraints pceHardConstraints,
-            String serviceType) {
+            String serviceType, PceConstraintMode mode) {
         LOG.info("path = {}", path);
         // check if the path is empty
         if (path.getEdgeList().isEmpty()) {
@@ -139,7 +145,7 @@ public class PostAlgoPathValidator {
                     return pceResult;
                 }
                 // Check if nodes are included in the hard constraints
-                if (!checkInclude(path, pceHardConstraints)) {
+                if (!checkInclude(path, pceHardConstraints, mode)) {
                     pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
                     pceResult.setLocalCause(PceResult.LocalCause.HD_NODE_INCLUDE);
                     return pceResult;
@@ -202,15 +208,17 @@ public class PostAlgoPathValidator {
     }
 
     // Check the inclusion if it is defined in the hard constraints
-    private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput) {
-        List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude()
-            .stream().sorted((rp1, rp2) -> rp1.getName().compareTo(rp2.getName()))
-            .collect(Collectors.toList());
+    //TODO: remove this checkstyle false positive warning when the checkstyle bug will be fixed
+    @SuppressWarnings("MissingSwitchDefault")
+    private boolean checkInclude(GraphPath<String, PceGraphEdge> path, PceConstraints pceHardConstraintsInput,
+            PceConstraintMode mode) {
+        List<ResourcePair> listToInclude = pceHardConstraintsInput.getListToInclude();
         if (listToInclude.isEmpty()) {
             return true;
         }
         List<PceGraphEdge> pathEdges = path.getEdgeList();
         LOG.debug(" in checkInclude vertex list: [{}]", path.getVertexList());
+        LOG.debug("listToInclude = {}", listToInclude);
         List<String> listOfElementsSubNode = new ArrayList<>();
         listOfElementsSubNode.add(pathEdges.get(0).link().getsourceNetworkSupNodeId());
         listOfElementsSubNode.addAll(
@@ -225,10 +233,17 @@ public class PostAlgoPathValidator {
         listOfElementsSRLG.addAll(
             listOfElementsBuild(pathEdges, PceConstraints.ResourceType.SRLG, pceHardConstraintsInput));
         // validation: check each type for each element
-        return listOfElementsSubNode.containsAll(
-                listToInclude
-                    .stream().filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
-                    .map(ResourcePair::getName).collect(Collectors.toList()))
+        LOG.debug("listOfElementsSubNode = {}", listOfElementsSubNode);
+        return switch (mode) {
+            case Loose -> listOfElementsSubNode
+                .containsAll(listToInclude.stream()
+                    .filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
+                    .map(ResourcePair::getName).collect(Collectors.toList()));
+            case Strict -> listOfElementsSubNode
+                .equals(listToInclude.stream()
+                    .filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
+                    .map(ResourcePair::getName).collect(Collectors.toList()));
+        }
             && listOfElementsSRLG.containsAll(
                 listToInclude
                     .stream().filter(rp -> PceConstraints.ResourceType.SRLG.equals(rp.getType()))
@@ -241,7 +256,7 @@ public class PostAlgoPathValidator {
 
     private List<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
             PceConstraints pceHardConstraints) {
-        List<String> listOfElements = new ArrayList<>();
+        Set<String> listOfElements = new LinkedHashSet<>();
         for (PceGraphEdge link : pathEdges) {
             switch (type) {
                 case NODE:
@@ -277,7 +292,7 @@ public class PostAlgoPathValidator {
                     LOG.debug("listOfElementsBuild unsupported resource type");
             }
         }
-        return listOfElements;
+        return new ArrayList<>(listOfElements);
     }
 
     private Map<String, Uint16> chooseTribPort(GraphPath<String,
@@ -370,7 +385,7 @@ public class PostAlgoPathValidator {
 
     private List<OpucnTribSlotDef> getMinMaxTpTs(Map<String, Uint16> tribPort, Map<String, List<Uint16>> tribSlot) {
         String tribport = tribPort.values().toArray()[0].toString();
-        List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
+        @SuppressWarnings("unchecked") List<Uint16> tsList = (List<Uint16>) tribSlot.values().toArray()[0];
         return new ArrayList<>(List.of(
             OpucnTribSlotDef.getDefaultInstance(String.join(".", tribport, tsList.get(0).toString())),
             OpucnTribSlotDef.getDefaultInstance(String.join(".", tribport, tsList.get(tsList.size() - 1).toString()))));
@@ -992,36 +1007,53 @@ public class PostAlgoPathValidator {
         BitSet result = BitSet.valueOf(freqMap);
         boolean isFlexGrid = true;
         LOG.debug("Processing path {} with length {}", path, path.getLength());
+        BitSet pceNodeFreqMap;
+        Set<PceNode> pceNodes = new LinkedHashSet<>();
+
         for (PceGraphEdge edge : path.getEdgeList()) {
-            NodeId srcNodeId = edge.link().getSourceId();
-            LOG.debug("Processing source {} ", srcNodeId);
-            if (allPceNodes.containsKey(srcNodeId)) {
-                PceNode pceNode = allPceNodes.get(srcNodeId);
-                LOG.debug("Processing PCE node {}", pceNode);
-                String pceNodeVersion = pceNode.getVersion();
-                NodeId pceNodeId = pceNode.getNodeId();
-                BigDecimal sltWdthGran = pceNode.getSlotWidthGranularity();
-                BigDecimal ctralFreqGran = pceNode.getCentralFreqGranularity();
-                if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNodeVersion)) {
-                    LOG.debug("Node {}: version is {} and slot width granularity is {} -> fixed grid mode",
-                        pceNodeId, pceNodeVersion, sltWdthGran);
-                    isFlexGrid = false;
-                }
-                if (sltWdthGran.setScale(0, RoundingMode.CEILING).equals(GridConstant.SLOT_WIDTH_50)
-                        && ctralFreqGran.setScale(0, RoundingMode.CEILING).equals(GridConstant.SLOT_WIDTH_50)) {
-                    LOG.debug("Node {}: version is {} with slot width granularity {} and central "
-                            + "frequency granularity is {} -> fixed grid mode",
-                        pceNodeId, pceNodeVersion, sltWdthGran, ctralFreqGran);
-                    isFlexGrid = false;
-                }
-                BitSet pceNodeFreqMap = pceNode.getBitSetData();
-                LOG.debug("Pce node bitset {}", pceNodeFreqMap);
-                if (pceNodeFreqMap != null) {
-                    result.and(pceNodeFreqMap);
-                    LOG.debug("intermediate bitset {}", result);
-                }
+            NodeId srcId = edge.link().getSourceId();
+            NodeId dstId = edge.link().getDestId();
+            LOG.debug("Processing {} to {}", srcId.getValue(), dstId.getValue());
+            if (allPceNodes.containsKey(srcId)) {
+                pceNodes.add(allPceNodes.get(srcId));
+            }
+            if (allPceNodes.containsKey(dstId)) {
+                pceNodes.add(allPceNodes.get(dstId));
             }
         }
+
+        for (PceNode pceNode : pceNodes) {
+            LOG.debug("Processing PCE node {}", pceNode);
+            pceNodeFreqMap = pceNode.getBitSetData();
+            LOG.debug("Pce node bitset {}", pceNodeFreqMap);
+            if (pceNodeFreqMap != null) {
+                result.and(pceNodeFreqMap);
+                LOG.debug("intermediate bitset {}", result);
+            }
+            String pceNodeVersion = pceNode.getVersion();
+            BigDecimal sltWdthGran = pceNode.getSlotWidthGranularity();
+            if (StringConstants.OPENROADM_DEVICE_VERSION_1_2_1.equals(pceNodeVersion)) {
+                LOG.debug("Node {}: version is {} with slot width granularity {} - fixed grid mode",
+                    pceNode.getNodeId(), pceNodeVersion, sltWdthGran);
+                isFlexGrid = false;
+                continue;
+            }
+            if (!sltWdthGran.setScale(0, RoundingMode.CEILING).equals(GridConstant.SLOT_WIDTH_50)) {
+                continue;
+            }
+            BigDecimal ctralFreqGran = pceNode.getCentralFreqGranularity();
+            if (!ctralFreqGran.setScale(0, RoundingMode.CEILING).equals(GridConstant.SLOT_WIDTH_50)) {
+                continue;
+            }
+            LOG.debug(
+                "Node {}: version is {} with slot width and central frequency granularities {} {} - fixed grid mode",
+                pceNode.getNodeId(), pceNodeVersion, sltWdthGran, ctralFreqGran);
+            isFlexGrid = false;
+        }
+        if (spectrumConstraint != null) {
+            result.and(spectrumConstraint);
+        }
+
         LOG.debug("Bitset result {}", result);
         return computeBestSpectrumAssignment(result, spectralWidthSlotNumber, isFlexGrid);
     }