blob: 9371fd5e9bc2608f9b7e5406bd94e9dd98422ada [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;
10
11/**
12 * @author Toshio Koide (t-koide@onlab.us)
13 */
14public class PathIntents {
15 protected LinkedList<PathIntent> intents = new LinkedList<PathIntent>();
16 protected HashMap<Link, HashSet<PathIntent>> linkToIntents = new HashMap<Link, HashSet<PathIntent>>();
17
18 public void addIntent(PathIntent intent) {
19 intents.add(intent);
20 for (Link link: intent.getPath()) {
21 HashSet<PathIntent> value = linkToIntents.get(link);
22 if (value == null) {
23 value = new HashSet<PathIntent>();
24 linkToIntents.put(link, value);
25 }
26 value.add(intent);
27 }
28 }
Toshio Koideba291c52014-02-11 20:26:51 -080029
30 public void removeIntent(PathIntent intent) {
31 intents.remove(intent);
32 for (Link link: intent.getPath()) {
33 HashSet<PathIntent> value = linkToIntents.get(link);
34 value.remove(intent);
35 }
36 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080037
38 public void addIntents(PathIntents intents) {
39 for(PathIntent intent: intents.getIntents()) {
40 addIntent(intent);
41 }
42 }
43
Toshio Koideba291c52014-02-11 20:26:51 -080044 public void removeIntents(PathIntents intents) {
45 for(PathIntent intent: intents.getIntents()) {
46 removeIntent(intent);
47 }
48 }
49
Toshio Koide5526c4f2014-02-11 12:39:03 -080050 public Collection<PathIntent> getIntentByLink(Link link) {
51 return Collections.unmodifiableCollection(linkToIntents.get(link));
52 }
53
54 public Collection<PathIntent> getIntents() {
55 return Collections.unmodifiableCollection(intents);
56 }
57
58 /**
59 * calculate available bandwidth of specified link
60 * @param link
61 * @return
62 */
63 public Double getAvailableBandwidth(Link link) {
64 Double bandwidth = link.getCapacity();
65 if (!bandwidth.isInfinite() && linkToIntents.containsKey(link)) {
66 for (PathIntent intent: getIntentByLink(link)) {
67 Double intentBandwidth = intent.getBandwidth();
68 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
69 continue;
70 bandwidth -= intentBandwidth;
71 }
72 }
73 return bandwidth;
74 }
75}