blob: 4d9d4ef2faf8dfef3c7c63788520d47e85e841f5 [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;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070010import net.onrc.onos.core.util.Dpid;
Toshio Koidec8c34322014-02-11 12:59:44 -080011
12/**
Brian O'Connora581b9d2014-06-15 23:32:36 -070013 * This class creates bandwidth constrained breadth first tree and returns paths
14 * from root switch to leaf switches which satisfies the bandwidth condition. If
15 * bandwidth parameter is not specified, the normal breadth first tree will be
16 * calculated. The paths are snapshot paths at the point of the class
17 * instantiation.
Toshio Koidec8c34322014-02-11 12:59:44 -080018 */
19public class ConstrainedBFSTree {
Ray Milkey269ffb92014-04-03 14:43:30 -070020 LinkedList<Switch> switchQueue = new LinkedList<>();
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070021 HashSet<Dpid> switchSearched = new HashSet<>();
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070022 HashMap<Dpid, LinkEvent> upstreamLinks = new HashMap<>();
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070023 HashMap<Dpid, Path> paths = new HashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070024 Switch rootSwitch;
25 PathIntentMap intents = null;
26 double bandwidth = 0.0; // 0.0 means no limit for bandwidth (normal BFS tree)
Toshio Koidec8c34322014-02-11 12:59:44 -080027
Brian O'Connora581b9d2014-06-15 23:32:36 -070028 /**
29 * Constructor.
30 *
31 * @param rootSwitch root of the BFS tree
32 */
Ray Milkey269ffb92014-04-03 14:43:30 -070033 public ConstrainedBFSTree(Switch rootSwitch) {
34 this.rootSwitch = rootSwitch;
35 calcTree();
36 }
Toshio Koidec8c34322014-02-11 12:59:44 -080037
Brian O'Connora581b9d2014-06-15 23:32:36 -070038 /**
39 * Constructor.
40 *
41 * @param rootSwitch root switch of the BFS tree
42 * @param intents map of Intents
Sho SHIMIZU69474452014-06-20 09:17:40 -070043 * @param bandwidth bandwidth constraint
Brian O'Connora581b9d2014-06-15 23:32:36 -070044 */
Ray Milkey269ffb92014-04-03 14:43:30 -070045 public ConstrainedBFSTree(Switch rootSwitch, PathIntentMap intents, double bandwidth) {
46 this.rootSwitch = rootSwitch;
47 this.intents = intents;
48 this.bandwidth = bandwidth;
49 calcTree();
50 }
Toshio Koidec8c34322014-02-11 12:59:44 -080051
Brian O'Connora581b9d2014-06-15 23:32:36 -070052 /**
53 * Calculates the BFS tree using any provided constraints and Intents.
54 */
Ray Milkey4373cbe2014-08-12 09:58:58 -070055 protected final void calcTree() {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 switchQueue.add(rootSwitch);
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070057 switchSearched.add(rootSwitch.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070058 while (!switchQueue.isEmpty()) {
59 Switch sw = switchQueue.poll();
60 for (Link link : sw.getOutgoingLinks()) {
61 Switch reachedSwitch = link.getDstPort().getSwitch();
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070062 if (switchSearched.contains(reachedSwitch.getDpid())) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070063 continue;
64 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065 if (intents != null &&
Brian O'Connora581b9d2014-06-15 23:32:36 -070066 intents.getAvailableBandwidth(link) < bandwidth) {
Ray Milkey269ffb92014-04-03 14:43:30 -070067 continue;
68 }
69 switchQueue.add(reachedSwitch);
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070070 switchSearched.add(reachedSwitch.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070071 upstreamLinks.put(reachedSwitch.getDpid(), new LinkEvent(link));
72 }
73 }
74 }
Toshio Koidec8c34322014-02-11 12:59:44 -080075
Brian O'Connora581b9d2014-06-15 23:32:36 -070076 /**
77 * Return the computed path from the root switch to the leaf switch.
78 *
79 * @param leafSwitch the leaf switch
80 * @return the Path from the root switch to the leaf switch
81 */
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public Path getPath(Switch leafSwitch) {
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070083 Path path = paths.get(leafSwitch.getDpid());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070084 Dpid rootSwitchDpid = rootSwitch.getDpid();
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070085 if (path == null && switchSearched.contains(leafSwitch.getDpid())) {
Ray Milkey269ffb92014-04-03 14:43:30 -070086 path = new Path();
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070087 Dpid sw = leafSwitch.getDpid();
Ray Milkey269ffb92014-04-03 14:43:30 -070088 while (!sw.equals(rootSwitchDpid)) {
89 LinkEvent upstreamLink = upstreamLinks.get(sw);
90 path.add(0, upstreamLink);
91 sw = upstreamLink.getSrc().getDpid();
92 }
Yuta HIGUCHIa617e152014-07-16 19:44:39 -070093 paths.put(leafSwitch.getDpid(), path);
Ray Milkey269ffb92014-04-03 14:43:30 -070094 }
95 return path;
96 }
Toshio Koidec8c34322014-02-11 12:59:44 -080097}