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 | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * @author Toshio Koide (t-koide@onlab.us) |
| 14 | */ |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 15 | public class PathIntentMap extends IntentMap { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 16 | private HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents; |
| 17 | |
| 18 | public PathIntentMap() { |
| 19 | intents = new HashMap<>(); |
| 20 | } |
| 21 | |
| 22 | private HashSet<PathIntent> get(SwitchPort swPort) { |
| 23 | Long dpid = swPort.getDpid(); |
| 24 | Long port = swPort.getNumber(); |
| 25 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 26 | if (portToIntents == null) { |
| 27 | portToIntents = new HashMap<>(); |
| 28 | intents.put(dpid, portToIntents); |
| 29 | } |
| 30 | HashSet<PathIntent> targetIntents = portToIntents.get(port); |
| 31 | if (targetIntents == null) { |
| 32 | targetIntents = new HashSet<>(); |
| 33 | portToIntents.put(port, targetIntents); |
| 34 | } |
| 35 | return targetIntents; |
| 36 | } |
| 37 | |
| 38 | private void put(SwitchPort swPort, PathIntent intent) { |
| 39 | get(swPort).add(intent); |
| 40 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 41 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 42 | @Override |
| 43 | protected void putIntent(Intent intent) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 44 | if (!(intent instanceof PathIntent)) return; // TODO throw exception |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 45 | super.putIntent(intent); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 46 | |
| 47 | PathIntent pathIntent = (PathIntent) intent; |
| 48 | for (LinkEvent linkEvent: pathIntent.getPath()) { |
| 49 | put(linkEvent.getSrc(), (PathIntent) intent); |
| 50 | put(linkEvent.getDst(), (PathIntent) intent); |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 53 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 54 | @Override |
| 55 | protected void removeIntent(String intentId) { |
| 56 | PathIntent intent = (PathIntent) getIntent(intentId); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 57 | for (LinkEvent linkEvent: intent.getPath()) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 58 | get(linkEvent.getSrc()).remove(intent); |
| 59 | get(linkEvent.getDst()).remove(intent); |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 60 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 61 | super.removeIntent(intentId); |
Toshio Koide | ba291c5 | 2014-02-11 20:26:51 -0800 | [diff] [blame] | 62 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 63 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 64 | public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 65 | return getIntentsByPort( |
| 66 | linkEvent.getSrc().getDpid(), |
| 67 | linkEvent.getSrc().getNumber()); |
| 68 | } |
| 69 | |
| 70 | public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) { |
| 71 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 72 | if (portToIntents != null) { |
| 73 | HashSet<PathIntent> targetIntents = portToIntents.get(port); |
| 74 | if (targetIntents != null) { |
| 75 | return Collections.unmodifiableCollection(targetIntents); |
| 76 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 77 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 78 | return new HashSet<>(); |
| 79 | } |
| 80 | |
| 81 | public Collection<PathIntent> getIntentsByDpid(Long dpid) { |
| 82 | HashSet<PathIntent> result = new HashSet<>(); |
| 83 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 84 | if (portToIntents != null) { |
| 85 | for (HashSet<PathIntent> targetIntents: portToIntents.values()) { |
| 86 | result.addAll(targetIntents); |
| 87 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 88 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 89 | return result; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 92 | /** |
| 93 | * calculate available bandwidth of specified link |
| 94 | * @param link |
| 95 | * @return |
| 96 | */ |
| 97 | public Double getAvailableBandwidth(Link link) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 98 | if (link == null) return null; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 99 | Double bandwidth = link.getCapacity(); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 100 | LinkEvent linkEvent = new LinkEvent(link); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 101 | if (!bandwidth.isInfinite()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 102 | for (PathIntent intent: getIntentsByLink(linkEvent)) { |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 103 | Double intentBandwidth = intent.getBandwidth(); |
| 104 | if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) |
| 105 | continue; |
| 106 | bandwidth -= intentBandwidth; |
| 107 | } |
| 108 | } |
| 109 | return bandwidth; |
| 110 | } |
| 111 | } |