Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Collections; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 7 | |
| 8 | import net.onrc.onos.ofcontroller.networkgraph.Link; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 10 | |
| 11 | /** |
| 12 | * @author Toshio Koide (t-koide@onlab.us) |
| 13 | */ |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 14 | public class PathIntentMap extends IntentMap { |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 15 | protected HashMap<Link, HashSet<PathIntent>> linkToIntents = new HashMap<Link, HashSet<PathIntent>>(); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 16 | protected NetworkGraph graph; |
| 17 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 18 | public PathIntentMap(NetworkGraph graph) { |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 19 | this.graph = graph; |
| 20 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 21 | |
| 22 | public void addIntent(PathIntent intent) { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 23 | if (intents.containsKey(intent.getId())) |
| 24 | removeIntent((PathIntent)intents.get(intent.getId())); |
| 25 | intents.put(intent.getId(), intent); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 26 | for (Link link: intent.getPath(graph)) { |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 27 | 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 Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 35 | |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 36 | public void removeIntent(PathIntent intent) { |
| 37 | intents.remove(intent); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 38 | for (Link link: intent.getPath(graph)) { |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 39 | HashSet<PathIntent> value = linkToIntents.get(link); |
| 40 | value.remove(intent); |
| 41 | } |
| 42 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 43 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 44 | public Collection<PathIntent> getIntentByLink(Link link) { |
| 45 | return Collections.unmodifiableCollection(linkToIntents.get(link)); |
| 46 | } |
| 47 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 48 | /** |
| 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 | } |