tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.trivial.impl; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 2 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 3 | import com.google.common.collect.HashMultimap; |
| 4 | import com.google.common.collect.ImmutableSet; |
| 5 | import com.google.common.collect.Multimap; |
| 6 | import com.google.common.collect.Sets; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Service; |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 11 | import org.onlab.onos.net.Annotations; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.ConnectPoint; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.DefaultHost; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.DeviceId; |
| 15 | import org.onlab.onos.net.Host; |
| 16 | import org.onlab.onos.net.HostId; |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 17 | import org.onlab.onos.net.HostLocation; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.host.HostDescription; |
| 19 | import org.onlab.onos.net.host.HostEvent; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.host.HostStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.host.HostStoreDelegate; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.host.PortAddresses; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.provider.ProviderId; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 24 | import org.onlab.onos.store.AbstractStore; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 29 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 30 | import java.util.HashSet; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | import java.util.concurrent.ConcurrentHashMap; |
| 34 | |
| 35 | import static org.onlab.onos.net.host.HostEvent.Type.*; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 37 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Manages inventory of end-station hosts using trivial in-memory |
| 40 | * implementation. |
| 41 | */ |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 42 | @Component(immediate = true) |
| 43 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 44 | public class SimpleHostStore |
| 45 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 46 | implements HostStore { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 47 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | // Host inventory |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 51 | private final Map<HostId, StoredHost> hosts = new ConcurrentHashMap<>(2000000, 0.75f, 16); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 52 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 53 | // Hosts tracked by their location |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 54 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
tom | e615ee4 | 2014-09-11 10:52:10 -0700 | [diff] [blame] | 55 | |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 56 | private final Map<ConnectPoint, PortAddresses> portAddresses = |
| 57 | new ConcurrentHashMap<>(); |
| 58 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 59 | @Activate |
| 60 | public void activate() { |
| 61 | log.info("Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | log.info("Stopped"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 71 | HostDescription hostDescription) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 72 | StoredHost host = hosts.get(hostId); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 73 | if (host == null) { |
| 74 | return createHost(providerId, hostId, hostDescription); |
| 75 | } |
| 76 | return updateHost(providerId, host, hostDescription); |
| 77 | } |
| 78 | |
| 79 | // creates a new host and sends HOST_ADDED |
| 80 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 81 | HostDescription descr) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 82 | StoredHost newhost = new StoredHost(providerId, hostId, |
| 83 | descr.hwAddress(), |
| 84 | descr.vlan(), |
| 85 | descr.location(), |
| 86 | ImmutableSet.of(descr.ipAddress())); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 87 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 88 | hosts.put(hostId, newhost); |
| 89 | locations.put(descr.location(), newhost); |
| 90 | } |
| 91 | return new HostEvent(HOST_ADDED, newhost); |
| 92 | } |
| 93 | |
| 94 | // checks for type of update to host, sends appropriate event |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 95 | private HostEvent updateHost(ProviderId providerId, StoredHost host, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 96 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 97 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 98 | if (!host.location().equals(descr.location())) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 99 | host.setLocation(descr.location()); |
| 100 | return new HostEvent(HOST_MOVED, host); |
| 101 | } |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 102 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 103 | if (host.ipAddresses().contains(descr.ipAddress())) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 104 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 105 | } |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 106 | |
| 107 | Set<IpPrefix> addresses = new HashSet<>(host.ipAddresses()); |
| 108 | addresses.add(descr.ipAddress()); |
| 109 | StoredHost updated = new StoredHost(providerId, host.id(), |
| 110 | host.mac(), host.vlan(), |
| 111 | descr.location(), addresses); |
| 112 | event = new HostEvent(HOST_UPDATED, updated); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 113 | synchronized (this) { |
| 114 | hosts.put(host.id(), updated); |
| 115 | locations.remove(host.location(), host); |
| 116 | locations.put(updated.location(), updated); |
| 117 | } |
| 118 | return event; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 123 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 124 | Host host = hosts.remove(hostId); |
| 125 | if (host != null) { |
| 126 | locations.remove((host.location()), host); |
| 127 | return new HostEvent(HOST_REMOVED, host); |
| 128 | } |
| 129 | return null; |
| 130 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 131 | } |
| 132 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 135 | return hosts.size(); |
| 136 | } |
| 137 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public Iterable<Host> getHosts() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 140 | return ImmutableSet.<Host>copyOf(hosts.values()); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 141 | } |
| 142 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 145 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 146 | } |
| 147 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 150 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 151 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 152 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 153 | vlanset.add(h); |
| 154 | } |
| 155 | } |
| 156 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 157 | } |
| 158 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 161 | Set<Host> macset = new HashSet<>(); |
| 162 | for (Host h : hosts.values()) { |
| 163 | if (h.mac().equals(mac)) { |
| 164 | macset.add(h); |
| 165 | } |
| 166 | } |
| 167 | return macset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 170 | @Override |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 171 | public Set<Host> getHosts(IpPrefix ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 172 | Set<Host> ipset = new HashSet<>(); |
| 173 | for (Host h : hosts.values()) { |
| 174 | if (h.ipAddresses().contains(ip)) { |
| 175 | ipset.add(h); |
| 176 | } |
| 177 | } |
| 178 | return ipset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 179 | } |
| 180 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 183 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 184 | } |
| 185 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 186 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 187 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 188 | Set<Host> hostset = new HashSet<>(); |
| 189 | for (ConnectPoint p : locations.keySet()) { |
| 190 | if (p.deviceId().equals(deviceId)) { |
| 191 | hostset.addAll(locations.get(p)); |
| 192 | } |
| 193 | } |
| 194 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void updateAddressBindings(PortAddresses addresses) { |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 199 | synchronized (portAddresses) { |
| 200 | PortAddresses existing = portAddresses.get(addresses.connectPoint()); |
| 201 | if (existing == null) { |
| 202 | portAddresses.put(addresses.connectPoint(), addresses); |
| 203 | } else { |
| 204 | Set<IpPrefix> union = Sets.union(existing.ips(), addresses.ips()) |
| 205 | .immutableCopy(); |
| 206 | |
| 207 | MacAddress newMac = (addresses.mac() == null) ? existing.mac() |
| 208 | : addresses.mac(); |
| 209 | |
| 210 | PortAddresses newAddresses = |
| 211 | new PortAddresses(addresses.connectPoint(), union, newMac); |
| 212 | |
| 213 | portAddresses.put(newAddresses.connectPoint(), newAddresses); |
| 214 | } |
| 215 | } |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 219 | public void removeAddressBindings(PortAddresses addresses) { |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 220 | synchronized (portAddresses) { |
| 221 | PortAddresses existing = portAddresses.get(addresses.connectPoint()); |
| 222 | if (existing != null) { |
| 223 | Set<IpPrefix> difference = |
| 224 | Sets.difference(existing.ips(), addresses.ips()).immutableCopy(); |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 225 | |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 226 | // If they removed the existing mac, set the new mac to null. |
| 227 | // Otherwise, keep the existing mac. |
| 228 | MacAddress newMac = existing.mac(); |
| 229 | if (addresses.mac() != null && addresses.mac().equals(existing.mac())) { |
| 230 | newMac = null; |
| 231 | } |
| 232 | |
| 233 | PortAddresses newAddresses = |
| 234 | new PortAddresses(addresses.connectPoint(), difference, newMac); |
| 235 | |
| 236 | portAddresses.put(newAddresses.connectPoint(), newAddresses); |
| 237 | } |
| 238 | } |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void clearAddressBindings(ConnectPoint connectPoint) { |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 243 | synchronized (portAddresses) { |
| 244 | portAddresses.remove(connectPoint); |
| 245 | } |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public Set<PortAddresses> getAddressBindings() { |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 250 | synchronized (portAddresses) { |
| 251 | return new HashSet<>(portAddresses.values()); |
| 252 | } |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) { |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 257 | PortAddresses addresses; |
| 258 | |
| 259 | synchronized (portAddresses) { |
| 260 | addresses = portAddresses.get(connectPoint); |
| 261 | } |
| 262 | |
| 263 | if (addresses == null) { |
| 264 | addresses = new PortAddresses(connectPoint, null, null); |
| 265 | } |
| 266 | |
| 267 | return addresses; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 268 | } |
| 269 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame^] | 270 | // Auxiliary extension to allow location to mutate. |
| 271 | private class StoredHost extends DefaultHost { |
| 272 | private HostLocation location; |
| 273 | |
| 274 | /** |
| 275 | * Creates an end-station host using the supplied information. |
| 276 | * |
| 277 | * @param providerId provider identity |
| 278 | * @param id host identifier |
| 279 | * @param mac host MAC address |
| 280 | * @param vlan host VLAN identifier |
| 281 | * @param location host location |
| 282 | * @param ips host IP addresses |
| 283 | * @param annotations optional key/value annotations |
| 284 | */ |
| 285 | public StoredHost(ProviderId providerId, HostId id, |
| 286 | MacAddress mac, VlanId vlan, HostLocation location, |
| 287 | Set<IpPrefix> ips, Annotations... annotations) { |
| 288 | super(providerId, id, mac, vlan, location, ips, annotations); |
| 289 | this.location = location; |
| 290 | } |
| 291 | |
| 292 | void setLocation(HostLocation location) { |
| 293 | this.location = location; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public HostLocation location() { |
| 298 | return location; |
| 299 | } |
| 300 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 301 | } |