2 * Copyright © 2018 Orange, Inc. 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.gnpy;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
14 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
15 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
16 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApiBuilder;
17 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.ServiceFileBuilder;
18 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.TopologyFileBuilder;
19 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Connections;
20 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Elements;
21 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.properties.path.properties.PathRouteObjects;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.service.PathRequest;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.Synchronization;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
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.AToZDirectionBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ZToADirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Uint32;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Class that implements the functions asked to gnpy.
37 * @author Ahmed Triki ( ahmed.triki@orange.com )
41 public class GnpyUtilitiesImpl {
43 private static final Logger LOG = LoggerFactory.getLogger(GnpyUtilitiesImpl.class);
44 private NetworkTransactionService networkTransaction;
45 private PathComputationRequestInput input;
46 private GnpyTopoImpl gnpyTopo = null;
47 private GnpyResult gnpyAtoZ;
48 private GnpyResult gnpyZtoA;
49 private Uint32 requestId;
51 public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input)
52 throws GnpyException {
54 this.networkTransaction = networkTransaction;
55 this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
59 this.requestId = Uint32.valueOf(0);
62 public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
63 throws GnpyException, Exception {
65 if (atoz == null || atoz.getAToZ() == null || ztoa == null || ztoa.getZToA() == null) {
66 throw new GnpyException("In GnpyUtilities: the path transmitted to Gnpy is null");
69 GnpyServiceImpl gnpySvc1 = new GnpyServiceImpl(input, atoz, requestId, gnpyTopo, pceHardConstraints);
70 this.gnpyAtoZ = gnpyResponseOneDirection(gnpySvc1);
71 boolean isPcePathFeasible = false;
72 isPcePathFeasible = this.gnpyAtoZ.getPathFeasibility();
73 GnpyServiceImpl gnpySvc2 = new GnpyServiceImpl(input, ztoa, requestId, gnpyTopo, pceHardConstraints);
74 this.gnpyZtoA = gnpyResponseOneDirection(gnpySvc2);
75 isPcePathFeasible &= this.gnpyZtoA.getPathFeasibility();
76 return isPcePathFeasible;
79 public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException, Exception {
80 requestId = Uint32.valueOf((requestId.toJava()) + 1);
81 List<PathRequest> pathRequestList = new ArrayList(gnpySvc.getPathRequest().values());
82 List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
83 // Send the computed path to GNPY tool
84 List<Elements> elementsList = new ArrayList(gnpyTopo.getElements().values());
85 List<Connections> connectionsList = gnpyTopo.getConnections();
86 String gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
88 // Analyze the response
89 if (gnpyResponse == null) {
90 throw new GnpyException("In GnpyUtilities: no response from GNPy server");
92 GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
93 result.analyzeResult();
97 public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
98 throws GnpyException, Exception {
100 AToZDirection atoztmp = new AToZDirectionBuilder()
101 .setRate(input.getServiceAEnd().getServiceRate())
103 GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
104 GnpyResult result = gnpyResponseOneDirection(gnpySvc);
106 if (result == null) {
107 throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
110 if (!result.getPathFeasibility()) {
113 List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
114 return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
117 public String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
118 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList)
119 throws GnpyException, Exception {
120 GnpyApi gnpyApi = new GnpyApiBuilder()
122 new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
124 new ServiceFileBuilder().setPathRequest(pathRequestList).build())
126 InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
128 ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(networkTransaction);
129 gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
130 LOG.debug("GNPy Id: {} / json created : {}", idGnpyApi, gnpyJson);
131 ConnectToGnpyServer connect = new ConnectToGnpyServer();
132 String gnpyJsonModified = gnpyJson
133 .replace("gnpy-eqpt-config:", "")
134 .replace("gnpy-path-computation-simplified:", "")
135 .replace("gnpy-network-topology:", "");
137 return connect.returnGnpyResponse(gnpyJsonModified);
140 public GnpyResult getGnpyAtoZ() {
144 public GnpyResult getGnpyZtoA() {