Fix spotbugs issues of the PCE module
[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.rev200202.generic.path.properties.path.properties.PathRouteObjects;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.service.PathRequest;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.Synchronization;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.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.opendaylight.yangtools.yang.common.Uint32;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
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 static final Logger LOG = LoggerFactory.getLogger(GnpyUtilitiesImpl.class);
44     private NetworkTransactionService networkTransaction;
45     private PathComputationRequestInput input;
46     private GnpyTopoImpl gnpyTopo = null;
47     private GnpyResult gnpyAtoZ;
48     private GnpyResult gnpyZtoA;
49     private Uint32 requestId;
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 = Uint32.valueOf(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 = Uint32.valueOf((requestId.toJava()) + 1);
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 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, 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 result from the GNPy server");
109         }
110
111         if (!result.getPathFeasibility()) {
112             return null;
113         }
114         List<PathRouteObjects> pathRouteObjectList = result.analyzeResult();
115         return result.computeHardConstraintsFromGnpyPath(pathRouteObjectList);
116     }
117
118     public String getGnpyResponse(List<Elements> elementsList, List<Connections> connectionsList,
119         List<PathRequest> pathRequestList, List<Synchronization> synchronizationList) throws GnpyException, Exception {
120         GnpyApi gnpyApi = new GnpyApiBuilder()
121             .setTopologyFile(
122                 new TopologyFileBuilder().setElements(elementsList).setConnections(connectionsList).build())
123             .setServiceFile(
124                 new ServiceFileBuilder().setPathRequest(pathRequestList).build())
125             .build();
126         InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
127         String gnpyJson;
128         ServiceDataStoreOperationsImpl sd = new ServiceDataStoreOperationsImpl(networkTransaction);
129         gnpyJson = sd.createJsonStringFromDataObject(idGnpyApi, gnpyApi);
130         LOG.debug("GNPy Id: {} / json created : {}", idGnpyApi, gnpyJson);
131         ConnectToGnpyServer connect = new ConnectToGnpyServer();
132         String gnpyJsonModified = gnpyJson
133             .replace("gnpy-eqpt-config:", "")
134             .replace("gnpy-path-computation-simplified:", "")
135             .replace("gnpy-network-topology:", "");
136
137         return connect.returnGnpyResponse(gnpyJsonModified);
138     }
139
140     public GnpyResult getGnpyAtoZ() {
141         return gnpyAtoZ;
142     }
143
144     public GnpyResult getGnpyZtoA() {
145         return gnpyZtoA;
146     }
147 }