bada4cb4b0cd626f560e104724419de6aede3396
[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 java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.transportpce.pce.PceResult.LocalCause;
15 import org.opendaylight.transportpce.pce.gnpy.ConnectToGnpyServer;
16 import org.opendaylight.transportpce.pce.gnpy.ExtractTopoDataStoreImpl;
17 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
18 import org.opendaylight.transportpce.pce.gnpy.ServiceDataStoreOperationsImpl;
19 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
20 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApiBuilder;
21 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.ServiceFileBuilder;
22 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.TopologyFileBuilder;
23 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Connections;
24 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Elements;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.service.PathRequest;
26 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.Synchronization;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.service.path.rpc.result.PathDescriptionBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /*
37  * Class for Sending
38  * PCE requests :
39  * - path-computation-request
40  * - cancel-resource-reserve.
41  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
42  *
43  */
44 public class PceSendingPceRPCs {
45
46     /* Logging. */
47     private static final Logger LOG = LoggerFactory.getLogger(PceSendingPceRPCs.class);
48     /* define procedure success (or not ). */
49     private PceResult rc = new PceResult();
50
51     /*
52      * define type of request<br> <code>true</code> pathcomputation <br>
53      * <code>false</code> cancelresourcereserve .
54      */
55     private PathDescriptionBuilder pathDescription;
56     private PathComputationRequestInput input;
57     private DataBroker dataBroker;
58     private PceConstraints pceHardConstraints = new PceConstraints();
59     private PceConstraints pceSoftConstraints = new PceConstraints();
60     private Long gnpyRequestId = new Long(0);
61
62     public PceSendingPceRPCs() {
63         setPathDescription(null);
64         this.input = null;
65         this.dataBroker = null;
66     }
67
68     public PceSendingPceRPCs(PathComputationRequestInput input, DataBroker dataBroker) {
69         setPathDescription(null);
70
71         // TODO compliance check to check that input is not empty
72         this.input = input;
73         this.dataBroker = dataBroker;
74     }
75
76     public void cancelResourceReserve() {
77         LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
78         try {
79             Thread.sleep(10000); // sleep for 10s
80         } catch (InterruptedException e) {
81             LOG.error(e.toString());
82         }
83         LOG.info("cancelResourceReserve ...");
84     }
85
86     public void pathComputation() throws Exception {
87         // Comput the path according to the constraints of PCE
88         rc = pathComputationPCE();
89
90         LOG.info("setPathDescription ...");
91         AToZDirection atoz = rc.getAtoZDirection();
92         ZToADirection ztoa = rc.getZtoADirection();
93         ConnectToGnpyServer connectToGnpy = new ConnectToGnpyServer();
94         if ((atoz == null) || (atoz.getAToZ() == null)) {
95             rc.setRC("400");
96             LOG.warn("In PCE pathComputation: empty atoz path after description: result = {}", rc.toString());
97             return;
98         } else {
99             // Send the computed path A-to-Z to GNPY tool
100             if (connectToGnpy.isGnpyURLExist()) {
101                 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, atoz, gnpyRequestId);
102                 gnpyRequestId++;
103                 List<Elements> elementsList1 = xtrTopo.getElements();
104                 List<Connections> connectionsList1 = xtrTopo.getConnections();
105                 List<PathRequest> pathRequestList1 = xtrTopo.getPathRequest();
106                 List<Synchronization> synchronizationList1 = xtrTopo.getSynchronization();
107                 String gnpyResponse1 = getGnpyResponse(elementsList1, connectionsList1, pathRequestList1,
108                     synchronizationList1);
109                 // Analyze the response
110                 if (gnpyResponse1 != null) {
111                     GnpyResult gnpyResult = new GnpyResult(gnpyResponse1);
112                     LOG.debug("GNPy result created");
113                     gnpyResult.analyzeResult();
114                 } else {
115                     LOG.error("No response from the GNPy server");
116                 }
117             }
118         }
119
120         if ((ztoa == null) || (ztoa.getZToA() == null)) {
121             rc.setRC("400");
122             LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
123             return;
124         } else {
125             // Send the computed path Z-to-A to GNPY tool
126             if (connectToGnpy.isGnpyURLExist()) {
127                 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, ztoa, gnpyRequestId);
128                 gnpyRequestId++;
129                 List<Elements> elementsList2 = xtrTopo.getElements();
130                 List<Connections> connectionsList2 = xtrTopo.getConnections();
131                 List<PathRequest> pathRequestList2 = xtrTopo.getPathRequest();
132                 List<Synchronization> synchronizationList2 = xtrTopo.getSynchronization();
133                 String gnpyResponse2 = getGnpyResponse(elementsList2, connectionsList2, pathRequestList2,
134                         synchronizationList2);
135                 // Analyze the response
136                 if (gnpyResponse2 != null) {
137                     GnpyResult gnpyResult = new GnpyResult(gnpyResponse2);
138                     LOG.debug("GNPy result created");
139                     gnpyResult.analyzeResult();
140                 } else {
141                     LOG.info("No response from the GNPy server");
142                 }
143             }
144         }
145         // Set the description of the path
146         setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
147         LOG.info("In pathComputation Graph is Found");
148     }
149
150     public PceResult pathComputationPCE() {
151         LOG.info("PathComputation ...");
152
153         PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
154         pceHardConstraints = constraints.getPceHardConstraints();
155         pceSoftConstraints = constraints.getPceSoftConstraints();
156
157         LOG.info("nwAnalizer ...");
158         PceCalculation nwAnalizer = new PceCalculation(input, dataBroker, pceHardConstraints, pceSoftConstraints, rc);
159         nwAnalizer.calcPath();
160         rc = nwAnalizer.getReturnStructure();
161         if (!rc.getStatus()) {
162             LOG.error("In pathComputation nwAnalizer: result = {}", rc.toString());
163             return null;
164         }
165
166         LOG.info("PceGraph ...");
167         LOG.warn("PathComputation: aPceNode '{}' - zPceNode '{}'", nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode());
168         PceGraph graph = new PceGraph(nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode(), nwAnalizer.getAllPceNodes(),
169                 pceHardConstraints, pceSoftConstraints, rc);
170         graph.calcPath();
171         rc = graph.getReturnStructure();
172         if (!rc.getStatus()) {
173             LOG.warn("In pathComputation : Graph return without Path ");
174             // TODO fix. This is quick workaround for algorithm problem
175             if ((rc.getLocalCause() == LocalCause.TOO_HIGH_LATENCY)
176                     && (pceHardConstraints.getPceMetrics() == PceMetric.HopCount)
177                     && (pceHardConstraints.getMaxLatency() != -1)) {
178                 pceHardConstraints.setPceMetrics(PceMetric.PropagationDelay);
179                 graph = patchRerunGraph(graph);
180             }
181             if (!rc.getStatus()) {
182                 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
183                 return null;
184             }
185         }
186
187         LOG.info("PcePathDescription ...");
188         PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
189         description.buildDescriptions();
190         rc = description.getReturnStructure();
191         if (!rc.getStatus()) {
192             LOG.error("In pathComputation description: result = {}", rc.toString());
193             return null;
194         }
195         return rc;
196     }
197
198     private String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
199             List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws Exception {
200         GnpyApi gnpyApi = new GnpyApiBuilder()
201                 .setTopologyFile(
202                         new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
203                 .setServiceFile(new ServiceFileBuilder().setPathRequest(pathRequestList).build()).build();
204         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
205         String gnpyJson;
206         ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(dataBroker);
207         gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
208         LOG.debug("GNPy  Id: {} / json created : {}", idGnpyApi, gnpyJson);
209         ConnectToGnpyServer connect = new ConnectToGnpyServer();
210         String gnpyJsonModified = gnpyJson.replace("gnpy-eqpt-config:", "")
211                 .replace("gnpy-path-computation-simplified:", "").replace("gnpy-network-topology:", "");
212         //sd.writeStringFile(gnpyJsonModified);
213         String gnpyResponse = connect.gnpyCnx(gnpyJsonModified);
214         return gnpyResponse;
215     }
216
217     private PceGraph patchRerunGraph(PceGraph graph) {
218         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
219         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
220         graph.calcPath();
221         return graph;
222
223     }
224
225     public PathDescriptionBuilder getPathDescription() {
226         return pathDescription;
227     }
228
229     private void setPathDescription(PathDescriptionBuilder pathDescription) {
230         this.pathDescription = pathDescription;
231     }
232
233     public Boolean getSuccess() {
234         return rc.getStatus();
235     }
236
237     public String getMessage() {
238         return rc.getMessage();
239     }
240
241     public String getResponseCode() {
242         return rc.getResponseCode();
243     }
244
245 }