Adapt TransportPCE code to Sulfur
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / constraints / PceConstraintsCalc.java
index ae6b8d5611bd59edf9ebe0bd46741260dd915750..091da934063ed8055628044e82eadee88e4f291d 100644 (file)
@@ -1,63 +1,70 @@
 /*
- * Copyright © 2017 AT&T, Inc. and others.  All rights reserved.
+ * Copyright © 2016 AT&T and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.transportpce.pce;
+package org.opendaylight.transportpce.pce.constraints;
 
-import com.google.common.base.Optional;
 import java.util.ArrayList;
+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;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.Timeouts;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInput;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.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.Latency;
-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.common.network.NetworkTransactionService;
+import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220615.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(PceCalculation.class);
+    private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
 
     private PceConstraints pceHardConstraints = new PceConstraints();
     private PceConstraints pceSoftConstraints = new PceConstraints();
     private PceMetric pceMetrics = PceMetric.HopCount;
-    private DataBroker dataBroker;
-
+    private NetworkTransactionService networkTransactionService;
 
-    public PceConstraintsCalc(PathComputationRequestInput input, DataBroker dataBroker) {
+    public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
         LOG.debug("In PceconstraintsCalc start");
 
-        this.pceMetrics = input.getPceMetric();
+        pceMetrics = input.getPceRoutingMetric();
 
-        this.dataBroker = dataBroker;
+        this.networkTransactionService = networkTransactionService;
 
         // TODO. for now metrics are set into hard structure
-        LOG.info("In PceConstraintsCalc: read PceMetric {}", this.pceMetrics.toString());
-        this.pceHardConstraints.setPceMetrics(this.pceMetrics);
+        LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
+        pceHardConstraints.setPceMetrics(pceMetrics);
 
         calcHardconstraints(input);
         calcSoftconstraints(input);
@@ -69,10 +76,7 @@ public class PceConstraintsCalc {
             LOG.info("In calcHardconstraints: no hard constraints.");
             return;
         }
-
-        CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
-
+        readConstraints(servicePathHardConstraints, pceHardConstraints);
     }
 
     private void calcSoftconstraints(PathComputationRequestInput input) {
@@ -81,123 +85,156 @@ public class PceConstraintsCalc {
             LOG.info("In calcSoftconstraints: no soft constraints.");
             return;
         }
-
-        CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, this.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.toString());
-            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.toString());
-            tmpCoRouting = (CoRouting) coRoutingOrGeneral;
-            readCoRoutingContrains(tmpCoRouting, constraints);
-            return;
+        if (hardConstraints.getCoRouting() != null) {
+            readCoRouting(hardConstraints.getCoRouting(), constraints);
+        }
+        if (hardConstraints.getDiversity() != null) {
+            readDiversity(hardConstraints.getDiversity(), constraints);
+        }
+        if (hardConstraints.getLatency() != null) {
+            constraints.setMaxLatency(hardConstraints.getLatency().getMaxLatency().longValue());
         }
-
-        return;
-
     }
 
-    private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
-        LOG.debug("In readGeneralContrains start");
-
-        if (tmpGeneral  ==  null) {
-            LOG.info("In readGeneralContrains: no General constraints.");
-            return;
+    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);
         }
-
-        Latency latency = tmpGeneral.getLatency();
-        if (latency != null) {
-            constraints.setMaxLatency(latency.getMaxLatency());
-            LOG.info("In readGeneralContrains: read latency {}", latency.toString());
+        if (exclude.getSrlgId() != null) {
+            List<Long> elementsToExclude = new ArrayList<>();
+            for (Uint32 srlg : exclude.getSrlgId()) {
+                elementsToExclude.add(srlg.longValue());
+            }
+            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");
+        }
+    }
 
-        Exclude exclude = tmpGeneral.getExclude();
-        List<String> elementsToExclude = null;
-        if (exclude != null) {
-            elementsToExclude = exclude.getNodeId();
-            if (elementsToExclude != null) {
-                constraints.setExcludeNodes(elementsToExclude);
+    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()));
             }
-            elementsToExclude = exclude.getSRLG();
-            if (elementsToExclude != null) {
-                constraints.setExcludeSRLG(elementsToExclude);
+        }
+        if (include.getSrlgId() != null) {
+            for (Uint32 srlg : include.getSrlgId()) {
+                constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.SRLG, srlg.toString()));
             }
         }
-
-        Diversity diversity = tmpGeneral.getDiversity();
-        if ((diversity != null) && (diversity.getExistingServiceApplicability().isNode())) {
-            LOG.info("in readGeneralContrains {}", diversity.toString());
-            readDiversityNodes(diversity.getExistingService(), constraints);
+        if (include.getSite() != null) {
+            for (String site : include.getSite()) {
+                constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.CLLI, site));
+            }
         }
-
     }
 
-    private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
-
-        List<String> elementsToExclude = new ArrayList<String>();
-        LOG.info("in readDiversityNodes {}", srvList.toString());
-
-        for (String srv : srvList) {
-            Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
-            if (service.isPresent()) {
-                elementsToExclude.addAll(getAToZNodeList(service.get()));
-                LOG.info("readDiversityNodes : {}", elementsToExclude);
-
-            } else {
-                LOG.info("in readDiversityNodes srv={} is not present", srv);
-            }
+    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");
         }
+    }
 
-        if (elementsToExclude != null) {
-            constraints.setExcludeNodes(elementsToExclude);
+    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);
+                }
+            }
         }
     }
 
     private List<String> getAToZNodeList(PathDescription pathDescription) {
-        List<AToZ> atozList = pathDescription.getAToZDirection().getAToZ();
-        return atozList.stream().filter(aToZ -> {
-            if ((aToZ.getResource() == null) || (aToZ.getResource().getResource() == null)) {
+        List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
+        return aendToZList.stream().filter(aToZ -> {
+            if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
                 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
                 return false;
             }
             return aToZ.getResource().getResource() instanceof Node;
-        }).map(aToZ -> {
+        }).filter(aToZ -> {
             Node node = (Node) aToZ.getResource().getResource();
             if (node.getNodeId() == null) {
                 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
-                return null;
+                return false;
             }
-            return node.getNodeId().toString();
+            return true;
+        }).map(aToZ -> {
+            Node node = ((Node) aToZ.getResource().getResource());
+            return node.getNodeId();
+        }).collect(Collectors.toList());
+    }
+
+    private List<String> getSRLGList(PathDescription pathDescription) {
+        List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
+        return aendToZList.stream().filter(aToZ -> {
+            if (aToZ.getResource() == null
+                    || aToZ.getResource().getResource() == null) {
+                LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
+                return false;
+            }
+            return aToZ.getResource().getResource() instanceof Link;
+        }).filter(aToZ -> {
+            Link link = (Link) aToZ.getResource().getResource();
+            if (link.getLinkId() == null) {
+                LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
+                return false;
+            }
+            return true;
+        }).map(aToZ -> {
+            return ((Link) aToZ.getResource().getResource()).getLinkId();
         }).collect(Collectors.toList());
     }
 
     private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
-        Optional<PathDescription> result = Optional.absent();
+        Optional<PathDescription> result = Optional.empty();
         InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
-        ReadOnlyTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
         try {
             LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
-            ServicePaths servicePaths = pathDescReadTx.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
+            ServicePaths servicePaths =
+                networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
                     .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
             if (servicePaths != null) {
                 PathDescription path = servicePaths.getPathDescription();
@@ -214,27 +251,16 @@ 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.");
-            return;
-        }
-
-    }
-
     public PceConstraints getPceHardConstraints() {
-        return this.pceHardConstraints;
+        return pceHardConstraints;
     }
 
     public PceConstraints getPceSoftConstraints() {
-        return this.pceSoftConstraints;
+        return pceSoftConstraints;
     }
 
     public PceMetric getPceMetrics() {
-        return this.pceMetrics;
+        return pceMetrics;
     }
 
-
 }