blob: c72ef4eb1a11f83fc173712d7a964a09e82f7c28 [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();
49 if (switchSearched.contains(reachedSwitch)) continue;
50 if (intents != null &&
51 intents.getAvailableBandwidth(link) < bandwidth) {
52 continue;
53 }
54 switchQueue.add(reachedSwitch);
55 switchSearched.add(reachedSwitch);
56 upstreamLinks.put(reachedSwitch.getDpid(), new LinkEvent(link));
57 }
58 }
59 }
Toshio Koidec8c34322014-02-11 12:59:44 -080060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 public Path getPath(Switch leafSwitch) {
62 Path path = paths.get(leafSwitch);
63 Long rootSwitchDpid = rootSwitch.getDpid();
64 if (path == null && switchSearched.contains(leafSwitch)) {
65 path = new Path();
66 Long sw = leafSwitch.getDpid();
67 while (!sw.equals(rootSwitchDpid)) {
68 LinkEvent upstreamLink = upstreamLinks.get(sw);
69 path.add(0, upstreamLink);
70 sw = upstreamLink.getSrc().getDpid();
71 }
72 paths.put(leafSwitch, path);
73 }
74 return path;
75 }
Toshio Koidec8c34322014-02-11 12:59:44 -080076}