Update transportpce-pce YANG
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / constraints / PceConstraintsCalc.java
index dda3ea280407a93ee6c4d41d5206b9cc56dee8ac..0142d0fcc952cd6dbcc6ca96d8417b3c895a715a 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.transportpce.pce.constraints;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
@@ -19,36 +19,36 @@ import java.util.stream.Collectors;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.PathComputationRequestInput;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.PathDescription;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.atoz.direction.AToZ;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.pce.resource.resource.resource.Link;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.pce.resource.resource.resource.Node;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.CoRoutingOrGeneral;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.CoRouting;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.General;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Diversity;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Exclude;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Include;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Latency;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.diversity.existing.service.contraints.sp.ExistingServiceApplicability;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.ordered.constraints.sp.hop.type.HopType;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
+import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRequestInput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.Constraints;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierList;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.service.applicability.g.ServiceApplicability;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Link;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Node;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.PceMetric;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 public class PceConstraintsCalc {
     /* Logging. */
     private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
-    private static final Comparator<OrderedHops> ORDERED_HOP_COMPARATOR =
-            Comparator.comparing(OrderedHops::getHopNumber);
 
     private PceConstraints pceHardConstraints = new PceConstraints();
     private PceConstraints pceSoftConstraints = new PceConstraints();
@@ -58,7 +58,7 @@ public class PceConstraintsCalc {
     public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
         LOG.debug("In PceconstraintsCalc start");
 
-        pceMetrics = input.getPceMetric();
+        pceMetrics = input.getPceRoutingMetric();
 
         this.networkTransactionService = networkTransactionService;
 
@@ -76,10 +76,7 @@ public class PceConstraintsCalc {
             LOG.info("In calcHardconstraints: no hard constraints.");
             return;
         }
-
-        CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, pceHardConstraints);
-
+        readConstraints(servicePathHardConstraints, pceHardConstraints);
     }
 
     private void calcSoftconstraints(PathComputationRequestInput input) {
@@ -88,185 +85,104 @@ public class PceConstraintsCalc {
             LOG.info("In calcSoftconstraints: no soft constraints.");
             return;
         }
-
-        CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, pceSoftConstraints);
-
+        readConstraints(servicePathSoftConstraints, pceSoftConstraints);
     }
 
-    private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
-        LOG.debug("In readconstraints start");
+    private void readConstraints(Constraints hardConstraints, PceConstraints constraints) {
+        LOG.debug("In readHardconstraints start");
 
-        if (coRoutingOrGeneral == null) {
-            LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
-            return;
+        if (hardConstraints.getInclude() != null) {
+            readInclude(hardConstraints.getInclude(), constraints);
         }
-
-        General tmpGeneral = null;
-        CoRouting tmpCoRouting = null;
-
-        if (coRoutingOrGeneral instanceof General) {
-            LOG.info("In readconstraints General {}", coRoutingOrGeneral);
-            tmpGeneral = (General) coRoutingOrGeneral;
-            readGeneralContrains(tmpGeneral, constraints);
-            return;
+        if (hardConstraints.getExclude() != null) {
+            readExclude(hardConstraints.getExclude(), constraints);
         }
-
-        if (coRoutingOrGeneral instanceof CoRouting) {
-            LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral);
-            tmpCoRouting = (CoRouting) coRoutingOrGeneral;
-            readCoRoutingContrains(tmpCoRouting, constraints);
+        if (hardConstraints.getCoRouting() != null) {
+            readCoRouting(hardConstraints.getCoRouting(), constraints);
         }
-    }
-
-    private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
-        LOG.debug("In readGeneralContrains start");
-
-        if (tmpGeneral == null) {
-            LOG.info("In readGeneralContrains: no General constraints.");
-            return;
+        if (hardConstraints.getDiversity() != null) {
+            readDiversity(hardConstraints.getDiversity(), constraints);
         }
-
-        Latency latency = tmpGeneral.getLatency();
-        if (latency != null) {
-            constraints.setMaxLatency(latency.getMaxLatency().toJava());
-            LOG.info("In readGeneralContrains: read latency {}", latency);
+        if (hardConstraints.getLatency() != null) {
+            constraints.setMaxLatency(hardConstraints.getLatency().getMaxLatency().longValue());
         }
+    }
 
-        Exclude exclude = tmpGeneral.getExclude();
-        List<String> elementsToExclude = null;
-        if (exclude != null) {
-            elementsToExclude = exclude.getNodeId();
-            if (elementsToExclude != null) {
-                constraints.setExcludeSupNodes(elementsToExclude);
-            }
-
-            elementsToExclude = exclude.getSRLG();
-            if (elementsToExclude != null) {
-                List<Long> srlgToExclude = new ArrayList<>();
-                for (String str : elementsToExclude) {
-                    srlgToExclude.add(Long.parseLong(str));
-                }
-                constraints.setExcludeSRLG(srlgToExclude);
-            }
-
-            elementsToExclude = exclude.getClli();
-            if (elementsToExclude != null) {
-                constraints.setExcludeCLLI(elementsToExclude);
+    private void readExclude(Exclude exclude, PceConstraints constraints) {
+        //TODO: Implement other exclude constraints: fiber-bundle, link-identifier
+        // and supporting-service-name
+        if (exclude.getNodeId() != null) {
+            List<String> elementsToExclude = new ArrayList<>();
+            for (NodeIdType node : exclude.getNodeId()) {
+                elementsToExclude.add(node.getValue());
             }
+            constraints.setExcludeSupNodes(elementsToExclude);
         }
-
-        Include include = tmpGeneral.getInclude();
-        if (include != null) {
-            List<OrderedHops> listHops = new ArrayList<>(include.nonnullOrderedHops().values());
-            if (listHops != null) {
-                readIncludeNodes(listHops, constraints);
+        if (exclude.getSrlgId() != null) {
+            List<Long> elementsToExclude = new ArrayList<>();
+            for (Uint32 srlg : exclude.getSrlgId()) {
+                elementsToExclude.add(srlg.longValue());
             }
-            LOG.debug("in readGeneralContrains INCLUDE {} ", include);
+            constraints.setExcludeSRLG(elementsToExclude);
         }
+        if (exclude.getSite() != null) {
+            constraints.setExcludeCLLI(new ArrayList<>(exclude.getSite()));
+        }
+        if (exclude.getFiberBundle() != null || exclude.getLinkIdentifier() != null
+            || exclude.getSupportingServiceName() != null) {
+            LOG.warn("exclude constraints of type fiber-bundle, link-identifier"
+                + "or supporting-service-name are not implemented yet");
+        }
+    }
 
-        Diversity diversity = tmpGeneral.getDiversity();
-        PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
-        if (diversity != null) {
-            ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
-            if (temp == null) {
-                return;
+    private void readInclude(Include include, PceConstraints constraints) {
+        if (include.getNodeId() != null) {
+            for (NodeIdType node : include.getNodeId()) {
+                constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.NODE, node.getValue()));
             }
-            if (Boolean.TRUE.equals(temp.getNode())) {
-                rt = PceConstraints.ResourceType.NODE;
-            }
-            if (Boolean.TRUE.equals(temp.getSrlg())) {
-                rt = PceConstraints.ResourceType.SRLG;
+        }
+        if (include.getSrlgId() != null) {
+            for (Uint32 srlg : include.getSrlgId()) {
+                constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.SRLG, srlg.toString()));
             }
-            if (Boolean.TRUE.equals(temp.getClli())) {
-                rt = PceConstraints.ResourceType.CLLI;
+        }
+        if (include.getSite() != null) {
+            for (String site : include.getSite()) {
+                constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.CLLI, site));
             }
-            LOG.info("in readGeneralContrains {} list is :{}", rt, diversity);
-            readDiversity(diversity.getExistingService(), constraints, rt);
         }
-
     }
 
-    private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
-        Collections.sort(listHops, ORDERED_HOP_COMPARATOR);
-        for (int i = 0; i < listHops.size(); i++) {
-            HopType hoptype = listHops.get(i).getHopType().getHopType();
-
-            String hopt = hoptype.implementedInterface().getSimpleName();
-            LOG.info("in readIncludeNodes next hop to include {}", hopt);
-            switch (hopt) {
-                case "Node":
-                    org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
-                            .rev171017.ordered.constraints.sp.hop.type.hop.type.Node
-                            node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
-                            .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
-                    constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.NODE,
-                            node.getNodeId()));
-                    break;
-                case "SRLG":
-                    org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
-                            .rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG
-                            srlg = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
-                            .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG) hoptype;
-                    constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.SRLG,
-                            srlg.getSRLG()));
-                    break;
-                case "Clli":
-                    org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
-                            .rev171017.ordered.constraints.sp.hop.type.hop.type.Clli
-                            clli = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
-                            .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Clli) hoptype;
-                    constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.CLLI,
-                            clli.getClli()));
-                    break;
-                default:
-                    LOG.error("in readIncludeNodes unsupported include type {}", hopt);
-            }
+    private void readCoRouting(CoRouting tmpcoRouting, PceConstraints constraints) {
+        if (tmpcoRouting == null) {
+            LOG.info("In readCoRoutingContrains: no CoRouting constraints.");
+        } else {
+            LOG.warn("CoRouting constraints handling not implemented yet");
         }
     }
 
-    private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
-
-        List<String> elementsToExclude = new ArrayList<>();
-        LOG.info("in readDiversity {}", srvList);
-
-        for (String srv : srvList) {
-            Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
-            if (service.isPresent()) {
-                LOG.info("in readDiversity service list {}", service);
-                switch (rt) {
-                    case NODE:
-                        elementsToExclude.addAll(getAToZNodeList(service.get()));
-                        LOG.info("readDiversity NODE : {}", elementsToExclude);
-                        if (elementsToExclude != null) {
-                            constraints.setExcludeNodes(elementsToExclude);
-                        }
-                        break;
-                    case SRLG:
-                        elementsToExclude.addAll(getSRLGList(service.get()));
-                        LOG.info("readDiversity SRLG : {}", elementsToExclude);
-                        if (elementsToExclude != null) {
-                            constraints.setExcludeSrlgLinks(elementsToExclude);
-                        }
-                        break;
-                    case CLLI:
-                        /// Retrieve nodes into dedicated CLLI list
-                        /// during node validation check their CLLI and build CLLI exclude list
-                        elementsToExclude.addAll(getAToZNodeList(service.get()));
-                        LOG.info("readDiversity CLLI : {}", elementsToExclude);
-                        if (elementsToExclude != null) {
-                            constraints.setExcludeClliNodes(elementsToExclude);
-                        }
-                        break;
-                    default:
-                        LOG.info("in readDiversity unsupported divercity type {}", rt);
+    private void readDiversity(Diversity diversity, PceConstraints constraints) {
+        //TODO: How to implement the DiversityType: serial or synchronous?
+        Map<ServiceIdentifierListKey, ServiceIdentifierList> serviceIdList = diversity.getServiceIdentifierList();
+        Collection<ServiceIdentifierList> services = serviceIdList.values();
+        for (ServiceIdentifierList serviceIdentifier : services) {
+            String serviceId = serviceIdentifier.getServiceIndentifier();
+            ServiceApplicability serviceApplicability = serviceIdentifier.getServiceApplicability();
+            Optional<PathDescription> serviceOpt = getPathDescriptionFromDatastore(serviceId);
+            if (serviceOpt.isPresent()) {
+                List<String> serviceNodes = getAToZNodeList(serviceOpt.get());
+                if (serviceApplicability.getNode() && !serviceNodes.isEmpty()) {
+                    constraints.setExcludeNodes(serviceNodes);
+                }
+                List<String> serviceLinks = getSRLGList(serviceOpt.get());
+                if (serviceApplicability.getLink() && !serviceLinks.isEmpty()) {
+                    constraints.setExcludeSrlgLinks(serviceLinks);
+                }
+                if (serviceApplicability.getSite() && !serviceNodes.isEmpty()) {
+                    constraints.setExcludeClliNodes(serviceNodes);
                 }
-
-            } else {
-                LOG.info("in readDiversity srv={} is not present", srv);
             }
         }
-
     }
 
     private List<String> getAToZNodeList(PathDescription pathDescription) {
@@ -335,15 +251,6 @@ public class PceConstraintsCalc {
         return result;
     }
 
-    private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
-        LOG.info("In readCoRoutingContrains start");
-
-        if (tmpcoRouting == null) {
-            LOG.info("In readCoRoutingContrains: no General constraints.");
-        }
-
-    }
-
     public PceConstraints getPceHardConstraints() {
         return pceHardConstraints;
     }