Remove javadoc warnings
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / impl / ServicehandlerImpl.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.impl;
10
11 import com.google.common.base.Optional;
12
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.Future;
24
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.transportpce.servicehandler.CheckCoherencyHardSoft;
32 import org.opendaylight.transportpce.servicehandler.MappingAndSendingPCRequest;
33 import org.opendaylight.transportpce.servicehandler.MappingAndSendingSIRequest;
34 import org.opendaylight.transportpce.servicehandler.ServicehandlerCompliancyCheck;
35 import org.opendaylight.transportpce.servicehandler.ServicehandlerTxRxCheck;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServiceRpcResultSh;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServiceRpcResultShBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServiceStateModifyInput;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServiceStateModifyOutput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServiceStateModifyOutputBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev170930.ServicehandlerService;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutput;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutputBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathRpcResult;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.StubpceListener;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceRpcResultSp;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.StubrendererListener;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ConnectionType;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.RpcActions;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ServiceEndpoint;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ServiceNotificationTypes;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommon;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeader;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeaderBuilder;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.ServiceAEnd;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.ServiceAEndBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.ServiceZEnd;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.ServiceZEndBuilder;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.Topology;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.TopologyBuilder;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.LifecycleState;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.RpcStatus;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.routing.constraints.HardConstraints;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.routing.constraints.SoftConstraints;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationInput;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationOutput;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationInput;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationOutput;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.OrgOpenroadmServiceService;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutput;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutputBuilder;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutput;
77 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutputBuilder;
78 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInput;
79 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutput;
80 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutputBuilder;
81 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
82 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
83 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureInput;
84 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureOutput;
85 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureOutputBuilder;
86 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmInput;
87 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmOutput;
88 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteInput;
89 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteOutput;
90 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationInput;
91 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationOutput;
92 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationOutputBuilder;
93 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionInput;
94 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionOutput;
95 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollInput;
96 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollOutput;
97 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
98 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateOutput;
99 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteInput;
100 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteOutput;
101 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
102 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
103 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
104 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.AToZ;
105 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.AToZBuilder;
106 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.AToZKey;
107 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.ZToA;
108 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.ZToABuilder;
109 import org.opendaylight.yang.gen.v1.http.org.openroadm.topology.rev161014.topology.ZToAKey;
110 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
111 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response.parameters.sp.response.parameters.PathDescription;
112 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response.parameters.sp.response.parameters.PathDescriptionBuilder;
113 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.service.rpc.result.sp.PathTopology;
114 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.service.rpc.result.sp.PathTopologyBuilder;
115 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
116 import org.opendaylight.yangtools.yang.common.RpcResult;
117 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
118 import org.slf4j.Logger;
119 import org.slf4j.LoggerFactory;
120
121
122 /**
123  * Class to implement ServicehandlerService and ServicehandlerListener.
124  *
125  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on behalf of Orange
126  */
127 public class ServicehandlerImpl implements OrgOpenroadmServiceService, ServicehandlerService, StubpceListener,
128     StubrendererListener,AutoCloseable {
129     /** Logging. */
130     private static final Logger LOG = LoggerFactory.getLogger(ServicehandlerImpl.class);
131     /** Permit to access database. */
132     private DataBroker db;
133     /** check service sdnc-request-header compliancy. */
134     private ServicehandlerCompliancyCheck compliancyCheck;
135     /** check missing info on Tx/Rx for A/Z end. */
136     private ServicehandlerTxRxCheck txrxCheck;
137     /** check coherency between hard and soft constraints. */
138     private CheckCoherencyHardSoft checkCoherencyHardSoft;
139     /**
140      * Map and Send PCE requests : -
141      * path-computation-request/cancel-resource-reserve.
142      */
143     private MappingAndSendingPCRequest mappingAndSendingPCRequest;
144     /**
145      * Map and Send Service Implemention requests : - service
146      * implementation/service delete.
147      */
148     private MappingAndSendingSIRequest mappingAndSendingSIRequest;
149
150     private RpcProviderRegistry rpcRegistry;
151     private NotificationPublishService notificationPublishService;
152     private final ListeningExecutorService executor;
153
154     private ServicePathRpcResult servicePathRpcResult = null;
155     private ServiceRpcResultSp serviceRpcResultSp = null;
156
157     private RpcActions action;
158
159     private PathDescription pathDescription;
160     private PathTopology pathTopology;
161     private ServiceCreateInput serviceCreateInput;
162     private ServiceDeleteInput serviceDeleteInput;
163     private ServiceReconfigureInput serviceReconfigureInput;
164     private Services service;
165     private ServiceFeasibilityCheckInput serviceFeasibilityCheckInput;
166
167     public ServicehandlerImpl(DataBroker databroker, RpcProviderRegistry rpcRegistry,
168             NotificationPublishService notificationPublishService) {
169         this.db = databroker;
170         this.rpcRegistry = rpcRegistry;
171         this.notificationPublishService = notificationPublishService;
172         executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
173         serviceCreateInput = null;
174         setServiceDeleteInput(null);
175         setServiceReconfigureInput(null);
176         initializeDataTree(db);
177     }
178
179     /**
180      * delete service from
181      * datastore after receiving
182      * Stubrenderer notification.
183      *
184      */
185     private void deleteServiceFromDatastore() {
186         String serviceName = null;
187         if (serviceDeleteInput != null) {
188             LOG.info("deleteServiceFromDatastore came from RPC serviceDelete");
189             serviceName = serviceDeleteInput.getServiceDeleteReqInfo().getServiceName();
190         } else if (service != null) {
191             LOG.info("deleteServiceFromDatastore came from RPC serviceRestoration");
192             serviceName = service.getServiceName();
193         }
194         if (serviceName != null) {
195             LOG.info("deleting service '{}' from datastore ...", serviceName);
196             ServiceRpcResultSh notification = null;
197             String message = "";
198             /**
199              * Service delete confirmed deleting service from
200              * database
201              */
202             if (writeOrModifyOrDeleteServiceList(serviceName, null,
203                     null,1) == null) {
204                 /** Service delete. */
205                 message = "Service deleted from database";
206             } else {
207                 message = "deleting service from database failed !";
208             }
209             LOG.info(message);
210             notification = new ServiceRpcResultShBuilder()
211                     .setNotificationType(ServiceNotificationTypes.ServiceDeleteResult)
212                     .setServiceName(serviceDeleteInput.getServiceDeleteReqInfo().getServiceName())
213                     .setStatus(RpcStatusEx.Successful).setStatusMessage(message).build();
214             try {
215                 notificationPublishService.putNotification(notification);
216             } catch (InterruptedException e) {
217                 LOG.info("notification offer rejected : {}", e);
218             }
219         } else {
220             LOG.error("Parameter 'ServiceName' fro deleteServiceFromDatastore is null !");
221         }
222     }
223
224
225     /**
226      *Put Service status to up
227      *and add topology information
228      *after receiving Stubrenderer
229      *service implementation
230      *notification.
231      *
232      * @param input ServiceCreateInput or
233      */
234     private <T> void updateServiceStatus(T input) {
235         LOG.info("Updating Service Status ...");
236         ServiceRpcResultSh notification = null;
237         String message = "";
238         String serviceName = null;
239         ServiceNotificationTypes notif = null;
240         if (input instanceof ServiceCreateInput) {
241             LOG.info("Updating Service Status came from RPC serviceCreateInput ...");
242             serviceName = serviceCreateInput.getServiceName();
243             notif = ServiceNotificationTypes.ServiceCreateResult;
244         } else if (input instanceof ServiceReconfigureInput) {
245             LOG.info("Updating Service Status came from RPC serviceReconfigure ...");
246             serviceName = serviceReconfigureInput.getNewServiceName();
247             notif = ServiceNotificationTypes.ServiceReconfigureResult;
248         } else if (input instanceof Services) {
249             LOG.info("Updating Service Status came from RPC serviceRestoration ...");
250             serviceName = service.getServiceName();
251             notif = ServiceNotificationTypes.ServiceRestorationResult;
252         }
253         if (serviceName != null && notif != null) {
254             if (pathTopology != null) {
255                 LOG.info("PathTopology contains in Stubrenderer notification received !");
256                 Topology topo = new TopologyBuilder()
257                         .setAToZ(pathTopology.getAToZ())
258                         .setZToA(pathTopology.getZToA())
259                         .build();
260
261                 /**
262                  * Service implemented setting
263                  * Service op status to up.
264                  */
265                 if (writeOrModifyOrDeleteServiceList(serviceName, null,topo,0) == null) {
266                     /**
267                      * Service modified.
268                      */
269                     StringBuilder build = new StringBuilder();
270                     build.append(message);
271                     build.append(" : Service Op Status changed to Up !");
272                     message = build.toString();
273                 } else {
274                     StringBuilder build = new StringBuilder();
275                     build.append(message);
276                     build.append(" but Failed to modify service from Service List !");
277                     message = build.toString();
278                 }
279                 notification = new ServiceRpcResultShBuilder()
280                         .setNotificationType(notif)
281                         .setServiceName(serviceName)
282                         .setStatus(RpcStatusEx.Successful).setStatusMessage(message)
283                         .build();
284                 try {
285                     notificationPublishService.putNotification(notification);
286                 } catch (InterruptedException e) {
287                     LOG.info("notification offer rejected : {}", e);
288                 }
289             } else {
290                 message = "pathTopology not in stubrenderer notification, cancelling pce resource reserve ...";
291                 LOG.info(message);
292                 notification = new ServiceRpcResultShBuilder()
293                         .setNotificationType(ServiceNotificationTypes.ServiceCreateResult)
294                         .setServiceName(serviceCreateInput.getServiceName())
295                         .setStatus(RpcStatusEx.Failed)
296                         .setStatusMessage("message")
297                         .build();
298                 try {
299                     notificationPublishService.putNotification(notification);
300                 } catch (InterruptedException e) {
301                     LOG.info("notification offer rejected : {}", e);
302                 }
303                 pceCancelResResource();
304             }
305         } else {
306             LOG.info("Parameters 'serviceName' or/ and 'notiftype' is null");
307         }
308     }
309
310     /**
311      * Send pathComputation
312      * request to PCE.
313      *
314      * @param input ServiceCreate or ServiceReconfigure or Services
315      */
316     private <T> void  pcePathComputation(T input) {
317         LOG.info("sending pathcomputation request to pce ...");
318         ServiceRpcResultSh notification = null;
319         String serviceName = null;
320         ServiceNotificationTypes type = null;
321         /**
322          * Before sending the PCE request, input data need to be
323          * formatted according to the Service Handler PCE
324          * interface data model.
325          */
326         if (input instanceof ServiceReconfigureInput) {
327             LOG.info("PCR came from RPC serviceReconfigure ...");
328             ServiceReconfigureInput tmp = (ServiceReconfigureInput)input;
329             serviceName = tmp.getNewServiceName();
330             type = ServiceNotificationTypes.ServiceReconfigureResult;
331             mappingAndSendingPCRequest = new MappingAndSendingPCRequest(rpcRegistry, tmp, true);
332         } else if (input instanceof ServiceCreateInput) {
333             LOG.info("PCR came from RPC serviceCreate ...");
334             ServiceCreateInput tmp = (ServiceCreateInput)input;
335             serviceName = tmp.getServiceName();
336             type = ServiceNotificationTypes.ServiceCreateResult;
337             mappingAndSendingPCRequest = new MappingAndSendingPCRequest(rpcRegistry, tmp, true);
338         } else if (input instanceof Services) {
339             LOG.info("PCR came from RPC serviceRestoration ...");
340             Services tmp = (Services)input;
341             serviceName = tmp.getServiceName();
342             type = ServiceNotificationTypes.ServiceRestorationResult;
343             mappingAndSendingPCRequest = new MappingAndSendingPCRequest(rpcRegistry, tmp, true);
344         } else if (input instanceof ServiceFeasibilityCheckInput) {
345             LOG.info("PCR came from RPC ServiceFeasibilityCheck ...");
346             ServiceFeasibilityCheckInput tmp = (ServiceFeasibilityCheckInput)input;
347             serviceName = "no name";
348             type = ServiceNotificationTypes.ServiceCreateResult;
349             mappingAndSendingPCRequest = new MappingAndSendingPCRequest(rpcRegistry, tmp, false);
350         }
351         final String name = serviceName;
352         final ServiceNotificationTypes notifType = type;
353
354         /**
355          * Once PCE request is being sent to the PCE on
356          * interface B, PCE reply is expected until a timer
357          * expires.
358          */
359         notification = new ServiceRpcResultShBuilder()
360                 .setNotificationType(notifType)
361                 .setServiceName(serviceName).setStatus(RpcStatusEx.Pending)
362                 .setStatusMessage("Service compliant, submitting PathComputation Request ...").build();
363         try {
364             notificationPublishService.putNotification(notification);
365         } catch (InterruptedException e) {
366             LOG.info("notification offer rejected : {}", e);
367         }
368         FutureCallback<Boolean> pceCallback =  new FutureCallback<Boolean>() {
369             String message = "";
370             ServiceRpcResultSh notification = null;
371
372             @Override
373             public void onSuccess(Boolean response) {
374                 if (response) {
375                     /**
376                      * If PCE reply is received before timer
377                      * expiration with a positive result, a
378                      * service is created with admin and
379                      * operational status 'down'.
380                      */
381                     message = "PCE replied to PCR Request !";
382                     LOG.info(message);
383                     notification = new ServiceRpcResultShBuilder()
384                             .setNotificationType(notifType)
385                             .setServiceName(name).setStatus(RpcStatusEx.Pending)
386                             .setStatusMessage(message).build();
387                     try {
388                         notificationPublishService.putNotification(notification);
389                     } catch (InterruptedException e) {
390                         LOG.info("notification offer rejected : {}", e);
391                     }
392                 } else {
393                     message = mappingAndSendingPCRequest.getError();
394                     notification = new ServiceRpcResultShBuilder()
395                             .setNotificationType(notifType)
396                             .setServiceName("").setStatus(RpcStatusEx.Failed).setStatusMessage(message)
397                             .build();
398                     try {
399                         notificationPublishService.putNotification(notification);
400                     } catch (InterruptedException e) {
401                         LOG.info("notification offer rejected : {}", e);
402                     }
403                 }
404             }
405
406             @Override
407             public void onFailure(Throwable arg0) {
408                 LOG.error("Path not calculated..");
409                 notification = new ServiceRpcResultShBuilder()
410                         .setNotificationType(notifType)
411                         .setServiceName(name).setStatus(RpcStatusEx.Failed)
412                         .setStatusMessage("PCR Request failed  : " + arg0.getMessage()).build();
413                 try {
414                     notificationPublishService.putNotification(notification);
415                 } catch (InterruptedException e) {
416                     LOG.info("notification offer rejected : {}", e);
417                 }
418
419             }
420         };
421         ListenableFuture<Boolean> pce = mappingAndSendingPCRequest.pathComputationRequest();
422         Futures.addCallback(pce, pceCallback, executor);
423     }
424
425     /**
426      * Send RPC cancel reserve
427      * resource to PCE.
428      */
429     private void pceCancelResResource() {
430         LOG.info("sending RPC cancel reserve resource to PCE ...");
431         Services pceService = null;
432         ServiceNotificationTypes notif = null;
433         if (serviceDeleteInput != null) {
434             LOG.info("pceCancelResResource came from RPC serviceDelete");
435             notif = ServiceNotificationTypes.ServiceDeleteResult;
436             String serviceName = serviceDeleteInput.getServiceDeleteReqInfo().getServiceName();
437             if (serviceName != null) {
438                 pceService = readServiceList(serviceName);
439             } else {
440                 LOG.info("Parameter 'serviceName' for pceCancelResResource is null");
441             }
442         } else if (service != null) {
443             notif = ServiceNotificationTypes.ServiceRestorationResult;
444             LOG.info("pceCancelResResource came from RPC serviceRestoration");
445             pceService = service;
446         } else if (serviceReconfigureInput != null) {
447             notif = ServiceNotificationTypes.ServiceReconfigureResult;
448             LOG.info("pceCancelResResource came from RPC serviceReconfigure");
449             String serviceName = serviceReconfigureInput.getServiceName();
450             if (serviceName != null) {
451                 pceService = readServiceList(serviceName);
452             } else {
453                 LOG.info("Parameter 'serviceName' for pceCancelResResource is null");
454             }
455         } else if (serviceCreateInput != null) {
456             notif = ServiceNotificationTypes.ServiceCreateResult;
457             LOG.info("pceCancelResResource came from RPC serviceCreate");
458             String serviceName = serviceCreateInput.getServiceName();
459             if (serviceName != null) {
460                 pceService = readServiceList(serviceName);
461             } else {
462                 LOG.info("Parameter 'serviceName' for pceCancelResResource is null");
463             }
464         }
465         if (pceService != null && notif != null) {
466             final Services cancelService = pceService;
467             final ServiceNotificationTypes type = notif;
468             mappingAndSendingPCRequest = new MappingAndSendingPCRequest(rpcRegistry, pceService, false);
469             FutureCallback<Boolean> pceCallback =  new FutureCallback<Boolean>() {
470                 String message = "";
471                 ServiceRpcResultSh notification = null;
472                 @Override
473                 public void onSuccess(Boolean response) {
474                     if (response) {
475                         /**
476                          * If PCE reply is received before timer
477                          * expiration with a positive result, a
478                          * service is created with admin and
479                          * operational status 'down'.
480                          */
481                         message = "PCE replied to cancel resource Request !";
482                         LOG.info(message);
483                         notification = new ServiceRpcResultShBuilder()
484                                 .setNotificationType(type)
485                                 .setServiceName(cancelService.getServiceName()).setStatus(RpcStatusEx.Pending)
486                                 .setStatusMessage(message).build();
487                         try {
488                             notificationPublishService.putNotification(notification);
489                         } catch (InterruptedException e) {
490                             LOG.info("notification offer rejected : {}", e);
491                         }
492                     } else {
493                         message = mappingAndSendingPCRequest.getError();
494                         notification = new ServiceRpcResultShBuilder()
495                                 .setNotificationType(type)
496                                 .setServiceName("").setStatus(RpcStatusEx.Failed).setStatusMessage(message)
497                                 .build();
498                         try {
499                             notificationPublishService.putNotification(notification);
500                         } catch (InterruptedException e) {
501                             LOG.info("notification offer rejected : {}", e);
502                         }
503                     }
504                 }
505
506                 @Override
507                 public void onFailure(Throwable arg0) {
508                     message = "Cancel resource request failed !";
509                     LOG.error(message);
510                     notification = new ServiceRpcResultShBuilder()
511                             .setNotificationType(type)
512                             .setServiceName(cancelService.getServiceName()).setStatus(RpcStatusEx.Failed)
513                             .setStatusMessage(message + " : " + arg0.getMessage()).build();
514                     try {
515                         notificationPublishService.putNotification(notification);
516                     } catch (InterruptedException e) {
517                         LOG.info("notification offer rejected : {}", e);
518                     }
519
520                 }
521             };
522             ListenableFuture<Boolean> pce = mappingAndSendingPCRequest.cancelResourceReserve();
523             Futures.addCallback(pce, pceCallback, executor);
524         }
525     }
526
527     private void stubrendererDelete() {
528         LOG.info("sending RPC service delete to stubrenderer ...");
529         String tmp = null;
530         String id = null;
531         if (service != null) {
532             LOG.info("RPC service delete came from RPC serviceRestoration !");
533             tmp = service.getServiceName();
534             id = service.getCommonId();
535         } else if (serviceDeleteInput != null) {
536             LOG.info("RPC service delete came from ServiceDelete !");
537             tmp = serviceDeleteInput.getServiceDeleteReqInfo().getServiceName();
538             id = serviceDeleteInput.getSdncRequestHeader().getRequestId();
539         }
540
541         if (tmp != null && id != null) {
542             final String serviceName = tmp;
543             LOG.info("stubrendererDelete service '{}'", serviceName);
544             mappingAndSendingSIRequest = new MappingAndSendingSIRequest(rpcRegistry, id, serviceName);
545             ListenableFuture<Boolean> renderer = mappingAndSendingSIRequest.serviceDelete();
546             FutureCallback<Boolean> rendererCallback = new FutureCallback<Boolean>() {
547                 String message = "";
548                 ServiceRpcResultSh notification = null;
549
550                 @Override
551                 public void onFailure(Throwable arg0) {
552                     message = "ServiceDelete Request failed : " + arg0;
553                     LOG.error("ServiceDelete Request failed !");
554                     notification = new ServiceRpcResultShBuilder()
555                             .setNotificationType(ServiceNotificationTypes.ServiceRestorationResult)
556                             .setServiceName(serviceName).setStatus(RpcStatusEx.Failed)
557                             .setStatusMessage(message).build();
558                     try {
559                         notificationPublishService.putNotification(notification);
560                     } catch (InterruptedException e) {
561                         LOG.info("notification offer rejected : {}", e);
562                     }
563
564                 }
565
566                 @Override
567                 public void onSuccess(Boolean response) {
568                     if (response) {
569                         message = "Service deleted !";
570                         notification = new ServiceRpcResultShBuilder()
571                                 .setNotificationType(ServiceNotificationTypes.ServiceRestorationResult)
572                                 .setServiceName(serviceName).setStatus(RpcStatusEx.Pending)
573                                 .setStatusMessage(message).build();
574                         try {
575                             notificationPublishService.putNotification(notification);
576                         } catch (InterruptedException e) {
577                             LOG.info("notification offer rejected : {}", e);
578                         }
579                     } else {
580                         message = "deleting service failed !";
581                         notification = new ServiceRpcResultShBuilder()
582                                 .setNotificationType(ServiceNotificationTypes.ServiceRestorationResult)
583                                 .setServiceName(serviceName).setStatus(RpcStatusEx.Failed)
584                                 .setStatusMessage(message)
585                                 .build();
586                         try {
587                             notificationPublishService.putNotification(notification);
588                         } catch (InterruptedException e) {
589                             LOG.info("notification offer rejected : {}", e);
590                         }
591                     }
592                 }
593             };
594             Futures.addCallback(renderer, rendererCallback, executor);
595         } else {
596             LOG.info("Parameter 'serviceName' and / or 'id' is null");
597         }
598     }
599
600     /**
601      * send a RPC serviceImplementation
602      * to stubrenderer after
603      * receiving a stubpce notification.
604      *
605      * @param input ServiceCreate or ServiceReconfigure
606      */
607     private <T> void stubrendererImplementation(T input) {
608         ServiceRpcResultSh notification = null;
609         String serviceName = null;
610         String message = "";
611         String newServiceName = null;
612         ServiceNotificationTypes type = null;
613         Boolean create = false;
614         Boolean delete = true;
615         if (pathDescription != null) {
616             LOG.info("Pathdescription conatins in Stubpce notification received !");
617             String result = null;
618             PathComputationRequestOutput pathComputationResponse =
619                     new PathComputationRequestOutputBuilder()
620                     .setResponseParameters(new org.opendaylight.yang.gen.v1.http.org
621                             .transportpce.b.c._interface.service.types.rev170426.response
622                             .parameters.sp.ResponseParametersBuilder()
623                             .setPathDescription(pathDescription)
624                             .build())
625                     .build();
626             if (input instanceof ServiceReconfigureInput) {
627                 /** delete and write . */
628                 LOG.info("RPC serviceImplementation came from RPC serviceReconfigure ...");
629                 ServiceReconfigureInput tmp = (ServiceReconfigureInput)input;
630                 serviceName = tmp.getServiceName();
631                 newServiceName = tmp.getNewServiceName();
632                 type = ServiceNotificationTypes.ServiceReconfigureResult;
633                 delete = false;
634                 mappingAndSendingSIRequest = new MappingAndSendingSIRequest(rpcRegistry, tmp,pathComputationResponse);
635             } else if (input instanceof ServiceCreateInput) {
636                 /** create. */
637                 LOG.info("RPC serviceImplementation came from RPC serviceCreate ...");
638                 ServiceCreateInput tmp = (ServiceCreateInput)input;
639                 serviceName = tmp.getServiceName();
640                 type = ServiceNotificationTypes.ServiceCreateResult;
641                 mappingAndSendingSIRequest = new MappingAndSendingSIRequest(rpcRegistry, tmp,pathComputationResponse);
642                 delete = true;
643                 create = true;
644             } else if (input instanceof Services) {
645                 /** update. */
646                 LOG.info("RPC serviceImplementation came from RPC ServiceRestoration ...");
647                 Services tmp = new ServicesBuilder((Services)input)
648                         .setAdministrativeState(State.OutOfService)
649                         .setOperationalState(State.OutOfService)
650                         .setLifecycleState(LifecycleState.Planned)
651                         .build();
652                 serviceName = tmp.getServiceName();
653                 type = ServiceNotificationTypes.ServiceRestorationResult;
654                 mappingAndSendingSIRequest = new MappingAndSendingSIRequest(rpcRegistry, tmp, pathComputationResponse);
655                 delete = true;
656                 create = true;
657             }
658             final String name = serviceName;
659             final ServiceNotificationTypes notifType = type;
660             if (!create) { /** ServiceReconfigure. */
661                 if ((result = writeOrModifyOrDeleteServiceList(serviceName, pathComputationResponse,null, 1)) == null) {
662                     LOG.info("Service '{}' deleted from datastore", serviceName);
663                     serviceName = newServiceName;
664                     delete = true;
665                 } else {
666                     LOG.info("deleting Service '{}' failed !", serviceName);
667                 }
668             }
669             if (delete) {
670                 if ((result = writeOrModifyOrDeleteServiceList(serviceName,pathComputationResponse,null, 2)) != null) {
671                     LOG.info("writting Service failed !");
672                     StringBuilder build = new StringBuilder();
673                     build.append(message);
674                     build.append(" " + result);
675                     message = build.toString();
676                 } else {
677                     /**
678                      * Send Implementation order to renderer
679                      */
680                     notification = new ServiceRpcResultShBuilder()
681                             .setNotificationType(notifType)
682                             .setServiceName(name)
683                             .setStatus(RpcStatusEx.Pending)
684                             .setStatusMessage("Submitting ServiceImplementation Request ...")
685                             .build();
686                     try {
687                         notificationPublishService.putNotification(notification);
688                     } catch (InterruptedException e) {
689                         LOG.info("notification offer rejected : {}", e);
690                     }
691
692                     FutureCallback<Boolean> rendererCallback = new FutureCallback<Boolean>() {
693                         String message = "";
694                         ServiceRpcResultSh notification = null;
695
696                         @Override
697                         public void onSuccess(Boolean response) {
698                             if (response) {
699                                 /**
700                                  * If stubrenderer reply is received before timer
701                                  * expiration with a positive result, a
702                                  * service is created with admin and
703                                  * operational status 'down'.
704                                  */
705                                 message = "StubRenderer replied to Request !";
706                                 LOG.info(message);
707                                 notification = new ServiceRpcResultShBuilder()
708                                         .setNotificationType(notifType)
709                                         .setServiceName(name).setStatus(RpcStatusEx.Pending)
710                                         .setStatusMessage(message).build();
711                                 try {
712                                     notificationPublishService.putNotification(notification);
713                                 } catch (InterruptedException e) {
714                                     LOG.info("notification offer rejected : {}", e);
715                                 }
716                             } else {
717                                 message = mappingAndSendingSIRequest.getError();
718                                 notification = new ServiceRpcResultShBuilder()
719                                         .setNotificationType(notifType)
720                                         .setServiceName(name).setStatus(RpcStatusEx.Failed).setStatusMessage(message)
721                                         .build();
722                                 try {
723                                     notificationPublishService.putNotification(notification);
724                                 } catch (InterruptedException e) {
725                                     LOG.info("notification offer rejected : {}", e);
726                                 }
727                             }
728                         }
729
730                         @Override
731                         public void onFailure(Throwable arg0) {
732                             LOG.error("Service not implemented ...");
733                             notification = new ServiceRpcResultShBuilder()
734                                     .setNotificationType(notifType)
735                                     .setServiceName(name).setStatus(RpcStatusEx.Failed)
736                                     .setStatusMessage("Service implementation failed  : " + arg0.getMessage()).build();
737                             try {
738                                 notificationPublishService.putNotification(notification);
739                             } catch (InterruptedException e) {
740                                 LOG.info("notification offer rejected : {}", e);
741                             }
742                         }
743                     };
744                     ListenableFuture<Boolean> renderer = mappingAndSendingSIRequest.serviceImplementation();
745                     Futures.addCallback(renderer, rendererCallback, executor);
746                 }
747             } else {
748                 LOG.info("deleting Service failed");
749             }
750         } else {
751             message = "PathDescription contains in Stubpce notification "
752                     + "not recieved !";
753             LOG.info(message);
754         }
755     }
756
757     /**
758      * Checking Service Compliancy.
759      *
760      * @return String if not compliant, null else
761      */
762     private String serviceCompliancy(SdncRequestHeader sdncRequestHeader, String serviceName,
763             ConnectionType connectionType, RpcActions rpcActions, ServiceEndpoint aend, ServiceEndpoint zend,
764             String commonIdValue, HardConstraints hard, SoftConstraints soft) {
765         String message = null;
766         Boolean contype = false;
767         Boolean sdncRequest = false;
768         Boolean commonId = true;
769         Boolean coherencyHardSoft = false;
770
771         if (rpcActions.equals(RpcActions.ServiceCreate) || rpcActions.equals(RpcActions.ServiceFeasibilityCheck)
772                 || rpcActions.equals(RpcActions.ServiceDelete)) {
773             sdncRequest = true;
774         } else if (rpcActions.equals(RpcActions.ServiceCreate)
775                 || rpcActions.equals(RpcActions.ServiceFeasibilityCheck)
776                 || rpcActions.equals(RpcActions.ServiceReconfigure)) {
777             contype = true;
778         }
779         compliancyCheck = new ServicehandlerCompliancyCheck(sdncRequestHeader, serviceName,
780                 connectionType, rpcActions);
781         if (compliancyCheck.check(contype, sdncRequest)) {
782             LOG.info("Service compliant !");
783             /**
784              * If compliant, service-request parameters are verified in order to
785              * check if there is no missing parameter that prevents calculating
786              * a path and implement a service.
787              */
788             LOG.info("checking Tx/Rx Info for AEnd ...");
789             txrxCheck = new ServicehandlerTxRxCheck(aend, 1);
790             if (txrxCheck.check()) {
791                 LOG.info("Tx/Rx Info for AEnd checked !");
792                 LOG.info("checking Tx/Rx Info for ZEnd ...");
793                 txrxCheck = new ServicehandlerTxRxCheck(zend, 2);
794                 if (txrxCheck.check()) {
795                     LOG.info("Tx/Rx Info for ZEnd checked");
796                     /**
797                      * If OK, common-id is verified in order to see if there is
798                      * no routing policy provided. If yes, the routing
799                      * constraints of the policy are recovered and coherency
800                      * with hard/soft constraints provided in the input of the
801                      * RPC.
802                      */
803                     if (commonIdValue != null) {
804                         LOG.info("Common-id specified");
805                         /**
806                          * Check coherency with hard/soft constraints.
807                          */
808                         checkCoherencyHardSoft = new CheckCoherencyHardSoft(hard,soft);
809                         if (checkCoherencyHardSoft.check()) {
810                             LOG.info("hard/soft constraints coherent !");
811                             coherencyHardSoft = true;
812                         } else {
813                             LOG.info("hard/soft constraints are not coherent !");
814                             message = "hard/soft constraints are not coherent !";
815                         }
816                     } else {
817                         commonId = false;
818                     }
819                     if (!commonId || (commonId && coherencyHardSoft)) {
820                         message = null;
821                     }
822                 } else {
823                     message = txrxCheck.getMessage();
824                 }
825             } else {
826                 message = txrxCheck.getMessage();
827             }
828         } else {
829             message = compliancyCheck.getMessage();
830         }
831         return message;
832     }
833
834
835     @Override
836     public Future<RpcResult<ServiceCreateOutput>> serviceCreate(ServiceCreateInput input) {
837         LOG.info("RPC service creation received");
838         pathDescription = null;
839         pathTopology = null;
840         action = RpcActions.ServiceCreate;
841         setPathDescription(null);
842         serviceCreateInput = input;
843         setServiceDeleteInput(null);
844         setServiceReconfigureInput(null);
845         service = null;
846         String message = "";
847         String responseCode = "";
848         ConfigurationResponseCommon configurationResponseCommon;
849         LOG.info("checking Service Compliancy ...");
850         /**
851          * Upon receipt of service-create RPC, service header and sdnc-request
852          * header compliancy are verified.
853          */
854         String serviceCompliancy = null;
855         if ((serviceCompliancy = serviceCompliancy(input.getSdncRequestHeader(), input.getServiceName(),
856                 input.getConnectionType(), RpcActions.ServiceCreate, input.getServiceAEnd(), input.getServiceZEnd(),
857                 input.getCommonId(), input.getHardConstraints(), input.getSoftConstraints())) != null) {
858             message = "Service not compliant : " + serviceCompliancy;
859             responseCode = "500";
860             LOG.info(message);
861         } else {
862             LOG.info("Service compliant !");
863             String name = input.getServiceName();
864             if (readServiceList(name) == null) {
865                 pcePathComputation(input);
866                 LOG.info("PCR Request in progress ");
867                 configurationResponseCommon = new ConfigurationResponseCommonBuilder()
868                         .setAckFinalIndicator("No").setRequestId(input.getSdncRequestHeader().getRequestId())
869                         .setResponseMessage("Service compliant, serviceCreate in progress...")
870                         .setResponseCode("200").build();
871
872                 ServiceCreateOutputBuilder output = new ServiceCreateOutputBuilder()
873                         .setConfigurationResponseCommon(configurationResponseCommon);
874
875                 return RpcResultBuilder.success(output.build()).buildFuture();
876             } else {
877                 LOG.info("Service '{}' already created !", name);
878                 configurationResponseCommon = new ConfigurationResponseCommonBuilder()
879                         .setAckFinalIndicator("Yes").setRequestId(input.getSdncRequestHeader().getRequestId())
880                         .setResponseMessage("Service already created !")
881                         .setResponseCode("500").build();
882
883                 ServiceCreateOutputBuilder output = new ServiceCreateOutputBuilder()
884                         .setConfigurationResponseCommon(configurationResponseCommon);
885
886                 return RpcResultBuilder.success(output.build()).buildFuture();
887             }
888         }
889         /*compliancyCheck = new ServicehandlerCompliancyCheck(input.getSdncRequestHeader(), input.getServiceName(),
890                 input.getConnectionType(), RpcActions.ServiceCreate);
891         if (compliancyCheck.check(true, true)) {
892             LOG.info("Service compliant !");
893             LOG.info("checking Tx/Rx Info for AEnd ...");
894             txrxCheck = new ServicehandlerTxRxCheck(input.getServiceAEnd(), 1);
895             if (txrxCheck.check()) {
896                 LOG.info("Tx/Rx Info for AEnd checked !");
897                 LOG.info("checking Tx/Rx Info for ZEnd ...");
898                 txrxCheck = new ServicehandlerTxRxCheck(input.getServiceZEnd(), 2);
899                 if (txrxCheck.check()) {
900                     LOG.info("Tx/Rx Info for ZEnd checked");
901                     if (input.getCommonId() != null) {
902                         LOG.info("Common-id specified");
903                         checkCoherencyHardSoft = new CheckCoherencyHardSoft(input.getHardConstraints(),
904                                 input.getSoftConstraints());
905                         if (checkCoherencyHardSoft.check()) {
906                             LOG.info("hard/soft constraints coherent !");
907                             coherencyHardSoft = true;
908                         } else {
909                             LOG.info("hard/soft constraints are not coherent !");
910                             message = "hard/soft constraints are not coherent !";
911                             responseCode = "500";
912                         }
913                     } else {
914                         commonId = false;
915                     }
916                     if (!commonId || (commonId && coherencyHardSoft)) {
917                         pcePathComputation(input);
918                         LOG.info("PCR Request in progress ");
919                         configurationResponseCommon = new ConfigurationResponseCommonBuilder()
920                                 .setAckFinalIndicator("No").setRequestId(input.getSdncRequestHeader().getRequestId())
921                                 .setResponseMessage("Service compliant, serviceCreate in progress...")
922                                 .setResponseCode("200").build();
923
924                         ServiceCreateOutputBuilder output = new ServiceCreateOutputBuilder()
925                                 .setConfigurationResponseCommon(configurationResponseCommon);
926
927                         return RpcResultBuilder.success(output.build()).buildFuture();
928                     }
929                 } else {
930                     message = txrxCheck.getMessage();
931                     responseCode = "500";
932                 }
933             } else {
934                 message = txrxCheck.getMessage();
935                 responseCode = "500";
936             }
937         } else {
938             message = compliancyCheck.getMessage();
939             responseCode = "500";
940         }*/
941
942         ConfigurationResponseCommonBuilder builder = new ConfigurationResponseCommonBuilder()
943                 .setAckFinalIndicator("Yes")
944                 .setResponseMessage(message)
945                 .setResponseCode(responseCode);
946         SdncRequestHeader sdnc = input.getSdncRequestHeader();
947         if (sdnc != null) {
948             String requestId = sdnc.getRequestId();
949             if (requestId != null) {
950                 builder.setRequestId(requestId);
951             }
952         }
953         configurationResponseCommon = builder.build();
954
955         ServiceCreateOutputBuilder output = new ServiceCreateOutputBuilder()
956                 .setConfigurationResponseCommon(configurationResponseCommon);
957
958         return RpcResultBuilder.success(output.build()).buildFuture();
959     }
960
961     @Override
962     public Future<RpcResult<ServiceDeleteOutput>> serviceDelete(ServiceDeleteInput input) {
963         LOG.info("RPC serviceDelete request received for Service '{}'", input.getServiceDeleteReqInfo()
964                 .getServiceName());
965         setServiceDeleteInput(input);
966         setServiceReconfigureInput(null);
967         serviceCreateInput = null;
968         service = null;
969         String message = "";
970         String responseCode = "";
971         LOG.info("checking Service Compliancy ...");
972         /**
973          * Upon receipt of service-delete RPC, service header and sdnc-request
974          * header compliancy are verified.
975          */
976         compliancyCheck = new ServicehandlerCompliancyCheck(input.getSdncRequestHeader(),
977                 input.getServiceDeleteReqInfo().getServiceName(), RpcActions.ServiceDelete);
978         if (compliancyCheck.check(false, true)) {
979             LOG.info("Service compliant !");
980             String serviceName = input.getServiceDeleteReqInfo().getServiceName();
981             Services service = readServiceList(serviceName);
982             if (service != null) {
983                 LOG.debug("Service '{}' present in datastore !", serviceName);
984                 stubrendererDelete();
985                 LOG.info("ServiceDelete Request in progress ... ");
986                 ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
987                         .setAckFinalIndicator("No").setRequestId(input.getSdncRequestHeader().getRequestId())
988                         .setResponseMessage("ServiceDelete Request in progress ...").setResponseCode("200").build();
989                 ServiceDeleteOutput output = new ServiceDeleteOutputBuilder()
990                         .setConfigurationResponseCommon(configurationResponseCommon).build();
991                 return RpcResultBuilder.success(output).buildFuture();
992             } else {
993                 message = "Service '" + serviceName + "' not exists in datastore";
994                 LOG.info(message);
995             }
996
997         } else {
998             message = "Service not compliant !";
999             responseCode = "500";
1000             LOG.info(message);
1001         }
1002         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
1003                 .setAckFinalIndicator("Yes").setRequestId(input.getSdncRequestHeader().getRequestId())
1004                 .setResponseMessage(message).setResponseCode(responseCode).build();
1005         ServiceDeleteOutput output = new ServiceDeleteOutputBuilder()
1006                 .setConfigurationResponseCommon(configurationResponseCommon).build();
1007         return RpcResultBuilder.success(output).buildFuture();
1008     }
1009
1010     @Override
1011     public Future<RpcResult<ServiceFeasibilityCheckOutput>> serviceFeasibilityCheck(
1012             ServiceFeasibilityCheckInput input) {
1013         action = RpcActions.ServiceFeasibilityCheck;
1014         LOG.info("RPC service feasibilityCheck received");
1015         mappingAndSendingPCRequest = null;
1016         serviceFeasibilityCheckInput = input;
1017         serviceCreateInput = null;
1018         serviceDeleteInput = null;
1019         service = null;
1020         serviceReconfigureInput = null;
1021
1022         ConfigurationResponseCommon configurationResponseCommon = null;
1023         String message = "";
1024         String responseCode = "";
1025         LOG.info("checking Service Compliancy ...");
1026         /**
1027          * Upon receipt of service-create RPC, service header and sdnc-request
1028          * header compliancy are verified.
1029          */
1030         String name = "no name";
1031         String serviceCompliancy = null;
1032         if ((serviceCompliancy = serviceCompliancy(input.getSdncRequestHeader(), name, input.getConnectionType(),
1033                 RpcActions.ServiceFeasibilityCheck, input.getServiceAEnd(), input.getServiceZEnd(), input.getCommonId(),
1034                 input.getHardConstraints(), input.getSoftConstraints())) != null) {
1035             message = "Service not compliant : " + serviceCompliancy;
1036             LOG.info(message);
1037         } else {
1038             LOG.info("Service compliant !");
1039             pcePathComputation(input);
1040             LOG.info("PCR Request in progress ");
1041             configurationResponseCommon = new ConfigurationResponseCommonBuilder()
1042                     .setAckFinalIndicator("No").setRequestId(input.getSdncRequestHeader().getRequestId())
1043                     .setResponseMessage("Service compliant, ServiceFeasibilityCheck in progress...")
1044                     .setResponseCode("200").build();
1045
1046             ServiceFeasibilityCheckOutput output = new ServiceFeasibilityCheckOutputBuilder()
1047                     .setConfigurationResponseCommon(configurationResponseCommon).build();
1048             return RpcResultBuilder.success(output).buildFuture();
1049         }
1050         /*compliancyCheck = new ServicehandlerCompliancyCheck(input.getSdncRequestHeader(), name,
1051                 input.getConnectionType(), RpcActions.ServiceFeasibilityCheck);
1052         if (compliancyCheck.check(true, true)) {
1053             LOG.info("Service compliant !");
1054             LOG.info("checking Tx/Rx Info for AEnd ...");
1055             txrxCheck = new ServicehandlerTxRxCheck(input.getServiceAEnd(), 1);
1056             if (txrxCheck.check()) {
1057                 LOG.info("Tx/Rx Info for AEnd checked !");
1058                 LOG.info("checking Tx/Rx Info for ZEnd ...");
1059                 txrxCheck = new ServicehandlerTxRxCheck(input.getServiceZEnd(), 2);
1060                 if (txrxCheck.check()) {
1061                     LOG.info("Tx/Rx Info for ZEnd checked");
1062                     if (input.getCommonId() != null) {
1063                         LOG.info("Common-id specified");
1064                         checkCoherencyHardSoft = new CheckCoherencyHardSoft(input.getHardConstraints(),
1065                                 input.getSoftConstraints());
1066                         if (checkCoherencyHardSoft.check()) {
1067                             LOG.info("hard/soft constraints coherent !");
1068                             coherencyHardSoft = true;
1069                         } else {
1070                             LOG.info("hard/soft constraints are not coherent !");
1071                             message = "hard/soft constraints are not coherent !";
1072                             responseCode = "500";
1073                         }
1074                     } else {
1075                         commonId = false;
1076                     }
1077
1078                     if (!commonId || (commonId && coherencyHardSoft)) {
1079                         pcePathComputation(input);
1080                         LOG.info("PCR Request in progress ");
1081                         configurationResponseCommon = new ConfigurationResponseCommonBuilder()
1082                                 .setAckFinalIndicator("No").setRequestId(input.getSdncRequestHeader().getRequestId())
1083                                 .setResponseMessage("Service compliant, ServiceFeasibilityCheck in progress...")
1084                                 .setResponseCode("200").build();
1085
1086                         ServiceFeasibilityCheckOutput output = new ServiceFeasibilityCheckOutputBuilder()
1087                                 .setConfigurationResponseCommon(configurationResponseCommon).build();
1088                         return RpcResultBuilder.success(output).buildFuture();
1089                     }
1090                 } else {
1091                     message = txrxCheck.getMessage();
1092                     responseCode = "500";
1093                 }
1094             } else {
1095                 message = txrxCheck.getMessage();
1096                 responseCode = "500";
1097             }
1098         } else {
1099             message = compliancyCheck.getMessage();
1100             responseCode = "500";
1101         }*/
1102         configurationResponseCommon = new ConfigurationResponseCommonBuilder().setAckFinalIndicator("Yes")
1103                 .setRequestId(input.getSdncRequestHeader().getRequestId()).setResponseMessage(message)
1104                 .setResponseCode(responseCode).build();
1105         ServiceFeasibilityCheckOutput output = new ServiceFeasibilityCheckOutputBuilder()
1106                 .setConfigurationResponseCommon(configurationResponseCommon).build();
1107
1108         return RpcResultBuilder.success(output).buildFuture();
1109     }
1110
1111     @Override
1112     public Future<RpcResult<ServiceReconfigureOutput>> serviceReconfigure(ServiceReconfigureInput input) {
1113         LOG.info("RPC service reconfigure received");
1114         setServiceReconfigureInput(input);
1115         setServiceDeleteInput(null);
1116         serviceCreateInput = null;
1117         service = null;
1118         String message = "";
1119         LOG.info("checking Service Compliancy ...");
1120         /**
1121          * Upon receipt of service-create RPC, service header and sdnc-request
1122          * header compliancy are verified.
1123          */
1124         String serviceCompliancy = null;
1125         if ((serviceCompliancy = serviceCompliancy(null, input.getServiceName(), input.getConnectionType(),
1126                 RpcActions.ServiceReconfigure, input.getServiceAEnd(), input.getServiceZEnd(), input.getCommonId(),
1127                 input.getHardConstraints(), input.getSoftConstraints())) != null) {
1128             message = "Service not compliant : " + serviceCompliancy;
1129             LOG.info(message);
1130         } else {
1131             LOG.info("Service compliant !");
1132             /**
1133              * Retrieving initial service topology.
1134              */
1135             String serviceName = input.getServiceName();
1136             Services service = readServiceList(serviceName);
1137             if (service != null) {
1138                 LOG.debug("Service '{}' present in datastore !", serviceName);
1139                 /**
1140                  * Sending cancel resource reserve request to PCE.
1141                  */
1142                 pceCancelResResource();
1143                 ServiceReconfigureOutput output = new ServiceReconfigureOutputBuilder()
1144                         .setStatus(RpcStatus.Successful)
1145                         .setStatusMessage("ServiceReconfigure in progress ...").build();
1146                 return RpcResultBuilder.success(output).buildFuture();
1147             } else {
1148                 message = "Service '" + serviceName + "' not exists in datastore";
1149                 LOG.info(message);
1150             }
1151         }
1152         /*compliancyCheck = new ServicehandlerCompliancyCheck(input.getServiceName(), input.getConnectionType(),
1153                 RpcActions.ServiceReconfigure);
1154         if (compliancyCheck.check(true, false)) {
1155             LOG.info("Service compliant !");
1156             LOG.info("checking Tx/Rx Info for AEnd ...");
1157             txrxCheck = new ServicehandlerTxRxCheck(input.getServiceAEnd(), 1);
1158             if (txrxCheck.check()) {
1159                 LOG.info("Tx/Rx Info for AEnd checked !");
1160                 LOG.info("checking Tx/Rx Info for ZEnd ...");
1161                 txrxCheck = new ServicehandlerTxRxCheck(input.getServiceZEnd(), 2);
1162                 if (txrxCheck.check()) {
1163                     LOG.info("Tx/Rx Info for ZEnd checked");
1164                     if (input.getCommonId() != null) {
1165                         LOG.info("Common-id specified");
1166                         checkCoherencyHardSoft = new CheckCoherencyHardSoft(input.getHardConstraints(),
1167                                 input.getSoftConstraints());
1168                         if (checkCoherencyHardSoft.check()) {
1169                             LOG.info("hard/soft constraints coherent !");
1170                             coherencyHardSoft = true;
1171                         } else {
1172                             LOG.info("hard/soft constraints are not coherent !");
1173                             message = "hard/soft constraints are not coherent !";
1174                         }
1175                     } else {
1176                         commonId = false;
1177                     }
1178
1179                     if (!commonId || (commonId && coherencyHardSoft)) {
1180                         String serviceName = input.getServiceName();
1181                         Services service = readServiceList(serviceName);
1182                         if (service != null) {
1183                             LOG.debug("Service '{}' present in datastore !", serviceName);
1184                             pceCancelResResource();
1185                             ServiceReconfigureOutput output = new ServiceReconfigureOutputBuilder()
1186                                     .setStatus(RpcStatusEx.Pending)
1187                                     .setStatusMessage("ServiceReconfigure in progress ...").build();
1188                             return RpcResultBuilder.success(output).buildFuture();
1189                         } else {
1190                             message = "Service '" + serviceName + "' not exists in datastore";
1191                             LOG.error("Service '{}' not exists in datastore", serviceName);
1192                         }
1193                     }
1194                 } else {
1195                     message = txrxCheck.getMessage();
1196                 }
1197             } else {
1198                 message = txrxCheck.getMessage();
1199             }
1200         } else {
1201             message = compliancyCheck.getMessage();
1202         }*/
1203
1204         ServiceReconfigureOutput output = new ServiceReconfigureOutputBuilder().setStatus(RpcStatus.Failed)
1205                 .setStatusMessage(message).build();
1206         return RpcResultBuilder.success(output).buildFuture();
1207     }
1208
1209     @Override
1210     public Future<RpcResult<ServiceRestorationOutput>> serviceRestoration(ServiceRestorationInput input) {
1211         LOG.info("RPC service restoration received");
1212         ServiceRpcResultSh notification = null;
1213         setServiceDeleteInput(null);
1214         setServiceReconfigureInput(null);
1215         String message = "";
1216         LOG.info("checking Service Compliancy ...");
1217         compliancyCheck = new ServicehandlerCompliancyCheck(input.getServiceName(), RpcActions.ServiceRestoration);
1218         if (compliancyCheck.check(false, false)) {
1219             LOG.info("Service compliant !");
1220             /**
1221              * If compliant, Getting path from service DB.
1222              */
1223             String serviceName = input.getServiceName();
1224             Services service = readServiceList(serviceName);
1225             if (service != null) {
1226                 this.service = service;
1227                 LOG.debug("Service '{}' present in datastore !", serviceName);
1228                 /** verify if service state is down. */
1229                 State state = service.getOperationalState();
1230                 if (state != null && !state.equals(State.InService)) {
1231                     notification = new ServiceRpcResultShBuilder()
1232                             .setNotificationType(ServiceNotificationTypes.ServiceRestorationResult)
1233                             .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Pending)
1234                             .setStatusMessage("Service '" + serviceName + "' present in datastore, deleting service ..")
1235                             .build();
1236                     try {
1237                         notificationPublishService.putNotification(notification);
1238                     } catch (InterruptedException e) {
1239                         LOG.info("notification offer rejected : {}", e);
1240                     }
1241                     stubrendererDelete();
1242                     LOG.info("PCR Request in progress ");
1243                     ServiceRestorationOutput output = new ServiceRestorationOutputBuilder()
1244                             .setStatus(RpcStatus.Successful)
1245                             .setStatusMessage("ServiceRestoration in progress...").build();
1246                     return RpcResultBuilder.success(output).buildFuture();
1247                 } else {
1248                     LOG.info("Service '{}' still in '{}' state", serviceName, state);
1249                     ServiceRestorationOutput output = new ServiceRestorationOutputBuilder()
1250                             .setStatus(RpcStatus.Failed)
1251                             .setStatusMessage("ServiceRestoration failed : Service still in service !")
1252                             .build();
1253                     return RpcResultBuilder.success(output).buildFuture();
1254                 }
1255             } else {
1256                 message = "Service '" + serviceName + "' not exists in datastore";
1257                 LOG.error(message);
1258             }
1259         } else {
1260             message = compliancyCheck.getMessage();
1261             LOG.error(message);
1262         }
1263
1264         ServiceRestorationOutput output = new ServiceRestorationOutputBuilder().setStatus(RpcStatus.Failed)
1265                 .setStatusMessage(message).build();
1266
1267         return RpcResultBuilder.success(output).buildFuture();
1268     }
1269
1270     @Override
1271     public Future<RpcResult<TempServiceDeleteOutput>> tempServiceDelete(TempServiceDeleteInput input) {
1272         // TODO Auto-generated method stub
1273         return null;
1274     }
1275
1276     @Override
1277     public Future<RpcResult<TempServiceCreateOutput>> tempServiceCreate(TempServiceCreateInput input) {
1278         // TODO Auto-generated method stub
1279         return null;
1280     }
1281
1282     /**
1283      * Initialize ServiceList Structure on Datastore.
1284      *
1285      * @param DataBroker
1286      *            Access Datastore
1287      */
1288     private void initializeDataTree(DataBroker db) {
1289         LOG.info("Preparing to initialize the greeting registry");
1290         WriteTransaction transaction = db.newWriteOnlyTransaction();
1291         InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
1292         ServiceList greetingRegistry = new ServiceListBuilder().build();
1293         transaction.put(LogicalDatastoreType.OPERATIONAL, iid, greetingRegistry);
1294         Future<Void> future = transaction.submit();
1295         try {
1296             Futures.getChecked(future, ExecutionException.class);
1297         } catch (ExecutionException e) {
1298             LOG.error("Failed to create Service List");
1299         }
1300     }
1301
1302     /**
1303      * Map Input (ServiceCreateInmput, ServiceReconfigureInput) and output
1304      * (PathComputationRequestOutput) to Service.
1305      *
1306      * @param serviceCreateInput
1307      *            ServiceCreateInput parameter
1308      * @param serviceReconfigureInput
1309      *            serviceReconfigureInput parameter
1310      * @param output
1311      *            PathComputationRequestOutput parameter
1312      *
1313      * @return Services Service data
1314      */
1315     private Services mappingServices(ServiceCreateInput serviceCreateInput,
1316             ServiceReconfigureInput serviceReconfigureInput, PathComputationRequestOutput output) {
1317         LOG.info("Mapping informations to Services");
1318         ServiceAEnd aend = null;
1319         ServiceZEnd zend = null;
1320         ServicesBuilder service = new ServicesBuilder();
1321         if (serviceCreateInput != null) {
1322             aend = new ServiceAEndBuilder(serviceCreateInput.getServiceAEnd()).build();
1323             zend = new ServiceZEndBuilder(serviceCreateInput.getServiceZEnd()).build();
1324             service.setServiceName(serviceCreateInput.getServiceName())
1325                 .setAdministrativeState(State.OutOfService)
1326                 .setOperationalState(State.OutOfService).setCommonId(serviceCreateInput.getCommonId())
1327                 .setConnectionType(serviceCreateInput.getConnectionType())
1328                 .setCustomer(serviceCreateInput.getCustomer())
1329                 .setCustomerContact(serviceCreateInput.getCustomerContact())
1330                 .setHardConstraints(serviceCreateInput.getHardConstraints())
1331                 .setSoftConstraints(serviceCreateInput.getSoftConstraints())
1332                 .setLifecycleState(LifecycleState.Planned).setServiceAEnd(aend).setServiceZEnd(zend)
1333                 .setSdncRequestHeader(new SdncRequestHeaderBuilder()
1334                         .setRequestId(serviceCreateInput.getSdncRequestHeader().getRequestId())
1335                         .setRpcAction(serviceCreateInput.getSdncRequestHeader().getRpcAction())
1336                         .build());
1337
1338         } else if (serviceReconfigureInput != null) {
1339             aend = new ServiceAEndBuilder(serviceReconfigureInput.getServiceAEnd()).build();
1340             zend = new ServiceZEndBuilder(serviceReconfigureInput.getServiceZEnd()).build();
1341             service.setServiceName(serviceReconfigureInput.getNewServiceName())
1342                 .setAdministrativeState(State.OutOfService).setOperationalState(State.OutOfService)
1343                 .setCommonId(serviceReconfigureInput.getCommonId())
1344                 .setConnectionType(serviceReconfigureInput.getConnectionType())
1345                 .setCustomer(serviceReconfigureInput.getCustomer())
1346                 .setCustomerContact(serviceReconfigureInput.getCustomerContact())
1347                 .setHardConstraints(serviceReconfigureInput.getHardConstraints())
1348                 .setSoftConstraints(serviceReconfigureInput.getSoftConstraints())
1349                 .setLifecycleState(LifecycleState.Planned).setServiceAEnd(aend).setServiceZEnd(zend)
1350                 .setSdncRequestHeader(new SdncRequestHeaderBuilder()
1351                         .setRequestId("reconfigure_" + serviceReconfigureInput.getNewServiceName())
1352                         .setRpcAction(RpcActions.ServiceReconfigure)
1353                         .build());
1354         }
1355
1356         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response
1357             .parameters.sp.ResponseParameters responseParameters = output.getResponseParameters();
1358         if (responseParameters != null) {
1359             // service.setPceMetric(responseParameters.getPceMetric());
1360             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426
1361                 .response.parameters.sp.response.parameters.PathDescription pathDescription =
1362                 responseParameters.getPathDescription();
1363             if (pathDescription != null) {
1364                 List<AToZ> atozList = new ArrayList<AToZ>();
1365                 List<ZToA> ztoaList = new ArrayList<ZToA>();
1366
1367                 for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426
1368                         .path.description.atoz.direction.AToZ
1369                         tmp : pathDescription.getAToZDirection().getAToZ()) {
1370
1371                     AToZKey key = new AToZKey(tmp.getKey().getId());
1372                     AToZ atoz = new AToZBuilder().setId(tmp.getId()).setKey(key)
1373                             // .setResource(tmp.getResource())
1374                             .build();
1375                     atozList.add(atoz);
1376                 }
1377
1378                 for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426
1379                         .path.description.ztoa.direction.ZToA
1380                         tmp : pathDescription.getZToADirection().getZToA()) {
1381                     ZToAKey key = new ZToAKey(tmp.getKey().getId());
1382                     ZToA ztoa = new ZToABuilder().setId(tmp.getId()).setKey(key)
1383                             // .setResource(tmp.getResource())
1384                             .build();
1385                     ztoaList.add(ztoa);
1386                 }
1387
1388                 Topology topology = new TopologyBuilder().setAToZ(atozList).setZToA(ztoaList).build();
1389                 service.setTopology(topology);
1390             }
1391         }
1392         return service.build();
1393     }
1394
1395     /**
1396      * read Service from ServiceList DataStore.
1397      *
1398      * @param serviceName
1399      *            Name of Service
1400      *
1401      * @return <code>Services</code>
1402      */
1403     private Services readServiceList(String serviceName) {
1404         Services result = null;
1405         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
1406         InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
1407                 new ServicesKey(serviceName));
1408         Future<Optional<Services>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
1409         Optional<Services> optional = Optional.absent();
1410         try {
1411             optional = Futures.getChecked(future, ExecutionException.class);
1412         } catch (ExecutionException e) {
1413             LOG.error("Reading service failed:", e);
1414         }
1415         if (optional.isPresent()) {
1416             LOG.debug("Service '{}' present !", serviceName);
1417             result = new ServicesBuilder(optional.get()).build();
1418         }
1419         return result;
1420     }
1421
1422     /**
1423      * Write or Modify or Delete Service from/to ServiceList.
1424      *
1425      * @param serviceName
1426      *            Name of service
1427      * @param output
1428      *            PathComputationRequestOutput
1429      * @param topo
1430      *            Topology
1431      * @param choice
1432      *            0 - Modify 1 - Delete 2 - Write
1433      * @return String operations result, null if ok or not otherwise
1434      */
1435     private String writeOrModifyOrDeleteServiceList(String serviceName, PathComputationRequestOutput output,
1436             Topology topo, int choice) {
1437         LOG.info("WriteOrModifyOrDeleting '{}' Service", serviceName);
1438         WriteTransaction writeTx = db.newWriteOnlyTransaction();
1439         String result = null;
1440         Services readService = readServiceList(serviceName);
1441         Future<Void> future = null;
1442         if (readService != null) {
1443             /**
1444              * Modify / Delete Service.
1445              */
1446             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
1447                     new ServicesKey(serviceName));
1448             ServicesBuilder service = new ServicesBuilder(readService);
1449
1450             String action = null;
1451             switch (choice) {
1452                 case 0: /** Modify. */
1453                     LOG.info("Modifying '{}' Service", serviceName);
1454                     service.setOperationalState(State.InService).setAdministrativeState(State.InService);
1455                     service.setTopology(topo);
1456                     writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
1457                     action = "modify";
1458                     break;
1459
1460                 case 1: /** Delete. */
1461                     LOG.info("Deleting '{}' Service", serviceName);
1462                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
1463                     action = "delete";
1464                     break;
1465
1466                 default:
1467                     LOG.info("No choice found");
1468                     break;
1469             }
1470             future = writeTx.submit();
1471             try {
1472                 Futures.getChecked(future, ExecutionException.class);
1473             } catch (ExecutionException e) {
1474                 LOG.info("Failed to {} service from Service List", action);
1475                 result = "Failed to " + action + " service from Service List";
1476             }
1477         } else if (choice == 2) { /** Write Service. */
1478             LOG.info("Writing '{}' Service", serviceName);
1479             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
1480                     new ServicesKey(serviceName));
1481             Services writeService = null;
1482             if (this.service != null) {
1483                 writeService = service;
1484             } else {
1485                 writeService = mappingServices(serviceCreateInput, serviceReconfigureInput, output);
1486             }
1487             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, writeService);
1488             future = writeTx.submit();
1489             try {
1490                 Futures.getChecked(future, ExecutionException.class);
1491                 result = null;
1492             } catch (ExecutionException e) {
1493                 LOG.error("Failed to write service to Service List");
1494                 result = "Failed to write service to Service List";
1495             }
1496         } else {
1497             LOG.info("Service is not present  in datastore ! ");
1498             result = "Service is not present  in datastore ! ";
1499         }
1500         return result;
1501     }
1502
1503     @Override
1504     public void close() throws Exception {
1505         executor.shutdown();
1506     }
1507
1508     @Override
1509     public Future<RpcResult<EquipmentNotificationOutput>> equipmentNotification(EquipmentNotificationInput input) {
1510         // TODO Auto-generated method stub
1511         return null;
1512     }
1513
1514     @Override
1515     public Future<RpcResult<ServiceRerouteConfirmOutput>> serviceRerouteConfirm(ServiceRerouteConfirmInput input) {
1516         // TODO Auto-generated method stub
1517         return null;
1518     }
1519
1520     @Override
1521     public Future<RpcResult<ServiceRerouteOutput>> serviceReroute(ServiceRerouteInput input) {
1522         // TODO Auto-generated method stub
1523         return null;
1524     }
1525
1526     @Override
1527     public Future<RpcResult<ServiceReversionOutput>> serviceReversion(ServiceReversionInput input) {
1528         // TODO Auto-generated method stub
1529         return null;
1530     }
1531
1532     @Override
1533     public Future<RpcResult<ServiceRollOutput>> serviceRoll(ServiceRollInput input) {
1534         // TODO Auto-generated method stub
1535         return null;
1536     }
1537
1538     @Override
1539     public Future<RpcResult<NetworkReOptimizationOutput>> networkReOptimization(NetworkReOptimizationInput input) {
1540         // TODO Auto-generated method stub
1541         return null;
1542     }
1543
1544     @Override
1545     public void onServiceRpcResultSp(ServiceRpcResultSp notification) {
1546         if (notification != null && !compareServiceRpcResultSp(notification)) {
1547             serviceRpcResultSp = notification;
1548             StringBuilder build = new StringBuilder();
1549             build.append(
1550                     "Received '" + notification.getNotificationType() + "' StubRenderer notification" + "from service '"
1551                             + notification.getServiceName() + "' " + "with status '" + notification.getStatus() + "'");
1552             build.append(" with StatusMessage '" + notification.getStatusMessage() + "'");
1553             LOG.info(build.toString());
1554             switch (serviceRpcResultSp.getNotificationType().getIntValue()) {
1555                 case 3 : /** service-implementation-request. */
1556                     if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
1557                         if (serviceRpcResultSp.getPathTopology() != null) {
1558                             pathTopology = new PathTopologyBuilder()
1559                                     .setAToZ(serviceRpcResultSp.getPathTopology().getAToZ())
1560                                     .setZToA(serviceRpcResultSp.getPathTopology().getZToA())
1561                                     .build();
1562                             LOG.info("PathTopology gets !");
1563                         } else {
1564                             LOG.info("'serviceRpcResultSp.getPathTopology()' parameter is null ");
1565                         }
1566                         if (serviceCreateInput != null) {
1567                             updateServiceStatus(serviceCreateInput);
1568                         } else if (serviceReconfigureInput != null) {
1569                             updateServiceStatus(serviceReconfigureInput);
1570                         } else if (service != null) {
1571                             updateServiceStatus(service);
1572                         }
1573                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
1574                         LOG.info("Stubrenderer computation failed !");
1575                         pceCancelResResource();
1576                     }
1577                     break;
1578
1579                 case 4 : /** service-delete. */
1580                     if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
1581                         if (service != null) { //serviceRestoration
1582                             LOG.info("RPC service delete came from serviceRestoration");
1583                             pcePathComputation(service);
1584                         } else {
1585                             pceCancelResResource();
1586                         }
1587                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
1588                         LOG.info("Stubrenderer computation failed !");
1589                     }
1590                     break;
1591
1592                 default:
1593                     break;
1594             }
1595         } else {
1596             LOG.info("ServiceRpcResultSp already wired !");
1597         }
1598     }
1599
1600     @Override
1601     public void onServicePathRpcResult(ServicePathRpcResult notification) {
1602         if (notification != null && !compareServicePathRpcResult(notification)) {
1603             servicePathRpcResult = notification;
1604             StringBuilder build = new StringBuilder();
1605             build.append(
1606                     "Received '" + notification.getNotificationType() + "' Stubpce notification" + "from service '"
1607                             + notification.getServiceName() + "' " + "with status '" + notification.getStatus() + "'");
1608             build.append(" with StatusMessage '" + notification.getStatusMessage() + "'");
1609             LOG.info(build.toString());
1610             switch (servicePathRpcResult.getNotificationType().getIntValue()) {
1611                 case 1 : /** path-computation-request. */
1612                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
1613                         if (servicePathRpcResult.getPathDescription() != null) {
1614                             pathDescription = new PathDescriptionBuilder()
1615                                     .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
1616                                     .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
1617                                     .build();
1618                             LOG.info("PathDescription gets !");
1619                             if (serviceReconfigureInput != null) {
1620                                 stubrendererImplementation(serviceReconfigureInput);
1621                             } else if (serviceCreateInput != null) {
1622                                 stubrendererImplementation(serviceCreateInput);
1623                             } else if (service != null) {
1624                                 stubrendererImplementation(service);
1625                             }
1626                         } else {
1627                             LOG.info("'servicePathRpcResult.getPathDescription()'parameter is null ");
1628                         }
1629                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
1630                         LOG.info("Stupce computation failed !");
1631                     }
1632                     break;
1633
1634                 case 2 : /** cancel-resource-reserve. */
1635                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
1636                         /**if it was an RPC serviceReconfigure, relaunch
1637                          * PCR else delete the service.
1638                          */
1639                         if (serviceReconfigureInput != null) {
1640                             LOG.info("cancel reserve resource request came from RPC serviceReconfigure !");
1641                             pcePathComputation(serviceReconfigureInput);
1642                         } else {
1643                             deleteServiceFromDatastore();
1644                         }
1645                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
1646                         LOG.info("Stupce computation failed !");
1647                     }
1648                     break;
1649
1650                 default:
1651                     break;
1652             }
1653         } else {
1654             LOG.info("ServicePathRpcResult already wired !");
1655         }
1656     }
1657
1658     public RpcActions getAction() {
1659         return action;
1660     }
1661
1662     public void setAction(RpcActions action) {
1663         this.action = action;
1664     }
1665
1666     /**
1667      * Compare ServicePathRpcResult.
1668      *
1669      * @param notification ServicePathRpcResult
1670      * @return <code>Boolean</code> true if idem, false else
1671      */
1672     public Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
1673         Boolean result = true;
1674         if (servicePathRpcResult == null) {
1675             result = false;
1676         } else {
1677             if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
1678                 result = false;
1679             }
1680             if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
1681                 result = false;
1682             }
1683             if (servicePathRpcResult.getStatus() != notification.getStatus()) {
1684                 result = false;
1685             }
1686             if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
1687                 result = false;
1688             }
1689         }
1690         return result;
1691     }
1692
1693     /**
1694      * Compare ServiceRpcResultSp.
1695      *
1696      * @param notification ServiceRpcResultSp
1697      * @return <code>Boolean</code> true if idem, false else
1698      */
1699     public Boolean compareServiceRpcResultSp(ServiceRpcResultSp notification) {
1700         Boolean result = true;
1701         if (serviceRpcResultSp == null) {
1702             result = false;
1703         } else {
1704             if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
1705                 result = false;
1706             }
1707             if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
1708                 result = false;
1709             }
1710             if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
1711                 result = false;
1712             }
1713             if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
1714                 result = false;
1715             }
1716         }
1717         return result;
1718     }
1719
1720     public PathDescription getPathDescription() {
1721         return pathDescription;
1722     }
1723
1724     public void setPathDescription(PathDescription pathDescription) {
1725         this.pathDescription = pathDescription;
1726     }
1727
1728     public ServiceDeleteInput getServiceDeleteInput() {
1729         return serviceDeleteInput;
1730     }
1731
1732     public void setServiceDeleteInput(ServiceDeleteInput serviceDeleteInput) {
1733         this.serviceDeleteInput = serviceDeleteInput;
1734     }
1735
1736     public ServiceReconfigureInput getServiceReconfigureInput() {
1737         return serviceReconfigureInput;
1738     }
1739
1740     public void setServiceReconfigureInput(ServiceReconfigureInput serviceReconfigureInput) {
1741         this.serviceReconfigureInput = serviceReconfigureInput;
1742     }
1743
1744     public ServiceFeasibilityCheckInput getServiceFeasibilityCheckInput() {
1745         return serviceFeasibilityCheckInput;
1746     }
1747
1748     public void setServiceFeasibilityCheckInput(ServiceFeasibilityCheckInput serviceFeasibilityCheckInput) {
1749         this.serviceFeasibilityCheckInput = serviceFeasibilityCheckInput;
1750     }
1751
1752     @Override
1753     public Future<RpcResult<ServiceStateModifyOutput>> serviceStateModify(ServiceStateModifyInput input) {
1754         LOG.info("RPC service state modify received");
1755         String servicename = input.getServiceName();
1756         String message = "";
1757         String responseCode = "500";
1758         if (servicename != null) {
1759             Services service = readServiceList(servicename);
1760             if (service != null) {
1761                 LOG.info("Modify '{}' Service state", servicename);
1762                 WriteTransaction writeTx = db.newWriteOnlyTransaction();
1763                 String result = null;
1764                 Future<Void> future = null;
1765                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
1766                         new ServicesKey(servicename));
1767                 ServicesBuilder modifyService = new ServicesBuilder(service);
1768                 modifyService.setOperationalState(State.OutOfService);
1769                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, modifyService.build());
1770                 future = writeTx.submit();
1771                 try {
1772                     Futures.getChecked(future, ExecutionException.class);
1773                 } catch (ExecutionException e) {
1774                     LOG.info("Failed to {} service from Service List", action);
1775                     result = "Failed to " + action + " service from Service List";
1776                 }
1777                 if (result == null) {
1778                     responseCode = "200";
1779                     message = "Service state modified !";
1780                 }
1781             } else {
1782                 message = "Service not present in datastore ! ";
1783             }
1784         } else {
1785             message = "servicename not set in ServiceStateModifyInput";
1786         }
1787         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
1788                 .setAckFinalIndicator("Yes")
1789                 .setResponseCode(responseCode)
1790                 .setResponseMessage(message)
1791                 .build();
1792         ServiceStateModifyOutput output = new ServiceStateModifyOutputBuilder()
1793                 .setConfigurationResponseCommon(configurationResponseCommon)
1794                 .build();
1795         return RpcResultBuilder.success(output).buildFuture();
1796     }
1797 }