upgrade portmapping YANG to introduce OTN
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceSendingPceRPCs.java
old mode 100644 (file)
new mode 100755 (executable)
index 324b022..3ca6b94
@@ -8,13 +8,23 @@
 
 package org.opendaylight.transportpce.pce;
 
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.transportpce.pce.PceResult.LocalCause;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.service.path.rpc.result.PathDescriptionBuilder;
+import org.opendaylight.transportpce.common.ResponseCodes;
+import org.opendaylight.transportpce.common.network.NetworkTransactionService;
+import org.opendaylight.transportpce.pce.constraints.PceConstraints;
+import org.opendaylight.transportpce.pce.constraints.PceConstraintsCalc;
+import org.opendaylight.transportpce.pce.gnpy.ConnectToGnpyServer;
+import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
+import org.opendaylight.transportpce.pce.gnpy.GnpyUtilitiesImpl;
+import org.opendaylight.transportpce.pce.graph.PceGraph;
+import org.opendaylight.transportpce.pce.networkanalyzer.PceCalculation;
+import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInput;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInputBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.service.path.rpc.result.PathDescriptionBuilder;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
 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.routing.constraints.sp.HardConstraints;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -38,112 +48,161 @@ public class PceSendingPceRPCs {
      * <code>false</code> cancelresourcereserve .
      */
     private PathDescriptionBuilder pathDescription;
-
     private PathComputationRequestInput input;
-    private DataBroker dataBroker;
-
+    private NetworkTransactionService networkTransaction;
     private PceConstraints pceHardConstraints = new PceConstraints();
     private PceConstraints pceSoftConstraints = new PceConstraints();
+    private GnpyResult gnpyAtoZ;
+    private GnpyResult gnpyZtoA;
+    private Boolean success;
+    private String message;
+    private String responseCode;
 
     public PceSendingPceRPCs() {
         setPathDescription(null);
-
         this.input = null;
-        this.dataBroker = null;
+        this.networkTransaction = null;
     }
 
-    public PceSendingPceRPCs(PathComputationRequestInput input, DataBroker dataBroker) {
+    public PceSendingPceRPCs(PathComputationRequestInput input,
+        NetworkTransactionService networkTransaction) {
         setPathDescription(null);
 
         // TODO compliance check to check that input is not empty
         this.input = input;
-        this.dataBroker = dataBroker;
+        this.networkTransaction = networkTransaction;
     }
 
     public void cancelResourceReserve() {
+        success = false;
         LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
         try {
-            Thread.sleep(10000); // sleep for 10s
+            // sleep for 10s
+            Thread.sleep(10000);
         } catch (InterruptedException e) {
             LOG.error(e.toString());
         }
+        success = true;
         LOG.info("cancelResourceReserve ...");
     }
 
-    public void pathComputation() {
-        LOG.info("PathComputation ...");
-
-        PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
-        pceHardConstraints = constraints.getPceHardConstraints();
-        pceSoftConstraints = constraints.getPceSoftConstraints();
+    public void pathComputationWithConstraints(PceConstraints hardConstraints, PceConstraints softConstraints)
+            throws Exception {
 
-
-        LOG.info("nwAnalizer ...");
-        PceCalculation nwAnalizer = new PceCalculation(input, dataBroker,
-                pceHardConstraints, pceSoftConstraints, rc);
+        PceCalculation nwAnalizer =
+            new PceCalculation(input, networkTransaction, hardConstraints, softConstraints, rc);
         nwAnalizer.calcPath();
         rc = nwAnalizer.getReturnStructure();
         if (!rc.getStatus()) {
-            LOG.error("In pathComputation nwAnalizer: result = {}", rc.toString());
+            LOG.error("In pathComputationWithConstraints, nwAnalizer: result = {}", rc.toString());
             return;
         }
-
         LOG.info("PceGraph ...");
-        LOG.warn("PathComputation: aPceNode '{}' - zPceNode '{}'", nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode());
-        PceGraph graph = new PceGraph(
-                nwAnalizer.getaPceNode(),
-                nwAnalizer.getzPceNode(),
-                nwAnalizer.getAllPceNodes(),
-                pceHardConstraints, pceSoftConstraints, rc);
+        PceGraph graph = new PceGraph(nwAnalizer.getaendPceNode(),
+            nwAnalizer.getzendPceNode(), nwAnalizer.getAllPceNodes(),
+            hardConstraints, softConstraints, rc);
         graph.calcPath();
         rc = graph.getReturnStructure();
         if (!rc.getStatus()) {
-
             LOG.warn("In pathComputation : Graph return without Path ");
-
             // TODO fix. This is quick workaround for algorithm problem
-            if ((rc.getLocalCause() == LocalCause.TOO_HIGH_LATENCY)
-                && (pceHardConstraints.getPceMetrics() == PceMetric.HopCount)
-                && (pceHardConstraints.getMaxLatency() != -1)) {
-
-                pceHardConstraints.setPceMetrics(PceMetric.PropagationDelay);
+            if ((rc.getLocalCause() == PceResult.LocalCause.TOO_HIGH_LATENCY)
+                && (hardConstraints.getPceMetrics() == PceMetric.HopCount)
+                && (hardConstraints.getMaxLatency() != -1)) {
+                hardConstraints.setPceMetrics(PceMetric.PropagationDelay);
                 graph = patchRerunGraph(graph);
             }
-
             if (!rc.getStatus()) {
-                LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
+                LOG.error("In pathComputationWithConstraints, graph.calcPath: result = {}", rc.toString());
                 return;
             }
         }
-
         LOG.info("PcePathDescription ...");
-        PcePathDescription description = new PcePathDescription(
-                graph.getPathAtoZ(),
-                nwAnalizer.getAllPceLinks(), rc);
+        PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
         description.buildDescriptions();
         rc = description.getReturnStructure();
         if (!rc.getStatus()) {
-            LOG.error("In pathComputation description: result = {}", rc.toString());
+            LOG.error("In pathComputationWithConstraints, description: result = {}", rc.toString());
             return;
         }
+    }
 
-        LOG.info("setPathDescription ...");
-        AToZDirection atoz = rc.getAtoZDirection();
-        ZToADirection ztoa = rc.getZtoADirection();
-        if ((atoz == null) || (atoz.getAToZ() == null)) {
-            rc.setRC("400");
-            LOG.error("In pathComputation empty atoz path after description: result = {}", rc.toString());
+    public void pathComputation() throws Exception {
+
+        PceConstraintsCalc constraints = new PceConstraintsCalc(input, networkTransaction);
+        pceHardConstraints = constraints.getPceHardConstraints();
+        pceSoftConstraints = constraints.getPceSoftConstraints();
+        pathComputationWithConstraints(pceHardConstraints, pceSoftConstraints);
+        this.success = rc.getStatus();
+        this.message = rc.getMessage();
+        this.responseCode = rc.getResponseCode();
+
+        if (!rc.getStatus()) {
+            LOG.error("In pathComputation, pathComputationWithConstraints: result = {}", rc.toString());
             return;
         }
-        if ((ztoa == null) || (ztoa.getZToA() == null)) {
-            rc.setRC("400");
-            LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
-            return;
+
+        // Verify the path with GNPy
+        AToZDirection atoz = rc.getAtoZDirection();
+        ZToADirection ztoa = rc.getZtoADirection();
+        ConnectToGnpyServer connectToGnpy = new ConnectToGnpyServer();
+
+        // Verify that the GNPy server exists
+        if (connectToGnpy.isGnpyURLExist()) {
+            LOG.info("Gnpy instance is connected T-PCE");
+            GnpyUtilitiesImpl gnpy = new GnpyUtilitiesImpl(networkTransaction, input);
+            if (gnpy.verifyComputationByGnpy(atoz, ztoa, pceHardConstraints)) {
+                LOG.info("The path is feasible according to Gnpy");
+                setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
+            }
+            else {
+                LOG.info("The path is not feasible according to Gnpy");
+                HardConstraints gnpyPathAsHC = null;
+                gnpyPathAsHC = gnpy.askNewPathFromGnpy(atoz, ztoa, gnpyPathAsHC, pceHardConstraints);
+                if (gnpyPathAsHC != null) {
+                    LOG.info("GNPy succeed to find another path");
+                    // Compute the path
+                    PathComputationRequestInput inputFromGnpy = new PathComputationRequestInputBuilder()
+                        .setServiceName(input.getServiceName())
+                        .setHardConstraints(gnpyPathAsHC)
+                        .setSoftConstraints(input.getSoftConstraints())
+                        .setPceMetric(PceMetric.HopCount)
+                        .setServiceAEnd(input.getServiceAEnd())
+                        .setServiceZEnd(input.getServiceZEnd())
+                        .build();
+                    PceConstraintsCalc constraintsGnpy = new PceConstraintsCalc(inputFromGnpy, networkTransaction);
+                    PceConstraints gnpyHardConstraints = constraintsGnpy.getPceHardConstraints();
+                    PceConstraints gnpySoftConstraints = constraintsGnpy.getPceSoftConstraints();
+                    pathComputationWithConstraints(gnpyHardConstraints, gnpySoftConstraints);
+                    atoz = rc.getAtoZDirection();
+                    ztoa = rc.getZtoADirection();
+                    setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
+                    if (gnpy.verifyComputationByGnpy(atoz, ztoa, pceHardConstraints)) {
+                        LOG.info("In pathComputation: the new path computed by GNPy is valid");
+                        setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
+                        this.success = true;
+                        this.message = "Path is calculated by GNPy ";
+                        this.responseCode = ResponseCodes.RESPONSE_OK;
+                    } else {
+                        LOG.info("In pathComputation: the new path computed by GNPy is not valid");
+                        this.success = false;
+                        this.message = "No path available ";
+                        this.responseCode = ResponseCodes.RESPONSE_FAILED;
+                    }
+                } else {
+                    LOG.info("GNPy failed to find another path");
+                    this.success = false;
+                    this.message = "No path available by PCE and GNPy ";
+                    this.responseCode = ResponseCodes.RESPONSE_FAILED;
+                }
+            }
+            gnpyAtoZ = gnpy.getGnpyAtoZ();
+            gnpyZtoA = gnpy.getGnpyZtoA();
+        } else {
+            LOG.info("in PCESendingPceRPCs: Cannot connect to GNPy!!");
+            setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
+            LOG.info("In pathComputation Graph is Found");
         }
-        setPathDescription(new PathDescriptionBuilder()
-                .setAToZDirection(atoz)
-                .setZToADirection(ztoa));
-        LOG.info("In pathComputation Graph is Found");
     }
 
     private PceGraph patchRerunGraph(PceGraph graph) {
@@ -151,7 +210,6 @@ public class PceSendingPceRPCs {
         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
         graph.calcPath();
         return graph;
-
     }
 
     public PathDescriptionBuilder getPathDescription() {
@@ -163,15 +221,22 @@ public class PceSendingPceRPCs {
     }
 
     public Boolean getSuccess() {
-        return rc.getStatus();
+        return this.success;
     }
 
     public String getMessage() {
-        return rc.getMessage();
+        return this.message;
     }
 
     public String getResponseCode() {
-        return rc.getResponseCode();
+        return this.responseCode;
     }
 
+    public GnpyResult getGnpyAtoZ() {
+        return gnpyAtoZ;
+    }
+
+    public GnpyResult getGnpyZtoA() {
+        return gnpyZtoA;
+    }
 }