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