blob: 02444ff776c5e83463302e27b9367be09c06537d [file] [log] [blame]
Toshio Koide5526c4f2014-02-11 12:39:03 -08001package net.onrc.onos.intent;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
Toshio Koide5526c4f2014-02-11 12:39:03 -08007
8import net.onrc.onos.ofcontroller.networkgraph.Link;
Toshio Koidec406e792014-02-14 16:52:42 -08009import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Toshio Koide5526c4f2014-02-11 12:39:03 -080010
11/**
12 * @author Toshio Koide (t-koide@onlab.us)
13 */
Toshio Koide4f308732014-02-18 15:19:48 -080014public class PathIntentMap extends IntentMap {
Toshio Koide5526c4f2014-02-11 12:39:03 -080015 protected HashMap<Link, HashSet<PathIntent>> linkToIntents = new HashMap<Link, HashSet<PathIntent>>();
Toshio Koidec406e792014-02-14 16:52:42 -080016 protected NetworkGraph graph;
17
Toshio Koide4f308732014-02-18 15:19:48 -080018 public PathIntentMap(NetworkGraph graph) {
Toshio Koidec406e792014-02-14 16:52:42 -080019 this.graph = graph;
20 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080021
22 public void addIntent(PathIntent intent) {
Toshio Koide4f308732014-02-18 15:19:48 -080023 if (intents.containsKey(intent.getId()))
24 removeIntent((PathIntent)intents.get(intent.getId()));
25 intents.put(intent.getId(), intent);
Toshio Koidec406e792014-02-14 16:52:42 -080026 for (Link link: intent.getPath(graph)) {
Toshio Koide5526c4f2014-02-11 12:39:03 -080027 HashSet<PathIntent> value = linkToIntents.get(link);
28 if (value == null) {
29 value = new HashSet<PathIntent>();
30 linkToIntents.put(link, value);
31 }
32 value.add(intent);
33 }
34 }
Toshio Koidec406e792014-02-14 16:52:42 -080035
Toshio Koideba291c52014-02-11 20:26:51 -080036 public void removeIntent(PathIntent intent) {
37 intents.remove(intent);
Toshio Koidec406e792014-02-14 16:52:42 -080038 for (Link link: intent.getPath(graph)) {
Toshio Koideba291c52014-02-11 20:26:51 -080039 HashSet<PathIntent> value = linkToIntents.get(link);
40 value.remove(intent);
41 }
42 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080043
Toshio Koide5526c4f2014-02-11 12:39:03 -080044 public Collection<PathIntent> getIntentByLink(Link link) {
45 return Collections.unmodifiableCollection(linkToIntents.get(link));
46 }
47
Toshio Koide5526c4f2014-02-11 12:39:03 -080048 /**
49 * calculate available bandwidth of specified link
50 * @param link
51 * @return
52 */
53 public Double getAvailableBandwidth(Link link) {
54 Double bandwidth = link.getCapacity();
55 if (!bandwidth.isInfinite() && linkToIntents.containsKey(link)) {
56 for (PathIntent intent: getIntentByLink(link)) {
57 Double intentBandwidth = intent.getBandwidth();
58 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
59 continue;
60 bandwidth -= intentBandwidth;
61 }
62 }
63 return bandwidth;
64 }
65}