Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.store.host.impl; |
| 17 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 20 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -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.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onlab.util.KryoNamespace; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 31 | import org.onlab.util.Tools; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Annotations; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DefaultAnnotations; |
| 35 | import org.onosproject.net.DefaultHost; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.HostId; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 39 | import org.onosproject.net.HostLocation; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 40 | import org.onosproject.net.host.HostDescription; |
| 41 | import org.onosproject.net.host.HostEvent; |
| 42 | import org.onosproject.net.host.HostStore; |
| 43 | import org.onosproject.net.host.HostStoreDelegate; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 44 | import org.onosproject.net.provider.ProviderId; |
| 45 | import org.onosproject.store.AbstractStore; |
| 46 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 47 | import org.onosproject.store.service.ConsistentMap; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 48 | import org.onosproject.store.service.ConsistentMapException; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 49 | import org.onosproject.store.service.MapEvent; |
| 50 | import org.onosproject.store.service.MapEventListener; |
| 51 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 52 | import org.onosproject.store.service.StorageService; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 53 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 56 | import java.util.Collection; |
| 57 | import java.util.HashSet; |
| 58 | import java.util.Map; |
| 59 | import java.util.Objects; |
| 60 | import java.util.Set; |
| 61 | import java.util.concurrent.ConcurrentHashMap; |
| 62 | import java.util.function.Predicate; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 63 | import java.util.function.Supplier; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static com.google.common.base.Preconditions.checkNotNull; |
| 67 | import static com.google.common.base.Preconditions.checkState; |
| 68 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 69 | import static org.onosproject.net.host.HostEvent.Type.*; |
| 70 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | @Service |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 77 | public class DistributedHostStore |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 78 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 79 | implements HostStore { |
| 80 | |
| 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected StorageService storageService; |
| 85 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 86 | private ConsistentMap<HostId, DefaultHost> host; |
| 87 | private Map<HostId, DefaultHost> hosts; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 88 | |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 89 | private final ConcurrentHashMap<HostId, DefaultHost> prevHosts = |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 90 | new ConcurrentHashMap<>(); |
| 91 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 92 | private MapEventListener<HostId, DefaultHost> hostLocationTracker = |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 93 | new HostLocationTracker(); |
| 94 | |
| 95 | @Activate |
| 96 | public void activate() { |
| 97 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 98 | .register(KryoNamespaces.API); |
| 99 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 100 | host = storageService.<HostId, DefaultHost>consistentMapBuilder() |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 101 | .withName("onos-hosts") |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 102 | .withRelaxedReadConsistency() |
| 103 | .withSerializer(Serializer.using(hostSerializer.build())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 104 | .build(); |
| 105 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 106 | hosts = host.asJavaMap(); |
| 107 | |
| 108 | host.addListener(hostLocationTracker); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 109 | |
| 110 | log.info("Started"); |
| 111 | } |
| 112 | |
| 113 | @Deactivate |
| 114 | public void deactivate() { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 115 | host.removeListener(hostLocationTracker); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 116 | prevHosts.clear(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 117 | |
| 118 | log.info("Stopped"); |
| 119 | } |
| 120 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 121 | private boolean shouldUpdate(DefaultHost existingHost, |
| 122 | ProviderId providerId, |
| 123 | HostId hostId, |
| 124 | HostDescription hostDescription, |
| 125 | boolean replaceIPs) { |
| 126 | if (existingHost == null) { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | if (!Objects.equals(existingHost.providerId(), providerId) || |
| 131 | !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) || |
| 132 | !Objects.equals(existingHost.vlan(), hostDescription.vlan()) || |
| 133 | !Objects.equals(existingHost.location(), hostDescription.location())) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | if (replaceIPs) { |
| 138 | if (!Objects.equals(hostDescription.ipAddress(), |
| 139 | existingHost.ipAddresses())) { |
| 140 | return true; |
| 141 | } |
| 142 | } else { |
| 143 | if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // check to see if any of the annotations provided by hostDescription |
| 149 | // differ from those in the existing host |
| 150 | return hostDescription.annotations().keys().stream() |
| 151 | .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k), |
| 152 | existingHost.annotations().value(k))); |
| 153 | |
| 154 | |
| 155 | } |
| 156 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 157 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 160 | HostId hostId, |
| 161 | HostDescription hostDescription, |
| 162 | boolean replaceIPs) { |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 163 | Supplier<Versioned<DefaultHost>> supplier = |
| 164 | () -> host.computeIf(hostId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 165 | existingHost -> shouldUpdate(existingHost, providerId, hostId, |
| 166 | hostDescription, replaceIPs), |
| 167 | (id, existingHost) -> { |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 168 | HostLocation location = hostDescription.location(); |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 169 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 170 | final Set<IpAddress> addresses; |
| 171 | if (existingHost == null || replaceIPs) { |
| 172 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 173 | } else { |
| 174 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 175 | addresses.addAll(hostDescription.ipAddress()); |
| 176 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 177 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 178 | final Annotations annotations; |
| 179 | if (existingHost != null) { |
| 180 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 181 | hostDescription.annotations()); |
| 182 | } else { |
| 183 | annotations = hostDescription.annotations(); |
| 184 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 185 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame^] | 186 | return new DefaultHost(providerId, |
| 187 | hostId, |
| 188 | hostDescription.hwAddress(), |
| 189 | hostDescription.vlan(), |
| 190 | location, |
| 191 | addresses, |
| 192 | annotations); |
| 193 | }); |
| 194 | |
| 195 | Tools.retryable(supplier, |
| 196 | ConsistentMapException.ConcurrentModification.class, |
| 197 | Integer.MAX_VALUE, |
| 198 | 50).get(); |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 199 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 200 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 203 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 206 | hosts.remove(hostId); |
| 207 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 210 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 211 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 212 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 213 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 214 | if (existingHost != null) { |
| 215 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 216 | "Existing and new MAC addresses differ."); |
| 217 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 218 | "Existing and new VLANs differ."); |
| 219 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 220 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 221 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 222 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 223 | addresses.remove(ipAddress); |
| 224 | return new DefaultHost(existingHost.providerId(), |
| 225 | hostId, |
| 226 | existingHost.mac(), |
| 227 | existingHost.vlan(), |
| 228 | existingHost.location(), |
| 229 | ImmutableSet.copyOf(addresses), |
| 230 | existingHost.annotations()); |
| 231 | } else { |
| 232 | return existingHost; |
| 233 | } |
| 234 | } |
| 235 | return null; |
| 236 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 237 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 241 | public int getHostCount() { |
| 242 | return hosts.size(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Iterable<Host> getHosts() { |
| 247 | return ImmutableSet.copyOf(hosts.values()); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Host getHost(HostId hostId) { |
| 252 | return hosts.get(hostId); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public Set<Host> getHosts(VlanId vlanId) { |
| 257 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public Set<Host> getHosts(MacAddress mac) { |
| 262 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Set<Host> getHosts(IpAddress ip) { |
| 267 | return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 272 | Set<Host> filtered = hosts.entrySet().stream() |
| 273 | .filter(entry -> entry.getValue().location().equals(connectPoint)) |
| 274 | .map(Map.Entry::getValue) |
| 275 | .collect(Collectors.toSet()); |
| 276 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 281 | Set<Host> filtered = hosts.entrySet().stream() |
| 282 | .filter(entry -> entry.getValue().location().deviceId().equals(deviceId)) |
| 283 | .map(Map.Entry::getValue) |
| 284 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 285 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 288 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 289 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 290 | } |
| 291 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 292 | private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 293 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 294 | public void event(MapEvent<HostId, DefaultHost> event) { |
| 295 | DefaultHost host = checkNotNull(event.value().value()); |
| 296 | if (event.type() == MapEvent.Type.INSERT) { |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 297 | Host prevHost = prevHosts.put(host.id(), host); |
| 298 | if (prevHost == null) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 299 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 300 | } else if (!Objects.equals(prevHost.location(), host.location())) { |
| 301 | notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost)); |
| 302 | } else if (!Objects.equals(prevHost, host)) { |
| 303 | notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost)); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 304 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 305 | } else if (event.type() == MapEvent.Type.REMOVE) { |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 306 | if (prevHosts.remove(host.id()) != null) { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 307 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
| 308 | } |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |