e8f0f9aaca300146f0baf8b2b4773e3284c8d218
[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.rev190624.PathComputationRequestInput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInputBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.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         if (!rc.getStatus()) {
97             LOG.error("In pathComputationWithConstraints, nwAnalizer: result = {}", rc.toString());
98             return;
99         }
100         LOG.info("PceGraph ...");
101         PceGraph graph = new PceGraph(nwAnalizer.getaendPceNode(),
102             nwAnalizer.getzendPceNode(), nwAnalizer.getAllPceNodes(),
103             hardConstraints, softConstraints, rc);
104         graph.calcPath();
105         rc = graph.getReturnStructure();
106         if (!rc.getStatus()) {
107             LOG.warn("In pathComputationWithConstraints : Graph return without Path ");
108             // TODO fix. This is quick workaround for algorithm problem
109             if ((rc.getLocalCause() == PceResult.LocalCause.TOO_HIGH_LATENCY)
110                 && (hardConstraints.getPceMetrics() == PceMetric.HopCount)
111                 && (hardConstraints.getMaxLatency() != -1)) {
112                 hardConstraints.setPceMetrics(PceMetric.PropagationDelay);
113                 graph = patchRerunGraph(graph);
114             }
115
116             if (rc.getLocalCause() == PceResult.LocalCause.HD_NODE_INCLUDE) {
117                 graph.setKpathsToBring(graph.getKpathsToBring() * 10);
118                 graph = patchRerunGraph(graph);
119             }
120
121             if (!rc.getStatus()) {
122                 LOG.error("In pathComputationWithConstraints, graph.calcPath: result = {}", rc.toString());
123                 return;
124             }
125         }
126         LOG.info("PcePathDescription ...");
127         PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
128         description.buildDescriptions();
129         rc = description.getReturnStructure();
130         if (!rc.getStatus()) {
131             LOG.error("In pathComputationWithConstraints, description: result = {}", rc.toString());
132             return;
133         }
134     }
135
136     public void pathComputation() throws Exception {
137
138         PceConstraintsCalc constraints = new PceConstraintsCalc(input, networkTransaction);
139         pceHardConstraints = constraints.getPceHardConstraints();
140         pceSoftConstraints = constraints.getPceSoftConstraints();
141         pathComputationWithConstraints(pceHardConstraints, pceSoftConstraints);
142         this.success = rc.getStatus();
143         this.message = rc.getMessage();
144         this.responseCode = rc.getResponseCode();
145
146         AToZDirection atoz = null;
147         ZToADirection ztoa = null;
148         if (rc.getStatus()) {
149             atoz = rc.getAtoZDirection();
150             ztoa = rc.getZtoADirection();
151         }
152
153         //Connect to Gnpy to check path feasibility and recompute another path in case of path non-feasibility
154         try {
155             ConnectToGnpyServer connectToGnpy = new ConnectToGnpyServer();
156             if (connectToGnpy.isGnpyURLExist()) {
157                 GnpyUtilitiesImpl gnpy = new GnpyUtilitiesImpl(networkTransaction, input);
158                 if (rc.getStatus() && gnpyToCheckFeasiblity(atoz,ztoa,gnpy)) {
159                     setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
160                     return;
161                 }
162                 callGnpyToComputeNewPath(gnpy);
163             } else {
164                 setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
165                 return;
166             }
167         }
168         catch (GnpyException e) {
169             LOG.error("Exception raised by GNPy {}",e.getMessage());
170             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
171             return;
172         }
173         return;
174     }
175
176     private boolean gnpyToCheckFeasiblity(AToZDirection atoz, ZToADirection ztoa, GnpyUtilitiesImpl gnpy)
177             throws GnpyException, Exception {
178
179         //Call GNPy for path verification
180         if (gnpy.verifyComputationByGnpy(atoz, ztoa, pceHardConstraints)) {
181             LOG.info("In pceSendingPceRPC: the path is feasible according to Gnpy");
182             gnpyAtoZ = gnpy.getGnpyAtoZ();
183             gnpyZtoA = gnpy.getGnpyZtoA();
184             return true;
185         }
186         return false;
187     }
188
189     private void callGnpyToComputeNewPath(GnpyUtilitiesImpl gnpy) throws GnpyException, Exception {
190
191         //Call GNPy in the case of non feasibility
192         LOG.info("In pceSendingPceRPC: the path is not feasible according to Gnpy");
193         HardConstraints gnpyPathAsHC = null;
194         gnpyPathAsHC = gnpy.askNewPathFromGnpy(gnpyPathAsHC, pceHardConstraints);
195         if (gnpyPathAsHC == null) {
196             LOG.info("In pceSendingPceRPC: GNPy failed to find another path");
197             this.success = false;
198             this.message = "No path available by PCE and GNPy ";
199             this.responseCode = ResponseCodes.RESPONSE_FAILED;
200             gnpyAtoZ = gnpy.getGnpyAtoZ();
201             gnpyZtoA = gnpy.getGnpyZtoA();
202             return;
203         }
204
205         LOG.info("In pceSendingPceRPC: GNPy succeed to find another path");
206         // Compute the path
207         PathComputationRequestInput inputFromGnpy = new PathComputationRequestInputBuilder()
208             .setServiceName(input.getServiceName()).setHardConstraints(gnpyPathAsHC)
209             .setSoftConstraints(input.getSoftConstraints()).setPceMetric(PceMetric.HopCount)
210             .setServiceAEnd(input.getServiceAEnd()).setServiceZEnd(input.getServiceZEnd()).build();
211         PceConstraintsCalc constraintsGnpy = new PceConstraintsCalc(inputFromGnpy, networkTransaction);
212         PceConstraints gnpyHardConstraints = constraintsGnpy.getPceHardConstraints();
213         PceConstraints gnpySoftConstraints = constraintsGnpy.getPceSoftConstraints();
214         pathComputationWithConstraints(gnpyHardConstraints, gnpySoftConstraints);
215         AToZDirection atoz = rc.getAtoZDirection();
216         ZToADirection ztoa = rc.getZtoADirection();
217         if (gnpyToCheckFeasiblity(atoz, ztoa,gnpy)) {
218             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is valid");
219             this.success = true;
220             this.message = "Path is calculated by GNPy";
221             this.responseCode = ResponseCodes.RESPONSE_OK;
222             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
223         } else {
224             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is not valid");
225             this.success = false;
226             this.message = "No path available";
227             this.responseCode = ResponseCodes.RESPONSE_FAILED;
228             setPathDescription(new PathDescriptionBuilder().setAToZDirection(null).setZToADirection(null));
229         }
230     }
231
232     private PceGraph patchRerunGraph(PceGraph graph) {
233         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
234         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
235         graph.calcPath();
236         return graph;
237     }
238
239     public PathDescriptionBuilder getPathDescription() {
240         return pathDescription;
241     }
242
243     private void setPathDescription(PathDescriptionBuilder pathDescription) {
244         this.pathDescription = pathDescription;
245     }
246
247     public Boolean getSuccess() {
248         return this.success;
249     }
250
251     public String getMessage() {
252         return this.message;
253     }
254
255     public String getResponseCode() {
256         return this.responseCode;
257     }
258
259     public GnpyResult getGnpyAtoZ() {
260         return gnpyAtoZ;
261     }
262
263     public GnpyResult getGnpyZtoA() {
264         return gnpyZtoA;
265     }
266 }