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