blob: 2ea27aea5b8809feb332f3a2d2c2c7269dc86005 [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;
26 Double bandwidth = null;
27
28 public ConstrainedBFSTree(Switch rootSwitch) {
29 this.rootSwitch = rootSwitch;
30 this.intents = new PathIntents();
31 calcTree();
32 }
33
34 public ConstrainedBFSTree(Switch rootSwitch, PathIntents intents, Double bandwidth) {
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()) {
47 Switch reachedSwitch = link.getDestinationPort().getSwitch();
48 if (switchSearched.contains(reachedSwitch)) continue;
49 if (bandwidth != null && intents.getAvailableBandwidth(link) < bandwidth) continue;
50 switchQueue.add(reachedSwitch);
51 switchSearched.add(reachedSwitch);
52 upstreamLinks.put(reachedSwitch, link);
53 }
54 }
55 }
56
57 public Path getPath(Switch leafSwitch) {
58 Path path = paths.get(leafSwitch);
59 if (path == null && switchSearched.contains(leafSwitch)) {
60 path = new Path();
61 Switch sw = leafSwitch;
62 while (sw != rootSwitch) {
63 Link upstreamLink = upstreamLinks.get(sw);
64 path.add(0, upstreamLink);
65 sw = upstreamLink.getSourcePort().getSwitch();
66 }
67 paths.put(leafSwitch, path);
68 }
69 return path;
70 }
71}