blob: b5dcc769ef341103c9a1b6bbd30e33574e849810 [file] [log] [blame]
Madan Jampani38a88212015-09-15 11:21:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampani38a88212015-09-15 11:21:27 -07003 *
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 */
16package org.onosproject.store.host.impl;
17
Charles Chan35a32322017-08-14 11:42:11 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalNotification;
alshabib8a4a6002015-11-25 14:31:16 -080021import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Sets;
Madan Jampanic7f49f92015-12-10 11:35:06 -080023
Madan Jampani38a88212015-09-15 11:21:27 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.packet.IpAddress;
31import org.onlab.packet.MacAddress;
32import org.onlab.packet.VlanId;
33import org.onlab.util.KryoNamespace;
34import org.onosproject.net.Annotations;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DefaultAnnotations;
37import org.onosproject.net.DefaultHost;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.Host;
40import org.onosproject.net.HostId;
Brian O'Connorf107bd72015-09-21 15:31:03 -070041import org.onosproject.net.HostLocation;
Madan Jampani38a88212015-09-15 11:21:27 -070042import org.onosproject.net.host.HostDescription;
43import org.onosproject.net.host.HostEvent;
44import org.onosproject.net.host.HostStore;
45import org.onosproject.net.host.HostStoreDelegate;
Madan Jampani38a88212015-09-15 11:21:27 -070046import org.onosproject.net.provider.ProviderId;
47import org.onosproject.store.AbstractStore;
48import org.onosproject.store.serializers.KryoNamespaces;
alshabib8a4a6002015-11-25 14:31:16 -080049import org.onosproject.store.service.ConsistentMap;
50import org.onosproject.store.service.MapEvent;
51import org.onosproject.store.service.MapEventListener;
52import org.onosproject.store.service.Serializer;
Madan Jampani38a88212015-09-15 11:21:27 -070053import org.onosproject.store.service.StorageService;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053054import org.onosproject.store.service.DistributedPrimitive.Status;
Charles Chan35a32322017-08-14 11:42:11 -070055import org.onosproject.store.service.Versioned;
Madan Jampani38a88212015-09-15 11:21:27 -070056import org.slf4j.Logger;
57
alshabib8a4a6002015-11-25 14:31:16 -080058import java.util.Collection;
59import java.util.HashSet;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053060import java.util.Iterator;
alshabib8a4a6002015-11-25 14:31:16 -080061import java.util.Map;
62import java.util.Objects;
63import java.util.Set;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053064import java.util.concurrent.ConcurrentHashMap;
Charles Chan35a32322017-08-14 11:42:11 -070065import java.util.concurrent.Executor;
66import java.util.concurrent.Executors;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053067import java.util.concurrent.ScheduledExecutorService;
Charles Chan35a32322017-08-14 11:42:11 -070068import java.util.concurrent.TimeUnit;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053069import java.util.function.Consumer;
alshabib8a4a6002015-11-25 14:31:16 -080070import java.util.function.Predicate;
71import java.util.stream.Collectors;
72
73import static com.google.common.base.Preconditions.checkNotNull;
74import static com.google.common.base.Preconditions.checkState;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053075import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
76import static org.onlab.util.Tools.groupedThreads;
alshabib8a4a6002015-11-25 14:31:16 -080077import static org.onosproject.net.DefaultAnnotations.merge;
78import static org.onosproject.net.host.HostEvent.Type.*;
79import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani38a88212015-09-15 11:21:27 -070080
81/**
82 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
83 */
84@Component(immediate = true)
85@Service
alshabib8a4a6002015-11-25 14:31:16 -080086public class DistributedHostStore
Madan Jampani38a88212015-09-15 11:21:27 -070087 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 Jampanic6371882016-06-03 21:30:17 -070095 private ConsistentMap<HostId, DefaultHost> hostsConsistentMap;
alshabib8a4a6002015-11-25 14:31:16 -080096 private Map<HostId, DefaultHost> hosts;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053097 private Map<IpAddress, Set<Host>> hostsByIp;
alshabib8a4a6002015-11-25 14:31:16 -080098 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070099 new HostLocationTracker();
100
Charles Chan35a32322017-08-14 11:42:11 -0700101 private ConsistentMap<MacAddress, PendingHostLocation> pendingHostsConsistentMap;
102 private Map<MacAddress, PendingHostLocation> pendingHosts;
103 private MapEventListener<MacAddress, PendingHostLocation> pendingHostListener =
104 new PendingHostListener();
105
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530106 private ScheduledExecutorService executor;
107
108 private Consumer<Status> statusChangeListener;
109
Charles Chan35a32322017-08-14 11:42:11 -0700110 // 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 Jampani38a88212015-09-15 11:21:27 -0700134 @Activate
135 public void activate() {
136 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000137 .register(KryoNamespaces.API);
Madan Jampanic6371882016-06-03 21:30:17 -0700138 hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -0700139 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -0800140 .withRelaxedReadConsistency()
141 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -0700142 .build();
Charles Chan35a32322017-08-14 11:42:11 -0700143 hostsConsistentMap.addListener(hostLocationTracker);
Madan Jampanic6371882016-06-03 21:30:17 -0700144 hosts = hostsConsistentMap.asJavaMap();
alshabib8a4a6002015-11-25 14:31:16 -0800145
Charles Chan35a32322017-08-14 11:42:11 -0700146 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();
alshabib1400ce92015-12-16 15:05:47 -0800156
Charles Chan35a32322017-08-14 11:42:11 -0700157 cacheCleaner.scheduleAtFixedRate(pendingHostsCache::cleanUp, 0,
158 PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
Madan Jampani38a88212015-09-15 11:21:27 -0700159
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530160 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 Jampani38a88212015-09-15 11:21:27 -0700168 log.info("Started");
169 }
170
171 @Deactivate
172 public void deactivate() {
Madan Jampanic6371882016-06-03 21:30:17 -0700173 hostsConsistentMap.removeListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700174
Charles Chan35a32322017-08-14 11:42:11 -0700175 cacheCleaner.shutdown();
176
Madan Jampani38a88212015-09-15 11:21:27 -0700177 log.info("Stopped");
178 }
179
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530180 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'Connordab09742015-12-07 20:06:29 -0800194 private boolean shouldUpdate(DefaultHost existingHost,
195 ProviderId providerId,
Brian O'Connordab09742015-12-07 20:06:29 -0800196 HostDescription hostDescription,
197 boolean replaceIPs) {
198 if (existingHost == null) {
199 return true;
200 }
201
Charles Chan69ebcbb2017-04-27 14:33:21 -0700202 // Avoid overriding configured host with learnt host
203 if (existingHost.configured() && !hostDescription.configured()) {
Charles Chan29ecdee2017-02-22 18:46:56 -0800204 return false;
205 }
206
Brian O'Connordab09742015-12-07 20:06:29 -0800207 if (!Objects.equals(existingHost.providerId(), providerId) ||
208 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
209 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
Charles Chancd06c692017-04-27 20:46:06 -0700210 !Objects.equals(existingHost.locations(), hostDescription.locations())) {
Brian O'Connordab09742015-12-07 20:06:29 -0800211 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 Vaddireddy0a71c8b2017-01-19 21:20:45 +0530228 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
229 existingHost.annotations().value(k)));
Brian O'Connordab09742015-12-07 20:06:29 -0800230
231
232 }
233
Charles Chan009c3082015-11-10 14:18:04 -0800234 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700235 @Override
236 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700237 HostId hostId,
238 HostDescription hostDescription,
239 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700240 hostsConsistentMap.computeIf(hostId,
Charles Chan69ebcbb2017-04-27 14:33:21 -0700241 existingHost -> shouldUpdate(existingHost, providerId,
Brian O'Connordab09742015-12-07 20:06:29 -0800242 hostDescription, replaceIPs),
243 (id, existingHost) -> {
Brian O'Connorf107bd72015-09-21 15:31:03 -0700244
Madan Jampanic7f49f92015-12-10 11:35:06 -0800245 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'Connorf107bd72015-09-21 15:31:03 -0700252
Madan Jampanic7f49f92015-12-10 11:35:06 -0800253 final Annotations annotations;
254 if (existingHost != null) {
255 annotations = merge((DefaultAnnotations) existingHost.annotations(),
256 hostDescription.annotations());
257 } else {
258 annotations = hostDescription.annotations();
259 }
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000260
Madan Jampanic7f49f92015-12-10 11:35:06 -0800261 return new DefaultHost(providerId,
262 hostId,
263 hostDescription.hwAddress(),
264 hostDescription.vlan(),
Charles Chancd06c692017-04-27 20:46:06 -0700265 hostDescription.locations(),
Madan Jampanic7f49f92015-12-10 11:35:06 -0800266 addresses,
Charles Chanb1e99242017-07-07 14:11:09 -0700267 hostDescription.configured(),
Madan Jampanic7f49f92015-12-10 11:35:06 -0800268 annotations);
269 });
Charles Chan009c3082015-11-10 14:18:04 -0800270 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700271 }
272
Charles Chan009c3082015-11-10 14:18:04 -0800273 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700274 @Override
275 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800276 hosts.remove(hostId);
277 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700278 }
279
Charles Chan009c3082015-11-10 14:18:04 -0800280 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700281 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700282 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800283 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700284 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 pale7c08de2015-09-24 21:59:49 -0700290 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700291 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700292 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700293 addresses.remove(ipAddress);
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530294 removeIpFromHostsByIp(existingHost, ipAddress);
samanwita palc40e5ed2015-09-24 11:01:51 -0700295 return new DefaultHost(existingHost.providerId(),
296 hostId,
297 existingHost.mac(),
298 existingHost.vlan(),
Charles Chancd06c692017-04-27 20:46:06 -0700299 existingHost.locations(),
samanwita palc40e5ed2015-09-24 11:01:51 -0700300 ImmutableSet.copyOf(addresses),
Charles Chancd06c692017-04-27 20:46:06 -0700301 existingHost.configured(),
samanwita palc40e5ed2015-09-24 11:01:51 -0700302 existingHost.annotations());
303 } else {
304 return existingHost;
305 }
306 }
307 return null;
308 });
Charles Chan009c3082015-11-10 14:18:04 -0800309 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700310 }
311
312 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700313 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 Jampani38a88212015-09-15 11:21:27 -0700336 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 Vaddireddy0a71c8b2017-01-19 21:20:45 +0530362 Set<Host> hosts = hostsByIp.get(ip);
363 return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of();
Madan Jampani38a88212015-09-15 11:21:27 -0700364 }
365
366 @Override
367 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800368 Set<Host> filtered = hosts.entrySet().stream()
Charles Chancd06c692017-04-27 20:46:06 -0700369 .filter(entry -> entry.getValue().locations().contains(connectPoint))
Charles Chan009c3082015-11-10 14:18:04 -0800370 .map(Map.Entry::getValue)
371 .collect(Collectors.toSet());
372 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700373 }
374
375 @Override
376 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800377 Set<Host> filtered = hosts.entrySet().stream()
Charles Chancd06c692017-04-27 20:46:06 -0700378 .filter(entry -> entry.getValue().locations().stream()
379 .map(HostLocation::deviceId).anyMatch(dpid -> dpid.equals(deviceId)))
Charles Chan009c3082015-11-10 14:18:04 -0800380 .map(Map.Entry::getValue)
381 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700382 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700383 }
384
Charles Chan35a32322017-08-14 11:42:11 -0700385 @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 Jampani38a88212015-09-15 11:21:27 -0700411 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
412 return collection.stream().filter(predicate).collect(Collectors.toSet());
413 }
414
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530415 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
alshabib8a4a6002015-11-25 14:31:16 -0800467 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700468 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800469 public void event(MapEvent<HostId, DefaultHost> event) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530470 DefaultHost host = checkNotNull(event.value().value());
alshabib1400ce92015-12-16 15:05:47 -0800471 switch (event.type()) {
472 case INSERT:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530473 updateHostsByIp(host);
Charles Chan009c3082015-11-10 14:18:04 -0800474 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800475 break;
476 case UPDATE:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530477 updateHostsByIp(host);
478 DefaultHost prevHost = checkNotNull(event.oldValue().value());
Charles Chancd06c692017-04-27 20:46:06 -0700479 if (!Objects.equals(prevHost.locations(), host.locations())) {
alshabib1400ce92015-12-16 15:05:47 -0800480 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 Chan21720342017-05-13 00:19:09 -0700486 removeHostsByIp(host);
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700487 notifyDelegate(new HostEvent(HOST_REMOVED, host));
alshabib1400ce92015-12-16 15:05:47 -0800488 break;
489 default:
490 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700491 }
492 }
493 }
Charles Chan35a32322017-08-14 11:42:11 -0700494
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 Jampani38a88212015-09-15 11:21:27 -0700518}