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.rev220221.RequestBuilder;
18 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.request.ServiceBuilder;
19 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.request.TopologyBuilder;
20 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev220221.topo.Connections;
21 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev220221.topo.Elements;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.Result;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathRouteObjects;
24 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.service.PathRequest;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.synchronization.info.Synchronization;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestInput;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirectionBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.HardConstraints;
31 import org.opendaylight.yangtools.yang.common.Uint32;
34 * Class that implements the functions asked to gnpy.
36 * @author Ahmed Triki ( ahmed.triki@orange.com )
40 public class GnpyUtilitiesImpl {
42 private PathComputationRequestInput input;
43 private GnpyTopoImpl gnpyTopo = null;
44 private GnpyResult gnpyAtoZ;
45 private GnpyResult gnpyZtoA;
46 private Uint32 requestId;
47 private final GnpyConsumer gnpyConsumer;
49 public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
50 GnpyConsumer gnpyConsumer)
51 throws GnpyException {
52 this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
56 this.requestId = Uint32.valueOf(0);
57 this.gnpyConsumer = gnpyConsumer;
60 public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
61 throws GnpyException {
63 if (atoz == null || atoz.getAToZ() == null || ztoa == null || ztoa.getZToA() == null) {
64 throw new GnpyException("In GnpyUtilities: the path transmitted to Gnpy is null");
67 GnpyServiceImpl gnpySvc1 = new GnpyServiceImpl(input, atoz, requestId, gnpyTopo, pceHardConstraints);
68 this.gnpyAtoZ = gnpyResponseOneDirection(gnpySvc1);
69 boolean isPcePathFeasible = false;
70 isPcePathFeasible = this.gnpyAtoZ.getPathFeasibility();
71 GnpyServiceImpl gnpySvc2 = new GnpyServiceImpl(input, ztoa, requestId, gnpyTopo, pceHardConstraints);
72 this.gnpyZtoA = gnpyResponseOneDirection(gnpySvc2);
73 isPcePathFeasible &= this.gnpyZtoA.getPathFeasibility();
74 return isPcePathFeasible;
77 @SuppressWarnings("checkstyle:illegalcatch")
78 public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException {
79 requestId = Uint32.valueOf((requestId.toJava()) + 1);
80 List<PathRequest> pathRequestList = new ArrayList<>(gnpySvc.getPathRequest().values());
81 List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
82 // Send the computed path to GNPY tool
83 List<Elements> elementsList = new ArrayList<>(gnpyTopo.getElements().values());
84 List<Connections> connectionsList = gnpyTopo.getConnections();
85 Result gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
87 // Analyze the response
88 if (gnpyResponse == null) {
89 throw new GnpyException("In GnpyUtilities: no response from GNPy server");
91 GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
92 result.analyzeResult();
96 public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
97 throws GnpyException {
99 AToZDirection atoztmp = new AToZDirectionBuilder()
100 .setRate(input.getServiceAEnd().getServiceRate())
102 GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
103 GnpyResult result = gnpyResponseOneDirection(gnpySvc);
105 if (result == null) {
106 throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
109 if (!result.getPathFeasibility()) {
112 List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
113 return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
116 public Result getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
117 List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) {
119 return gnpyConsumer.computePaths(new RequestBuilder()
121 new TopologyBuilder()
122 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
123 .setConnections(connectionsList).build())
126 .setPathRequest(pathRequestList.stream()
127 .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
132 public GnpyResult getGnpyAtoZ() {
136 public GnpyResult getGnpyZtoA() {