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");
81 } catch (InterruptedException e) {
82 LOG.error(e.toString());
84 LOG.info("cancelResourceReserve ...");
87 public void pathComputation() throws Exception {
88 // Comput the path according to the constraints of PCE
89 rc = pathComputationPCE();
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)) {
97 LOG.warn("In PCE pathComputation: empty atoz path after description: result = {}", rc.toString());
100 // Send the computed path A-to-Z to GNPY tool
101 if (connectToGnpy.isGnpyURLExist()) {
102 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, atoz, 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();
116 LOG.error("No response from the GNPy server");
121 if ((ztoa == null) || (ztoa.getZToA() == null)) {
123 LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
126 // Send the computed path Z-to-A to GNPY tool
127 if (connectToGnpy.isGnpyURLExist()) {
128 ExtractTopoDataStoreImpl xtrTopo = new ExtractTopoDataStoreImpl(dataBroker, input, ztoa, 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();
142 LOG.info("No response from the GNPy server");
146 // Set the description of the path
147 setPathDescription(new PathDescriptionBuilder().setAToZDirection(atoz).setZToADirection(ztoa));
148 LOG.info("In pathComputation Graph is Found");
151 public PceResult pathComputationPCE() {
152 LOG.info("PathComputation ...");
154 PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
155 pceHardConstraints = constraints.getPceHardConstraints();
156 pceSoftConstraints = constraints.getPceSoftConstraints();
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());
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);
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);
182 if (!rc.getStatus()) {
183 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
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());
199 private String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
200 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws Exception {
201 GnpyApi gnpyApi = new GnpyApiBuilder()
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();
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);
218 private PceGraph patchRerunGraph(PceGraph graph) {
219 LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
220 graph.setConstrains(pceHardConstraints, pceSoftConstraints);
226 public PathDescriptionBuilder getPathDescription() {
227 return pathDescription;
230 private void setPathDescription(PathDescriptionBuilder pathDescription) {
231 this.pathDescription = pathDescription;
234 public Boolean getSuccess() {
235 return rc.getStatus();
238 public String getMessage() {
239 return rc.getMessage();
242 public String getResponseCode() {
243 return rc.getResponseCode();