Upgrade to Service Path 1.7
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceSendingPceRPCs.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.pce;
10
11 import org.opendaylight.transportpce.common.ResponseCodes;
12 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
13 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
14 import org.opendaylight.transportpce.pce.constraints.PceConstraintsCalc;
15 import org.opendaylight.transportpce.pce.gnpy.ConnectToGnpyServer;
16 import org.opendaylight.transportpce.pce.gnpy.GnpyException;
17 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
18 import org.opendaylight.transportpce.pce.gnpy.GnpyUtilitiesImpl;
19 import org.opendaylight.transportpce.pce.graph.PceGraph;
20 import org.opendaylight.transportpce.pce.networkanalyzer.PceCalculation;
21 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInputBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /*
33  * Class for Sending
34  * PCE requests :
35  * - path-computation-request
36  * - cancel-resource-reserve.
37  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
38  *
39  */
40 public class PceSendingPceRPCs {
41
42     /* Logging. */
43     private static final Logger LOG = LoggerFactory.getLogger(PceSendingPceRPCs.class);
44     /* define procedure success (or not ). */
45     private PceResult rc = new PceResult();
46
47     /*
48      * define type of request<br> <code>true</code> pathcomputation <br>
49      * <code>false</code> cancelresourcereserve .
50      */
51     private PathDescriptionBuilder pathDescription;
52     private PathComputationRequestInput input;
53     private NetworkTransactionService networkTransaction;
54     private PceConstraints pceHardConstraints = new PceConstraints();
55     private PceConstraints pceSoftConstraints = new PceConstraints();
56     private GnpyResult gnpyAtoZ;
57     private GnpyResult gnpyZtoA;
58     private Boolean success;
59     private String message;
60     private String responseCode;
61
62     public PceSendingPceRPCs() {
63         setPathDescription(null);
64         this.input = null;
65         this.networkTransaction = null;
66     }
67
68     public PceSendingPceRPCs(PathComputationRequestInput input,
69         NetworkTransactionService networkTransaction) {
70         setPathDescription(null);
71
72         // TODO compliance check to check that input is not empty
73         this.input = input;
74         this.networkTransaction = networkTransaction;
75     }
76
77     public void cancelResourceReserve() {
78         success = false;
79         LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
80         try {
81             // sleep for 10s
82             Thread.sleep(10000);
83         } catch (InterruptedException e) {
84             LOG.error(e.toString());
85         }
86         success = true;
87         LOG.info("cancelResourceReserve ...");
88     }
89
90     public void pathComputationWithConstraints(PceConstraints hardConstraints, PceConstraints softConstraints) {
91
92         PceCalculation nwAnalizer =
93             new PceCalculation(input, networkTransaction, hardConstraints, softConstraints, rc);
94         nwAnalizer.retrievePceNetwork();
95         rc = nwAnalizer.getReturnStructure();
96         String serviceType = nwAnalizer.getServiceType();
97         if (!rc.getStatus()) {
98             LOG.error("In pathComputationWithConstraints, nwAnalizer: result = {}", rc.toString());
99             return;
100         }
101         LOG.info("PceGraph ...");
102         PceGraph graph = new PceGraph(nwAnalizer.getaendPceNode(),
103                 nwAnalizer.getzendPceNode(), nwAnalizer.getAllPceNodes(),
104                 pceHardConstraints, pceSoftConstraints, rc, serviceType);
105         graph.calcPath();
106         rc = graph.getReturnStructure();
107         if (!rc.getStatus()) {
108             LOG.warn("In pathComputationWithConstraints : Graph return without Path ");
109             // TODO fix. This is quick workaround for algorithm problem
110             if ((rc.getLocalCause() == PceResult.LocalCause.TOO_HIGH_LATENCY)
111                 && (hardConstraints.getPceMetrics() == PceMetric.HopCount)
112                 && (hardConstraints.getMaxLatency() != -1)) {
113                 hardConstraints.setPceMetrics(PceMetric.PropagationDelay);
114                 graph = patchRerunGraph(graph);
115             }
116
117             if (rc.getLocalCause() == PceResult.LocalCause.HD_NODE_INCLUDE) {
118                 graph.setKpathsToBring(graph.getKpathsToBring() * 10);
119                 graph = patchRerunGraph(graph);
120             }
121
122             if (!rc.getStatus()) {
123                 LOG.error("In pathComputationWithConstraints, graph.calcPath: result = {}", rc.toString());
124                 return;
125             }
126         }
127         LOG.info("PcePathDescription ...");
128         PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
129         description.buildDescriptions();
130         rc = description.getReturnStructure();
131         if (!rc.getStatus()) {
132             LOG.error("In pathComputationWithConstraints, description: result = {}", rc.toString());
133             return;
134         }
135     }
136
137     public void pathComputation() throws Exception {
138
139         PceConstraintsCalc constraints = new PceConstraintsCalc(input, networkTransaction);
140         pceHardConstraints = constraints.getPceHardConstraints();
141         pceSoftConstraints = constraints.getPceSoftConstraints();
142         pathComputationWithConstraints(pceHardConstraints, pceSoftConstraints);
143         this.success = rc.getStatus();
144         this.message = rc.getMessage();
145         this.responseCode = rc.getResponseCode();
146
147         AToZDirection atoz = null;
148         ZToADirection ztoa = null;
149         if (rc.getStatus()) {
150             atoz = rc.getAtoZDirection();
151             ztoa = rc.getZtoADirection();
152         }
153
154         //Connect to Gnpy to check path feasibility and recompute another path in case of path non-feasibility
155         try {
156             ConnectToGnpyServer connectToGnpy = new ConnectToGnpyServer();
157             if (connectToGnpy.isGnpyURLExist()) {
158                 GnpyUtilitiesImpl gnpy = new GnpyUtilitiesImpl(networkTransaction, input);
159                 if (rc.getStatus() && gnpyToCheckFeasiblity(atoz,ztoa,gnpy)) {
160                     setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
161                     return;
162                 }
163                 callGnpyToComputeNewPath(gnpy);
164             } else {
165                 setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
166                 return;
167             }
168         }
169         catch (GnpyException e) {
170             LOG.error("Exception raised by GNPy {}",e.getMessage());
171             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
172             return;
173         }
174         return;
175     }
176
177     private boolean gnpyToCheckFeasiblity(AToZDirection atoz, ZToADirection ztoa, GnpyUtilitiesImpl gnpy)
178             throws GnpyException, Exception {
179
180         //Call GNPy for path verification
181         if (gnpy.verifyComputationByGnpy(atoz, ztoa, pceHardConstraints)) {
182             LOG.info("In pceSendingPceRPC: the path is feasible according to Gnpy");
183             gnpyAtoZ = gnpy.getGnpyAtoZ();
184             gnpyZtoA = gnpy.getGnpyZtoA();
185             return true;
186         }
187         return false;
188     }
189
190     private void callGnpyToComputeNewPath(GnpyUtilitiesImpl gnpy) throws GnpyException, Exception {
191
192         //Call GNPy in the case of non feasibility
193         LOG.info("In pceSendingPceRPC: the path is not feasible according to Gnpy");
194         HardConstraints gnpyPathAsHC = null;
195         gnpyPathAsHC = gnpy.askNewPathFromGnpy(gnpyPathAsHC, pceHardConstraints);
196         if (gnpyPathAsHC == null) {
197             LOG.info("In pceSendingPceRPC: GNPy failed to find another path");
198             this.success = false;
199             this.message = "No path available by PCE and GNPy ";
200             this.responseCode = ResponseCodes.RESPONSE_FAILED;
201             gnpyAtoZ = gnpy.getGnpyAtoZ();
202             gnpyZtoA = gnpy.getGnpyZtoA();
203             return;
204         }
205
206         LOG.info("In pceSendingPceRPC: GNPy succeed to find another path");
207         // Compute the path
208         PathComputationRequestInput inputFromGnpy = new PathComputationRequestInputBuilder()
209             .setServiceName(input.getServiceName()).setHardConstraints(gnpyPathAsHC)
210             .setSoftConstraints(input.getSoftConstraints()).setPceMetric(PceMetric.HopCount)
211             .setServiceAEnd(input.getServiceAEnd()).setServiceZEnd(input.getServiceZEnd()).build();
212         PceConstraintsCalc constraintsGnpy = new PceConstraintsCalc(inputFromGnpy, networkTransaction);
213         PceConstraints gnpyHardConstraints = constraintsGnpy.getPceHardConstraints();
214         PceConstraints gnpySoftConstraints = constraintsGnpy.getPceSoftConstraints();
215         pathComputationWithConstraints(gnpyHardConstraints, gnpySoftConstraints);
216         AToZDirection atoz = rc.getAtoZDirection();
217         ZToADirection ztoa = rc.getZtoADirection();
218         if (gnpyToCheckFeasiblity(atoz, ztoa,gnpy)) {
219             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is valid");
220             this.success = true;
221             this.message = "Path is calculated by GNPy";
222             this.responseCode = ResponseCodes.RESPONSE_OK;
223             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
224         } else {
225             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is not valid");
226             this.success = false;
227             this.message = "No path available";
228             this.responseCode = ResponseCodes.RESPONSE_FAILED;
229             setPathDescription(new PathDescriptionBuilder().setAToZDirection(null).setZToADirection(null));
230         }
231     }
232
233     private PceGraph patchRerunGraph(PceGraph graph) {
234         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
235         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
236         graph.calcPath();
237         return graph;
238     }
239
240     public PathDescriptionBuilder getPathDescription() {
241         return pathDescription;
242     }
243
244     private void setPathDescription(PathDescriptionBuilder pathDescription) {
245         this.pathDescription = pathDescription;
246     }
247
248     public Boolean getSuccess() {
249         return this.success;
250     }
251
252     public String getMessage() {
253         return this.message;
254     }
255
256     public String getResponseCode() {
257         return this.responseCode;
258     }
259
260     public GnpyResult getGnpyAtoZ() {
261         return gnpyAtoZ;
262     }
263
264     public GnpyResult getGnpyZtoA() {
265         return gnpyZtoA;
266     }
267 }