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