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 | |
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()), |
| 109 | descr.annotations()); |
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, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 119 | HostDescription descr, boolean replaceIps) { |
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 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 126 | if (host.ipAddresses().containsAll(descr.ipAddress()) && |
| 127 | descr.annotations().keys().isEmpty()) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 128 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 129 | } |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 130 | |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 131 | final Set<IpAddress> addresses; |
| 132 | if (replaceIps) { |
| 133 | addresses = ImmutableSet.copyOf(descr.ipAddress()); |
| 134 | } else { |
| 135 | addresses = new HashSet<>(host.ipAddresses()); |
| 136 | addresses.addAll(descr.ipAddress()); |
| 137 | } |
| 138 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 139 | Annotations annotations = merge((DefaultAnnotations) host.annotations(), |
| 140 | descr.annotations()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 141 | StoredHost updated = new StoredHost(providerId, host.id(), |
| 142 | host.mac(), host.vlan(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 143 | descr.location(), addresses, |
| 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) { |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | @Override |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 174 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 175 | return hosts.size(); |
| 176 | } |
| 177 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 178 | @Override |
| 179 | public Iterable<Host> getHosts() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 180 | return ImmutableSet.<Host>copyOf(hosts.values()); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 181 | } |
| 182 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 185 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 186 | } |
| 187 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 190 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 191 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 192 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 193 | vlanset.add(h); |
| 194 | } |
| 195 | } |
| 196 | return vlanset; |
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(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 201 | Set<Host> macset = new HashSet<>(); |
| 202 | for (Host h : hosts.values()) { |
| 203 | if (h.mac().equals(mac)) { |
| 204 | macset.add(h); |
| 205 | } |
| 206 | } |
| 207 | return macset; |
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 |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 211 | public Set<Host> getHosts(IpAddress ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 212 | Set<Host> ipset = new HashSet<>(); |
| 213 | for (Host h : hosts.values()) { |
| 214 | if (h.ipAddresses().contains(ip)) { |
| 215 | ipset.add(h); |
| 216 | } |
| 217 | } |
| 218 | return ipset; |
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 |
| 222 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 223 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 224 | } |
| 225 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 226 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 227 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 228 | Set<Host> hostset = new HashSet<>(); |
| 229 | for (ConnectPoint p : locations.keySet()) { |
| 230 | if (p.deviceId().equals(deviceId)) { |
| 231 | hostset.addAll(locations.get(p)); |
| 232 | } |
| 233 | } |
| 234 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 235 | } |
| 236 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 237 | // Auxiliary extension to allow location to mutate. |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 238 | private static final class StoredHost extends DefaultHost { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 239 | private HostLocation location; |
| 240 | |
| 241 | /** |
| 242 | * Creates an end-station host using the supplied information. |
| 243 | * |
| 244 | * @param providerId provider identity |
| 245 | * @param id host identifier |
| 246 | * @param mac host MAC address |
| 247 | * @param vlan host VLAN identifier |
| 248 | * @param location host location |
| 249 | * @param ips host IP addresses |
| 250 | * @param annotations optional key/value annotations |
| 251 | */ |
| 252 | public StoredHost(ProviderId providerId, HostId id, |
| 253 | MacAddress mac, VlanId vlan, HostLocation location, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 254 | Set<IpAddress> ips, Annotations... annotations) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 255 | super(providerId, id, mac, vlan, location, ips, annotations); |
| 256 | this.location = location; |
| 257 | } |
| 258 | |
| 259 | void setLocation(HostLocation location) { |
| 260 | this.location = location; |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public HostLocation location() { |
| 265 | return location; |
| 266 | } |
| 267 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 268 | } |