Servicehandler Tests
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / impl / StubpceImpl.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.stubpce.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17
18 import java.util.Iterator;
19 import java.util.SortedSet;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.Future;
23
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.transportpce.stubpce.CheckCoherencyHardSoft;
30 import org.opendaylight.transportpce.stubpce.SendingPceRPCs;
31 import org.opendaylight.transportpce.stubpce.StubpceCompliancyCheck;
32 import org.opendaylight.transportpce.stubpce.StubpceTxRxCheck;
33 import org.opendaylight.transportpce.stubpce.topology.PathDescriptionsOrdered;
34 import org.opendaylight.transportpce.stubpce.topology.SuperNodePath;
35 import org.opendaylight.transportpce.stubpce.topology.Topology;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveInput;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutput;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutputBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestInput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutput;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutputBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathDescriptionList;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathDescriptionListBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathList;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathListBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathRpcResult;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathRpcResultBuilder;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.StubpceService;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.path.description.list.PathDescriptions;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.path.description.list.PathDescriptionsBuilder;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.path.description.list.PathDescriptionsKey;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePaths;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePathsBuilder;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePathsKey;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.rpc.result.PathDescriptionBuilder;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
61 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
62 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.ServicePathNotificationTypes;
63 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.service.path.rpc.result.PathDescription;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 /**
71  * Class to implement StubpceService StubpceListener.
72  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
73  *         behalf of Orange
74  */
75
76 public class StubpceImpl implements StubpceService {
77     /** Logging. */
78     private static final Logger LOG = LoggerFactory.getLogger(StubpceImpl.class);
79     /** Permit to access database. */
80     private DataBroker db;
81     /** check service sdnc-request-header compliancy. */
82     private StubpceCompliancyCheck compliancyCheck;
83     /** check missing info on Tx/Rx for A/Z end. */
84     private StubpceTxRxCheck txrxCheck;
85     /** check coherency between hard and soft constraints. */
86     private CheckCoherencyHardSoft checkCoherencyHardSoft;
87     private NotificationPublishService notificationPublishService;
88     private ServicePathRpcResult notification;
89     private PathDescriptionBuilder pathDescriptionBuilder;
90     private SendingPceRPCs sendingPCE;
91     private final ListeningExecutorService executor = MoreExecutors
92             .listeningDecorator(Executors.newFixedThreadPool(10));
93
94     public StubpceImpl(NotificationPublishService notificationPublishService, DataBroker databroker) {
95         this.notificationPublishService = notificationPublishService;
96         this.db = databroker;
97         pathDescriptionBuilder = null;
98         if (initializePathDescriptionList(databroker)) {
99             fillPathDesciptionList();
100         }
101         initializeServicePathList(databroker);
102         /*if (initializeServicePathList(databroker)) {
103             fillServicePathList();
104         }*/
105     }
106
107     @Override
108     public Future<RpcResult<CancelResourceReserveOutput>> cancelResourceReserve(CancelResourceReserveInput input) {
109         LOG.info("RPC cancelResourceReserve  request received");
110         String message = "";
111         String responseCode = "";
112         ConfigurationResponseCommonBuilder configurationResponseCommon = null;
113         String serviceName = input.getServiceName();
114         LOG.info("serviceName : {}", serviceName);
115         if (serviceName != null) {
116             sendingPCE = new SendingPceRPCs(input,db,executor);
117             FutureCallback<Boolean> pceCallback = new FutureCallback<Boolean>() {
118                 String message = "";
119                 ServicePathRpcResult notification = null;
120
121                 @Override
122                 public void onFailure(Throwable arg0) {
123                     LOG.error("Cancel resource failed : {}", arg0);
124                     notification = new ServicePathRpcResultBuilder()
125                             .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
126                             .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
127                             .setStatusMessage("Cancel resource request failed  : " + arg0.getMessage()).build();
128                     try {
129                         notificationPublishService.putNotification(notification);
130                     } catch (InterruptedException e) {
131                         LOG.info("notification offer rejected : {}", e);
132                     }
133                 }
134
135                 @Override
136                 public void onSuccess(Boolean response) {
137                     LOG.info("response : {}", response);
138                     if (response) {
139                         message = "Resource cancelled !";
140                         notification = new ServicePathRpcResultBuilder()
141                                 .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
142                                 .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Successful)
143                                 .setStatusMessage(message)
144                                 .build();
145                     } else {
146                         message = sendingPCE.getError();
147                         notification = new ServicePathRpcResultBuilder()
148                                 .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
149                                 .setServiceName("")
150                                 .setStatus(RpcStatusEx.Failed).setStatusMessage(message)
151                                 .build();
152                         message = "Cancel request failed !";
153                     }
154                     LOG.info(notification.toString());
155                     try {
156                         notificationPublishService.putNotification(notification);
157                     } catch (InterruptedException e) {
158                         LOG.info("notification offer rejected : {}", e);
159                     }
160                     LOG.info(message);
161                 }
162             };
163             ListenableFuture<Boolean> pce = sendingPCE.cancelResourceReserve();
164             Futures.addCallback(pce, pceCallback, executor);
165             LOG.info("Cancel Resource Request in progress ...");
166             configurationResponseCommon = new ConfigurationResponseCommonBuilder()
167                     .setAckFinalIndicator("No")
168                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
169                     .setResponseMessage("Service compliant, Cancel resource request in progress...")
170                     .setResponseCode("200");
171
172             CancelResourceReserveOutputBuilder output = new CancelResourceReserveOutputBuilder()
173                     .setConfigurationResponseCommon(configurationResponseCommon.build());
174             return RpcResultBuilder.success(output.build()).buildFuture();
175         } else {
176             message = "serviceName / requestId is not correct !";
177             responseCode = "500";
178             notification = new ServicePathRpcResultBuilder()
179                     .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
180                     .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
181                     .setStatusMessage(message).build();
182             try {
183                 notificationPublishService.putNotification(notification);
184             } catch (InterruptedException e) {
185                 LOG.info("notification offer rejected : {}", e);
186             }
187         }
188         configurationResponseCommon = new ConfigurationResponseCommonBuilder();
189         configurationResponseCommon.setAckFinalIndicator("Yes")
190         .setRequestId(input.getServiceHandlerHeader().getRequestId())
191         .setResponseMessage(message)
192         .setResponseCode(responseCode).build();
193         CancelResourceReserveOutputBuilder output = new CancelResourceReserveOutputBuilder();
194         output.setConfigurationResponseCommon(configurationResponseCommon.build());
195         return RpcResultBuilder.success(output.build()).buildFuture();
196     }
197
198     @Override
199     public Future<RpcResult<PathComputationRequestOutput>> pathComputationRequest(PathComputationRequestInput input) {
200         LOG.info("RPC pathcomputation request received");
201         String message = "";
202         String responseCode = "";
203         boolean coherencyHardSoft = false;
204         boolean commonId = true;
205         ConfigurationResponseCommonBuilder configurationResponseCommon = null;
206         compliancyCheck = new StubpceCompliancyCheck(input.getServiceName(), input.getServiceHandlerHeader());
207         if (compliancyCheck.check(false, true)) {
208             LOG.info("Service compliant !");
209             /**
210              * If compliant, service-request parameters are verified in order to
211              * check if there is no missing parameter that prevents calculating
212              * a path and implement a service.
213              */
214             LOG.info("checking Tx/Rx Info for AEnd ...");
215             txrxCheck = new StubpceTxRxCheck(input.getServiceAEnd(), 1);
216             if (txrxCheck.check()) {
217                 LOG.info("Tx/Rx Info for AEnd checked !");
218                 LOG.info("checking Tx/Rx Info for ZEnd ...");
219                 txrxCheck = new StubpceTxRxCheck(input.getServiceZEnd(), 2);
220                 if (txrxCheck.check()) {
221                     LOG.info("Tx/Rx Info for ZEnd checked !");
222                     /**
223                      * If OK, common-id is verified in order to see if there is
224                      * no routing policy provided. If yes, the routing
225                      * constraints of the policy are recovered and coherency
226                      * with hard/soft constraints provided in the input of the
227                      * RPC.
228                      */
229                     if (input.getHardConstraints() != null || input.getSoftConstraints() != null) {
230                         LOG.info("Constraints specified !");
231                         checkCoherencyHardSoft = new CheckCoherencyHardSoft(input.getHardConstraints(),
232                                 input.getSoftConstraints());
233                         if (checkCoherencyHardSoft.check()) {
234                             LOG.info("hard/soft constraints coherent !");
235                             coherencyHardSoft = true;
236                         } else {
237                             LOG.info("hard/soft constraints are not coherent !");
238                             message = "hard/soft constraints are not coherent !";
239                             responseCode = "500";
240                         }
241                     } else {
242                         commonId = false;
243                     }
244                     if (!commonId || (commonId && coherencyHardSoft)) {
245                         notification = new ServicePathRpcResultBuilder()
246                                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
247                                 .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Pending)
248                                 .setStatusMessage("Service compliant, submitting pathComputation Request ...").build();
249                         try {
250                             notificationPublishService.putNotification(notification);
251                         } catch (InterruptedException e) {
252                             LOG.info("notification offer rejected : {}", e);
253                         }
254                         sendingPCE = new SendingPceRPCs(input,db,executor);
255                         FutureCallback<Boolean> pceCallback = new FutureCallback<Boolean>() {
256                             String message = "";
257                             ServicePathRpcResult notification = null;
258
259                             @Override
260                             public void onFailure(Throwable arg0) {
261                                 LOG.error("Failure message : {}", arg0.toString());
262                                 LOG.error("Path calculation failed !");
263                                 notification = new ServicePathRpcResultBuilder()
264                                         .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
265                                         .setStatusMessage("PCR Request failed  : " + arg0.getMessage()).build();
266                                 try {
267                                     notificationPublishService.putNotification(notification);
268                                 } catch (InterruptedException e) {
269                                     LOG.info("notification offer rejected : {}", e);
270                                 }
271                             }
272
273                             @Override
274                             public void onSuccess(Boolean response) {
275                                 LOG.info("response : {}", response);
276                                 if (response) {
277                                     message = "Path Computated !";
278                                     ServicePathRpcResultBuilder tmp = new ServicePathRpcResultBuilder()
279                                             .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
280                                             .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Successful)
281                                             .setStatusMessage(message);
282                                     pathDescriptionBuilder = sendingPCE.getPathDescription();
283                                     if (pathDescriptionBuilder != null) {
284                                         PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org
285                                                 .transportpce.b.c._interface.servicepath.rev170426.service.path
286                                                 .rpc.result.PathDescriptionBuilder()
287                                                 .setAToZDirection(pathDescriptionBuilder.getAToZDirection())
288                                                 .setZToADirection(pathDescriptionBuilder.getZToADirection())
289                                                 .build();
290                                         tmp.setPathDescription(new PathDescriptionBuilder()
291                                                 .setAToZDirection(pathDescription.getAToZDirection())
292                                                 .setZToADirection(pathDescription.getZToADirection())
293                                                 .build());
294                                     }
295                                     notification = tmp.build();
296                                 } else {
297                                     message = sendingPCE.getError();
298                                     notification = new ServicePathRpcResultBuilder()
299                                             .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
300                                             .setServiceName("")
301                                             .setStatus(RpcStatusEx.Failed).setStatusMessage(message)
302                                             .build();
303                                     message = "Path not calculated!";
304                                 }
305                                 try {
306                                     notificationPublishService.putNotification(notification);
307                                 } catch (InterruptedException e) {
308                                     LOG.info("notification offer rejected : {}", e);
309                                 }
310                                 LOG.info(message);
311                             }
312                         };
313                         ListenableFuture<Boolean> pce = sendingPCE.pathComputation();
314                         Futures.addCallback(pce, pceCallback, executor);
315                         LOG.info("PathComputation Request in progress ...");
316                         configurationResponseCommon = new ConfigurationResponseCommonBuilder()
317                                 .setAckFinalIndicator("No")
318                                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
319                                 .setResponseMessage("Service compliant, Path calculating in progress...")
320                                 .setResponseCode("200");
321
322                         PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder()
323                                 .setConfigurationResponseCommon(configurationResponseCommon.build());
324                         return RpcResultBuilder.success(output.build()).buildFuture();
325                     }
326
327                 } else {
328                     message = txrxCheck.getMessage();
329                     responseCode = "500";
330                 }
331             } else {
332                 message = txrxCheck.getMessage();
333                 responseCode = "500";
334             }
335         } else {
336             message = compliancyCheck.getMessage();
337             responseCode = "500";
338             notification = new ServicePathRpcResultBuilder()
339                     .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
340                     .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
341                     .setStatusMessage("Service not compliant : " + message).build();
342             try {
343                 notificationPublishService.putNotification(notification);
344             } catch (InterruptedException e) {
345                 LOG.info("notification offer rejected : {}", e);
346             }
347         }
348         configurationResponseCommon = new ConfigurationResponseCommonBuilder();
349         configurationResponseCommon.setAckFinalIndicator("Yes")
350         .setRequestId(input.getServiceHandlerHeader().getRequestId())
351         .setResponseMessage(message)
352         .setResponseCode(responseCode).build();
353         PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder();
354         output.setConfigurationResponseCommon(configurationResponseCommon.build());
355         return RpcResultBuilder.success(output.build()).buildFuture();
356
357     }
358
359     /**
360      * Initialize PathDescriptionList Structure on DataStore.
361      *
362      * @param DataBroker
363      *            Access DataStore
364      */
365     private boolean initializePathDescriptionList(DataBroker db) {
366         Boolean result = true;
367         LOG.info("Preparing to initialize the PathDescription List");
368         WriteTransaction transaction = db.newWriteOnlyTransaction();
369         InstanceIdentifier<PathDescriptionList> iid = InstanceIdentifier.create(PathDescriptionList.class);
370         PathDescriptionList pathDescriptionList = new PathDescriptionListBuilder().build();
371         transaction.put(LogicalDatastoreType.OPERATIONAL, iid, pathDescriptionList);
372         Future<Void> future = transaction.submit();
373         try {
374             Futures.getChecked(future, ExecutionException.class);
375         } catch (ExecutionException e) {
376             LOG.error("Failed to create PathDescription List");
377             result = false;
378         }
379         return result;
380     }
381
382     /**
383      * Initialize ServicePathList Structure on DataStore.
384      *
385      * @param DataBroker
386      *            Access DataStore
387      * @return <code>true</code> if ok, <code>false</code> else
388      */
389     private boolean initializeServicePathList(DataBroker db) {
390         Boolean result = true;
391         LOG.info("Preparing to initialize the ServicePathList registry");
392         WriteTransaction transaction = db.newWriteOnlyTransaction();
393         InstanceIdentifier<ServicePathList> iid = InstanceIdentifier.create(ServicePathList.class);
394         ServicePathList servicePathList = new ServicePathListBuilder().build();
395         transaction.put(LogicalDatastoreType.OPERATIONAL, iid, servicePathList);
396         Future<Void> future = transaction.submit();
397         try {
398             Futures.getChecked(future, ExecutionException.class);
399         } catch (ExecutionException e) {
400             LOG.error("Failed to create ServicePathList List : {}", e.toString());
401             result = false;
402         }
403         return result;
404     }
405
406     /**
407      * fill PathDescriptionList
408      * with direct paths and
409      * indirect paths.
410      */
411     private void fillPathDesciptionList() {
412         LOG.info("filling PathDescription List...");
413         Topology topo = new Topology();
414         topo.start();
415         LOG.info("Network : {}", topo.getNetwork());
416         SuperNodePath superNodePath = new SuperNodePath(topo.getNetwork());
417         String aend = "NodeA";
418         String zend = "NodeZ";
419         superNodePath.run(aend, zend);
420         fill(superNodePath.getDirectPathDesc(aend, zend, superNodePath.getPaths()));
421         fill(superNodePath.getIndirectPathDesc(aend, zend, superNodePath.getPaths()));
422     }
423
424     /**
425      * fill datastore with
426      * Pathdescriptions List.
427      *
428      * @param sortedSet PathDescriptionsOrdered List
429      */
430     private void fill(SortedSet<PathDescriptionsOrdered> sortedSet) {
431         InstanceIdentifier<PathDescriptions> iid;
432         WriteTransaction writeTx;
433         Future<Void> future;
434         if (!sortedSet.isEmpty()) {
435             Iterator<PathDescriptionsOrdered> it = sortedSet.iterator();
436             while (it.hasNext()) {
437                 PathDescriptions pathDesc = it.next().getPathDescriptions();
438                 if (pathDesc != null) {
439                     iid = InstanceIdentifier.create(PathDescriptionList.class)
440                             .child(PathDescriptions.class, new PathDescriptionsKey(pathDesc.getPathName()));
441                     writeTx = db.newWriteOnlyTransaction();
442                     writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, pathDesc);
443                     future = writeTx.submit();
444                     try {
445                         Futures.getChecked(future, ExecutionException.class);
446                     } catch (ExecutionException e) {
447                         LOG.error("Failed to write PathDescriptions to PathDescriptionsList : {}", e.toString());
448                     }
449                 } else {
450                     LOG.error("PathDescriptions gets is null !");
451                 }
452             }
453         } else {
454             LOG.info("PathDescriptions List is empty !");
455         }
456     }
457
458     /**
459      * read Service from ServiceList DataStore.
460      *
461      * @param serviceName
462      *            Name of Service
463      *
464      * @return <code>Services</code>
465      */
466     @SuppressWarnings("unused")
467     private Services readServiceList(String serviceName) {
468         Services result = null;
469         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
470         InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
471                 new ServicesKey(serviceName));
472         Future<Optional<Services>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
473         Optional<Services> optional = Optional.absent();
474         try {
475             optional = Futures.getChecked(future, ExecutionException.class);
476         } catch (ExecutionException e) {
477             LOG.error("Reading service failed:", e);
478         }
479         if (optional.isPresent()) {
480             LOG.debug("Service '{}' present !", serviceName);
481             result = new ServicesBuilder(optional.get()).build();
482         }
483         return result;
484     }
485
486     /**
487      * read ServicePath Service from ServicePathList DataStore.
488      *
489      * @param serviceName
490      *            Name of ServicePath
491      *
492      * @return <code>Services</code>
493      */
494     @SuppressWarnings("unused")
495     private ServicePaths readServicePathList(String serviceName) {
496         ServicePaths result = null;
497         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
498         InstanceIdentifier<ServicePaths> iid = InstanceIdentifier.create(ServicePathList.class)
499                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
500         Future<Optional<ServicePaths>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
501         Optional<ServicePaths> optional = Optional.absent();
502         try {
503             optional = Futures.getChecked(future, ExecutionException.class);
504         } catch (ExecutionException e) {
505             LOG.error("Reading service failed:", e);
506         }
507         if (optional.isPresent()) {
508             LOG.debug("Service '{}' present !", serviceName);
509             result = new ServicePathsBuilder(optional.get()).build();
510         }
511         return result;
512     }
513
514     /**
515      * read PathDescription information from PathDescriptionList.
516      *
517      * @param pathName
518      *            Name of PathDescription
519      *
520      * @return <code>PathDescriptions</code>
521      */
522     @SuppressWarnings("unused")
523     private PathDescriptions readPathDescriptionList(String pathName) {
524         PathDescriptions result = null;
525         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
526         InstanceIdentifier<PathDescriptions> iid = InstanceIdentifier.create(PathDescriptionList.class)
527                 .child(PathDescriptions.class, new PathDescriptionsKey(pathName));
528         Future<Optional<PathDescriptions>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
529         Optional<PathDescriptions> optional = Optional.absent();
530         try {
531             optional = Futures.getChecked(future, ExecutionException.class);
532         } catch (ExecutionException e) {
533             LOG.error("Reading service failed:", e);
534         }
535         if (optional.isPresent()) {
536             LOG.debug("PathDescritions '{}' present !", pathName);
537             result = new PathDescriptionsBuilder(optional.get()).build();
538         }
539         return result;
540     }
541
542     /**
543      * register ServicePath Service to ServicePathList with PathDescription
544      * information.
545      *
546      * @param input
547      *            PathComputationRequestInput
548      * @return String operations result, null if ok or not otherwise
549      */
550     @SuppressWarnings("unused")
551     private String writeServicePathList(PathComputationRequestInput input) {
552         String serviceName = input.getServiceName();
553         LOG.debug("Write ServicePath '{}' Service", serviceName);
554         String result = null;
555         LOG.debug("Writing '{}' ServicePath", serviceName);
556         InstanceIdentifier<ServicePaths> iid = InstanceIdentifier.create(ServicePathList.class)
557                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
558
559         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service
560             .path.PathDescriptionBuilder path = new org.opendaylight.yang.gen.v1.http.org.transportpce
561             .b.c._interface.service.types.rev170426.service.path.PathDescriptionBuilder();
562         path.setAToZDirection(pathDescriptionBuilder.getAToZDirection());
563         path.setZToADirection(pathDescriptionBuilder.getZToADirection());
564         ServicePaths service = new ServicePathsBuilder().setServicePathName(input.getServiceName())
565                 .setSoftConstraints(input.getSoftConstraints()).setHardConstraints(input.getHardConstraints())
566                 .setPathDescription(path.build()).build();
567         WriteTransaction writeTx = db.newWriteOnlyTransaction();
568         writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
569         Future<Void> future = writeTx.submit();
570         try {
571             Futures.getChecked(future, ExecutionException.class);
572             result = null;
573         } catch (ExecutionException e) {
574             LOG.error("Failed to write service to Service List");
575             result = "Failed to write service to Service List";
576         }
577         return result;
578     }
579
580     public PathDescriptionBuilder getPathDescriptionBuilder() {
581         return pathDescriptionBuilder;
582     }
583
584     public void setPathDescriptionBuilder(PathDescriptionBuilder pathDescriptionBuilder) {
585         this.pathDescriptionBuilder = pathDescriptionBuilder;
586     }
587 }