Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.topology.Link; |
| 9 | import net.onrc.onos.core.topology.LinkEvent; |
| 10 | import net.onrc.onos.core.topology.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 { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | private HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents; |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 17 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | public PathIntentMap() { |
| 19 | intents = new HashMap<>(); |
| 20 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 21 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 37 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | @Override |
| 43 | protected void putIntent(Intent intent) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 44 | if (!(intent instanceof PathIntent)) { |
| 45 | return; // TODO throw exception |
| 46 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | super.putIntent(intent); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 48 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | PathIntent pathIntent = (PathIntent) intent; |
| 50 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 51 | put(linkEvent.getSrc(), (PathIntent) intent); |
| 52 | put(linkEvent.getDst(), (PathIntent) intent); |
| 53 | } |
| 54 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | @Override |
| 57 | protected void removeIntent(String intentId) { |
| 58 | PathIntent intent = (PathIntent) getIntent(intentId); |
| 59 | for (LinkEvent linkEvent : intent.getPath()) { |
| 60 | get(linkEvent.getSrc()).remove(intent); |
| 61 | get(linkEvent.getDst()).remove(intent); |
| 62 | } |
| 63 | super.removeIntent(intentId); |
| 64 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 65 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) { |
| 67 | return getIntentsByPort( |
| 68 | linkEvent.getSrc().getDpid(), |
| 69 | linkEvent.getSrc().getNumber()); |
| 70 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 71 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) { |
| 73 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 74 | if (portToIntents != null) { |
| 75 | HashSet<PathIntent> targetIntents = portToIntents.get(port); |
| 76 | if (targetIntents != null) { |
| 77 | return Collections.unmodifiableCollection(targetIntents); |
| 78 | } |
| 79 | } |
| 80 | return new HashSet<>(); |
| 81 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 82 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | public Collection<PathIntent> getIntentsByDpid(Long dpid) { |
| 84 | HashSet<PathIntent> result = new HashSet<>(); |
| 85 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 86 | if (portToIntents != null) { |
| 87 | for (HashSet<PathIntent> targetIntents : portToIntents.values()) { |
| 88 | result.addAll(targetIntents); |
| 89 | } |
| 90 | } |
| 91 | return result; |
| 92 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 95 | * calculate available bandwidth of specified link. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param link |
| 98 | * @return |
| 99 | */ |
| 100 | public Double getAvailableBandwidth(Link link) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 101 | if (link == null) { |
| 102 | return null; |
| 103 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | Double bandwidth = link.getCapacity(); |
| 105 | LinkEvent linkEvent = new LinkEvent(link); |
| 106 | if (!bandwidth.isInfinite()) { |
| 107 | for (PathIntent intent : getIntentsByLink(linkEvent)) { |
| 108 | Double intentBandwidth = intent.getBandwidth(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 109 | if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 111 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | bandwidth -= intentBandwidth; |
| 113 | } |
| 114 | } |
| 115 | return bandwidth; |
| 116 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 117 | } |