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