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