blob: 2fd501356f434ede9f629466c2dd4f982fd1ee4a [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;
8import net.onrc.onos.ofcontroller.networkgraph.Path;
9import 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 */
19public 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;
25 PathIntents intents = null;
Toshio Koide0e4d8d22014-02-14 10:56:10 -080026 double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree)
Toshio Koidec8c34322014-02-11 12:59:44 -080027
28 public ConstrainedBFSTree(Switch rootSwitch) {
29 this.rootSwitch = rootSwitch;
Toshio Koidec8c34322014-02-11 12:59:44 -080030 calcTree();
31 }
32
Toshio Koide0e4d8d22014-02-14 10:56:10 -080033 public ConstrainedBFSTree(Switch rootSwitch, PathIntents intents, double bandwidth) {
Toshio Koidec8c34322014-02-11 12:59:44 -080034 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 Koidec406e792014-02-14 16:52:42 -080048 if (intents != null && intents.getAvailableBandwidth(link) < bandwidth) continue;
Toshio Koidec8c34322014-02-11 12:59:44 -080049 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}