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; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * @author Toshio Koide (t-koide@onlab.us) |
| 14 | */ |
| 15 | public class PathIntents { |
| 16 | protected LinkedList<PathIntent> intents = new LinkedList<PathIntent>(); |
| 17 | protected HashMap<Link, HashSet<PathIntent>> linkToIntents = new HashMap<Link, HashSet<PathIntent>>(); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 18 | protected NetworkGraph graph; |
| 19 | |
| 20 | public PathIntents(NetworkGraph graph) { |
| 21 | this.graph = graph; |
| 22 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 23 | |
| 24 | public void addIntent(PathIntent intent) { |
| 25 | intents.add(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 | |
| 44 | public void addIntents(PathIntents intents) { |
| 45 | for(PathIntent intent: intents.getIntents()) { |
| 46 | addIntent(intent); |
| 47 | } |
| 48 | } |
| 49 | |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 50 | public void removeIntents(PathIntents intents) { |
| 51 | for(PathIntent intent: intents.getIntents()) { |
| 52 | removeIntent(intent); |
| 53 | } |
| 54 | } |
| 55 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 56 | public Collection<PathIntent> getIntentByLink(Link link) { |
| 57 | return Collections.unmodifiableCollection(linkToIntents.get(link)); |
| 58 | } |
| 59 | |
| 60 | public Collection<PathIntent> getIntents() { |
| 61 | return Collections.unmodifiableCollection(intents); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * calculate available bandwidth of specified link |
| 66 | * @param link |
| 67 | * @return |
| 68 | */ |
| 69 | public Double getAvailableBandwidth(Link link) { |
| 70 | Double bandwidth = link.getCapacity(); |
| 71 | if (!bandwidth.isInfinite() && linkToIntents.containsKey(link)) { |
| 72 | for (PathIntent intent: getIntentByLink(link)) { |
| 73 | Double intentBandwidth = intent.getBandwidth(); |
| 74 | if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) |
| 75 | continue; |
| 76 | bandwidth -= intentBandwidth; |
| 77 | } |
| 78 | } |
| 79 | return bandwidth; |
| 80 | } |
| 81 | } |