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); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 91 | if (host == null) { |
| 92 | return createHost(providerId, hostId, hostDescription); |
| 93 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 94 | return updateHost(providerId, host, hostDescription, replaceIps); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // creates a new host and sends HOST_ADDED |
| 98 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 99 | HostDescription descr) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 100 | StoredHost newhost = new StoredHost(providerId, hostId, |
| 101 | descr.hwAddress(), |
| 102 | descr.vlan(), |
| 103 | descr.location(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 104 | ImmutableSet.copyOf(descr.ipAddress()), |
| 105 | descr.annotations()); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 106 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 107 | hosts.put(hostId, newhost); |
| 108 | locations.put(descr.location(), newhost); |
| 109 | } |
| 110 | return new HostEvent(HOST_ADDED, newhost); |
| 111 | } |
| 112 | |
| 113 | // checks for type of update to host, sends appropriate event |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 114 | private HostEvent updateHost(ProviderId providerId, StoredHost host, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 115 | HostDescription descr, boolean replaceIps) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 116 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 117 | if (!host.location().equals(descr.location())) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 118 | host.setLocation(descr.location()); |
| 119 | return new HostEvent(HOST_MOVED, host); |
| 120 | } |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 121 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 122 | if (host.ipAddresses().containsAll(descr.ipAddress()) && |
| 123 | descr.annotations().keys().isEmpty()) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 124 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 125 | } |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 126 | |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 127 | final Set<IpAddress> addresses; |
| 128 | if (replaceIps) { |
| 129 | addresses = ImmutableSet.copyOf(descr.ipAddress()); |
| 130 | } else { |
| 131 | addresses = new HashSet<>(host.ipAddresses()); |
| 132 | addresses.addAll(descr.ipAddress()); |
| 133 | } |
| 134 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 135 | Annotations annotations = merge((DefaultAnnotations) host.annotations(), |
| 136 | descr.annotations()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 137 | StoredHost updated = new StoredHost(providerId, host.id(), |
| 138 | host.mac(), host.vlan(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 139 | descr.location(), addresses, |
| 140 | annotations); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 141 | event = new HostEvent(HOST_UPDATED, updated); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 142 | synchronized (this) { |
| 143 | hosts.put(host.id(), updated); |
| 144 | locations.remove(host.location(), host); |
| 145 | locations.put(updated.location(), updated); |
| 146 | } |
| 147 | return event; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 148 | } |
| 149 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 152 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 153 | Host host = hosts.remove(hostId); |
| 154 | if (host != null) { |
| 155 | locations.remove((host.location()), host); |
| 156 | return new HostEvent(HOST_REMOVED, host); |
| 157 | } |
| 158 | return null; |
| 159 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 160 | } |
| 161 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 162 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 163 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | @Override |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 168 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 169 | return hosts.size(); |
| 170 | } |
| 171 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public Iterable<Host> getHosts() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 174 | return ImmutableSet.<Host>copyOf(hosts.values()); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 175 | } |
| 176 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 177 | @Override |
| 178 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 179 | return hosts.get(hostId); |
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(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 184 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 185 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 186 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 187 | vlanset.add(h); |
| 188 | } |
| 189 | } |
| 190 | return vlanset; |
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 |
| 194 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 195 | Set<Host> macset = new HashSet<>(); |
| 196 | for (Host h : hosts.values()) { |
| 197 | if (h.mac().equals(mac)) { |
| 198 | macset.add(h); |
| 199 | } |
| 200 | } |
| 201 | return macset; |
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 |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 205 | public Set<Host> getHosts(IpAddress ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 206 | Set<Host> ipset = new HashSet<>(); |
| 207 | for (Host h : hosts.values()) { |
| 208 | if (h.ipAddresses().contains(ip)) { |
| 209 | ipset.add(h); |
| 210 | } |
| 211 | } |
| 212 | return ipset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 213 | } |
| 214 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 217 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 220 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 221 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 222 | Set<Host> hostset = new HashSet<>(); |
| 223 | for (ConnectPoint p : locations.keySet()) { |
| 224 | if (p.deviceId().equals(deviceId)) { |
| 225 | hostset.addAll(locations.get(p)); |
| 226 | } |
| 227 | } |
| 228 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 229 | } |
| 230 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 231 | // Auxiliary extension to allow location to mutate. |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 232 | private static final class StoredHost extends DefaultHost { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 233 | private HostLocation location; |
| 234 | |
| 235 | /** |
| 236 | * Creates an end-station host using the supplied information. |
| 237 | * |
| 238 | * @param providerId provider identity |
| 239 | * @param id host identifier |
| 240 | * @param mac host MAC address |
| 241 | * @param vlan host VLAN identifier |
| 242 | * @param location host location |
| 243 | * @param ips host IP addresses |
| 244 | * @param annotations optional key/value annotations |
| 245 | */ |
| 246 | public StoredHost(ProviderId providerId, HostId id, |
| 247 | MacAddress mac, VlanId vlan, HostLocation location, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 248 | Set<IpAddress> ips, Annotations... annotations) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 249 | super(providerId, id, mac, vlan, location, ips, annotations); |
| 250 | this.location = location; |
| 251 | } |
| 252 | |
| 253 | void setLocation(HostLocation location) { |
| 254 | this.location = location; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public HostLocation location() { |
| 259 | return location; |
| 260 | } |
| 261 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 262 | } |