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 | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
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 | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 15 | protected HashMap<LinkEvent, HashSet<PathIntent>> linkToIntents = new HashMap<>(); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 16 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 17 | @Override |
| 18 | protected void putIntent(Intent intent) { |
| 19 | super.putIntent(intent); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 20 | for (LinkEvent linkEvent: ((PathIntent) intent).getPath()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 21 | HashSet<PathIntent> value = linkToIntents.get(linkEvent); |
| 22 | if (value == null) |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 23 | value = new HashSet<PathIntent>(); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 24 | value.add((PathIntent) intent); |
| 25 | linkToIntents.put(linkEvent, value); |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 26 | } |
| 27 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 28 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 29 | @Override |
| 30 | protected void removeIntent(String intentId) { |
| 31 | PathIntent intent = (PathIntent) getIntent(intentId); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 32 | for (LinkEvent linkEvent: intent.getPath()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 33 | HashSet<PathIntent> value = linkToIntents.get(linkEvent); |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 34 | value.remove(intent); |
| 35 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 36 | super.removeIntent(intentId); |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 37 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 38 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 39 | 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 Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 49 | /** |
| 50 | * calculate available bandwidth of specified link |
| 51 | * @param link |
| 52 | * @return |
| 53 | */ |
| 54 | public Double getAvailableBandwidth(Link link) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 55 | if (link == null) return null; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 56 | Double bandwidth = link.getCapacity(); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 57 | LinkEvent linkEvent = new LinkEvent(link); |
| 58 | if (!bandwidth.isInfinite() && linkToIntents.containsKey(linkEvent)) { |
| 59 | for (PathIntent intent: getIntentsByLink(linkEvent)) { |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 60 | Double intentBandwidth = intent.getBandwidth(); |
| 61 | if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) |
| 62 | continue; |
| 63 | bandwidth -= intentBandwidth; |
| 64 | } |
| 65 | } |
| 66 | return bandwidth; |
| 67 | } |
| 68 | } |