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