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 | /** |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 13 | * In addition to maintaining the Intent ID to Intent mapping of its |
| 14 | * superclass, this class maintains a mapping from switch port to |
| 15 | * PathIntent. It is used to quickly identify Intents that are affected |
| 16 | * when a network event involves a particular switch port. |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 17 | */ |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 18 | public class PathIntentMap extends IntentMap { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 19 | private final HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents; |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 20 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Constructor. |
| 23 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | public PathIntentMap() { |
| 25 | intents = new HashMap<>(); |
| 26 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 27 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Retrieve all PathIntents that contain the specified switch port. |
| 30 | * |
| 31 | * @param swPort the switch port to retrieve Intents for. |
| 32 | * @return a set of all intents that contain swPort |
| 33 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | private HashSet<PathIntent> get(SwitchPort swPort) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 35 | Long dpid = swPort.getDpid().value(); |
| 36 | Long port = (long) swPort.getNumber().value(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 38 | if (portToIntents == null) { |
| 39 | portToIntents = new HashMap<>(); |
| 40 | intents.put(dpid, portToIntents); |
| 41 | } |
| 42 | HashSet<PathIntent> targetIntents = portToIntents.get(port); |
| 43 | if (targetIntents == null) { |
| 44 | targetIntents = new HashSet<>(); |
| 45 | portToIntents.put(port, targetIntents); |
| 46 | } |
| 47 | return targetIntents; |
| 48 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 49 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Add a PathIntent to a particular switch port. |
| 52 | * |
| 53 | * @param swPort switch port |
| 54 | * @param intent Path Intent |
| 55 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | private void put(SwitchPort swPort, PathIntent intent) { |
| 57 | get(swPort).add(intent); |
| 58 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 59 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Add a PathIntent to the map. The function will automatically |
| 62 | * update the map with all switch ports contained in the Intent. |
| 63 | * |
| 64 | * @param intent the PathIntent |
| 65 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | @Override |
| 67 | protected void putIntent(Intent intent) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 68 | if (!(intent instanceof PathIntent)) { |
| 69 | return; // TODO throw exception |
| 70 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | super.putIntent(intent); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 72 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | PathIntent pathIntent = (PathIntent) intent; |
| 74 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 75 | put(linkEvent.getSrc(), (PathIntent) intent); |
| 76 | put(linkEvent.getDst(), (PathIntent) intent); |
| 77 | } |
| 78 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 79 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Removes a PathIntent from the map, including all switch ports. |
| 82 | * |
| 83 | * @param intentId the ID of the PathIntent to be removed |
| 84 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | @Override |
| 86 | protected void removeIntent(String intentId) { |
| 87 | PathIntent intent = (PathIntent) getIntent(intentId); |
| 88 | for (LinkEvent linkEvent : intent.getPath()) { |
| 89 | get(linkEvent.getSrc()).remove(intent); |
| 90 | get(linkEvent.getDst()).remove(intent); |
| 91 | } |
| 92 | super.removeIntent(intentId); |
| 93 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 94 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Retrieve all intents that use a particular link. |
| 97 | * |
| 98 | * @param linkEvent the link to look up |
| 99 | * @return a collection of PathIntents that use the link |
| 100 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) { |
| 102 | return getIntentsByPort( |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 103 | linkEvent.getSrc().getDpid().value(), |
| 104 | (long) linkEvent.getSrc().getNumber().value()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 106 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Retrieve all intents that use a particular port. |
| 109 | * |
| 110 | * @param dpid the switch's DPID |
| 111 | * @param port the switch's port |
| 112 | * @return a collection of PathIntents that use the port |
| 113 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) { |
| 115 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 116 | if (portToIntents != null) { |
| 117 | HashSet<PathIntent> targetIntents = portToIntents.get(port); |
| 118 | if (targetIntents != null) { |
| 119 | return Collections.unmodifiableCollection(targetIntents); |
| 120 | } |
| 121 | } |
| 122 | return new HashSet<>(); |
| 123 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 124 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Retrieve all intents that use a particular switch. |
| 127 | * |
| 128 | * @param dpid the switch's DPID |
| 129 | * @return a collection of PathIntents that use the switch |
| 130 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | public Collection<PathIntent> getIntentsByDpid(Long dpid) { |
| 132 | HashSet<PathIntent> result = new HashSet<>(); |
| 133 | HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid); |
| 134 | if (portToIntents != null) { |
| 135 | for (HashSet<PathIntent> targetIntents : portToIntents.values()) { |
| 136 | result.addAll(targetIntents); |
| 137 | } |
| 138 | } |
| 139 | return result; |
| 140 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 141 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | /** |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 143 | * Calculate available bandwidth of specified link. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | * |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 145 | * @param link the Link |
| 146 | * @return the available bandwidth |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | */ |
| 148 | public Double getAvailableBandwidth(Link link) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 149 | if (link == null) { |
| 150 | return null; |
| 151 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | Double bandwidth = link.getCapacity(); |
| 153 | LinkEvent linkEvent = new LinkEvent(link); |
| 154 | if (!bandwidth.isInfinite()) { |
| 155 | for (PathIntent intent : getIntentsByLink(linkEvent)) { |
| 156 | Double intentBandwidth = intent.getBandwidth(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 157 | if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 159 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | bandwidth -= intentBandwidth; |
| 161 | } |
| 162 | } |
| 163 | return bandwidth; |
| 164 | } |
Toshio Koide | 5526c4f | 2014-02-11 12:39:03 -0800 | [diff] [blame] | 165 | } |