Servicehandler Tests
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / topology / InterNodePath.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.topology;
11
12 import com.google.common.collect.Lists;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.ListIterator;
17
18 import org.opendaylight.transportpce.stubpce.TpNodeTp;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirection;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirectionBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirection;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirectionBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZ;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZKey;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToA;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToABuilder;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToAKey;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.Resource;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.ResourceBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Link;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.LinkBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.TerminationPoint;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * class to build all path between superNode elements
39  * ( XPDR and ROADM).
40  *
41  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
42  *         behalf of Orange
43  */
44
45 public class InterNodePath {
46     /** Logging. */
47     private static final Logger LOG = LoggerFactory.getLogger(InterNodePath.class);
48     private SuperNode superNode;
49     private List<NodePath> nodepaths;
50     private List<AToZDirection> atoz;
51     private List<ZToADirection> ztoa;
52
53     /**
54      * InterNodePath constructor.
55      *
56      * @param supernode Supernode
57      */
58     public InterNodePath(SuperNode supernode) {
59         setSuperNode(supernode);
60         setNodepaths(new ArrayList<NodePath>());
61         setAtoz(new ArrayList<AToZDirection>());
62         setZtoa(new ArrayList<ZToADirection>());
63     }
64
65     /**
66      * build AToZdirection.
67      *
68      * @param paths <code>Path</code> List
69      */
70     private void getAToZDirection(List<Path> paths) {
71         int order = 0;
72         AToZDirectionBuilder atozDirection = new AToZDirectionBuilder();
73         List<AToZ> atozList = new ArrayList<AToZ>();
74         TpNodeTp tpNodeTp;
75         for (Path path : paths) {
76             tpNodeTp = path.getTpNodeTp();
77             tpNodeTp.createAToZListHop(order);
78             for (AToZ atoz : tpNodeTp.getAToZ()) {
79                 atozList.add(atoz);
80                 order++;
81             }
82             Link link = path.getLink();
83             AToZKey atozKey = new AToZKey(Integer.toString(order));
84             Resource resource = new ResourceBuilder().setResource(link).build();
85             AToZ hop = new AToZBuilder().setId(atozKey.getId()).setKey(atozKey).setResource(resource).build();
86             atozList.add(hop);
87             order++;
88         }
89         atozDirection.setRate((long) 100).setAToZWavelengthNumber((long) 200).setAToZ(atozList);
90         atoz.add(atozDirection.build());
91     }
92
93     /**
94      * build ZToAdirection.
95      *
96      * @param paths <code>Path</code> List
97      */
98     private void getZToADirection(List<Path> paths) {
99         int order = 0;
100         ZToADirectionBuilder ztoaDirection = new ZToADirectionBuilder();
101         List<ZToA> ztoaList = new ArrayList<ZToA>();
102         TpNodeTp tpNodeTp;
103         for (Path path : paths) {
104             tpNodeTp = path.getTpNodeTp();
105             tpNodeTp.createZToAListHop(order);
106             for (ZToA ztoa : tpNodeTp.getZToA()) {
107                 ztoaList.add(ztoa);
108                 order++;
109             }
110             Link link = path.getLink();
111             ZToAKey ztoaKey = new ZToAKey(Integer.toString(order));
112             Resource resource = new ResourceBuilder().setResource(link).build();
113             ZToA hop = new ZToABuilder().setId(ztoaKey.getId()).setKey(ztoaKey).setResource(resource).build();
114             ztoaList.add(hop);
115             order++;
116         }
117         ztoaDirection.setRate((long) 100).setZToAWavelengthNumber((long) 200).setZToA(ztoaList);
118         ztoa.add(ztoaDirection.build());
119     }
120
121     /**
122      * Build direction path
123      * for all Supernode elements.
124      *
125      * @param skip Boolean to specify an ROADM without XPDR
126      * @param nodeId Supernode element nodeId
127      * @param zend Supernode path ZEnd nodeId
128      */
129     public void build(Boolean skip, String nodeId,String zend) {
130         LOG.info("Building direction ...");
131         if (skip) {
132             buildDegToDeg(false);
133             buildDegToDeg(true);
134         } else {
135             boolean reverse = false;
136             if (nodeId.contains(zend)) {
137                 reverse = true;
138             }
139             NodePath xpdr = null;
140             NodePath srg = null;
141             NodePath deg1 = null;
142             NodePath deg2 = null;
143             for (NodePath node : nodepaths) {
144                 //LOG.info(node.toString());
145                 String id = node.getNodeId();
146                 if (id.contains("XPDR")) {
147                     xpdr = node;
148                 }
149                 if (id.contains("SRG")) {
150                     srg = node;
151                 }
152                 if (id.contains("DEG1")) {
153                     deg1 = node;
154                 }
155                 if (id.contains("DEG2")) {
156                     deg2 = node;
157                 }
158             }
159             buildXpdrToSrgToDeg(xpdr,srg,deg1,deg2,reverse);
160             buildDegToSrgToXpdr(xpdr,srg,deg1,deg2,reverse);
161         }
162     }
163
164     /**
165      * build direction path for
166      * Supernode element DEG1, DEG2.
167      *
168      * @param twotoOne boolean to determine direction
169      */
170     private void buildDegToDeg(boolean twotoOne) {
171         LOG.info("buildDegToDeg ...");
172         NodePath deg1 = null;
173         NodePath deg2 = null;
174         for (NodePath node : nodepaths) {
175             //LOG.info(node.toString());
176             String nodeId = node.getNodeId();
177             if (nodeId.contains("DEG1")) {
178                 deg1 = node;
179             }
180             if (nodeId.contains("DEG2")) {
181                 deg2 = node;
182             }
183         }
184         if (deg1 != null && deg2 != null) {
185             List<Path> result =  new ArrayList<Path>();
186             NodePath deb = deg1;
187             NodePath end = deg2;
188             if (twotoOne) {
189                 deb = deg2;
190                 end = deg1;
191             }
192             for (Path deg1Path : deb.getPath()) {
193                 TpNodeTp tmpdeg = deg1Path.getTpNodeTp();
194                 if (tmpdeg.getTpIn().getTpId().contains("TTP")) {
195                     result.clear();
196                     result.addAll(Lists.newArrayList(deg1Path));
197                     if (!twotoOne) {
198                         getAToZDirection(result);
199                     } else {
200                         getZToADirection(result);
201                     }
202                 }
203             }
204             for (Path deg2Path : end.getPath()) {
205                 TpNodeTp tmpdeg = deg2Path.getTpNodeTp();
206                 if (tmpdeg.getTpIn().getTpId().contains("CTP")) {
207                     result.clear();
208                     result.addAll(Lists.newArrayList(deg2Path));
209                     if (!twotoOne) {
210                         getAToZDirection(result);
211                     } else {
212                         getZToADirection(result);
213                     }
214                 }
215             }
216         } else {
217             LOG.info("deg1 or/and deg2 is null !");
218         }
219     }
220
221     /**
222      build direction from
223      *Degree  to SRG to XPDR
224      *in Supernode elements.
225      *
226      *@param xpdr NodePath XPDR
227      *@param srg NodePath SRG
228      *@param deg NodePath Degree
229      *@param reverse boolean to determine the direction
230      */
231     private void buildDeg(NodePath xpdr,NodePath srg,NodePath deg, Boolean reverse) {
232         List<Path> result =  new ArrayList<Path>();
233         for (Path degPath : deg.getPath()) {
234             TpNodeTp tmpdeg = degPath.getTpNodeTp();
235             if (tmpdeg.getTpIn().getTpId().contains("TTP")) {
236                 for (Path srgPath : srg.getPath()) {
237                     TpNodeTp tmpsrg = srgPath.getTpNodeTp();
238                     if (tmpsrg.getTpIn().getTpId().contains("CP")) {
239                         for (Path xpdrPath : xpdr.getPath()) {
240                             TpNodeTp tmpxpdr = xpdrPath.getTpNodeTp();
241                             if (tmpxpdr.getTpIn().getTpId().contains("NETWORK")) {
242                                 result.clear();
243                                 result.addAll(Lists.newArrayList(degPath,srgPath,xpdrPath));
244                                 if (reverse) {
245                                     getAToZDirection(result);
246                                 } else {
247                                     getZToADirection(result);
248                                 }
249                             }
250                         }
251                     }
252                 }
253             }
254         }
255     }
256
257     /**
258      *build direction from
259      *DEG1 / DEG2  to SRG to XPDR
260      *in Supernode elements.
261      *
262      *@param xpdr NodePath XPDR
263      *@param srg NodePath SRG
264      *@param deg1 NodePath Degree 1
265      *@param deg2 NodePath Degree 2
266      *@param reverse boolean to determine the direction
267      */
268     private void buildDegToSrgToXpdr(NodePath xpdr,NodePath srg,NodePath deg1,NodePath deg2,boolean reverse) {
269         LOG.info("buildDegToSrgToXpr ...");
270         if (xpdr != null && srg != null && deg1 != null && deg2 != null) {
271             buildDeg(xpdr, srg, deg1, reverse);
272             buildDeg(xpdr, srg, deg2, reverse);
273         } else {
274             LOG.info("xpdr, deg or/and srg is null !");
275         }
276     }
277
278     /**
279      *build direction from
280      *XPDR to SRG to DEG
281      *in Supernode elements.
282      *
283      *@param xpdr NodePath XPDR
284      *@param srg NodePath SRG
285      *@param deg1 NodePath Degree 1
286      *@param deg2 NodePath Degree 2
287      *@param reverse boolean to determine the direction
288      */
289     private void buildXpdrToSrgToDeg(NodePath xpdr,NodePath srg,NodePath deg1,NodePath deg2,boolean reverse) {
290         LOG.info("buildXpdrToSrgToDeg ...");
291         if (xpdr != null && srg != null && deg1 != null && deg2 != null) {
292             List<Path> result =  new ArrayList<Path>();
293             for (Path xpdrPath : xpdr.getPath()) {
294                 TpNodeTp tmpxpdr = xpdrPath.getTpNodeTp();
295                 if (tmpxpdr.getTpIn().getTpId().contains("CLIENT")) {
296                     for (Path srgPath : srg.getPath()) {
297                         TpNodeTp tmp = srgPath.getTpNodeTp();
298                         Link srglink = srgPath.getLink();
299                         if (tmp.getTpIn().getTpId().contains("PP")) {
300                             for (Path deg1Path : deg1.getPath()) {
301                                 TpNodeTp tmpdeg = deg1Path.getTpNodeTp();
302                                 if (tmpdeg.getTpIn().getTpId().contains("CTP")
303                                         && srglink.getLinkId().contains("DEG1")) {
304                                     result.clear();
305                                     result.addAll(Lists.newArrayList(xpdrPath,srgPath,deg1Path));
306                                     if (reverse) {
307                                         getZToADirection(result);
308                                     } else {
309                                         getAToZDirection(result);
310                                     }
311                                 }
312                             }
313                             for (Path deg2Path : deg2.getPath()) {
314                                 TpNodeTp tmpdeg = deg2Path.getTpNodeTp();
315                                 if (tmpdeg.getTpIn().getTpId().contains("CTP")
316                                         && srglink.getLinkId().contains("DEG2")) {
317                                     result.clear();
318                                     result.addAll(Lists.newArrayList(xpdrPath,srgPath,deg2Path));
319                                     if (reverse) {
320                                         getZToADirection(result);
321                                     } else {
322                                         getAToZDirection(result);
323                                     }
324                                 }
325                             }
326                         }
327                     }
328                 }
329             }
330         } else {
331             LOG.info("xpdr, deg or/and srg is null !");
332         }
333     }
334
335     /**
336      * get all AToZdirection containing
337      * a list of <code>AToZ</code> end by
338      * a String.
339      *
340      * @param endBy String
341      * @param atozDirection AToZdirection List
342      * @param index Integer to determine if it for last Link(1) or last TerminationPoint(2)
343      * @return <code>AToZDirection</code> List
344      */
345     public List<AToZDirection> getAToZDirectionEndBy(String endBy, List<AToZDirection> atozDirection, int index) {
346         List<AToZDirection> result = new ArrayList<AToZDirection>();
347         for (AToZDirection tmp : atozDirection) {
348             List<AToZ> atozList = tmp.getAToZ();
349             int size = atozList.size();
350             if (size > 2) {
351                 String id = Integer.toString(size - index);
352                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription
353                     .rev170426.pce.resource.resource.Resource res = null;
354                 for (AToZ atoz : atozList) {
355                     if (atoz.getId().compareTo(id) == 0) {
356                         res = atoz.getResource().getResource();
357                         if (res != null) {
358
359                             switch (index) {
360                                 case 1: //last link
361                                     if (res instanceof Link) {
362                                         Link tp = (Link) res;
363                                         if (tp != null && tp.getLinkId().contains(endBy)) {
364                                             result.add(tmp);
365                                         }
366                                     }
367                                     break;
368
369                                 case 2: //last tp
370                                     if (res instanceof TerminationPoint) {
371                                         TerminationPoint tp = (TerminationPoint) res;
372                                         if (tp != null && tp.getTpId().contains(endBy)) {
373                                             result.add(tmp);
374                                         }
375                                     }
376                                     break;
377
378                                 default :
379                                     break;
380                             }
381                         }
382                     }
383                 }
384             } else {
385                 LOG.info("firstnodeTpId is null !");
386             }
387         }
388         LOG.info("getAToZDirectionEndBy result size : {}\n{}", result.size(),result.toString());
389         return result;
390     }
391
392     /**
393      * replace or remove in
394      * Link in <code>AToZ</code> list
395      * containing in AToZdirection.
396      *
397      * @param endBy String
398      * @param beginBy String
399      * @param atozLink name of ROAMDTOROAMD link
400      * @param atozDirection AToZdirection List
401      * @param remove boolean to determine if removing or not
402      * @return <code>AToZDirection</code> List
403      */
404     public List<AToZDirection> replaceOrRemoveAToZDirectionEndLink(String endBy,String beginBy,String atozLink,
405             List<AToZDirection> atozDirection, boolean remove) {
406         List<AToZDirection> result = new ArrayList<AToZDirection>();
407         List<AToZDirection> tmp = new ArrayList<AToZDirection>();
408         if (remove) {
409             tmp = getAToZDirectionEndBy(endBy, atozDirection, 1);
410             if (!tmp.isEmpty()) {
411                 tmp = getAToZDirectionBeginBy(beginBy, tmp);
412             }
413         } else {
414             tmp = getAToZDirectionEndBy(endBy, atozDirection,2);
415         }
416         if (!tmp.isEmpty()) {
417             for (AToZDirection atozdir : tmp) {
418                 List<AToZ> atozList = atozdir.getAToZ();
419                 int size = atozList.size();
420                 if (size > 0) {
421                     String id = Integer.toString(size - 1);
422                     for (ListIterator<AToZ> it = atozList.listIterator(); it.hasNext();) {
423                         AToZ atoz = it.next();
424                         if (atoz.getId().compareTo(id) == 0) {
425                             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription
426                                 .rev170426.pce.resource.resource.Resource res = atoz.getResource().getResource();
427                             if (res != null  && res instanceof Link) {
428                                 Link link = new LinkBuilder()
429                                         .setLinkId(atozLink)
430                                         .build();
431                                 AToZKey atozKey = new AToZKey(atoz.getKey());
432                                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
433                                     .pathdescription.rev170426.pce.resource.Resource resource = new ResourceBuilder()
434                                     .setResource(link).build();
435                                 AToZ hop = new AToZBuilder().setId(atozKey.getId()).setKey(atozKey)
436                                         .setResource(resource).build();
437                                 it.remove();
438                                 if (!remove) {
439                                     it.add(hop);
440                                 }
441                                 result.add(atozdir);
442                             }
443                         }
444                     }
445                 }
446             }
447         } else {
448             LOG.info("getAToZDirectionEndBy size : {}", tmp.size());
449         }
450         return result;
451     }
452
453     /**
454      *get all AToZdirection containing
455      * a list of <code>AToZ</code> begin by
456      * a String.
457      *
458      * @param beginBy String
459      * @param atozDirection AToZdirection List
460      * @return <code>AToZDirection</code> List
461      */
462     public List<AToZDirection> getAToZDirectionBeginBy(String beginBy, List<AToZDirection> atozDirection) {
463         List<AToZDirection> result = new ArrayList<AToZDirection>();
464         for (AToZDirection tmp : atozDirection) {
465             List<AToZ> atozList = tmp.getAToZ();
466             int size = atozList.size();
467             if (size > 0) {
468                 String id = Integer.toString(0);
469                 for (AToZ atoz : atozList) {
470                     if (atoz.getId().compareTo(id) == 0) {
471                         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription
472                             .rev170426.pce.resource.resource.Resource res = atoz.getResource().getResource();
473                         if (res != null  && res instanceof TerminationPoint) {
474                             TerminationPoint tp = (TerminationPoint) res;
475                             if (tp != null && tp.getTpId().contains(beginBy)) {
476                                 LOG.info("tmp : {}", tmp.toString());
477                                 result.add(tmp);
478                             }
479                         }
480                     }
481                 }
482             }
483         }
484         LOG.info("result size : {}", result.size());
485         return result;
486     }
487
488     /**
489      * first launch process to
490      * build/fill <code>NodePath</code>
491      * And after that, start building
492      * direction between Supernode
493      * elements.
494      *
495      * @param zend path zend Supernode nodeId
496      */
497     public void buildPath(String zend) {
498         if (superNode != null) {
499             for (org.opendaylight.transportpce.stubpce.topology.Resource res : superNode.getResources()) {
500                 NodePath path = new NodePath(res, superNode.getSuperNodeId(), superNode.isXpdrSrgAbsent());
501                 path.fill();
502                 nodepaths.add(path);
503             }
504             LOG.info("nodepaths size : {}", nodepaths.size());
505             build(superNode.isXpdrSrgAbsent(),superNode.getSuperNodeId(), zend);
506         }
507     }
508
509     public static void main(String[] args) {
510         Topology topo = new Topology();
511         topo.start();
512         Network net = topo.getNetwork();
513         if (net != null) {
514             SuperNode res = net.getSuperNodes().get(0);
515             String zend = "NodeZ";
516             LOG.info(res.getSuperNodeId().toString());
517             InterNodePath path = new InterNodePath(res);
518             path.buildPath(zend);
519         }
520     }
521
522     public SuperNode getSuperNode() {
523         return superNode;
524     }
525
526     public void setSuperNode(SuperNode superNode) {
527         this.superNode = superNode;
528     }
529
530     public List<AToZDirection> getAtoz() {
531         return atoz;
532     }
533
534     public void setAtoz(List<AToZDirection> atoz) {
535         this.atoz = atoz;
536     }
537
538     public List<ZToADirection> getZtoa() {
539         return ztoa;
540     }
541
542     public void setZtoa(List<ZToADirection> ztoa) {
543         this.ztoa = ztoa;
544     }
545
546     public List<NodePath> getNodepaths() {
547         return nodepaths;
548     }
549
550     public void setNodepaths(List<NodePath> nodepaths) {
551         this.nodepaths = nodepaths;
552     }
553
554 }