blob: d70def27cf8f55adadf1b39be159f7dd7b8049fb [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 Koide0c9106d2014-02-19 15:26:38 -08009import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
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 Koide0c9106d2014-02-19 15:26:38 -080015 protected HashMap<LinkEvent, HashSet<PathIntent>> linkToIntents = new HashMap<>();
Toshio Koidec406e792014-02-14 16:52:42 -080016
Toshio Koide0c9106d2014-02-19 15:26:38 -080017 @Override
18 protected void putIntent(Intent intent) {
19 super.putIntent(intent);
Toshio Koided9fa2a82014-02-19 17:35:18 -080020 for (LinkEvent linkEvent: ((PathIntent) intent).getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080021 HashSet<PathIntent> value = linkToIntents.get(linkEvent);
22 if (value == null)
Toshio Koide5526c4f2014-02-11 12:39:03 -080023 value = new HashSet<PathIntent>();
Toshio Koide0c9106d2014-02-19 15:26:38 -080024 value.add((PathIntent) intent);
25 linkToIntents.put(linkEvent, value);
Toshio Koide5526c4f2014-02-11 12:39:03 -080026 }
27 }
Toshio Koidec406e792014-02-14 16:52:42 -080028
Toshio Koide0c9106d2014-02-19 15:26:38 -080029 @Override
30 protected void removeIntent(String intentId) {
31 PathIntent intent = (PathIntent) getIntent(intentId);
Toshio Koided9fa2a82014-02-19 17:35:18 -080032 for (LinkEvent linkEvent: intent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080033 HashSet<PathIntent> value = linkToIntents.get(linkEvent);
Toshio Koideba291c52014-02-11 20:26:51 -080034 value.remove(intent);
35 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080036 super.removeIntent(intentId);
Toshio Koideba291c52014-02-11 20:26:51 -080037 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080038
Toshio Koide0c9106d2014-02-19 15:26:38 -080039 public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) {
40 Collection<PathIntent> intents = linkToIntents.get(linkEvent);
41 if (intents == null) {
42 return null;
43 }
44 else {
45 return Collections.unmodifiableCollection(intents);
46 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080047 }
48
Toshio Koide5526c4f2014-02-11 12:39:03 -080049 /**
50 * calculate available bandwidth of specified link
51 * @param link
52 * @return
53 */
54 public Double getAvailableBandwidth(Link link) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080055 if (link == null) return null;
Toshio Koide5526c4f2014-02-11 12:39:03 -080056 Double bandwidth = link.getCapacity();
Toshio Koide0c9106d2014-02-19 15:26:38 -080057 LinkEvent linkEvent = new LinkEvent(link);
58 if (!bandwidth.isInfinite() && linkToIntents.containsKey(linkEvent)) {
59 for (PathIntent intent: getIntentsByLink(linkEvent)) {
Toshio Koide5526c4f2014-02-11 12:39:03 -080060 Double intentBandwidth = intent.getBandwidth();
61 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
62 continue;
63 bandwidth -= intentBandwidth;
64 }
65 }
66 return bandwidth;
67 }
68}