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.transportpce.common.network.NetworkTransactionService;
15 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
16 import org.opendaylight.transportpce.pce.gnpy.consumer.GnpyConsumer;
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.Result;
24 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathRouteObjects;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.service.PathRequest;
26 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.synchronization.info.Synchronization;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.PathComputationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirectionBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
32 import org.opendaylight.yangtools.yang.common.Uint32;
35 * Class that implements the functions asked to gnpy.
37 * @author Ahmed Triki ( ahmed.triki@orange.com )
41 public class GnpyUtilitiesImpl {
43 private PathComputationRequestInput input;
44 private GnpyTopoImpl gnpyTopo = null;
45 private GnpyResult gnpyAtoZ;
46 private GnpyResult gnpyZtoA;
47 private Uint32 requestId;
48 private final GnpyConsumer gnpyConsumer;
50 public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
51 GnpyConsumer gnpyConsumer)
52 throws GnpyException {
53 this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
57 this.requestId = Uint32.valueOf(0);
58 this.gnpyConsumer = gnpyConsumer;
61 public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
62 throws GnpyException {
64 if (atoz == null || atoz.getAToZ() == null || ztoa == null || ztoa.getZToA() == null) {
65 throw new GnpyException("In GnpyUtilities: the path transmitted to Gnpy is null");
68 GnpyServiceImpl gnpySvc1 = new GnpyServiceImpl(input, atoz, requestId, gnpyTopo, pceHardConstraints);
69 this.gnpyAtoZ = gnpyResponseOneDirection(gnpySvc1);
70 boolean isPcePathFeasible = false;
71 isPcePathFeasible = this.gnpyAtoZ.getPathFeasibility();
72 GnpyServiceImpl gnpySvc2 = new GnpyServiceImpl(input, ztoa, requestId, gnpyTopo, pceHardConstraints);
73 this.gnpyZtoA = gnpyResponseOneDirection(gnpySvc2);
74 isPcePathFeasible &= this.gnpyZtoA.getPathFeasibility();
75 return isPcePathFeasible;
78 @SuppressWarnings("checkstyle:illegalcatch")
79 public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException {
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 Result 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 {
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 Result getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
118 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) {
119 GnpyApi gnpyApi = new GnpyApiBuilder()
121 new TopologyFileBuilder()
122 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
123 .setConnections(connectionsList).build())
125 new ServiceFileBuilder()
126 .setPathRequest(pathRequestList.stream()
127 .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
130 return gnpyConsumer.computePaths(gnpyApi);
133 public GnpyResult getGnpyAtoZ() {
137 public GnpyResult getGnpyZtoA() {