Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.LinkedList; |
| 6 | |
| 7 | import net.onrc.onos.core.topology.Link; |
| 8 | import net.onrc.onos.core.topology.LinkData; |
| 9 | import net.onrc.onos.core.topology.Switch; |
| 10 | import net.onrc.onos.core.util.Dpid; |
| 11 | import net.onrc.onos.core.intent.Path; |
| 12 | |
| 13 | /** |
| 14 | * This class creates bandwidth constrained breadth first tree and returns paths |
| 15 | * from root switch to leaf switches which satisfies the bandwidth condition. If |
| 16 | * bandwidth parameter is not specified, the normal breadth first tree will be |
| 17 | * calculated. The paths are snapshot paths at the point of the class |
| 18 | * instantiation. |
| 19 | */ |
| 20 | public class ECMPShortestPathGraph { |
| 21 | LinkedList<Switch> switchQueue = new LinkedList<>(); |
| 22 | HashSet<Dpid> switchSearched = new HashSet<>(); |
| 23 | HashMap<Dpid, LinkData> upstreamLinks = new HashMap<>(); |
| 24 | HashMap<Dpid, Path> paths = new HashMap<>(); |
| 25 | Switch rootSwitch; |
| 26 | |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @param rootSwitch root of the BFS tree |
| 31 | */ |
| 32 | public ECMPShortestPathGraph(Switch rootSwitch) { |
| 33 | this.rootSwitch = rootSwitch; |
| 34 | calcECMPShortestPathGraph(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Calculates the BFS tree using any provided constraints and Intents. |
| 39 | */ |
| 40 | private void calcECMPShortestPathGraph() { |
| 41 | switchQueue.add(rootSwitch); |
| 42 | switchSearched.add(rootSwitch.getDpid()); |
| 43 | while (!switchQueue.isEmpty()) { |
| 44 | Switch sw = switchQueue.poll(); |
| 45 | for (Link link : sw.getOutgoingLinks()) { |
| 46 | Switch reachedSwitch = link.getDstPort().getSwitch(); |
| 47 | if (switchSearched.contains(reachedSwitch.getDpid())) { |
| 48 | continue; |
| 49 | } |
| 50 | switchQueue.add(reachedSwitch); |
| 51 | switchSearched.add(reachedSwitch.getDpid()); |
| 52 | upstreamLinks.put(reachedSwitch.getDpid(), new LinkData(link)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return the computed path from the root switch to the leaf switch. |
| 59 | * |
| 60 | * @param leafSwitch the leaf switch |
| 61 | * @return the Path from the root switch to the leaf switch |
| 62 | */ |
| 63 | public Path getPath(Switch leafSwitch) { |
| 64 | Path path = paths.get(leafSwitch.getDpid()); |
| 65 | Dpid rootSwitchDpid = rootSwitch.getDpid(); |
| 66 | if (path == null && switchSearched.contains(leafSwitch.getDpid())) { |
| 67 | path = new Path(); |
| 68 | Dpid sw = leafSwitch.getDpid(); |
| 69 | while (!sw.equals(rootSwitchDpid)) { |
| 70 | LinkData upstreamLink = upstreamLinks.get(sw); |
| 71 | path.add(0, upstreamLink); |
| 72 | sw = upstreamLink.getSrc().getDpid(); |
| 73 | } |
| 74 | paths.put(leafSwitch.getDpid(), path); |
| 75 | } |
| 76 | return path; |
| 77 | } |
| 78 | } |