6ef4a1b668f6bcd36261efd53489380491224908
[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.rev190502.generic.path.properties.path.properties.PathRouteObjects;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.service.PathRequest;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.Synchronization;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.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     //private boolean status;
50
51     public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input)
52         throws GnpyException {
53
54         this.networkTransaction = networkTransaction;
55         this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
56         this.input = input;
57         this.gnpyAtoZ = null;
58         this.gnpyZtoA = null;
59         this.requestId = (long) 0;
60     }
61
62     public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
63         throws GnpyException, Exception {
64
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");
67         }
68
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;
77     }
78
79     public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException, Exception {
80         requestId++;
81         List<PathRequest> pathRequestList = gnpySvc.getPathRequest();
82         List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
83         // Send the computed path to GNPY tool
84         List<Elements> elementsList = gnpyTopo.getElements();
85         List<Connections> connectionsList = gnpyTopo.getConnections();
86         String gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
87             synchronizationList);
88         // Analyze the response
89         if (gnpyResponse == null) {
90             throw new GnpyException("In GnpyUtilities: no respnse from GNPy server");
91         }
92         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
93         result.analyzeResult();
94         return result;
95     }
96
97     public HardConstraints askNewPathFromGnpy(HardConstraints gnpyPathAsHC, PceConstraints pceHardConstraints)
98             throws GnpyException, Exception {
99
100         AToZDirection atoztmp = new AToZDirectionBuilder()
101             .setRate(input.getServiceAEnd().getServiceRate())
102             .setAToZ(null)
103             .build();
104         GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
105         GnpyResult result = gnpyResponseOneDirection(gnpySvc);
106
107         if (result == null) {
108             throw new GnpyException("In GnpyUtilities: no response from the GNPy server");
109         }
110
111         if (!result.getPathFeasibility()) {
112             return null;
113         }
114         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
115         gnpyPathAsHC = result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
116
117         return gnpyPathAsHC;
118     }
119
120     public String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
121         List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws GnpyException, Exception {
122         GnpyApi gnpyApi = new GnpyApiBuilder()
123             .setTopologyFile(
124                 new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
125             .setServiceFile(
126                 new ServiceFileBuilder().setPathRequest(pathRequestList).build())
127             .build();
128         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
129         String gnpyJson;
130         ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(networkTransaction);
131         gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
132         LOG.debug("GNPy Id: {} / json created : {}", idGnpyApi, gnpyJson);
133         ConnectToGnpyServer connect = new ConnectToGnpyServer();
134         String gnpyJsonModified = gnpyJson
135             .replace("gnpy-eqpt-config:", "")
136             .replace("gnpy-path-computation-simplified:", "")
137             .replace("gnpy-network-topology:", "");
138
139         String gnpyResponse = connect.gnpyCnx(gnpyJsonModified);
140         return gnpyResponse;
141     }
142
143     public GnpyResult getGnpyAtoZ() {
144         return gnpyAtoZ;
145     }
146
147     public GnpyResult getGnpyZtoA() {
148         return gnpyZtoA;
149     }
150 }