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