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