Adapt code to support the new version of GNPy
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / GnpyUtilitiesImpl.java
1 /*
2  * Copyright © 2018 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.pce.gnpy;
10
11 import java.util.List;
12
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.rev171017.path.description.AToZDirection;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirectionBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Class that implements the functions asked to gnpy.
35  *
36  * @author Ahmed Triki ( ahmed.triki@orange.com )
37  *
38  */
39
40 public class GnpyUtilitiesImpl {
41
42     private static final Logger LOG = LoggerFactory.getLogger(GnpyResult.class);
43     private NetworkTransactionService networkTransaction;
44     private PathComputationRequestInput input;
45     private GnpyTopoImpl gnpyTopo = null;
46     private GnpyResult gnpyAtoZ;
47     private GnpyResult gnpyZtoA;
48     private Long requestId;
49
50     public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input)
51         throws GnpyException {
52
53         this.networkTransaction = networkTransaction;
54         this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
55         this.input = input;
56         this.gnpyAtoZ = null;
57         this.gnpyZtoA = null;
58         this.requestId = (long) 0;
59     }
60
61     public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
62         throws GnpyException, Exception {
63
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");
66         }
67
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;
76     }
77
78     public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException, Exception {
79         requestId++;
80         List<PathRequest> pathRequestList = gnpySvc.getPathRequest();
81         List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
82         // Send the computed path to GNPY tool
83         List<Elements> elementsList = gnpyTopo.getElements();
84         List<Connections> connectionsList = gnpyTopo.getConnections();
85         String gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
86             synchronizationList);
87         // Analyze the response
88         if (gnpyResponse == null) {
89             throw new GnpyException("In GnpyUtilities: no respnse from GNPy server");
90         }
91         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
92         result.analyzeResult();
93         return result;
94     }
95
96     public HardConstraints askNewPathFromGnpy(HardConstraints gnpyPathAsHC, PceConstraints pceHardConstraints)
97             throws GnpyException, Exception {
98
99         AToZDirection atoztmp = new AToZDirectionBuilder()
100             .setRate(input.getServiceAEnd().getServiceRate())
101             .setAToZ(null)
102             .build();
103         GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
104         GnpyResult result = gnpyResponseOneDirection(gnpySvc);
105
106         if (result == null) {
107             throw new GnpyException("In GnpyUtilities: no response from the GNPy server");
108         }
109
110         if (!result.getPathFeasibility()) {
111             return null;
112         }
113         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
114         gnpyPathAsHC = result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
115
116         return gnpyPathAsHC;
117     }
118
119     public String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
120         List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws GnpyException, Exception {
121         GnpyApi gnpyApi = new GnpyApiBuilder()
122             .setTopologyFile(
123                 new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
124             .setServiceFile(
125                 new ServiceFileBuilder().setPathRequest(pathRequestList).build())
126             .build();
127         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
128         String gnpyJson;
129         ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(networkTransaction);
130         gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
131         LOG.debug("GNPy Id: {} / json created : {}", idGnpyApi, gnpyJson);
132         ConnectToGnpyServer connect = new ConnectToGnpyServer();
133         String gnpyJsonModified = gnpyJson
134             .replace("gnpy-eqpt-config:", "")
135             .replace("gnpy-path-computation-simplified:", "")
136             .replace("gnpy-network-topology:", "");
137
138         String gnpyResponse = connect.gnpyCnx(gnpyJsonModified);
139         return gnpyResponse;
140     }
141
142     public GnpyResult getGnpyAtoZ() {
143         return gnpyAtoZ;
144     }
145
146     public GnpyResult getGnpyZtoA() {
147         return gnpyZtoA;
148     }
149 }