Servicehandler Tests
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / SendingPceRPCs.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
10 package org.opendaylight.transportpce.stubpce;
11
12 import com.google.common.base.Optional;
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
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.ListIterator;
21 import java.util.SortedSet;
22 import java.util.TreeSet;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.TimeUnit;
27
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.transportpce.stubpce.topology.PathDescriptionsOrdered;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveInput;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestInput;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathDescriptionList;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathList;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.path.description.list.PathDescriptions;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePaths;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePathsBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.list.ServicePathsKey;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.rpc.result.PathDescriptionBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirection;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirection;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZ;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.Resource;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Link;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Node;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.TerminationPoint;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.constraints.sp.co.routing.or.general.General;
50 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.constraints.sp.co.routing.or.general.general.Diversity;
51 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.constraints.sp.co.routing.or.general.general.Exclude;
52 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing.constraints.sp.HardConstraints;
53 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.routing.constraints.sp.SoftConstraints;
54 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service.handler.header.ServiceHandlerHeaderBuilder;
55 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service.path.ServiceAEnd;
56 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service.path.ServiceZEnd;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * Class for Sending
63  * PCE requests :
64  * - path-computation-request
65  * - cancel-resource-reserve.
66  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on behalf of Orange
67  *
68  */
69 public class SendingPceRPCs {
70     /** Logging. */
71     private static final Logger LOG = LoggerFactory.getLogger(SendingPceRPCs.class);
72     private Boolean success;
73     private PathComputationRequestInput input;
74     private CancelResourceReserveInput cancelInput;
75     private PathDescriptionBuilder pathDescription;
76     private DataBroker db;
77     private String error;
78     private final ListeningExecutorService executor;
79     private List<ServicePaths> servicePathList;
80
81     public SendingPceRPCs() {
82         success = true;
83         setPathDescription(null);
84         setError("");
85         executor = null;
86         setServicePathList(new ArrayList<ServicePaths>());
87     }
88
89     public SendingPceRPCs(PathComputationRequestInput input,DataBroker databroker,ListeningExecutorService executor) {
90         success = true;
91         setPathDescription(null);
92         setInput(input);
93         setCancelInput(null);
94         setDb(databroker);
95         setError("");
96         servicePathList = readServicePathList();
97         this.executor = executor;
98     }
99
100     public SendingPceRPCs(CancelResourceReserveInput input,DataBroker databroker,ListeningExecutorService executor) {
101         success = true;
102         setPathDescription(null);
103         setInput(null);
104         setCancelInput(input);
105         setDb(databroker);
106         setError("");
107         servicePathList = readServicePathList();
108         this.executor = executor;
109     }
110
111     /**
112      * Compare AEnd and ZEnd resource Node to
113      * AEnd and ZEnd String.
114      *
115      * @param pathAend AEnd resource Node
116      * @param pathZend ZEnd resource Node
117      * @param inputAend AEnd String Node
118      * @param inputZend ZEnd String Node
119      * @return Boolean result true if equal, false if not
120      */
121     private Boolean comp(Resource pathAend, Resource pathZend, String inputAend, String inputZend) {
122         Boolean result = false;
123         if (pathAend != null && pathZend != null && inputAend != null && inputZend != null) {
124             if (pathAend instanceof Node && pathZend instanceof Node) {
125                 Node aend = (Node) pathAend;
126                 Node zend = (Node) pathZend;
127                 if (aend.getNodeId().compareToIgnoreCase(inputAend) == 0) {
128                     if (zend.getNodeId().compareToIgnoreCase(inputZend) == 0) {
129                         result = true;
130                     }
131                 }
132             }
133         }
134         return result;
135     }
136
137     /**
138      * Compare two resource.
139      *
140      * @param res1 first resource
141      * @param res2 second resource
142      * @return Boolean result true if equal, false if not
143      */
144     private Boolean egalResource(Resource res1, Resource res2) {
145         LOG.info("comparing resource ...");
146         Boolean result = false;
147         LOG.info("{} - {}", res1.getClass().getName(), res2.getClass().getName());
148         if (res1.getClass().getName().compareToIgnoreCase(res2.getClass().getName()) == 0) {
149             if (res1 instanceof Node && res2 instanceof Node) {
150                 Node node1 = (Node)res1;
151                 Node node2 = (Node)res2;
152                 if (node1.getNodeId().compareTo(node2.getNodeId()) == 0) {
153                     result = true;
154                 }
155             }
156             if (res1 instanceof TerminationPoint && res2 instanceof TerminationPoint) {
157                 TerminationPoint tp1 = (TerminationPoint)res1;
158                 TerminationPoint tp2 = (TerminationPoint)res2;
159                 if (tp1.getTpNodeId().compareTo(tp2.getTpNodeId()) == 0) {
160                     if (tp1.getTpId().compareTo(tp2.getTpId()) == 0) {
161                         result = true;
162                     }
163                 }
164             }
165             if (res1 instanceof Link && res2 instanceof Link) {
166                 Link link1 = (Link)res1;
167                 Link link2 = (Link)res2;
168                 if (link1.getLinkId().compareTo(link2.getLinkId()) == 0) {
169                     result = true;
170                 }
171
172             }
173         }
174         return result;
175     }
176
177     /**
178      *  compare two AtoZDirection.
179      *
180      * @param atoz1 first AToZDirection
181      * @param atoz2 second AToZDirection
182      * @return Boolean result true if equal, false if not
183      */
184     private Boolean egalAtoZDirection(AToZDirection atoz1, AToZDirection atoz2) {
185         LOG.info("comparing AtoZDirection ...");
186         Boolean result = true;
187         if (atoz1.getAToZ().size() == atoz2.getAToZ().size()) {
188             int index = 0;
189             int size = atoz1.getAToZ().size();
190             LOG.info("size : {}", size);
191             String id1 = null;
192             String id2 = null;
193             while (index < size) {
194                 id1 = atoz1.getAToZ().get(index).getId();
195                 LOG.info("id : {}", id1);
196                 Resource res1 = atoz1.getAToZ().get(index).getResource().getResource();
197                 LOG.info("res1 : {}", res1.toString());
198                 Resource res2 = null;
199                 if (id1 != null) {
200                     Boolean trouve = false;
201                     for (int loop = 0;loop < size;loop++) {
202                         id2 = atoz2.getAToZ().get(loop).getId();
203                         if (id2 != null && id2.compareTo(id1) == 0) {
204                             res2 = atoz2.getAToZ().get(loop).getResource().getResource();
205                             LOG.info("res2 : {}", res2.toString());
206                             trouve = true;
207                             break;
208                         }
209                     }
210                     if (trouve) {
211                         if (!egalResource(res1, res2)) {
212                             result = false;
213                             break;
214                         }
215                     }
216                 } else {
217                     result = false;
218                     break;
219                 }
220                 index++;
221             }
222         } else {
223             LOG.info("AToZDirection size is not equal !");
224             result = false;
225         }
226         return result;
227     }
228
229     /**
230      *  compare two ZtoZDirection.
231      *
232      * @param ztoa1 first ZToZDirection
233      * @param ztoa2 second ZToZDirection
234      * @return Boolean result true if equal, false if not
235      */
236     private Boolean egalZtoADirection(ZToADirection ztoa1, ZToADirection ztoa2) {
237         LOG.info("comparing ZtoADirection ...");
238         Boolean result = true;
239         if (ztoa1.getZToA().size() == ztoa2.getZToA().size()) {
240             int index = 0;
241             int size = ztoa1.getZToA().size();
242             LOG.info("size : {}", size);
243             String id1 = null;
244             String id2 = null;
245             while (index < size) {
246                 id1 = ztoa1.getZToA().get(index).getId();
247                 LOG.info("id : {}", id1);
248                 Resource res1 = ztoa1.getZToA().get(index).getResource().getResource();
249                 LOG.info("res1 : {}", res1.toString());
250                 Resource res2 = null;
251                 if (id1 != null) {
252                     Boolean trouve = false;
253                     for (int loop = 0;loop < size;loop++) {
254                         id2 = ztoa2.getZToA().get(loop).getId();
255                         if (id2 != null && id2.compareTo(id1) == 0) {
256                             res2 = ztoa2.getZToA().get(loop).getResource().getResource();
257                             LOG.info("res2 : {}", res2.toString());
258                             trouve = true;
259                             break;
260                         }
261                     }
262                     if (trouve) {
263                         if (!egalResource(res1, res2)) {
264                             result = false;
265                             break;
266                         }
267                     }
268                 } else {
269                     result = false;
270                     break;
271                 }
272                 index++;
273             }
274         } else {
275             result = false;
276         }
277         return result;
278     }
279
280     /**
281      * Test if resources Nodes
282      * not include in PathDescriptions.
283      *
284      * @param path PathDescriptions
285      * @param nodes Nodes List
286      * @return Boolean result true if found, false if not
287      */
288     private Boolean excludeNode(PathDescriptions path, List<String> nodes) {
289         LOG.info("Testing exclude Nodes ...");
290         Boolean result = false;
291         if (path != null && !nodes.isEmpty()) {
292             List<AToZ> list = path.getAToZDirection().getAToZ();
293             if (!list.isEmpty()) {
294                 int index = 0;
295                 boolean found = false;
296                 while (index < list.size() && !found) {
297                     Resource res = list.get(index).getResource().getResource();
298                     if (res != null && res instanceof Node) {
299                         Node node = (Node) res;
300                         for (String exclude : nodes) {
301                             if (exclude.compareToIgnoreCase(node.getNodeId()) == 0) {
302                                 LOG.info("Node not excluded !");
303                                 found = true;
304                                 break;
305                             }
306                         }
307                     }
308                     index++;
309                 }
310                 if (!found) {
311                     result = true;
312                 }
313             }
314         } else {
315             LOG.info("exclude parameters not corrrect !");
316         }
317         return result;
318     }
319
320     /**
321      * check if existing services not
322      * in pathDescriptions.
323      *
324      *
325      * @param existingService existing service list
326      * @param path PathDescriptions
327      * @param choice 0:Nodes, 1:Clli, 2:Srlg
328      * @return Boolean result true if found, false if not
329      */
330     private Boolean diversityService(List<String> existingService, PathDescriptions path, int choice) {
331         LOG.info("Testing diversity ...");
332         Boolean result = false;
333         if (path != null && choice >= 0 && !existingService.isEmpty()) {
334             int index = 0;
335             while (index < existingService.size()) {
336                 String tmp = existingService.get(index);
337                 if (tmp != null) {
338                     org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service
339                         .types.rev170426.service.path.PathDescription pathDesc = null;
340                     if (servicePathList != null && !servicePathList.isEmpty()) {
341                         for (ServicePaths service : servicePathList) {
342                             if (service.getServicePathName().compareTo(tmp) == 0) {
343                                 LOG.info("Existing Service '{}' found in ServicePathList ...", tmp);
344                                 pathDesc = service.getPathDescription();
345                             }
346                         }
347                     }
348                     if (pathDesc != null) {
349                         switch (choice) {
350                             case 0: //Nodes
351                                 LOG.info("Checking Node existing-service-applicability ...");
352                                 if (!egalAtoZDirection(path.getAToZDirection(), pathDesc.getAToZDirection())) {
353                                     result = true;
354                                     break;
355                                 }
356                                 break;
357                             case 1: //Clli
358                                 LOG.info("Checking clli existing-service-applicability ...");
359                                 break;
360
361                             case 2: //Srlg
362                                 LOG.info("Checking srlg existing-service-applicability ...");
363
364                                 break;
365                             default:
366                                 break;
367                         }
368                     } else {
369                         LOG.info("Existing Service '{}' not found in ServicePathList !", tmp);
370                         result = true;
371                     }
372                     if (!result) {
373                         break;
374                     }
375                 }
376                 index++;
377             }
378         } else {
379             LOG.info("Diversity parameters not coherent !");
380         }
381         return result;
382     }
383
384     /**
385      * test if pathDescription
386      * already exists in ServicePathList.
387      *
388      * @param path PathDescriptions
389      * @return <code>Boolean</code> result
390      */
391     private Boolean testPathDescription(PathDescriptions path) {
392         LOG.info("Testing pathDescription ...");
393         Boolean result = false;
394         if (path != null) {
395             LOG.info("retrieving path from servicePath List ...");
396             try {
397                 if (!servicePathList.isEmpty()) {
398                     LOG.info("ServicePathList not empty, contains {} paths.", servicePathList.size());
399                     for (ServicePaths service : servicePathList) {
400                         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service
401                             .types.rev170426.service.path.PathDescription tmp = service.getPathDescription();
402                         if (tmp != null) {
403                             if (path.getAToZDirection() != null && tmp.getAToZDirection() != null
404                                     && egalAtoZDirection(path.getAToZDirection(), tmp.getAToZDirection())) {
405                                 if (path.getZToADirection() != null && tmp.getZToADirection() != null
406                                         && egalZtoADirection(path.getZToADirection(), tmp.getZToADirection())) {
407                                     result = true;
408                                     LOG.info("Path already present in servicePath List");
409                                     break;
410                                 } else {
411                                     LOG.info("ZtoADirection not equal or one of ZtoADirection is null!");
412                                     break;
413                                 }
414                             } else {
415                                 LOG.info("AtoZDirection not equal !");
416                                 break;
417                             }
418
419                         }
420                     }
421                 } else {
422                     LOG.info("ServicePathList is empty");
423                 }
424             } catch (NullPointerException e) {
425                 LOG.error("ServicePathList is empty");
426             }
427         } else {
428             LOG.info("PathDescriptions is null !");
429             result = true;
430         }
431         LOG.info("testPathDescription result : {}", result);
432         return result;
433     }
434
435     /**
436      * function to retrieve Paths based on
437      * AEnd and ZEnd.
438      *
439      * @param aendNodeId Aend Node Id
440      * @param zendNodeId Zend Node Id
441      * @return result PathDescriptions List
442      */
443     private List<PathDescriptions> retrievePath(String aendNodeId, String zendNodeId) {
444         List<PathDescriptions> result = new ArrayList<PathDescriptions>();
445         List<PathDescriptions> paths = readPathDescriptionList();
446         if (!paths.isEmpty() && aendNodeId != null && zendNodeId != null) {
447             LOG.info("retrieving paths from pathDescription List for {} / {}", aendNodeId, zendNodeId);
448             for (PathDescriptions tmp : paths) {
449                 Resource pathAend = null;
450                 Resource pathZend = null;
451                 String id = null;
452                 if (tmp != null) {
453                     LOG.info("Getting Aend & ZEnd from path '{}'...",tmp.getPathName());
454                     int index = 0;
455                     int size = tmp.getAToZDirection().getAToZ().size();
456                     while (index < size) {
457                         id = tmp.getAToZDirection().getAToZ().get(index).getId();
458                         if (id.compareToIgnoreCase("1") == 0) {
459                             Resource resource = tmp.getAToZDirection().getAToZ().get(index).getResource()
460                                     .getResource();
461                             LOG.info("{} : {}", resource.getClass().toString(), resource.toString());
462                             pathAend = resource;
463                             break;
464                         }
465                         index++;
466                     }
467                     index = 0;
468                     while (index < size) {
469                         id = tmp.getZToADirection().getZToA().get(index).getId();
470                         if (id.compareToIgnoreCase("1") == 0) {
471                             Resource resource = tmp.getZToADirection().getZToA().get(index).getResource()
472                                     .getResource();
473                             LOG.info(resource.toString());
474                             pathZend = resource;
475                             break;
476                         }
477                         index++;
478                     }
479                     if (pathAend != null && pathZend != null) {
480                         LOG.info("pathAend : {} - pathZend: {}",pathAend, pathZend);
481                         LOG.info("aendNodeId : {} - zendNodeId : {}", aendNodeId, zendNodeId);
482                         if (comp(pathAend, pathZend, aendNodeId, zendNodeId)) {
483                             LOG.info("PathDescription found !");
484                             result.add(tmp);
485                         }
486                     }
487                 }
488             }
489         }
490         return result;
491     }
492
493
494     /**
495      * found Pathdescriptions with
496      * name containing an expression.
497      *
498      * @param pathdescr PathDescriptions List
499      * @param contain String expression
500      * @return PathDescriptionsOrdered List
501      */
502     private SortedSet<PathDescriptionsOrdered> foundpath(List<PathDescriptions> pathdescr, String contain) {
503         SortedSet<PathDescriptionsOrdered> result = new TreeSet<PathDescriptionsOrdered>();
504         ListIterator<PathDescriptions> it = pathdescr.listIterator();
505         int odr = 0;
506         while (it.hasNext()) {
507             PathDescriptions path = it.next();
508             String name = path.getPathName();
509             LOG.info("path  : {}", name);
510             if (name != null && name.contains(contain)) {
511                 LOG.info("    path gets : {}", name);
512                 String [] split = name.split("_");
513                 if (split.length == 3) {
514                     odr = Integer.parseInt(split[2]);
515                     result.add(new PathDescriptionsOrdered(path, odr));
516                 }
517             }
518         }
519         return result;
520     }
521
522     /**
523      * order PathDescriptions List
524      * with first, ordered direct links
525      * and secondly ordered indirect
526      * links.
527      *
528      * @param pathdescr PathDescriptions List
529      * @return PathDescriptions List ordered
530      */
531     private List<PathDescriptions> orderPathdescriptionsList(List<PathDescriptions> pathdescr) {
532         SortedSet<PathDescriptionsOrdered> direct = new TreeSet<PathDescriptionsOrdered>();
533         SortedSet<PathDescriptionsOrdered> indirect = new TreeSet<PathDescriptionsOrdered>();
534         List<PathDescriptions> result = new ArrayList<PathDescriptions>();
535         int size = pathdescr.size();
536         if (size > 0) {
537             LOG.info("getting direct path first ...");
538             direct = foundpath(pathdescr, "_direct_");
539             LOG.info("getting indirect path first ...");
540             indirect = foundpath(pathdescr, "_indirect_");
541         }
542         if (direct.size() > 0) {
543             Iterator<PathDescriptionsOrdered> itset = direct.iterator();
544             while (itset.hasNext()) {
545                 result.add(itset.next().getPathDescriptions());
546             }
547             if (indirect.size() > 0) {
548                 Iterator<PathDescriptionsOrdered> itset2 = indirect.iterator();
549                 while (itset2.hasNext()) {
550                     result.add(itset2.next().getPathDescriptions());
551                 }
552             }
553
554         } else if (indirect.size() > 0) {
555             Iterator<PathDescriptionsOrdered> itset2 = indirect.iterator();
556             while (itset2.hasNext()) {
557                 result.add(itset2.next().getPathDescriptions());
558             }
559         }
560         if (result.size() == pathdescr.size()) {
561             return result;
562         } else {
563             return null;
564         }
565     }
566
567     public ListenableFuture<Boolean> cancelResourceReserve() {
568         LOG.info("In cancelResourceReserve request ...");
569         setSuccess(false);
570         return executor.submit(new Callable<Boolean>() {
571             @Override
572             public Boolean call() throws Exception {
573                 Boolean output = false;
574                 if (cancelInput != null) {
575                     Boolean found = false;
576                     String name = cancelInput.getServiceName();
577                     if (name != null && !servicePathList.isEmpty()) {
578                         for (ServicePaths service : servicePathList) {
579                             if (name.compareTo(service.getServicePathName()) == 0) {
580                                 LOG.info("ServicePaths found in ServicePathList !!!");
581                                 found = true;
582                                 break;
583                             }
584                         }
585                         if (found) {
586                             LOG.info("removing servicePaths from datastore ...");
587                             if (writeOrDeleteServicePathList(name,1)) {
588                                 LOG.info("Service deleted !");
589                                 setSuccess(true);
590                                 output = true;
591                             } else {
592                                 LOG.info("service deletion failed !");
593                             }
594                         }
595                     } else {
596                         LOG.info("serviceName is null or servicePathList is empty !");
597                     }
598                 } else {
599                     LOG.info("cancelresourcereserveinput parameter not valid !");
600                 }
601                 return output;
602             }
603         });
604     }
605
606     public ListenableFuture<Boolean> pathComputation() {
607         LOG.info("In pathComputation request ...");
608         setSuccess(false);
609         return executor.submit(new Callable<Boolean>() {
610             @Override
611             public Boolean call() throws Exception {
612                 Boolean output = false;
613                 List<PathDescriptions> pathsList = new ArrayList<PathDescriptions>();
614                 PathDescriptions path = null;
615                 int index ;
616                 Boolean constraints = false;
617                 if (input != null) {
618                     HardConstraints inputHard = input.getHardConstraints();
619                     SoftConstraints inputSoft = input.getSoftConstraints();
620                     if (inputHard != null || inputSoft != null) {
621                         constraints = true;
622                     }
623                     path = null;
624                     pathsList =  retrievePath(input.getServiceAEnd().getNodeId(), input.getServiceZEnd()
625                             .getNodeId());
626                     index = 0;
627                     output = false;
628                     /** get pathList ordered. */
629                     pathsList = orderPathdescriptionsList(pathsList);
630                     if (!pathsList.isEmpty()) {
631                         LOG.info("{} Paths get from Pathdescription List", pathsList.size());
632                         index = 0;
633                         output = false;
634                         while (index < pathsList.size()) {
635                             path = pathsList.get(index);
636                             LOG.info("path n°{} gets : '{}'!", index, path.getPathName());
637                             if (constraints) {
638                                 LOG.info("Calculating path with constraints ...");
639                                 if (inputHard.getCoRoutingOrGeneral() instanceof General) {
640                                     General general = (General)inputHard.getCoRoutingOrGeneral();
641                                     if (general != null) {
642                                         Diversity diversity = general.getDiversity();
643                                         if (diversity != null) {
644                                             LOG.info("Getting diversity ...");
645                                             List<String> existingService = diversity.getExistingService();
646                                             if (existingService.size() > 0) {
647                                                 LOG.info("Getting existing service applicability ...");
648                                                 int choice = -1;
649                                                 if (choice < 0
650                                                         && diversity.getExistingServiceApplicability().isNode()) {
651                                                     LOG.info("existing-service-applicability : Node");
652                                                     choice = 0;
653                                                 }
654                                                 if (choice < 0
655                                                         && diversity.getExistingServiceApplicability().isClli()) {
656                                                     LOG.info("existing-service-applicability : Clli");
657                                                     choice = 1;
658                                                 }
659                                                 if (choice < 0
660                                                         && diversity.getExistingServiceApplicability().isSrlg()) {
661                                                     LOG.info("existing-service-applicability : Srlg");
662                                                     choice = 2;
663                                                 }
664                                                 if (!diversityService(existingService, path, choice)) {
665                                                     error = "existing service applicability not satisfied";
666                                                     LOG.info(error);
667                                                     path = null;
668                                                 }
669                                             }
670                                         }
671                                         Exclude exclude = general.getExclude();
672                                         if (exclude != null) {
673                                             LOG.info("Getting exclude ...");
674                                             if (!excludeNode(path, exclude.getNodeId())) {
675                                                 error = "Exclude node constraints not satisfied !";
676                                                 LOG.info(error);
677                                                 path = null;
678                                             }
679                                         }
680                                     }
681                                 }
682                             }
683                             if (!testPathDescription(path)) {
684                                 LOG.info("Process finish !");
685                                 output = true;
686                                 break;
687                             }
688                             index++;
689                         }
690                     } else {
691                         LOG.info("failed to retrieve path from PathDescription List !");
692                     }
693                 } else {
694                     LOG.info("pathComputationRequestInput parameter not valid !");
695                 }
696                 if (path != null) {
697                     LOG.info("Path ok !");
698                     pathDescription = new PathDescriptionBuilder()
699                             .setAToZDirection(path.getAToZDirection())
700                             .setZToADirection(path.getZToADirection());
701                     if (input.isResourceReserve()) {
702                         LOG.info("reserving pce resource ...");
703                         setPathDescription(pathDescription);
704                         if (writeOrDeleteServicePathList(input.getServiceName(), 0)) {
705                             LOG.info("write ServicePaths to datastore");
706                             setSuccess(true);
707                         } else {
708                             LOG.error("writing ServicePaths to datastore failed ! ");
709                         }
710                     } else {
711                         LOG.info("no pce resource reserve !");
712                         setSuccess(true);
713                     }
714                 }
715                 return output;
716             }
717         });
718     }
719
720
721     /**
722      * get all ServicePaths in ServicePathlist.
723      *
724      * @return <code>ServicePaths List</code>
725      */
726     private List<ServicePaths> readServicePathList() {
727         LOG.info("Reading ServicePathList ...");
728         List<ServicePaths> result = null;
729         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
730         InstanceIdentifier<ServicePathList> iid = InstanceIdentifier.create(ServicePathList.class);
731         Future<Optional<ServicePathList>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
732         Optional<ServicePathList> optional = Optional.absent();
733         try {
734             optional = Futures.getChecked(future, ExecutionException.class, 60, TimeUnit.SECONDS);
735         } catch (ExecutionException e) {
736             LOG.error("Reading service failed:", e);
737         }
738         if (optional.isPresent()) {
739             LOG.info("ServicePath List present !");
740             result = optional.get().getServicePaths();
741         }
742         return result;
743     }
744
745
746     private List<PathDescriptions> readPathDescriptionList() {
747         LOG.info("Reading PathDescriptionsList ...");
748         List<PathDescriptions> result = null;
749         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
750         InstanceIdentifier<PathDescriptionList> iid = InstanceIdentifier.create(PathDescriptionList.class);
751         Future<Optional<PathDescriptionList>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,iid);
752         Optional<PathDescriptionList> optional = Optional.absent();
753         try {
754             optional = Futures.getChecked(future, ExecutionException.class, 60, TimeUnit.SECONDS);
755         } catch (ExecutionException e) {
756             LOG.error("Reading service failed:", e);
757         }
758         if (optional.isPresent()) {
759             LOG.info("ServicePath List present !");
760             result = optional.get().getPathDescriptions();
761         }
762         return result;
763
764     }
765
766     /**
767      * Write or Delete ServicePaths
768      * for ServicePathList.
769      *
770      * @param serviceName Service Name
771      * @param choice 0 : write or 1 : delete
772      * @return Boolean result true if deleted, false if not
773      */
774     private Boolean writeOrDeleteServicePathList(String serviceName, int choice) {
775         Boolean result = null;
776         if (serviceName != null && serviceName.compareTo(" ") != 0 && choice >= 0 && choice < 2) {
777             LOG.info("WriteOrDeleting '{}' ServicePaths", serviceName);
778             WriteTransaction writeTx = db.newWriteOnlyTransaction();
779             result = true;
780             String action = null;
781             InstanceIdentifier<ServicePaths> iid = InstanceIdentifier.create(ServicePathList.class)
782                     .child(ServicePaths.class,new ServicePathsKey(serviceName));
783             Future<Void> future = null;
784             switch (choice) {
785                 case 0: /** Write. */
786                     LOG.info("Writing '{}' Service", serviceName);
787                     org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.service
788                         .path.PathDescriptionBuilder path = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
789                         ._interface.service.types.rev170426.service.path.PathDescriptionBuilder();
790                     if (pathDescription != null) {
791                         if (pathDescription.getAToZDirection() != null) {
792                             path.setAToZDirection(pathDescription.getAToZDirection());
793                         }
794                         if (pathDescription.getZToADirection() != null) {
795                             path.setZToADirection(pathDescription.getZToADirection());
796                         }
797                         LOG.info("pathdescription gets");
798                     }
799                     ServiceAEnd aend = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
800                             ._interface.service.types.rev170426.service.path.ServiceAEndBuilder(input.getServiceAEnd())
801                             .build();
802                     ServiceZEnd zend = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
803                             ._interface.service.types.rev170426.service.path.ServiceZEndBuilder(input.getServiceZEnd())
804                             .build();
805                     ServicePaths service = new ServicePathsBuilder()
806                             .setServicePathName(serviceName)
807                             .setServiceHandlerHeader(new ServiceHandlerHeaderBuilder()
808                                     .setRequestId(input.getServiceHandlerHeader().getRequestId()).build())
809                             .setServiceAEnd(aend)
810                             .setServiceZEnd(zend)
811                             .setHardConstraints(input.getHardConstraints())
812                             .setSoftConstraints(input.getSoftConstraints())
813                             .setPathDescription(path.build())
814                             .build();
815                     LOG.info("Servicepath build");
816                     writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
817                     action = "write";
818                     //CheckedFuture<Void, OperationFailedException> future = transaction.submit();
819                     future = writeTx.submit();
820                     try {
821                         LOG.info("Sending '{}' command to datastore !", action);
822                         Futures.getChecked(future, ExecutionException.class);
823                     } catch (ExecutionException e) {
824                         LOG.error("Failed to {} service from Service List", action);
825                         result = false;
826                     }
827                     break;
828
829                 case 1: /** Delete */
830                     LOG.info("Deleting '{}' Service", serviceName);
831                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
832                     action = "delete";
833                     future = writeTx.submit();
834                     try {
835                         LOG.info("Sending '{}' command to datastore !", serviceName);
836                         Futures.getChecked(future, ExecutionException.class);
837                     } catch (ExecutionException e) {
838                         LOG.error("Failed to {} service from Service List", serviceName);
839                         result = false;
840                     }
841                     break;
842
843                 default:
844                     LOG.info("No choice found");
845                     break;
846
847             }
848
849         } else {
850             LOG.info("Parameters not correct !");
851         }
852         return result;
853     }
854
855     public PathDescriptionBuilder getPathDescription() {
856         return pathDescription;
857     }
858
859     public void setPathDescription(PathDescriptionBuilder pathDescription) {
860         this.pathDescription = pathDescription;
861     }
862
863     public Boolean getSuccess() {
864         return success;
865     }
866
867     public void setSuccess(Boolean success) {
868         this.success = success;
869     }
870
871     public PathComputationRequestInput getInput() {
872         return input;
873     }
874
875     public void setInput(PathComputationRequestInput input) {
876         this.input = input;
877     }
878
879     public CancelResourceReserveInput getCancelInput() {
880         return cancelInput;
881     }
882
883     public void setCancelInput(CancelResourceReserveInput input) {
884         this.cancelInput = input;
885     }
886
887     public DataBroker getDb() {
888         return db;
889     }
890
891     public void setDb(DataBroker db) {
892         this.db = db;
893     }
894
895     public String getError() {
896         return error;
897     }
898
899     public void setError(String error) {
900         this.error = error;
901     }
902
903     public List<ServicePaths> getServicePathList() {
904         return servicePathList;
905     }
906
907     public void setServicePathList(List<ServicePaths> servicePathList) {
908         this.servicePathList = servicePathList;
909     }
910 }