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