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