blob: 870498878f48addca216e4b058d461f67e64c072 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koidec8c34322014-02-11 12:59:44 -08002
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.LinkedList;
6
Jonathan Hart472062d2014-04-03 10:56:48 -07007import net.onrc.onos.core.topology.Link;
8import net.onrc.onos.core.topology.LinkEvent;
9import net.onrc.onos.core.topology.Path;
10import net.onrc.onos.core.topology.Switch;
Toshio Koidec8c34322014-02-11 12:59:44 -080011
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.
Ray Milkey269ffb92014-04-03 14:43:30 -070018 *
Toshio Koidec8c34322014-02-11 12:59:44 -080019 * @author Toshio Koide (t-koide@onlab.us)
20 */
21public class ConstrainedBFSTree {
Ray Milkey269ffb92014-04-03 14:43:30 -070022 LinkedList<Switch> switchQueue = new LinkedList<>();
23 HashSet<Switch> switchSearched = new HashSet<>();
24 HashMap<Long, LinkEvent> upstreamLinks = new HashMap<>();
25 HashMap<Switch, Path> paths = new HashMap<>();
26 Switch rootSwitch;
27 PathIntentMap intents = null;
28 double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree)
Toshio Koidec8c34322014-02-11 12:59:44 -080029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 public ConstrainedBFSTree(Switch rootSwitch) {
31 this.rootSwitch = rootSwitch;
32 calcTree();
33 }
Toshio Koidec8c34322014-02-11 12:59:44 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 public ConstrainedBFSTree(Switch rootSwitch, PathIntentMap intents, double bandwidth) {
36 this.rootSwitch = rootSwitch;
37 this.intents = intents;
38 this.bandwidth = bandwidth;
39 calcTree();
40 }
Toshio Koidec8c34322014-02-11 12:59:44 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 protected void calcTree() {
43 switchQueue.add(rootSwitch);
44 switchSearched.add(rootSwitch);
45 while (!switchQueue.isEmpty()) {
46 Switch sw = switchQueue.poll();
47 for (Link link : sw.getOutgoingLinks()) {
48 Switch reachedSwitch = link.getDstPort().getSwitch();
Ray Milkeyb29e6262014-04-09 16:02:14 -070049 if (switchSearched.contains(reachedSwitch)) {
50 continue;
51 }
Ray Milkey269ffb92014-04-03 14:43:30 -070052 if (intents != null &&
53 intents.getAvailableBandwidth(link) < bandwidth) {
54 continue;
55 }
56 switchQueue.add(reachedSwitch);
57 switchSearched.add(reachedSwitch);
58 upstreamLinks.put(reachedSwitch.getDpid(), new LinkEvent(link));
59 }
60 }
61 }
Toshio Koidec8c34322014-02-11 12:59:44 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 public Path getPath(Switch leafSwitch) {
64 Path path = paths.get(leafSwitch);
65 Long rootSwitchDpid = rootSwitch.getDpid();
66 if (path == null && switchSearched.contains(leafSwitch)) {
67 path = new Path();
68 Long sw = leafSwitch.getDpid();
69 while (!sw.equals(rootSwitchDpid)) {
70 LinkEvent upstreamLink = upstreamLinks.get(sw);
71 path.add(0, upstreamLink);
72 sw = upstreamLink.getSrc().getDpid();
73 }
74 paths.put(leafSwitch, path);
75 }
76 return path;
77 }
Toshio Koidec8c34322014-02-11 12:59:44 -080078}