2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.stubpce.topology;
11 import com.google.common.collect.Lists;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.ListIterator;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.path.description.list.PathDescriptionsBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirection;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirectionBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirection;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirectionBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZ;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.atoz.direction.AToZKey;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToA;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToABuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ztoa.direction.ZToAKey;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.Resource;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.ResourceBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Link;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.LinkBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
39 * Class to build Path between
42 * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
45 public class SuperNodePath {
47 private static final Logger LOG = LoggerFactory.getLogger(SuperNodePath.class);
48 /** List of NodeLinkNode. */
49 private List<NodeLinkNode> paths;
50 /** Supernode topology. */
51 private Network network;
54 * SuperNodePath constructor.
56 * @param network Supernode topology
58 public SuperNodePath(Network network) {
59 setPaths(new ArrayList<NodeLinkNode>());
60 this.network = network;
64 * test if Supernode is an
65 * extremity of path to build.
67 * @param end extremity node Id
68 * @param supernodes Supernodes list to build path
69 * @return true if link extremity, false else
71 private Boolean endNode(String end, List<String> supernodes) {
72 Boolean result = false;
73 if (end != null && end.compareTo(" ") != 0) {
74 for (String node : supernodes) {
75 if (node.compareTo(end) == 0) {
88 * @param nodeId supernode id to get
89 * @return SuperNode supernode gets
91 public SuperNode getSuperNode(String nodeId) {
92 SuperNode result = null;
93 if (network != null) {
94 for (SuperNode tmp : network.getSuperNodes()) {
95 if (tmp.getSuperNodeId().compareTo(nodeId) == 0) {
108 * @param aend begin extremity
109 * @param zend end extremity
110 * @param links Roadm to Roadm links
111 * @param direct determine if link is direct or not
112 * @return String list of links name
114 private List<String> findLinks(String aend, String zend, List<String> links, boolean direct) {
115 List<String> result = new ArrayList<String>();
116 if (links.size() > 0) {
117 aend = aend.replace("Node", "ROADM");
118 zend = zend.replace("Node", "ROADM");
119 String atozlink = null;
120 String ztoalink = null;
121 for (String tmp : links) {
122 if (tmp.contains(aend)
123 && tmp.contains(zend)) {
124 LOG.info("direct path found for : {} / {}", aend, zend);
125 if (tmp.startsWith(aend)) {
128 if (tmp.startsWith(zend)) {
131 if (atozlink != null && ztoalink != null) {
132 result.add(atozlink.concat("/").concat(ztoalink));
139 LOG.info("no roadm-to-roadm links !");
145 *find next Supernode hop.
147 * @param link roadm to roadm link
148 * @param aend begin supernode
149 * @param node list of supernode id
150 * @return String supernodeId next hop
152 private String findHop(String link, String aend, List<String> node) {
153 String result = null;
154 aend = aend.replace("Node", "ROADM");
155 for (String tmp : node) {
156 tmp = tmp.replace("Node", "ROADM");
157 if (tmp.compareTo(aend) != 0) {
158 if (link.contains(aend) && link.contains(tmp)) {
159 LOG.info("hop : {}", tmp);
168 *get all Supernode in
171 * @return String list of Supernode Id
173 private List<String> getSuperNodeId() {
174 List<String> result = new ArrayList<String>();
175 if (network.getSuperNodes().size() > 0) {
176 for (SuperNode tmp : network.getSuperNodes()) {
177 result.add(tmp.getSuperNodeId());
184 * get all roadm to roadm
187 * @return String list of roadm to roadm links
189 private List<String> getRoadmLinks() {
190 List<String> result = new ArrayList<String>();
191 if (network.getRoadmToroadm().getLinks().size() > 0) {
192 for (String tmp : network.getRoadmToroadm().getLinks()) {
200 * create NodeLinkNode
203 * @param links String list of roadm to roadm links
204 * @param aend beginning Supernode
205 * @param zend ending Supernode
206 * @param direct determine if link is direct or not
208 private void fill(List<String> links,String aend, String zend, boolean direct) {
209 String term = "indirect";
213 if (!links.isEmpty()) {
214 List<String> atoz = new ArrayList<String>();
215 List<String> ztoa = new ArrayList<String>();
216 for (String tmp : links) {
217 String [] split = tmp.split("/");
218 if (split.length == 2) {
223 if (!atoz.isEmpty() && atoz.size() == ztoa.size()) {
224 NodeLinkNode node = new NodeLinkNode(aend, zend, atoz,ztoa,direct);
229 LOG.info("{} links not found !", term);
234 * launch SupernodePath process
235 * to build NodeLinkNode.
237 * @param aend beginning extremity path
238 * @param zend ending extremity path
240 public void run(String aend, String zend) {
241 if (network != null) {
242 List<String> supernodes = getSuperNodeId();
243 List<String> roadmLinks = getRoadmLinks();
244 if (aend != null && zend != null) {
245 int size = supernodes.size();
247 List<String> links = null;
249 if (endNode(aend, supernodes) && endNode(zend, supernodes)) {
250 LOG.info("Getting direct links ...");
251 links = new ArrayList<String>();
252 links = findLinks(aend,zend,roadmLinks,true);
253 fill(links, aend, zend, true);
254 LOG.info("Getting indirect links ..");
255 links = new ArrayList<String>();
256 for (String tmp : roadmLinks) {
257 hop = findHop(tmp, aend, supernodes);
259 if (hop.compareTo(zend.replace("Node", "ROADM")) != 0) {
260 LOG.info("next hop found : {}", hop);
261 links.addAll(findLinks(aend,hop,roadmLinks,false));
262 links.addAll(findLinks(hop,zend,roadmLinks,false));
268 fill(links, aend, zend, false);
270 LOG.info("aend or/and zend not exists !");
274 LOG.info("aend or/and is null !");
277 LOG.info("network is null !!");
283 * in AToZ List containing
286 * @param order beginning order
287 * @param atozDirection AToZdirection List
288 * @return AToZdirection List
290 public List<AToZDirection> modifyOrder(int order, List<AToZDirection> atozDirection) {
291 List<AToZDirection> result = new ArrayList<AToZDirection>();
292 for (AToZDirection tmp : atozDirection) {
293 List<AToZ> atozList = tmp.getAToZ();
294 int size = atozList.size();
296 for (ListIterator<AToZ> it = atozList.listIterator(); it.hasNext();) {
297 AToZ atoz = it.next();
298 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription
299 .rev170426.pce.resource.resource.Resource res = atoz.getResource().getResource();
300 int tmpkey = order + Integer.parseInt(atoz.getKey().getId());
301 AToZKey atozKey = new AToZKey(Integer.toString(tmpkey));
302 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
303 .pathdescription.rev170426.pce.resource.Resource resource = new ResourceBuilder()
304 .setResource(res).build();
305 AToZ hop = new AToZBuilder().setId(atozKey.getId()).setKey(atozKey).setResource(resource).build();
317 * merge two AToZdirection List.
319 * @param cleanInterA first AToZdirection List
320 * @param cleanInterZ second AToZdirection List
321 * @param first boolean to determine if it is the first time merge is done
322 * @return AToZDirection List
324 public List<AToZDirection> merge(List<AToZDirection> cleanInterA, List<AToZDirection> cleanInterZ,
326 List<AToZDirection> result = new ArrayList<AToZDirection>();
327 if (!cleanInterA.isEmpty()) {
328 int order = cleanInterA.get(0).getAToZ().size();
330 List<AToZDirection> modify = modifyOrder(order, cleanInterZ);
331 if (!modify.isEmpty()) {
332 for (AToZDirection tmp : cleanInterA) {
333 List<AToZ> atozList = new ArrayList<AToZ>(tmp.getAToZ());
334 for (AToZDirection add : modify) {
335 ListIterator<AToZ> it = atozList.listIterator();
336 /** on va a la fin de la liste */
337 while (it.hasNext()) {
340 List<AToZ> addList = add.getAToZ();
341 for (AToZ atoz : addList) {
344 AToZDirectionBuilder newDirection = new AToZDirectionBuilder();
345 newDirection.setRate((long) 100).setAToZWavelengthNumber((long) 200).setAToZ(atozList);
346 result.add(newDirection.build());
347 atozList = new ArrayList<AToZ>(tmp.getAToZ());
351 LOG.info("modify List is empty ! ");
354 LOG.info("order is not superior to 0");
357 if (first && !cleanInterZ.isEmpty()) {
358 LOG.info("first merge so result is a copy of second AToZDirection List !");
359 result = new ArrayList<AToZDirection>(cleanInterZ);
361 LOG.info("cleanInterA is empty !");
371 * @param atozLink atoz roadm link
372 * @param ztoaLink ztoa roadm link
373 * @return String list of degree
375 public List<String> getDeg(String atozLink, String ztoaLink) {
376 List<String> result = new ArrayList<String>();
377 if (atozLink != null && ztoaLink != null) {
378 String [] split = atozLink.split("-", 4);
379 if (split.length == 4) {
380 result = Lists.newArrayList(split[1],split[3]);
383 LOG.info("atozlink and/or ztoalink is null !");
390 * (ROADMA-DEG1-ROADMZ-DEG2
392 * ROADMZ-DEG2-ROADMA-DEG1).
394 * @param linkId Link name
395 * @return String link name reversed
397 public String reverseLinkId(String linkId) {
398 StringBuilder builder = new StringBuilder();
399 String [] split = linkId.split("-");
400 int size = split.length;
403 if (linkId.contains("XPDR")) {
404 if (linkId.startsWith("XPDR")) {
405 builder.append(split[1]).append("-")
406 .append(split[2]).append("-")
409 builder.append(split[2]).append("-")
410 .append(split[0]).append("-")
417 builder.append(split[2]).append("-")
418 .append(split[3]).append("-")
419 .append(split[0]).append("-")
426 return builder.toString();
430 * convert AToAdirection to
433 * @param atozDirection AToZdirection to convert
434 * @return ZToAdirection
436 public ZToADirection convertToZtoaDirection(AToZDirection atozDirection) {
437 ZToADirectionBuilder ztoaDirection = new ZToADirectionBuilder();
438 if (atozDirection != null) {
439 List<AToZ> atozList = atozDirection.getAToZ();
440 List<ZToA> ztoaList = new ArrayList<ZToA>();
441 if (!atozList.isEmpty()) {
442 List<AToZ> reverse = Lists.reverse(atozList);
443 /** Building path. */
444 ZToAKey ztoaKey = null;
445 Resource resource = null;
446 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription
447 .rev170426.pce.resource.resource.Resource resLink = null;
450 for (AToZ atoz : reverse) {
451 ztoaKey = new ZToAKey(Integer.toString(odr));
452 resLink = atoz.getResource().getResource();
453 if (resLink != null) {
454 if (resLink instanceof Link) {
455 Link link = (Link) resLink;
456 String newLinkId = reverseLinkId(link.getLinkId());
457 if (newLinkId != null) {
458 resource = new ResourceBuilder().setResource(
460 .setLinkId(newLinkId)
466 resource = new ResourceBuilder().setResource(resLink).build();
469 if (resource != null) {
470 hop = new ZToABuilder()
472 .setResource(resource)
477 LOG.info("resource is null ");
480 if (!ztoaList.isEmpty()) {
481 ztoaDirection.setRate((long) 100).setZToAWavelengthNumber((long) 200).setZToA(ztoaList);
483 LOG.info("ztoaList is empty !");
487 return ztoaDirection.build();
491 *build Pathdescritions ordered List
492 *to be loaded in Pathdescriptions
495 * @param atozDirection AToZdirection List
496 * @param term direct ou indirect
497 * @return PathDescriptionsOrdered List
499 private SortedSet<PathDescriptionsOrdered> buildPathDescription(List<AToZDirection> atozDirection, String term) {
500 SortedSet<PathDescriptionsOrdered> result = new TreeSet<PathDescriptionsOrdered>();
501 PathDescriptionsBuilder pathDescr = new PathDescriptionsBuilder();
502 int size = atozDirection.size();
503 if (!atozDirection.isEmpty()) {
504 LOG.info("result list AToZDirection size : {}", atozDirection.size());
505 List<ZToADirection> ztoadirList = new ArrayList<ZToADirection>();
506 for (AToZDirection atozdir : atozDirection) {
507 ZToADirection ztodir = convertToZtoaDirection(atozdir);
508 if (ztodir != null) {
509 ztoadirList.add(ztodir);
512 if (!ztoadirList.isEmpty() && size == ztoadirList.size()) {
513 LOG.info("building PathDescriptions ...");
515 String pathName = null;
516 for (int indexPath = 0 ; indexPath < size ; indexPath++) {
517 pathName = term.concat(Integer.toString(index));
518 LOG.info("pathName : {}", pathName);
519 pathDescr.setAToZDirection(atozDirection.get(indexPath))
520 .setZToADirection(ztoadirList.get(indexPath)).setPathName(pathName);
521 LOG.info("pathdesciption : {}", pathDescr.build().toString());
522 result.add(new PathDescriptionsOrdered(pathDescr.build(),index));
526 LOG.info("Something wrong happen during atodir conversion...");
530 LOG.info("atozDirection is empty");
536 * gets link extremity.
539 * @return Supernode List of extremities
541 public List<SuperNode> getSuperNodeEndLink(String link) {
542 List<SuperNode> result = new ArrayList<SuperNode>();
544 String [] split = link.split("-");
545 if (split.length == 4) {
546 String aend = split[0].replaceAll("ROADM", "Node");
547 String zend = split[2].replaceAll("ROADM", "Node");
548 if (aend != null && zend != null) {
549 LOG.info("getting super node for : {} and {}", aend, zend);
550 SuperNode aendSp = getSuperNode(aend);
551 SuperNode zendSp = getSuperNode(zend);
552 if (aendSp != null && zendSp != null) {
563 * build all direct paths.
565 * @param aend beginning extremity path
566 * @param zend ending extremity path
567 * @param paths NodeLinkNode list paths
568 * @return PathDescriptionsOrdered List of direct paths
570 public SortedSet<PathDescriptionsOrdered> getDirectPathDesc(String aend, String zend,List<NodeLinkNode> paths) {
571 List<AToZDirection> atozdirectionPaths = new ArrayList<AToZDirection>();
572 SortedSet<PathDescriptionsOrdered> result = new TreeSet<PathDescriptionsOrdered>();
573 SuperNode aendSp = getSuperNode(aend);
574 SuperNode zendSp = getSuperNode(zend);
575 if (!paths.isEmpty()) {
576 for (NodeLinkNode tmp : paths) {
577 if (tmp.getDirect()) {
578 LOG.info("Direct NodeLinkNode : {}", tmp.toString());
579 String atozLink = null;
580 String ztoaLink = null;
581 atozLink = tmp.getAtozLink().get(0);
582 ztoaLink = tmp.getZtoaLink().get(0);
583 if (atozLink != null && ztoaLink != null) {
584 LOG.info("atozlink : {}", atozLink);
585 LOG.info("ztoalink : {}", ztoaLink);
586 InterNodePath interAend = new InterNodePath(aendSp);
587 interAend.buildPath(zend);
588 InterNodePath interZend = new InterNodePath(zendSp);
589 interZend.buildPath(zend);
590 List<String> deg = getDeg(atozLink,ztoaLink);
591 LOG.info("deg : {}", deg.toString());
592 if (deg.size() == 2) {
593 List<AToZDirection> cleanInterA =
594 interAend.replaceOrRemoveAToZDirectionEndLink(deg.get(0),"",atozLink,
595 interAend.getAtoz(),false);
596 List<AToZDirection> cleanInterZ =
597 interZend.replaceOrRemoveAToZDirectionEndLink("TAIL-LINKS",deg.get(1),"",
598 interZend.getAtoz(),true);
599 if (!cleanInterA.isEmpty() && !cleanInterZ.isEmpty()) {
600 atozdirectionPaths.addAll(merge(cleanInterA,cleanInterZ,false));
602 LOG.info("cleanInterA ad/or cleanInterZ is empty !");
605 LOG.info("deg size is not correct, must be 2 ! ");
608 LOG.info("atozlink and / or ztoalink is null");
614 LOG.info("List of direct path is empty !");
616 if (!atozdirectionPaths.isEmpty()) {
617 LOG.info("result size : {}", result.size());
618 result = buildPathDescription(atozdirectionPaths,aend.concat("To").concat(zend).concat("_direct_"));
620 LOG.info("result is empty");
626 * build all indirect paths.
628 * @param aend beginning extremity path
629 * @param zend ending extremity path
630 * @param paths NodeLinkNode list paths
631 * @return PathDescriptionsOrdered List of indirect paths
633 public SortedSet<PathDescriptionsOrdered> getIndirectPathDesc(String aend, String zend,List<NodeLinkNode> paths) {
634 List<AToZDirection> atozdirectionPaths = new ArrayList<AToZDirection>();
635 SortedSet<PathDescriptionsOrdered> result = new TreeSet<PathDescriptionsOrdered>();
636 SuperNode aendSp = getSuperNode(aend);
637 SuperNode zendSp = getSuperNode(zend);
638 if (!paths.isEmpty()) {
639 for (NodeLinkNode tmp : paths) {
640 if (!tmp.getDirect()) {
641 LOG.info("Indirect NodeLinkNode : {}", tmp.toString());
642 int size = tmp.getAtozLink().size();
643 /** must be two for now just one hop. */
644 LOG.info("number of links : {}", size);
645 boolean first = true;
647 List<String> atozLinks = tmp.getAtozLink();
648 List<String> ztoaLinks = tmp.getZtoaLink();
649 if (!atozLinks.isEmpty() && !ztoaLinks.isEmpty()) {
650 LOG.info("atozlink : {}", atozLinks.toString());
651 LOG.info("ztoalink : {}", ztoaLinks.toString());
654 List<SuperNode> hop = getSuperNodeEndLink(atozLinks.get(loop));
655 if (!hop.isEmpty() && hop.size() == 2) {
658 InterNodePath interAend = new InterNodePath(aendSp);
659 interAend.buildPath(zend);
660 LOG.info("interAend : {}", interAend.getAtoz().toString());
661 InterNodePath interZend = new InterNodePath(zendSp);
662 interZend.buildPath(zend);
663 LOG.info("interZend : {}", interZend.getAtoz().toString());
664 List<String> deg1 = getDeg(atozLinks.get(loop),ztoaLinks.get(loop));
665 LOG.info("deg1 : {}", deg1.toString());
666 if (!deg1.isEmpty() && deg1.size() == 2) {
667 List<AToZDirection> cleanInterA = null;
668 List<AToZDirection> cleanInterZ = null;
669 if (zendSp.getSuperNodeId().compareTo(zend) == 0) {
670 cleanInterA = interAend.replaceOrRemoveAToZDirectionEndLink(deg1.get(0),
671 "",atozLinks.get(loop),interAend.getAtoz(),false);
672 LOG.info("next hop is zend");
673 cleanInterZ = interZend.replaceOrRemoveAToZDirectionEndLink("TAIL-LINKS",
674 deg1.get(1),"",interZend.getAtoz(),true);
675 } else if (loop < 1) {
676 cleanInterA = interAend.replaceOrRemoveAToZDirectionEndLink(deg1.get(0),
677 "",atozLinks.get(loop),interAend.getAtoz(),false);
678 cleanInterZ = interZend.getAToZDirectionEndBy(deg1.get(1),
679 interZend.getAtoz(), 1);
681 if (!cleanInterA.isEmpty() && !cleanInterZ.isEmpty()) {
682 atozdirectionPaths = merge(atozdirectionPaths,cleanInterA,first);
683 atozdirectionPaths = merge(atozdirectionPaths, cleanInterZ,false);
686 LOG.info("cleanInterA ad/or cleanInterZ is empty !");
691 LOG.info("Hop list is empty");
697 LOG.info("Link size is not supported , must be two !");
702 LOG.info("List of indirect path is empty !");
704 if (!atozdirectionPaths.isEmpty()) {
705 LOG.info("result size : {}", result.size());
706 result = buildPathDescription(atozdirectionPaths,aend.concat("To").concat(zend).concat("_indirect_"));
708 LOG.info("result is empty");
713 public static void main(String[] args) {
714 Topology topo = new Topology();
716 SuperNodePath path = new SuperNodePath(topo.getNetwork());
717 String aend = "NodeA";
718 String zend = "NodeZ";
719 path.run(aend, zend);
720 path.getDirectPathDesc(aend, zend, path.getPaths());
721 path.getIndirectPathDesc(aend, zend, path.getPaths());
724 public List<NodeLinkNode> getPaths() {
728 public void setPaths(List<NodeLinkNode> paths) {