f7fc3a425dc715c1a931dc9d5b51a3078cd81af1
[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.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;
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 PathComputationRequestInput input;
43     private GnpyTopoImpl gnpyTopo = null;
44     private GnpyResult gnpyAtoZ;
45     private GnpyResult gnpyZtoA;
46     private Uint32 requestId;
47     private final GnpyConsumer gnpyConsumer;
48
49     public GnpyUtilitiesImpl(NetworkTransactionService networkTransaction, PathComputationRequestInput input,
50             GnpyConsumer gnpyConsumer)
51         throws GnpyException {
52         this.gnpyTopo = new GnpyTopoImpl(networkTransaction);
53         this.input = input;
54         this.gnpyAtoZ = null;
55         this.gnpyZtoA = null;
56         this.requestId = Uint32.valueOf(0);
57         this.gnpyConsumer = gnpyConsumer;
58     }
59
60     public boolean verifyComputationByGnpy(AToZDirection atoz, ZToADirection ztoa, PceConstraints pceHardConstraints)
61         throws GnpyException {
62
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");
65         }
66
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;
75     }
76
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,
86                 synchronizationList);
87         // Analyze the response
88         if (gnpyResponse == null) {
89             throw new GnpyException("In GnpyUtilities: no response from GNPy server");
90         }
91         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
92         result.analyzeResult();
93         return result;
94     }
95
96     public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
97             throws GnpyException {
98
99         AToZDirection atoztmp = new AToZDirectionBuilder()
100             .setRate(input.getServiceAEnd().getServiceRate())
101             .build();
102         GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
103         GnpyResult result = gnpyResponseOneDirection(gnpySvc);
104
105         if (result == null) {
106             throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
107         }
108
109         if (!result.getPathFeasibility()) {
110             return null;
111         }
112         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
113         return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
114     }
115
116     public Result getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
117             List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) {
118
119         return gnpyConsumer.computePaths(new RequestBuilder()
120             .setTopology(
121                 new TopologyBuilder()
122                 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
123                 .setConnections(connectionsList).build())
124             .setService(
125                 new ServiceBuilder()
126                 .setPathRequest(pathRequestList.stream()
127                         .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
128                 .build())
129             .build());
130     }
131
132     public GnpyResult getGnpyAtoZ() {
133         return gnpyAtoZ;
134     }
135
136     public GnpyResult getGnpyZtoA() {
137         return gnpyZtoA;
138     }
139 }