blob: 8d712e3aabf4cedbdd1f72c9ee1b32e4fba929e1 [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;
7import java.util.LinkedList;
8
9import net.onrc.onos.ofcontroller.networkgraph.Link;
Toshio Koidec406e792014-02-14 16:52:42 -080010import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Toshio Koide5526c4f2014-02-11 12:39:03 -080011
12/**
13 * @author Toshio Koide (t-koide@onlab.us)
14 */
15public class PathIntents {
16 protected LinkedList<PathIntent> intents = new LinkedList<PathIntent>();
17 protected HashMap<Link, HashSet<PathIntent>> linkToIntents = new HashMap<Link, HashSet<PathIntent>>();
Toshio Koidec406e792014-02-14 16:52:42 -080018 protected NetworkGraph graph;
19
20 public PathIntents(NetworkGraph graph) {
21 this.graph = graph;
22 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080023
24 public void addIntent(PathIntent intent) {
25 intents.add(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
44 public void addIntents(PathIntents intents) {
45 for(PathIntent intent: intents.getIntents()) {
46 addIntent(intent);
47 }
48 }
49
Toshio Koideba291c52014-02-11 20:26:51 -080050 public void removeIntents(PathIntents intents) {
51 for(PathIntent intent: intents.getIntents()) {
52 removeIntent(intent);
53 }
54 }
55
Toshio Koide5526c4f2014-02-11 12:39:03 -080056 public Collection<PathIntent> getIntentByLink(Link link) {
57 return Collections.unmodifiableCollection(linkToIntents.get(link));
58 }
59
60 public Collection<PathIntent> getIntents() {
61 return Collections.unmodifiableCollection(intents);
62 }
63
64 /**
65 * calculate available bandwidth of specified link
66 * @param link
67 * @return
68 */
69 public Double getAvailableBandwidth(Link link) {
70 Double bandwidth = link.getCapacity();
71 if (!bandwidth.isInfinite() && linkToIntents.containsKey(link)) {
72 for (PathIntent intent: getIntentByLink(link)) {
73 Double intentBandwidth = intent.getBandwidth();
74 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
75 continue;
76 bandwidth -= intentBandwidth;
77 }
78 }
79 return bandwidth;
80 }
81}