Initial commit for ServiceHandler
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / MappingAndSendingPCRequest.java
1 /*
2  * Copyright © 2017 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.servicehandler;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.MoreExecutors;
14
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.CancellationException;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20
21 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev161014.ServiceCreateInput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev161014.ServiceFeasibilityCheckInput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev161014.ServiceReconfigureInput;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev161014.service.list.Services;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveInput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestInput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestInputBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.StubpceService;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeader;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeaderBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.RoutingConstraintsSp.PceMetric;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing.constraints.sp.HardConstraints;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing.constraints.sp.SoftConstraints;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service.handler.header.ServiceHandlerHeaderBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /*
47  * class for Mapping and Sending PCE requests : - path-computation-request -
48  * cancel-resource-reserve.
49  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
50  *
51  */
52 public class MappingAndSendingPCRequest {
53     /* Logging. */
54     private static final Logger LOG = LoggerFactory.getLogger(MappingAndSendingPCRequest.class);
55     /* Permit to call PCE RPCs. */
56     private StubpceService service;
57     /* define procedure success (or not ). */
58     private Boolean success;
59     /* permit to call bundle service (PCE, Renderer, Servicehandler. */
60     private RpcProviderRegistry rpcRegistry;
61     PathComputationRequestInput pathComputationRequestInput = null;
62     CancelResourceReserveInput cancelResourceReserveInput = null;
63     HardConstraints hard = null;
64     SoftConstraints soft = null;
65     /* store all error messages. */
66     private String error;
67     private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
68
69     /*
70      * MappingAndSendingPCRequest Class constructor for RPC serviceReconfigure.
71      *
72      * @param rpcRegistry
73      *            RpcProviderRegistry
74      * @param serviceReconfigureInput
75      *            serviceReconfigureInput
76      * @param resvResource
77      *            Boolean to define resource reserve
78      */
79     public MappingAndSendingPCRequest(RpcProviderRegistry rpcRegistry, ServiceReconfigureInput serviceReconfigureInput,
80             Boolean resvResource) {
81         this.rpcRegistry = rpcRegistry;
82         if (rpcRegistry != null) {
83             service = rpcRegistry.getRpcService(StubpceService.class);
84         }
85         SdncRequestHeader head = new SdncRequestHeaderBuilder().build();
86         MappingConstraints map = new MappingConstraints(serviceReconfigureInput.getHardConstraints(),
87                 serviceReconfigureInput.getSoftConstraints());
88         map.serviceToServicePathConstarints();
89         /*
90          * mappingPCRequest(serviceReconfigureInput.getNewServiceName(),
91          * serviceReconfigureInput.getHardConstraints(),
92          * serviceReconfigureInput.getSoftConstraints(),head ,resvResource);
93          */
94         mappingPCRequest(serviceReconfigureInput.getNewServiceName(), map.getServicePathHardConstraints(),
95                 map.getServicePathSoftConstraints(), head, resvResource);
96         setSuccess(false);
97         setError("");
98     }
99
100     /*
101      * MappingAndSendingPCRequest Class constructor for RPC
102      * serviceFeasibilityCheck.
103      *
104      * @param rpcRegistry
105      *            RpcProviderRegistry
106      * @param serviceFeasibilityCheckInput
107      *            ServiceFeasibilityCheckInput
108      * @param resvResource
109      *            Boolean to reserve resource
110      */
111     public MappingAndSendingPCRequest(RpcProviderRegistry rpcRegistry,
112             ServiceFeasibilityCheckInput serviceFeasibilityCheckInput, Boolean resvResource) {
113         this.rpcRegistry = rpcRegistry;
114         if (rpcRegistry != null) {
115             service = rpcRegistry.getRpcService(StubpceService.class);
116         }
117         MappingConstraints map = new MappingConstraints(serviceFeasibilityCheckInput.getHardConstraints(),
118                 serviceFeasibilityCheckInput.getSoftConstraints());
119         map.serviceToServicePathConstarints();
120         mappingPCRequest("no name", map.getServicePathHardConstraints(), map.getServicePathSoftConstraints(),
121                 serviceFeasibilityCheckInput.getSdncRequestHeader(), resvResource);
122         setSuccess(false);
123         setError("");
124     }
125
126     /*
127      * MappingAndSendingPCRequest Class constructor for RPC serviceCreate.
128      *
129      * @param rpcRegistry
130      *            RpcProviderRegistry
131      * @param serviceCreateInput
132      *            ServiceCreateInput
133      * @param resvResource
134      *            Boolean to reserve resource
135      */
136     public MappingAndSendingPCRequest(RpcProviderRegistry rpcRegistry, ServiceCreateInput serviceCreateInput,
137             Boolean resvResource) {
138         this.rpcRegistry = rpcRegistry;
139         if (rpcRegistry != null) {
140             service = rpcRegistry.getRpcService(StubpceService.class);
141         }
142         MappingConstraints map = new MappingConstraints(serviceCreateInput.getHardConstraints(),
143                 serviceCreateInput.getSoftConstraints());
144         map.serviceToServicePathConstarints();
145         mappingPCRequest(serviceCreateInput.getServiceName(), map.getServicePathHardConstraints(),
146                 map.getServicePathSoftConstraints(), serviceCreateInput.getSdncRequestHeader(), resvResource);
147         setSuccess(false);
148         setError("");
149     }
150
151     /*
152      * MappingAndSendingPCRequest Class constructor for modify Service in ODL
153      * Datastore.
154      *
155      * @param rpcRegistry
156      *            RpcProviderRegistry
157      * @param input
158      *            Services
159      * @param resvResource
160      *            Boolean to reserve resource
161      */
162     public MappingAndSendingPCRequest(RpcProviderRegistry rpcRegistry, Services input, Boolean resvResource) {
163         this.rpcRegistry = rpcRegistry;
164         if (rpcRegistry != null) {
165             service = rpcRegistry.getRpcService(StubpceService.class);
166         }
167         MappingConstraints map = new MappingConstraints(input.getHardConstraints(), input.getSoftConstraints());
168         map.serviceToServicePathConstarints();
169         mappingPCRequest(input.getServiceName(), map.getServicePathHardConstraints(),
170                 map.getServicePathSoftConstraints(), input.getSdncRequestHeader(), resvResource);
171         setSuccess(false);
172         setError("");
173     }
174
175     /*
176      * Build pathComputationRequestInput or cancelResourceReserveInput with
177      * input parameters (serviceReconfigureInput or serviceFeasibilityCheckInput.
178      *
179      * @param String
180      *            serviceName
181      * @param HardConstraints
182      *            hardConstraints
183      * @param SoftConstraints
184      *            softConstraints
185      * @param SdncRequestHeader
186      *            sdncRequestHeader
187      * @param Boolean
188      *            resvResource
189      */
190     private void mappingPCRequest(String serviceName, HardConstraints hardConstraints, SoftConstraints softConstraints,
191             org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header
192             .SdncRequestHeader sdncRequestHeader, Boolean resvResource) {
193
194         LOG.info("Mapping ServiceCreateInput or ServiceFeasibilityCheckInput or serviceReconfigureInput "
195                 + "to PCE requests");
196
197         HardConstraints serviceCreateHard = hardConstraints;
198         SoftConstraints serviceCreateSoft = softConstraints;
199         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing
200             .constraints.sp.HardConstraints pceHardConstraints = null;
201         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing
202             .constraints.sp.SoftConstraints pceSoftConstraints = null;
203         if (serviceCreateHard != null) {
204             pceHardConstraints = serviceCreateHard;
205         }
206         if (serviceCreateSoft != null) {
207             pceSoftConstraints = serviceCreateSoft;
208         }
209
210         ServiceHandlerHeaderBuilder serviceHandlerHeader = new ServiceHandlerHeaderBuilder();
211         if (sdncRequestHeader != null) {
212             serviceHandlerHeader.setRequestId(sdncRequestHeader.getRequestId());
213         }
214
215         /* PathComputationRequestInput build */
216         pathComputationRequestInput = new PathComputationRequestInputBuilder().setServiceName(serviceName)
217                 .setResourceReserve(resvResource).setServiceHandlerHeader(serviceHandlerHeader.build())
218                 .setHardConstraints(pceHardConstraints).setSoftConstraints(pceSoftConstraints)
219                 .setPceMetric(PceMetric.TEMetric).build();
220
221         /* CancelResourceReserveInput build */
222         cancelResourceReserveInput = new CancelResourceReserveInputBuilder().setServiceName(serviceName)
223                 .setServiceHandlerHeader(serviceHandlerHeader.build()).build();
224     }
225
226     /* Send cancelResourceReserve request to PCE.
227      *
228      * @return CancelResourceReserveOutput data response from PCE
229      */
230     public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve() {
231         setSuccess(false);
232         return executor.submit(new Callable<CancelResourceReserveOutput>() {
233
234             @Override
235             public CancelResourceReserveOutput call() throws Exception {
236                 CancelResourceReserveOutput output = null;
237                 if (cancelResourceReserveInput != null) {
238
239                     RpcResult<CancelResourceReserveOutput> pceOutputResult = null;
240                     Future<RpcResult<CancelResourceReserveOutput>> pceOutputFuture = service
241                             .cancelResourceReserve(cancelResourceReserveInput);
242
243                     try {
244                         pceOutputResult = pceOutputFuture.get();
245                     } catch (InterruptedException | CancellationException | ExecutionException e) {
246                         setError("Did not receive the expected response from pce to cancelResourceReserve RPC "
247                                 + e.toString());
248                         LOG.error(error);
249                         pceOutputFuture.cancel(true);
250                     }
251
252                     if (pceOutputResult != null && pceOutputResult.isSuccessful()) {
253                         LOG.info("PCE replied to CancelResource request !");
254                         CancelResourceReserveOutput pceOutput = pceOutputResult.getResult();
255                         output = new CancelResourceReserveOutputBuilder()
256                                 .setConfigurationResponseCommon(pceOutput.getConfigurationResponseCommon()).build();
257                         setSuccess(true);
258                     }
259                 } else {
260                     LOG.info("cancelResourceReserveInput parameter not valid !");
261                 }
262                 return output;
263             }
264         });
265
266     }
267
268     public Boolean getSuccess() {
269         return success;
270     }
271
272     public void setSuccess(Boolean success) {
273         this.success = success;
274     }
275
276     public String getError() {
277         return error;
278     }
279
280     public void setError(String error) {
281         this.error = error;
282     }
283
284     /*Send pathComputationRequest request to PCE.
285      *
286      * @return PathComputationRequestOutput data response from PCE
287      */
288     public ListenableFuture<PathComputationRequestOutput> pathComputationRequest() {
289         LOG.info("In pathComputationRequest ...");
290         setSuccess(false);
291         return executor.submit(new Callable<PathComputationRequestOutput>() {
292             @Override
293             public PathComputationRequestOutput call() throws Exception {
294                 RpcResult<PathComputationRequestOutput> pceOutputResult = null;
295                 PathComputationRequestOutput output = null;
296                 if (pathComputationRequestInput != null) {
297                     Future<RpcResult<PathComputationRequestOutput>> pceOutputFuture = service
298                             .pathComputationRequest(pathComputationRequestInput);
299                     try {
300                         pceOutputResult = pceOutputFuture.get();// wait to get
301                                                                 // the result
302                     } catch (InterruptedException | CancellationException | ExecutionException e) {
303                         setError("Did not receive the expected response from pce to pathComputationRequest RPC "
304                                 + e.toString());
305                         LOG.error(error);
306                         pceOutputFuture.cancel(true);
307                     }
308
309                     if (pceOutputResult != null && pceOutputResult.isSuccessful()) {
310                         setSuccess(true);
311                         LOG.info("PCE replied to pathComputation request !");
312                         PathComputationRequestOutput pceOutput = pceOutputResult.getResult();
313                         output = new PathComputationRequestOutputBuilder()
314                                 .setConfigurationResponseCommon(pceOutput.getConfigurationResponseCommon())
315                                 .setResponseParameters(pceOutput.getResponseParameters()).build();
316                     }
317                 } else {
318                     LOG.info("pathComputationRequestInput parameter not valid !");
319                 }
320                 return output;
321             }
322         });
323
324     }
325 }