New version of transportpce-pathDescription
[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.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.rev201126.path.description.AToZDirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.AToZDirectionBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.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;
35
36 /**
37  * Class that implements the functions asked to gnpy.
38  *
39  * @author Ahmed Triki ( ahmed.triki@orange.com )
40  *
41  */
42
43 public class GnpyUtilitiesImpl {
44
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;
52
53
54     public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
55             BindingDOMCodecServices bindingDOMCodecServices)
56         throws GnpyException {
57         this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
58         this.input = input;
59         this.gnpyAtoZ = null;
60         this.gnpyZtoA = null;
61         this.requestId = Uint32.valueOf(0);
62         this.bindingDOMCodecServices = bindingDOMCodecServices;
63     }
64
65     public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
66         throws GnpyException {
67
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");
70         }
71
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;
80     }
81
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();
90         String gnpyResponse;
91         try {
92             gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
93                 synchronizationList);
94         } catch (Exception e) {
95             throw new GnpyException("Something went wrong", e);
96         }
97         // Analyze the response
98         if (gnpyResponse == null) {
99             throw new GnpyException("In GnpyUtilities: no response from GNPy server");
100         }
101         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo, bindingDOMCodecServices);
102         result.analyzeResult();
103         return result;
104     }
105
106     public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
107             throws GnpyException {
108
109         AToZDirection atoztmp = new AToZDirectionBuilder()
110             .setRate(input.getServiceAEnd().getServiceRate())
111             .build();
112         GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
113         GnpyResult result = gnpyResponseOneDirection(gnpySvc);
114
115         if (result == null) {
116             throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
117         }
118
119         if (!result.getPathFeasibility()) {
120             return null;
121         }
122         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
123         return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
124     }
125
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()
130             .setTopologyFile(
131                 new TopologyFileBuilder()
132                 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
133                 .setConnections(connectionsList).build())
134             .setServiceFile(
135                 new ServiceFileBuilder()
136                 .setPathRequest(pathRequestList.stream()
137                         .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
138                 .build())
139             .build();
140         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
141         String gnpyJson;
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:", "");
150
151         return connect.returnGnpyResponse(gnpyJsonModified);
152     }
153
154     public GnpyResult getGnpyAtoZ() {
155         return gnpyAtoZ;
156     }
157
158     public GnpyResult getGnpyZtoA() {
159         return gnpyZtoA;
160     }
161 }