Upgrade path-description model to 1.6.1 for otn
[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.rev200629.path.description.AToZDirection;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.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("in PCESendingPceRPC: ",e);
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);
99             return;
100         }
101         LOG.info("PceGraph ...");
102         PceGraph graph = new PceGraph(nwAnalizer.getaendPceNode(),
103                 nwAnalizer.getzendPceNode(), nwAnalizer.getAllPceNodes(),
104                 hardConstraints, softConstraints, 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);
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);
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             }
166         }
167         catch (GnpyException e) {
168             LOG.error("Exception raised by GNPy {}",e.getMessage());
169             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
170         }
171     }
172
173     private boolean gnpyToCheckFeasiblity(AToZDirection atoz, ZToADirection ztoa, GnpyUtilitiesImpl gnpy)
174             throws GnpyException, Exception {
175
176         //Call GNPy for path verification
177         if (gnpy.verifyComputationByGnpy(atoz, ztoa, pceHardConstraints)) {
178             LOG.info("In pceSendingPceRPC: the path is feasible according to Gnpy");
179             gnpyAtoZ = gnpy.getGnpyAtoZ();
180             gnpyZtoA = gnpy.getGnpyZtoA();
181             return true;
182         }
183         return false;
184     }
185
186     private void callGnpyToComputeNewPath(GnpyUtilitiesImpl gnpy) throws GnpyException, Exception {
187
188         //Call GNPy in the case of non feasibility
189         LOG.info("In pceSendingPceRPC: the path is not feasible according to Gnpy");
190         HardConstraints gnpyPathAsHC = null;
191         gnpyPathAsHC = gnpy.askNewPathFromGnpy(pceHardConstraints);
192         if (gnpyPathAsHC == null) {
193             LOG.info("In pceSendingPceRPC: GNPy failed to find another path");
194             this.success = false;
195             this.message = "No path available by PCE and GNPy ";
196             this.responseCode = ResponseCodes.RESPONSE_FAILED;
197             gnpyAtoZ = gnpy.getGnpyAtoZ();
198             gnpyZtoA = gnpy.getGnpyZtoA();
199             return;
200         }
201
202         LOG.info("In pceSendingPceRPC: GNPy succeed to find another path");
203         // Compute the path
204         PathComputationRequestInput inputFromGnpy = new PathComputationRequestInputBuilder()
205             .setServiceName(input.getServiceName()).setHardConstraints(gnpyPathAsHC)
206             .setSoftConstraints(input.getSoftConstraints()).setPceMetric(PceMetric.HopCount)
207             .setServiceAEnd(input.getServiceAEnd()).setServiceZEnd(input.getServiceZEnd()).build();
208         PceConstraintsCalc constraintsGnpy = new PceConstraintsCalc(inputFromGnpy, networkTransaction);
209         PceConstraints gnpyHardConstraints = constraintsGnpy.getPceHardConstraints();
210         PceConstraints gnpySoftConstraints = constraintsGnpy.getPceSoftConstraints();
211         pathComputationWithConstraints(gnpyHardConstraints, gnpySoftConstraints);
212         AToZDirection atoz = rc.getAtoZDirection();
213         ZToADirection ztoa = rc.getZtoADirection();
214         if (gnpyToCheckFeasiblity(atoz, ztoa,gnpy)) {
215             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is valid");
216             this.success = true;
217             this.message = "Path is calculated by GNPy";
218             this.responseCode = ResponseCodes.RESPONSE_OK;
219             setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
220         } else {
221             LOG.info("In pceSendingPceRPC: the new path computed by GNPy is not valid");
222             this.success = false;
223             this.message = "No path available";
224             this.responseCode = ResponseCodes.RESPONSE_FAILED;
225             setPathDescription(new PathDescriptionBuilder().setAToZDirection(null).setZToADirection(null));
226         }
227     }
228
229     private PceGraph patchRerunGraph(PceGraph graph) {
230         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
231         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
232         graph.calcPath();
233         return graph;
234     }
235
236     public PathDescriptionBuilder getPathDescription() {
237         return pathDescription;
238     }
239
240     private void setPathDescription(PathDescriptionBuilder pathDescription) {
241         this.pathDescription = pathDescription;
242     }
243
244     public Boolean getSuccess() {
245         return this.success;
246     }
247
248     public String getMessage() {
249         return this.message;
250     }
251
252     public String getResponseCode() {
253         return this.responseCode;
254     }
255
256     public GnpyResult getGnpyAtoZ() {
257         return gnpyAtoZ;
258     }
259
260     public GnpyResult getGnpyZtoA() {
261         return gnpyZtoA;
262     }
263 }