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