2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce;
11 import java.util.List;
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;
39 * - path-computation-request
40 * - cancel-resource-reserve.
41 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
44 public class PceSendingPceRPCs {
47 private static final Logger LOG = LoggerFactory.getLogger(PceSendingPceRPCs.class);
48 /* define procedure success (or not ). */
49 private PceResult rc = new PceResult();
52 * define type of request<br> <code>true</code> pathcomputation <br>
53 * <code>false</code> cancelresourcereserve .
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);
62 public PceSendingPceRPCs() {
63 setPathDescription(null);
65 this.dataBroker = null;
68 public PceSendingPceRPCs(PathComputationRequestInput input, DataBroker dataBroker) {
69 setPathDescription(null);
71 // TODO compliance check to check that input is not empty
73 this.dataBroker = dataBroker;
76 public void cancelResourceReserve() {
77 LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
79 Thread.sleep(10000); // sleep for 10s
80 } catch (InterruptedException e) {
81 LOG.error(e.toString());
83 LOG.info("cancelResourceReserve ...");
86 public void pathComputation() throws Exception {
87 // Comput the path according to the constraints of PCE
88 rc = pathComputationPCE();
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)) {
96 LOG.warn("In PCE pathComputation: empty atoz path after description: result = {}", rc.toString());
99 // Send the computed path A-to-Z to GNPY tool
100 if (connectToGnpy.isGnpyURLExist()) {
101 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, atoz, 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();
115 LOG.error("No response from the GNPy server");
120 if ((ztoa == null) || (ztoa.getZToA() == null)) {
122 LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
125 // Send the computed path Z-to-A to GNPY tool
126 if (connectToGnpy.isGnpyURLExist()) {
127 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, ztoa, 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();
141 LOG.info("No response from the GNPy server");
145 // Set the description of the path
146 setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
147 LOG.info("In pathComputation Graph is Found");
150 public PceResult pathComputationPCE() {
151 LOG.info("PathComputation ...");
153 PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
154 pceHardConstraints = constraints.getPceHardConstraints();
155 pceSoftConstraints = constraints.getPceSoftConstraints();
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());
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);
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);
181 if (!rc.getStatus()) {
182 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
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());
198 private String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
199 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws Exception {
200 GnpyApi gnpyApi = new GnpyApiBuilder()
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();
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);
217 private PceGraph patchRerunGraph(PceGraph graph) {
218 LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
219 graph.setConstrains(pceHardConstraints, pceSoftConstraints);
225 public PathDescriptionBuilder getPathDescription() {
226 return pathDescription;
229 private void setPathDescription(PathDescriptionBuilder pathDescription) {
230 this.pathDescription = pathDescription;
233 public Boolean getSuccess() {
234 return rc.getStatus();
237 public String getMessage() {
238 return rc.getMessage();
241 public String getResponseCode() {
242 return rc.getResponseCode();