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