blob: a1edb207e2935d69c4c16e6f40d3819f2e3dff9d [file] [log] [blame]
Toshio Koidec8c34322014-02-11 12:59:44 -08001package net.onrc.onos.intent;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.LinkedList;
6
7import net.onrc.onos.ofcontroller.networkgraph.Link;
Toshio Koided9fa2a82014-02-19 17:35:18 -08008import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidec8c34322014-02-11 12:59:44 -08009import net.onrc.onos.ofcontroller.networkgraph.Path;
10import 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 */
20public class ConstrainedBFSTree {
Toshio Koided9fa2a82014-02-19 17:35:18 -080021 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 Koidec8c34322014-02-11 12:59:44 -080025 Switch rootSwitch;
Toshio Koide4f308732014-02-18 15:19:48 -080026 PathIntentMap intents = null;
Toshio Koide0e4d8d22014-02-14 10:56:10 -080027 double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree)
Toshio Koidec8c34322014-02-11 12:59:44 -080028
29 public ConstrainedBFSTree(Switch rootSwitch) {
30 this.rootSwitch = rootSwitch;
Toshio Koidec8c34322014-02-11 12:59:44 -080031 calcTree();
32 }
33
Toshio Koide4f308732014-02-18 15:19:48 -080034 public ConstrainedBFSTree(Switch rootSwitch, PathIntentMap intents, double bandwidth) {
Toshio Koidec8c34322014-02-11 12:59:44 -080035 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 Radoslavov7c8f69a2014-02-19 19:01:45 -080047 Switch reachedSwitch = link.getDstPort().getSwitch();
Toshio Koidec8c34322014-02-11 12:59:44 -080048 if (switchSearched.contains(reachedSwitch)) continue;
Toshio Koidec406e792014-02-14 16:52:42 -080049 if (intents != null && intents.getAvailableBandwidth(link) < bandwidth) continue;
Toshio Koidec8c34322014-02-11 12:59:44 -080050 switchQueue.add(reachedSwitch);
51 switchSearched.add(reachedSwitch);
Toshio Koided9fa2a82014-02-19 17:35:18 -080052 upstreamLinks.put(reachedSwitch.getDpid(), new LinkEvent(link));
Toshio Koidec8c34322014-02-11 12:59:44 -080053 }
54 }
55 }
56
57 public Path getPath(Switch leafSwitch) {
58 Path path = paths.get(leafSwitch);
Toshio Koided9fa2a82014-02-19 17:35:18 -080059 Long rootSwitchDpid = rootSwitch.getDpid();
Toshio Koidec8c34322014-02-11 12:59:44 -080060 if (path == null && switchSearched.contains(leafSwitch)) {
61 path = new Path();
Toshio Koided9fa2a82014-02-19 17:35:18 -080062 Long sw = leafSwitch.getDpid();
Yuta HIGUCHI33505ff2014-02-25 18:45:44 -080063 while (!sw.equals(rootSwitchDpid)) {
Toshio Koided9fa2a82014-02-19 17:35:18 -080064 LinkEvent upstreamLink = upstreamLinks.get(sw);
Toshio Koidec8c34322014-02-11 12:59:44 -080065 path.add(0, upstreamLink);
Toshio Koided9fa2a82014-02-19 17:35:18 -080066 sw = upstreamLink.getSrc().getDpid();
Toshio Koidec8c34322014-02-11 12:59:44 -080067 }
68 paths.put(leafSwitch, path);
69 }
70 return path;
71 }
72}