GNPy client refactor
[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.GnpyException;
16 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
17 import org.opendaylight.transportpce.pce.gnpy.GnpyUtilitiesImpl;
18 import org.opendaylight.transportpce.pce.gnpy.consumer.GnpyConsumer;
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.rev201210.path.description.AToZDirection;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.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     private final GnpyConsumer gnpyConsumer;
62
63     public PceSendingPceRPCs(GnpyConsumer gnpyConsumer) {
64         setPathDescription(null);
65         this.input = null;
66         this.networkTransaction = null;
67         this.gnpyConsumer = gnpyConsumer;
68     }
69
70     public PceSendingPceRPCs(PathComputationRequestInput input,
71         NetworkTransactionService networkTransaction, GnpyConsumer gnpyConsumer) {
72         this.gnpyConsumer = gnpyConsumer;
73         setPathDescription(null);
74
75         // TODO compliance check to check that input is not empty
76         this.input = input;
77         this.networkTransaction = networkTransaction;
78     }
79
80     public void cancelResourceReserve() {
81         success = false;
82         LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
83         try {
84             // sleep for 10s
85             Thread.sleep(10000);
86         } catch (InterruptedException e) {
87             LOG.error("in PCESendingPceRPC: ",e);
88         }
89         success = true;
90         LOG.info("cancelResourceReserve ...");
91     }
92
93     public void pathComputationWithConstraints(PceConstraints hardConstraints, PceConstraints softConstraints) {
94
95         PceCalculation nwAnalizer =
96             new PceCalculation(input, networkTransaction, hardConstraints, softConstraints, rc);
97         nwAnalizer.retrievePceNetwork();
98         rc = nwAnalizer.getReturnStructure();
99         String serviceType = nwAnalizer.getServiceType();
100         if (!rc.getStatus()) {
101             LOG.error("In pathComputationWithConstraints, nwAnalizer: result = {}", rc);
102             return;
103         }
104         LOG.info("PceGraph ...");
105         PceGraph graph = new PceGraph(nwAnalizer.getaendPceNode(),
106                 nwAnalizer.getzendPceNode(), nwAnalizer.getAllPceNodes(),
107                 hardConstraints, softConstraints, rc, serviceType);
108         graph.calcPath();
109         rc = graph.getReturnStructure();
110         if (!rc.getStatus()) {
111             LOG.warn("In pathComputationWithConstraints : Graph return without Path ");
112             // TODO fix. This is quick workaround for algorithm problem
113             if ((rc.getLocalCause() == PceResult.LocalCause.TOO_HIGH_LATENCY)
114                 && (hardConstraints.getPceMetrics() == PceMetric.HopCount)
115                 && (hardConstraints.getMaxLatency() != -1)) {
116                 hardConstraints.setPceMetrics(PceMetric.PropagationDelay);
117                 graph = patchRerunGraph(graph);
118             }
119
120             if (rc.getLocalCause() == PceResult.LocalCause.HD_NODE_INCLUDE) {
121                 graph.setKpathsToBring(graph.getKpathsToBring() * 10);
122                 graph = patchRerunGraph(graph);
123             }
124
125             if (!rc.getStatus()) {
126                 LOG.error("In pathComputationWithConstraints, graph.calcPath: result = {}", rc);
127                 return;
128             }
129         }
130         LOG.info("PcePathDescription ...");
131         PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
132         description.buildDescriptions();
133         rc = description.getReturnStructure();
134         if (!rc.getStatus()) {
135             LOG.error("In pathComputationWithConstraints, description: result = {}", rc);
136         }
137     }
138
139     public void pathComputation() throws Exception {
140
141         PceConstraintsCalc constraints = new PceConstraintsCalc(input, networkTransaction);
142         pceHardConstraints = constraints.getPceHardConstraints();
143         pceSoftConstraints = constraints.getPceSoftConstraints();
144         pathComputationWithConstraints(pceHardConstraints, pceSoftConstraints);
145         this.success = rc.getStatus();
146         this.message = rc.getMessage();
147         this.responseCode = rc.getResponseCode();
148
149         AToZDirection atoz = null;
150         ZToADirection ztoa = null;
151         if (rc.getStatus()) {
152             atoz = rc.getAtoZDirection();
153             ztoa = rc.getZtoADirection();
154         }
155
156         //Connect to Gnpy to check path feasibility and recompute another path in case of path non-feasibility
157         try {
158             if (gnpyConsumer.isAvailable()) {
159                 GnpyUtilitiesImpl gnpy = new GnpyUtilitiesImpl(networkTransaction, input,
160                         gnpyConsumer);
161                 if (rc.getStatus() && gnpyToCheckFeasiblity(atoz,ztoa,gnpy)) {
162                     setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
163                     return;
164                 }
165                 callGnpyToComputeNewPath(gnpy);
166             } else {
167                 setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
168             }
169         }
170         catch (GnpyException e) {
171             LOG.error("Exception raised by GNPy {}",e.getMessage());
172             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
173         }
174     }
175
176     private boolean gnpyToCheckFeasiblity(AToZDirection atoz, ZToADirection ztoa, GnpyUtilitiesImpl gnpy)
177             throws GnpyException {
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 {
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(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 }