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