blob: 6cee929819ddd279e693f79d87a8853b261ad54d [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;
Jonathan Hart472062d2014-04-03 10:56:48 -07009import net.onrc.onos.core.topology.Switch;
Toshio Koidec8c34322014-02-11 12:59:44 -080010
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.
Ray Milkey269ffb92014-04-03 14:43:30 -070017 *
Toshio Koidec8c34322014-02-11 12:59:44 -080018 * @author Toshio Koide (t-koide@onlab.us)
19 */
20public class ConstrainedBFSTree {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 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<>();
25 Switch rootSwitch;
26 PathIntentMap intents = null;
27 double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree)
Toshio Koidec8c34322014-02-11 12:59:44 -080028
Ray Milkey269ffb92014-04-03 14:43:30 -070029 public ConstrainedBFSTree(Switch rootSwitch) {
30 this.rootSwitch = rootSwitch;
31 calcTree();
32 }
Toshio Koidec8c34322014-02-11 12:59:44 -080033
Ray Milkey269ffb92014-04-03 14:43:30 -070034 public ConstrainedBFSTree(Switch rootSwitch, PathIntentMap intents, double bandwidth) {
35 this.rootSwitch = rootSwitch;
36 this.intents = intents;
37 this.bandwidth = bandwidth;
38 calcTree();
39 }
Toshio Koidec8c34322014-02-11 12:59:44 -080040
Ray Milkey269ffb92014-04-03 14:43:30 -070041 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.getDstPort().getSwitch();
Ray Milkeyb29e6262014-04-09 16:02:14 -070048 if (switchSearched.contains(reachedSwitch)) {
49 continue;
50 }
Ray Milkey269ffb92014-04-03 14:43:30 -070051 if (intents != null &&
52 intents.getAvailableBandwidth(link) < bandwidth) {
53 continue;
54 }
55 switchQueue.add(reachedSwitch);
56 switchSearched.add(reachedSwitch);
57 upstreamLinks.put(reachedSwitch.getDpid(), new LinkEvent(link));
58 }
59 }
60 }
Toshio Koidec8c34322014-02-11 12:59:44 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 public Path getPath(Switch leafSwitch) {
63 Path path = paths.get(leafSwitch);
64 Long rootSwitchDpid = rootSwitch.getDpid();
65 if (path == null && switchSearched.contains(leafSwitch)) {
66 path = new Path();
67 Long sw = leafSwitch.getDpid();
68 while (!sw.equals(rootSwitchDpid)) {
69 LinkEvent upstreamLink = upstreamLinks.get(sw);
70 path.add(0, upstreamLink);
71 sw = upstreamLink.getSrc().getDpid();
72 }
73 paths.put(leafSwitch, path);
74 }
75 return path;
76 }
Toshio Koidec8c34322014-02-11 12:59:44 -080077}