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