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