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