Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -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 | */ |
| 16 | package org.onosproject.store.host.impl; |
| 17 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalNotification; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Sets; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 23 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.MacAddress; |
| 32 | import org.onlab.packet.VlanId; |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onosproject.net.Annotations; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.DefaultAnnotations; |
| 37 | import org.onosproject.net.DefaultHost; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.Host; |
| 40 | import org.onosproject.net.HostId; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 41 | import org.onosproject.net.HostLocation; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 42 | import org.onosproject.net.host.HostDescription; |
| 43 | import org.onosproject.net.host.HostEvent; |
| 44 | import org.onosproject.net.host.HostStore; |
| 45 | import org.onosproject.net.host.HostStoreDelegate; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 46 | import org.onosproject.net.provider.ProviderId; |
| 47 | import org.onosproject.store.AbstractStore; |
| 48 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 49 | import org.onosproject.store.service.ConsistentMap; |
| 50 | import org.onosproject.store.service.MapEvent; |
| 51 | import org.onosproject.store.service.MapEventListener; |
| 52 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.StorageService; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 54 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 58 | import java.util.Collection; |
| 59 | import java.util.HashSet; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 60 | import java.util.Iterator; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 61 | import java.util.Map; |
| 62 | import java.util.Objects; |
| 63 | import java.util.Set; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 64 | import java.util.concurrent.ConcurrentHashMap; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 65 | import java.util.concurrent.Executor; |
| 66 | import java.util.concurrent.Executors; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 67 | import java.util.concurrent.ScheduledExecutorService; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 68 | import java.util.concurrent.TimeUnit; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 69 | import java.util.function.Consumer; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 70 | import java.util.function.Predicate; |
| 71 | import java.util.stream.Collectors; |
| 72 | |
| 73 | import static com.google.common.base.Preconditions.checkNotNull; |
| 74 | import static com.google.common.base.Preconditions.checkState; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 75 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 76 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 77 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 78 | import static org.onosproject.net.host.HostEvent.Type.*; |
| 79 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 83 | */ |
| 84 | @Component(immediate = true) |
| 85 | @Service |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 86 | public class DistributedHostStore |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 87 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 88 | implements HostStore { |
| 89 | |
| 90 | private final Logger log = getLogger(getClass()); |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected StorageService storageService; |
| 94 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 95 | private ConsistentMap<HostId, DefaultHost> hostsConsistentMap; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 96 | private Map<HostId, DefaultHost> hosts; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 97 | private Map<IpAddress, Set<Host>> hostsByIp; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 98 | private MapEventListener<HostId, DefaultHost> hostLocationTracker = |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 99 | new HostLocationTracker(); |
| 100 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 101 | private ConsistentMap<MacAddress, PendingHostLocation> pendingHostsConsistentMap; |
| 102 | private Map<MacAddress, PendingHostLocation> pendingHosts; |
| 103 | private MapEventListener<MacAddress, PendingHostLocation> pendingHostListener = |
| 104 | new PendingHostListener(); |
| 105 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 106 | private ScheduledExecutorService executor; |
| 107 | |
| 108 | private Consumer<Status> statusChangeListener; |
| 109 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 110 | // TODO make this configurable |
Charles Chan | 8277b6b | 2017-12-03 13:48:46 -0800 | [diff] [blame] | 111 | private static final int PROBE_TIMEOUT_MS = 3000; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 112 | |
| 113 | private Cache<MacAddress, PendingHostLocation> pendingHostsCache = CacheBuilder.newBuilder() |
| 114 | .expireAfterWrite(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS) |
| 115 | .removalListener((RemovalNotification<MacAddress, PendingHostLocation> notification) -> { |
| 116 | switch (notification.getCause()) { |
| 117 | case EXPIRED: |
| 118 | PendingHostLocation expired = notification.getValue(); |
| 119 | if (expired != null) { |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 120 | if (timeoutPendingHostLocation(notification.getKey())) { |
| 121 | log.info("Evict {} from pendingHosts due to probe timeout", notification.getValue()); |
| 122 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 123 | } |
| 124 | break; |
| 125 | case EXPLICIT: |
| 126 | break; |
| 127 | default: |
| 128 | log.warn("Remove {} from pendingHostLocations for unexpected reason {}", |
| 129 | notification.getKey(), notification.getCause()); |
| 130 | } |
| 131 | }).build(); |
| 132 | |
| 133 | private ScheduledExecutorService cacheCleaner = Executors.newSingleThreadScheduledExecutor(); |
| 134 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 135 | @Activate |
| 136 | public void activate() { |
| 137 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 138 | .register(KryoNamespaces.API); |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 139 | hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder() |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 140 | .withName("onos-hosts") |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 141 | .withRelaxedReadConsistency() |
| 142 | .withSerializer(Serializer.using(hostSerializer.build())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 143 | .build(); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 144 | hostsConsistentMap.addListener(hostLocationTracker); |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 145 | hosts = hostsConsistentMap.asJavaMap(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 146 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 147 | KryoNamespace.Builder pendingHostSerializer = KryoNamespace.newBuilder() |
| 148 | .register(KryoNamespaces.API) |
| 149 | .register(PendingHostLocation.class); |
| 150 | pendingHostsConsistentMap = storageService.<MacAddress, PendingHostLocation>consistentMapBuilder() |
| 151 | .withName("onos-hosts-pending") |
| 152 | .withRelaxedReadConsistency() |
| 153 | .withSerializer(Serializer.using(pendingHostSerializer.build())) |
| 154 | .build(); |
| 155 | pendingHostsConsistentMap.addListener(pendingHostListener); |
| 156 | pendingHosts = pendingHostsConsistentMap.asJavaMap(); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 157 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 158 | cacheCleaner.scheduleAtFixedRate(pendingHostsCache::cleanUp, 0, |
| 159 | PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 160 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 161 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/hosts", "store", log)); |
| 162 | statusChangeListener = status -> { |
| 163 | if (status == Status.ACTIVE) { |
| 164 | executor.execute(this::loadHostsByIp); |
| 165 | } |
| 166 | }; |
| 167 | hostsConsistentMap.addStatusChangeListener(statusChangeListener); |
| 168 | loadHostsByIp(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 169 | log.info("Started"); |
| 170 | } |
| 171 | |
| 172 | @Deactivate |
| 173 | public void deactivate() { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 174 | hostsConsistentMap.removeListener(hostLocationTracker); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 175 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 176 | cacheCleaner.shutdown(); |
| 177 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 178 | log.info("Stopped"); |
| 179 | } |
| 180 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 181 | private void loadHostsByIp() { |
| 182 | hostsByIp = new ConcurrentHashMap<IpAddress, Set<Host>>(); |
| 183 | hostsConsistentMap.asJavaMap().values().forEach(host -> { |
| 184 | host.ipAddresses().forEach(ip -> { |
| 185 | Set<Host> existingHosts = hostsByIp.get(ip); |
| 186 | if (existingHosts == null) { |
| 187 | hostsByIp.put(ip, addHosts(host)); |
| 188 | } else { |
| 189 | existingHosts.add(host); |
| 190 | } |
| 191 | }); |
| 192 | }); |
| 193 | } |
| 194 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 195 | private boolean shouldUpdate(DefaultHost existingHost, |
| 196 | ProviderId providerId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 197 | HostDescription hostDescription, |
| 198 | boolean replaceIPs) { |
| 199 | if (existingHost == null) { |
| 200 | return true; |
| 201 | } |
| 202 | |
Charles Chan | 69ebcbb | 2017-04-27 14:33:21 -0700 | [diff] [blame] | 203 | // Avoid overriding configured host with learnt host |
| 204 | if (existingHost.configured() && !hostDescription.configured()) { |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 208 | if (!Objects.equals(existingHost.providerId(), providerId) || |
| 209 | !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) || |
| 210 | !Objects.equals(existingHost.vlan(), hostDescription.vlan()) || |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 211 | !Objects.equals(existingHost.locations(), hostDescription.locations())) { |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
| 215 | if (replaceIPs) { |
| 216 | if (!Objects.equals(hostDescription.ipAddress(), |
| 217 | existingHost.ipAddresses())) { |
| 218 | return true; |
| 219 | } |
| 220 | } else { |
| 221 | if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) { |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // check to see if any of the annotations provided by hostDescription |
| 227 | // differ from those in the existing host |
| 228 | return hostDescription.annotations().keys().stream() |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 229 | .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k), |
| 230 | existingHost.annotations().value(k))); |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 231 | |
| 232 | |
| 233 | } |
| 234 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 235 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 236 | @Override |
| 237 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 238 | HostId hostId, |
| 239 | HostDescription hostDescription, |
| 240 | boolean replaceIPs) { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 241 | hostsConsistentMap.computeIf(hostId, |
Charles Chan | 69ebcbb | 2017-04-27 14:33:21 -0700 | [diff] [blame] | 242 | existingHost -> shouldUpdate(existingHost, providerId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 243 | hostDescription, replaceIPs), |
| 244 | (id, existingHost) -> { |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 245 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 246 | final Set<IpAddress> addresses; |
| 247 | if (existingHost == null || replaceIPs) { |
| 248 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 249 | } else { |
| 250 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 251 | addresses.addAll(hostDescription.ipAddress()); |
| 252 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 253 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 254 | final Annotations annotations; |
| 255 | if (existingHost != null) { |
| 256 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 257 | hostDescription.annotations()); |
| 258 | } else { |
| 259 | annotations = hostDescription.annotations(); |
| 260 | } |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 261 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 262 | return new DefaultHost(providerId, |
| 263 | hostId, |
| 264 | hostDescription.hwAddress(), |
| 265 | hostDescription.vlan(), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 266 | hostDescription.locations(), |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 267 | addresses, |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 268 | hostDescription.configured(), |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 269 | annotations); |
| 270 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 271 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 274 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 277 | hosts.remove(hostId); |
| 278 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 281 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 282 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 283 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 284 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 285 | if (existingHost != null) { |
| 286 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 287 | "Existing and new MAC addresses differ."); |
| 288 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 289 | "Existing and new VLANs differ."); |
| 290 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 291 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 292 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 293 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 294 | addresses.remove(ipAddress); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 295 | removeIpFromHostsByIp(existingHost, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 296 | return new DefaultHost(existingHost.providerId(), |
| 297 | hostId, |
| 298 | existingHost.mac(), |
| 299 | existingHost.vlan(), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 300 | existingHost.locations(), |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 301 | ImmutableSet.copyOf(addresses), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 302 | existingHost.configured(), |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 303 | existingHost.annotations()); |
| 304 | } else { |
| 305 | return existingHost; |
| 306 | } |
| 307 | } |
| 308 | return null; |
| 309 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 310 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 314 | public void removeLocation(HostId hostId, HostLocation location) { |
| 315 | hosts.compute(hostId, (id, existingHost) -> { |
| 316 | if (existingHost != null) { |
| 317 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 318 | "Existing and new MAC addresses differ."); |
| 319 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 320 | "Existing and new VLANs differ."); |
| 321 | |
| 322 | Set<HostLocation> locations = new HashSet<>(existingHost.locations()); |
| 323 | locations.remove(location); |
| 324 | |
| 325 | // Remove entire host if we are removing the last location |
| 326 | return locations.isEmpty() ? null : |
| 327 | new DefaultHost(existingHost.providerId(), |
| 328 | hostId, existingHost.mac(), existingHost.vlan(), |
| 329 | locations, existingHost.ipAddresses(), |
| 330 | existingHost.configured(), existingHost.annotations()); |
| 331 | } |
| 332 | return null; |
| 333 | }); |
| 334 | } |
| 335 | |
| 336 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 337 | public int getHostCount() { |
| 338 | return hosts.size(); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public Iterable<Host> getHosts() { |
| 343 | return ImmutableSet.copyOf(hosts.values()); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public Host getHost(HostId hostId) { |
| 348 | return hosts.get(hostId); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public Set<Host> getHosts(VlanId vlanId) { |
| 353 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public Set<Host> getHosts(MacAddress mac) { |
| 358 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public Set<Host> getHosts(IpAddress ip) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 363 | Set<Host> hosts = hostsByIp.get(ip); |
| 364 | return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 369 | Set<Host> filtered = hosts.entrySet().stream() |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 370 | .filter(entry -> entry.getValue().locations().contains(connectPoint)) |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 371 | .map(Map.Entry::getValue) |
| 372 | .collect(Collectors.toSet()); |
| 373 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 378 | Set<Host> filtered = hosts.entrySet().stream() |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 379 | .filter(entry -> entry.getValue().locations().stream() |
| 380 | .map(HostLocation::deviceId).anyMatch(dpid -> dpid.equals(deviceId))) |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 381 | .map(Map.Entry::getValue) |
| 382 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 383 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public MacAddress addPendingHostLocation(HostId hostId, HostLocation hostLocation) { |
| 388 | // Use ONLab OUI (3 bytes) + atomic counter (3 bytes) as the source MAC of the probe |
| 389 | long nextIndex = storageService.getAtomicCounter("onos-hosts-probe-index").getAndIncrement(); |
| 390 | MacAddress probeMac = MacAddress.valueOf(MacAddress.NONE.toLong() + nextIndex); |
| 391 | PendingHostLocation phl = new PendingHostLocation(hostId, hostLocation); |
| 392 | |
| 393 | pendingHostsCache.put(probeMac, phl); |
| 394 | pendingHosts.put(probeMac, phl); |
| 395 | |
| 396 | return probeMac; |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public void removePendingHostLocation(MacAddress probeMac) { |
| 401 | pendingHostsCache.invalidate(probeMac); |
| 402 | pendingHosts.remove(probeMac); |
| 403 | } |
| 404 | |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 405 | private boolean timeoutPendingHostLocation(MacAddress probeMac) { |
| 406 | PendingHostLocation phl = pendingHosts.computeIfPresent(probeMac, (k, v) -> { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 407 | v.setExpired(true); |
| 408 | return v; |
| 409 | }); |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 410 | return phl != null; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 413 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 414 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 415 | } |
| 416 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 417 | private Set<Host> addHosts(Host host) { |
| 418 | Set<Host> hosts = Sets.newConcurrentHashSet(); |
| 419 | hosts.add(host); |
| 420 | return hosts; |
| 421 | } |
| 422 | |
| 423 | private Set<Host> updateHosts(Set<Host> existingHosts, Host host) { |
| 424 | Iterator<Host> iterator = existingHosts.iterator(); |
| 425 | while (iterator.hasNext()) { |
| 426 | Host existingHost = iterator.next(); |
| 427 | if (existingHost.id().equals(host.id())) { |
| 428 | iterator.remove(); |
| 429 | } |
| 430 | } |
| 431 | existingHosts.add(host); |
| 432 | return existingHosts; |
| 433 | } |
| 434 | |
| 435 | private Set<Host> removeHosts(Set<Host> existingHosts, Host host) { |
| 436 | if (existingHosts != null) { |
| 437 | Iterator<Host> iterator = existingHosts.iterator(); |
| 438 | while (iterator.hasNext()) { |
| 439 | Host existingHost = iterator.next(); |
| 440 | if (existingHost.id().equals(host.id())) { |
| 441 | iterator.remove(); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (existingHosts.isEmpty()) { |
| 447 | return null; |
| 448 | } |
| 449 | return existingHosts; |
| 450 | } |
| 451 | |
| 452 | private void updateHostsByIp(DefaultHost host) { |
| 453 | host.ipAddresses().forEach(ip -> { |
| 454 | hostsByIp.compute(ip, (k, v) -> v == null ? addHosts(host) |
| 455 | : updateHosts(v, host)); |
| 456 | }); |
| 457 | } |
| 458 | |
| 459 | private void removeHostsByIp(DefaultHost host) { |
| 460 | host.ipAddresses().forEach(ip -> { |
| 461 | hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host)); |
| 462 | }); |
| 463 | } |
| 464 | |
| 465 | private void removeIpFromHostsByIp(DefaultHost host, IpAddress ip) { |
| 466 | hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host)); |
| 467 | } |
| 468 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 469 | private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 470 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 471 | public void event(MapEvent<HostId, DefaultHost> event) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 472 | DefaultHost host = checkNotNull(event.value().value()); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 473 | switch (event.type()) { |
| 474 | case INSERT: |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 475 | updateHostsByIp(host); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 476 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 477 | break; |
| 478 | case UPDATE: |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 479 | updateHostsByIp(host); |
| 480 | DefaultHost prevHost = checkNotNull(event.oldValue().value()); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 481 | if (!Objects.equals(prevHost.locations(), host.locations())) { |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 482 | notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost)); |
| 483 | } else if (!Objects.equals(prevHost, host)) { |
| 484 | notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost)); |
| 485 | } |
| 486 | break; |
| 487 | case REMOVE: |
Charles Chan | 2172034 | 2017-05-13 00:19:09 -0700 | [diff] [blame] | 488 | removeHostsByIp(host); |
Yuta HIGUCHI | 215a7e4 | 2016-07-20 19:54:06 -0700 | [diff] [blame] | 489 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 490 | break; |
| 491 | default: |
| 492 | log.warn("Unknown map event type: {}", event.type()); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 496 | |
| 497 | private class PendingHostListener implements MapEventListener<MacAddress, PendingHostLocation> { |
| 498 | @Override |
| 499 | public void event(MapEvent<MacAddress, PendingHostLocation> event) { |
| 500 | Versioned<PendingHostLocation> newValue = event.newValue(); |
| 501 | switch (event.type()) { |
| 502 | case INSERT: |
| 503 | break; |
| 504 | case UPDATE: |
| 505 | if (newValue.value().expired()) { |
| 506 | Executor locationRemover = Executors.newSingleThreadScheduledExecutor(); |
| 507 | locationRemover.execute(() -> { |
| 508 | pendingHosts.remove(event.key()); |
| 509 | removeLocation(newValue.value().hostId(), newValue.value().location()); |
| 510 | }); |
| 511 | } |
| 512 | break; |
| 513 | case REMOVE: |
| 514 | break; |
| 515 | default: |
| 516 | log.warn("Unknown map event type: {}", event.type()); |
| 517 | } |
| 518 | } |
| 519 | } |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 520 | } |