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