Toshio Koide | c8c3432 | 2014-02-11 12:59:44 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.LinkedList; |
| 6 | |
| 7 | import net.onrc.onos.ofcontroller.networkgraph.Link; |
| 8 | import net.onrc.onos.ofcontroller.networkgraph.Path; |
| 9 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 10 | |
| 11 | /** |
| 12 | * This class creates bandwidth constrained breadth first tree |
| 13 | * and returns paths from root switch to leaf switches |
| 14 | * which satisfies the bandwidth condition. |
| 15 | * If bandwidth parameter is not specified, the normal breadth first tree will be calculated. |
| 16 | * The paths are snapshot paths at the point of the class instantiation. |
| 17 | * @author Toshio Koide (t-koide@onlab.us) |
| 18 | */ |
| 19 | public class ConstrainedBFSTree { |
| 20 | LinkedList<Switch> switchQueue = new LinkedList<Switch>(); |
| 21 | HashSet<Switch> switchSearched = new HashSet<Switch>(); |
| 22 | HashMap<Switch, Link> upstreamLinks = new HashMap<Switch, Link>(); |
| 23 | HashMap<Switch, Path> paths = new HashMap<Switch, Path>(); |
| 24 | Switch rootSwitch; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame^] | 25 | PathIntentMap intents = null; |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 26 | double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree) |
Toshio Koide | c8c3432 | 2014-02-11 12:59:44 -0800 | [diff] [blame] | 27 | |
| 28 | public ConstrainedBFSTree(Switch rootSwitch) { |
| 29 | this.rootSwitch = rootSwitch; |
Toshio Koide | c8c3432 | 2014-02-11 12:59:44 -0800 | [diff] [blame] | 30 | calcTree(); |
| 31 | } |
| 32 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame^] | 33 | public ConstrainedBFSTree(Switch rootSwitch, PathIntentMap intents, double bandwidth) { |
Toshio Koide | c8c3432 | 2014-02-11 12:59:44 -0800 | [diff] [blame] | 34 | this.rootSwitch = rootSwitch; |
| 35 | this.intents = intents; |
| 36 | this.bandwidth = bandwidth; |
| 37 | calcTree(); |
| 38 | } |
| 39 | |
| 40 | protected void calcTree() { |
| 41 | switchQueue.add(rootSwitch); |
| 42 | switchSearched.add(rootSwitch); |
| 43 | while (!switchQueue.isEmpty()) { |
| 44 | Switch sw = switchQueue.poll(); |
| 45 | for (Link link: sw.getOutgoingLinks()) { |
| 46 | Switch reachedSwitch = link.getDestinationPort().getSwitch(); |
| 47 | if (switchSearched.contains(reachedSwitch)) continue; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 48 | if (intents != null && intents.getAvailableBandwidth(link) < bandwidth) continue; |
Toshio Koide | c8c3432 | 2014-02-11 12:59:44 -0800 | [diff] [blame] | 49 | switchQueue.add(reachedSwitch); |
| 50 | switchSearched.add(reachedSwitch); |
| 51 | upstreamLinks.put(reachedSwitch, link); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public Path getPath(Switch leafSwitch) { |
| 57 | Path path = paths.get(leafSwitch); |
| 58 | if (path == null && switchSearched.contains(leafSwitch)) { |
| 59 | path = new Path(); |
| 60 | Switch sw = leafSwitch; |
| 61 | while (sw != rootSwitch) { |
| 62 | Link upstreamLink = upstreamLinks.get(sw); |
| 63 | path.add(0, upstreamLink); |
| 64 | sw = upstreamLink.getSourcePort().getSwitch(); |
| 65 | } |
| 66 | paths.put(leafSwitch, path); |
| 67 | } |
| 68 | return path; |
| 69 | } |
| 70 | } |