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; |
| 7 | import java.util.LinkedList; |
| 8 | |
| 9 | import net.onrc.onos.ofcontroller.networkgraph.Link; |
| 10 | |
| 11 | /** |
| 12 | * @author Toshio Koide (t-koide@onlab.us) |
| 13 | */ |
| 14 | public 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 Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 29 | |
| 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 Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 37 | |
| 38 | public void addIntents(PathIntents intents) { |
| 39 | for(PathIntent intent: intents.getIntents()) { |
| 40 | addIntent(intent); |
| 41 | } |
| 42 | } |
| 43 | |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 44 | public void removeIntents(PathIntents intents) { |
| 45 | for(PathIntent intent: intents.getIntents()) { |
| 46 | removeIntent(intent); |
| 47 | } |
| 48 | } |
| 49 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 50 | 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 | } |