Remove transportpce-routing-constraint model
[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.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraints;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirectionBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
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     public GnpyResult gnpyResponseOneDirection(GnpyServiceImpl gnpySvc) throws GnpyException {
78         requestId = Uint32.valueOf((requestId.toJava()) + 1);
79         List<PathRequest> pathRequestList = new ArrayList<>(gnpySvc.getPathRequest().values());
80         List<Synchronization> synchronizationList = gnpySvc.getSynchronization();
81         // Send the computed path to GNPY tool
82         List<Elements> elementsList = new ArrayList<>(gnpyTopo.getElements().values());
83         List<Connections> connectionsList = gnpyTopo.getConnections();
84         Result gnpyResponse = getGnpyResponse(elementsList, connectionsList, pathRequestList,
85                 synchronizationList);
86         // Analyze the response
87         if (gnpyResponse == null) {
88             throw new GnpyException("In GnpyUtilities: no response from GNPy server");
89         }
90         GnpyResult result = new GnpyResult(gnpyResponse, gnpyTopo);
91         result.analyzeResult();
92         return result;
93     }
94
95     public HardConstraints askNewPathFromGnpy(PceConstraints pceHardConstraints)
96             throws GnpyException {
97
98         AToZDirection atoztmp = new AToZDirectionBuilder()
99             .setRate(input.getServiceAEnd().getServiceRate())
100             .build();
101         GnpyServiceImpl gnpySvc = new GnpyServiceImpl(input, atoztmp, requestId, gnpyTopo, pceHardConstraints);
102         GnpyResult result = gnpyResponseOneDirection(gnpySvc);
103
104         if (result == null) {
105             throw new GnpyException("In GnpyUtilities: no result from the GNPy server");
106         }
107
108         if (!result.getPathFeasibility()) {
109             return null;
110         }
111         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
112         return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
113     }
114
115     public Result getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
116             List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) {
117
118         return gnpyConsumer.computePaths(new RequestBuilder()
119             .setTopology(
120                 new TopologyBuilder()
121                 .setElements(elementsList.stream().collect(Collectors.toMap(Elements::key, element -> element)))
122                 .setConnections(connectionsList).build())
123             .setService(
124                 new ServiceBuilder()
125                 .setPathRequest(pathRequestList.stream()
126                         .collect(Collectors.toMap(PathRequest::key, pathRequest -> pathRequest)))
127                 .build())
128             .build());
129     }
130
131     public GnpyResult getGnpyAtoZ() {
132         return gnpyAtoZ;
133     }
134
135     public GnpyResult getGnpyZtoA() {
136         return gnpyZtoA;
137     }
138 }