fix some sonar issues
[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             // sleep for 10s
80             Thread.sleep(10000);
81         } catch (InterruptedException e) {
82             LOG.error(e.toString());
83         }
84         LOG.info("cancelResourceReserve ...");
85     }
86
87     public void pathComputation() throws Exception {
88         // Comput the path according to the constraints of PCE
89         rc = pathComputationPCE();
90
91         LOG.info("setPathDescription ...");
92         AToZDirection atoz = rc.getAtoZDirection();
93         ZToADirection ztoa = rc.getZtoADirection();
94         ConnectToGnpyServer connectToGnpy = new ConnectToGnpyServer();
95         if ((atoz == null) || (atoz.getAToZ() == null)) {
96             rc.setRC("400");
97             LOG.warn("In PCE pathComputation: empty atoz path after description: result = {}", rc.toString());
98             return;
99         } else {
100             // Send the computed path A-to-Z to GNPY tool
101             if (connectToGnpy.isGnpyURLExist()) {
102                 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, atoz, gnpyRequestId);
103                 gnpyRequestId++;
104                 List<Elements> elementsList1 = xtrTopo.getElements();
105                 List<Connections> connectionsList1 = xtrTopo.getConnections();
106                 List<PathRequest> pathRequestList1 = xtrTopo.getPathRequest();
107                 List<Synchronization> synchronizationList1 = xtrTopo.getSynchronization();
108                 String gnpyResponse1 = getGnpyResponse(elementsList1, connectionsList1, pathRequestList1,
109                     synchronizationList1);
110                 // Analyze the response
111                 if (gnpyResponse1 != null) {
112                     GnpyResult gnpyResult = new GnpyResult(gnpyResponse1);
113                     LOG.debug("GNPy result created");
114                     gnpyResult.analyzeResult();
115                 } else {
116                     LOG.error("No response from the GNPy server");
117                 }
118             }
119         }
120
121         if ((ztoa == null) || (ztoa.getZToA() == null)) {
122             rc.setRC("400");
123             LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
124             return;
125         } else {
126             // Send the computed path Z-to-A to GNPY tool
127             if (connectToGnpy.isGnpyURLExist()) {
128                 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, ztoa, gnpyRequestId);
129                 gnpyRequestId++;
130                 List<Elements> elementsList2 = xtrTopo.getElements();
131                 List<Connections> connectionsList2 = xtrTopo.getConnections();
132                 List<PathRequest> pathRequestList2 = xtrTopo.getPathRequest();
133                 List<Synchronization> synchronizationList2 = xtrTopo.getSynchronization();
134                 String gnpyResponse2 = getGnpyResponse(elementsList2, connectionsList2, pathRequestList2,
135                         synchronizationList2);
136                 // Analyze the response
137                 if (gnpyResponse2 != null) {
138                     GnpyResult gnpyResult = new GnpyResult(gnpyResponse2);
139                     LOG.debug("GNPy result created");
140                     gnpyResult.analyzeResult();
141                 } else {
142                     LOG.info("No response from the GNPy server");
143                 }
144             }
145         }
146         // Set the description of the path
147         setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
148         LOG.info("In pathComputation Graph is Found");
149     }
150
151     public PceResult pathComputationPCE() {
152         LOG.info("PathComputation ...");
153
154         PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
155         pceHardConstraints = constraints.getPceHardConstraints();
156         pceSoftConstraints = constraints.getPceSoftConstraints();
157
158         LOG.info("nwAnalizer ...");
159         PceCalculation nwAnalizer = new PceCalculation(input, dataBroker, pceHardConstraints, pceSoftConstraints, rc);
160         nwAnalizer.calcPath();
161         rc = nwAnalizer.getReturnStructure();
162         if (!rc.getStatus()) {
163             LOG.error("In pathComputation nwAnalizer: result = {}", rc.toString());
164             return null;
165         }
166
167         LOG.info("PceGraph ...");
168         LOG.warn("PathComputation: aPceNode '{}' - zPceNode '{}'", nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode());
169         PceGraph graph = new PceGraph(nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode(), nwAnalizer.getAllPceNodes(),
170                 pceHardConstraints, pceSoftConstraints, rc);
171         graph.calcPath();
172         rc = graph.getReturnStructure();
173         if (!rc.getStatus()) {
174             LOG.warn("In pathComputation : Graph return without Path ");
175             // TODO fix. This is quick workaround for algorithm problem
176             if ((rc.getLocalCause() == LocalCause.TOO_HIGH_LATENCY)
177                     && (pceHardConstraints.getPceMetrics() == PceMetric.HopCount)
178                     && (pceHardConstraints.getMaxLatency() != -1)) {
179                 pceHardConstraints.setPceMetrics(PceMetric.PropagationDelay);
180                 graph = patchRerunGraph(graph);
181             }
182             if (!rc.getStatus()) {
183                 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
184                 return null;
185             }
186         }
187
188         LOG.info("PcePathDescription ...");
189         PcePathDescription description = new PcePathDescription(graph.getPathAtoZ(), nwAnalizer.getAllPceLinks(), rc);
190         description.buildDescriptions();
191         rc = description.getReturnStructure();
192         if (!rc.getStatus()) {
193             LOG.error("In pathComputation description: result = {}", rc.toString());
194             return null;
195         }
196         return rc;
197     }
198
199     private String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
200             List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws Exception {
201         GnpyApi gnpyApi = new GnpyApiBuilder()
202                 .setTopologyFile(
203                         new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
204                 .setServiceFile(new ServiceFileBuilder().setPathRequest(pathRequestList).build()).build();
205         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
206         String gnpyJson;
207         ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(dataBroker);
208         gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
209         LOG.debug("GNPy  Id: {} / json created : {}", idGnpyApi, gnpyJson);
210         ConnectToGnpyServer connect = new ConnectToGnpyServer();
211         String gnpyJsonModified = gnpyJson.replace("gnpy-eqpt-config:", "")
212                 .replace("gnpy-path-computation-simplified:", "").replace("gnpy-network-topology:", "");
213         //sd.writeStringFile(gnpyJsonModified);
214         String gnpyResponse = connect.gnpyCnx(gnpyJsonModified);
215         return gnpyResponse;
216     }
217
218     private PceGraph patchRerunGraph(PceGraph graph) {
219         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
220         graph.setConstrains(pceHardConstraints, pceSoftConstraints);
221         graph.calcPath();
222         return graph;
223
224     }
225
226     public PathDescriptionBuilder getPathDescription() {
227         return pathDescription;
228     }
229
230     private void setPathDescription(PathDescriptionBuilder pathDescription) {
231         this.pathDescription = pathDescription;
232     }
233
234     public Boolean getSuccess() {
235         return rc.getStatus();
236     }
237
238     public String getMessage() {
239         return rc.getMessage();
240     }
241
242     public String getResponseCode() {
243         return rc.getResponseCode();
244     }
245
246 }