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 java.util.stream.Collectors;
14 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
15 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
16 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
17 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
18 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApiBuilder;
19 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.ServiceFileBuilder;
20 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.gnpy.api.TopologyFileBuilder;
21 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Connections;
22 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Elements;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathRouteObjects;
24 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.service.PathRequest;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.synchronization.info.Synchronization;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.AToZDirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.AToZDirectionBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ZToADirection;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.Uint32;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class that implements the functions asked to gnpy.
39 * @author Ahmed Triki ( ahmed.triki@orange.com )
43 public class GnpyUtilitiesImpl {
45 private static final Logger LOG = LoggerFactory.getLogger(GnpyUtilitiesImpl.class);
46 private PathComputationRequestInput input;
47 private GnpyTopoImpl gnpyTopo = null;
48 private GnpyResult gnpyAtoZ;
49 private GnpyResult gnpyZtoA;
50 private Uint32 requestId;
51 private BindingDOMCodecServices bindingDOMCodecServices;
54 public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
55 BindingDOMCodecServices bindingDOMCodecServices)
56 throws GnpyException {
57 this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
61 this.requestId = Uint32.valueOf(0);
62 this.bindingDOMCodecServices = bindingDOMCodecServices;
65 public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
66 throws GnpyException {
68 if (atoz == null || atoz.getAToZ() == null || ztoa == null || ztoa.getZToA() == null) {
69 throw new GnpyException("In GnpyUtilities: the path transmitted to Gnpy is null");
72 GnpyServiceImpl gnpySvc1 = new GnpyServiceImpl(input, atoz, requestId, gnpyTopo, pceHardConstraints);
73 this.gnpyAtoZ = gnpyResponseOneDirection(gnpySvc1);
74 boolean isPcePathFeasible = false;
75 isPcePathFeasible = this.gnpyAtoZ.getPathFeasibility();
76 GnpyServiceImpl gnpySvc2 = new GnpyServiceImpl(input, ztoa, requestId, gnpyTopo, pceHardConstraints);
77 this.gnpyZtoA = gnpyResponseOneDirection(gnpySvc2);
78 isPcePathFeasible &= this.gnpyZtoA.getPathFeasibility();
79 return isPcePathFeasible;
82 @SuppressWarnings("checkstyle:illegalcatch")
83 public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException {
84 requestId = Uint32.valueOf((requestId.toJava()) + 1);
85 List<PathRequest> pathRequestList = new ArrayList<>(gnpySvc.getPathRequest().values());
86 List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
87 // Send the computed path to GNPY tool
88 List<Elements> elementsList = new ArrayList<>(gnpyTopo.getElements().values());
89 List<Connections> connectionsList = gnpyTopo.getConnections();
92 gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
94 } catch (Exception e) {
95 throw new GnpyException("Something went wrong", e);
97 // Analyze the response
98 if (gnpyResponse == null) {
99 throw new GnpyException("In GnpyUtilities: no response from GNPy server");
101 GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo, bindingDOMCodecServices);
102 result.analyzeResult();
106 public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
107 throws GnpyException {
109 AToZDirection atoztmp = new AToZDirectionBuilder()
110 .setRate(input.getServiceAEnd().getServiceRate())
112 GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
113 GnpyResult result = gnpyResponseOneDirection(gnpySvc);
115 if (result == null) {
116 throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
119 if (!result.getPathFeasibility()) {
122 List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
123 return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
126 public String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
127 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList)
128 throws GnpyException {
129 GnpyApi gnpyApi = new GnpyApiBuilder()
131 new TopologyFileBuilder()
132 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
133 .setConnections(connectionsList).build())
135 new ServiceFileBuilder()
136 .setPathRequest(pathRequestList.stream()
137 .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
140 InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
142 ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(bindingDOMCodecServices);
143 gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
144 LOG.debug("GNPy Id: {} / json created : {}", idGnpyApi, gnpyJson);
145 ConnectToGnpyServer connect = new ConnectToGnpyServer();
146 String gnpyJsonModified = gnpyJson
147 .replace("gnpy-eqpt-config:", "")
148 .replace("gnpy-path-computation-simplified:", "")
149 .replace("gnpy-network-topology:", "");
151 return connect.returnGnpyResponse(gnpyJsonModified);
154 public GnpyResult getGnpyAtoZ() {
158 public GnpyResult getGnpyZtoA() {