Upgrade to Service Path 1.7
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / constraints / PceConstraintsCalc.java
index ae6b8d5611bd59edf9ebe0bd46741260dd915750..94b27b01e75e40b2c41ed19897d88f7fd04f6668 100644 (file)
@@ -1,26 +1,26 @@
 /*
- * 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.List;
+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.transportpce.common.network.NetworkTransactionService;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.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.Link;
 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;
@@ -28,36 +28,40 @@ import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
 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.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.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.getPceMetric();
 
-        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.toString());
+        pceHardConstraints.setPceMetrics(pceMetrics);
 
         calcHardconstraints(input);
         calcSoftconstraints(input);
@@ -71,7 +75,7 @@ public class PceConstraintsCalc {
         }
 
         CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
+        readconstraints(coRoutingOrGeneral, pceHardConstraints);
 
     }
 
@@ -83,7 +87,7 @@ public class PceConstraintsCalc {
         }
 
         CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
-        readconstraints(coRoutingOrGeneral, this.pceSoftConstraints);
+        readconstraints(coRoutingOrGeneral, pceSoftConstraints);
 
     }
 
@@ -119,7 +123,7 @@ public class PceConstraintsCalc {
     private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
         LOG.debug("In readGeneralContrains start");
 
-        if (tmpGeneral  ==  null) {
+        if (tmpGeneral == null) {
             LOG.info("In readGeneralContrains: no General constraints.");
             return;
         }
@@ -135,69 +139,189 @@ public class PceConstraintsCalc {
         if (exclude != null) {
             elementsToExclude = exclude.getNodeId();
             if (elementsToExclude != null) {
-                constraints.setExcludeNodes(elementsToExclude);
+                constraints.setExcludeSupNodes(elementsToExclude);
             }
+
             elementsToExclude = exclude.getSRLG();
             if (elementsToExclude != null) {
-                constraints.setExcludeSRLG(elementsToExclude);
+                List<Long> srlgToExclude = new ArrayList<Long>();
+                for (String str : elementsToExclude) {
+                    srlgToExclude.add(Long.parseLong(str));
+                }
+                constraints.setExcludeSRLG(srlgToExclude);
+            }
+
+            elementsToExclude = exclude.getClli();
+            if (elementsToExclude != null) {
+                constraints.setExcludeCLLI(elementsToExclude);
+            }
+        }
+
+        Include include = tmpGeneral.getInclude();
+        if (include != null) {
+            List<OrderedHops> listHops = include.getOrderedHops();
+            if (listHops != null) {
+                readIncludeNodes(listHops, constraints);
             }
+            LOG.debug("in readGeneralContrains INCLUDE {} ", include.toString());
         }
 
         Diversity diversity = tmpGeneral.getDiversity();
-        if ((diversity != null) && (diversity.getExistingServiceApplicability().isNode())) {
-            LOG.info("in readGeneralContrains {}", diversity.toString());
-            readDiversityNodes(diversity.getExistingService(), constraints);
+        PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
+        if (diversity != null) {
+            ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
+            if (temp == null) {
+                return;
+            }
+            if (temp.isNode()) {
+                rt = PceConstraints.ResourceType.NODE;
+            }
+            if (temp.isSrlg()) {
+                rt = PceConstraints.ResourceType.SRLG;
+            }
+            if (temp.isClli()) {
+                rt = PceConstraints.ResourceType.CLLI;
+            }
+            LOG.info("in readGeneralContrains {} list is :{}", rt, diversity.toString());
+            readDiversity(diversity.getExistingService(), constraints, rt);
         }
 
     }
 
-    private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
+    private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
+        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(constraints.new 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(constraints.new 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(constraints.new ResourcePair(PceConstraints.ResourceType.CLLI,
+                            clli.getClli()));
+                    break;
+                default:
+                    LOG.error("in readIncludeNodes unsupported include type {}", hopt);
+            }
+        }
+    }
+
+    private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
 
         List<String> elementsToExclude = new ArrayList<String>();
-        LOG.info("in readDiversityNodes {}", srvList.toString());
+        LOG.info("in readDiversity {}", srvList.toString());
 
         for (String srv : srvList) {
             Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
             if (service.isPresent()) {
-                elementsToExclude.addAll(getAToZNodeList(service.get()));
-                LOG.info("readDiversityNodes : {}", elementsToExclude);
+                LOG.info("in readDiversity service list {}", service.toString());
+                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);
+                }
 
             } else {
-                LOG.info("in readDiversityNodes srv={} is not present", srv);
+                LOG.info("in readDiversity srv={} is not present", srv);
             }
         }
 
-        if (elementsToExclude != null) {
-            constraints.setExcludeNodes(elementsToExclude);
-        }
     }
 
     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 = pathDescription.getAToZDirection().getAToZ();
+        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 true;
+        }).map(aToZ -> {
+            Node node = ((Node) aToZ.getResource().getResource());
             return node.getNodeId().toString();
         }).collect(Collectors.toList());
     }
 
+    private List<String> getSRLGList(PathDescription pathDescription) {
+        List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
+        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();
@@ -217,7 +341,7 @@ public class PceConstraintsCalc {
     private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
         LOG.info("In readCoRoutingContrains start");
 
-        if (tmpcoRouting  ==  null) {
+        if (tmpcoRouting == null) {
             LOG.info("In readCoRoutingContrains: no General constraints.");
             return;
         }
@@ -225,16 +349,15 @@ public class PceConstraintsCalc {
     }
 
     public PceConstraints getPceHardConstraints() {
-        return this.pceHardConstraints;
+        return pceHardConstraints;
     }
 
     public PceConstraints getPceSoftConstraints() {
-        return this.pceSoftConstraints;
+        return pceSoftConstraints;
     }
 
     public PceMetric getPceMetrics() {
-        return this.pceMetrics;
+        return pceMetrics;
     }
 
-
 }