GNPy client refactor
[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.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.rev200128.PathComputationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.AToZDirection;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.AToZDirectionBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.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;
33
34 /**
35  * Class that implements the functions asked to gnpy.
36  *
37  * @author Ahmed Triki ( ahmed.triki@orange.com )
38  *
39  */
40
41 public class GnpyUtilitiesImpl {
42
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;
49
50     public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
51             GnpyConsumer gnpyConsumer)
52         throws GnpyException {
53         this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
54         this.input = input;
55         this.gnpyAtoZ = null;
56         this.gnpyZtoA = null;
57         this.requestId = Uint32.valueOf(0);
58         this.gnpyConsumer = gnpyConsumer;
59     }
60
61     public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
62         throws GnpyException {
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     @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,
87                 synchronizationList);
88         // Analyze the response
89         if (gnpyResponse == null) {
90             throw new GnpyException("In GnpyUtilities: no response from GNPy server");
91         }
92         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
93         result.analyzeResult();
94         return result;
95     }
96
97     public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
98             throws GnpyException {
99
100         AToZDirection atoztmp = new AToZDirectionBuilder()
101             .setRate(input.getServiceAEnd().getServiceRate())
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 result from the GNPy server");
108         }
109
110         if (!result.getPathFeasibility()) {
111             return null;
112         }
113         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
114         return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
115     }
116
117     public Result getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
118         List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) {
119         GnpyApi gnpyApi = new GnpyApiBuilder()
120             .setTopologyFile(
121                 new TopologyFileBuilder()
122                 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
123                 .setConnections(connectionsList).build())
124             .setServiceFile(
125                 new ServiceFileBuilder()
126                 .setPathRequest(pathRequestList.stream()
127                         .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
128                 .build())
129             .build();
130         return gnpyConsumer.computePaths(gnpyApi);
131     }
132
133     public GnpyResult getGnpyAtoZ() {
134         return gnpyAtoZ;
135     }
136
137     public GnpyResult getGnpyZtoA() {
138         return gnpyZtoA;
139     }
140 }