Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 20 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onlab.util.KryoNamespace; |
| 31 | import org.onosproject.net.Annotations; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.DefaultHost; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.HostId; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 38 | import org.onosproject.net.HostLocation; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 39 | import org.onosproject.net.host.HostDescription; |
| 40 | import org.onosproject.net.host.HostEvent; |
| 41 | import org.onosproject.net.host.HostStore; |
| 42 | import org.onosproject.net.host.HostStoreDelegate; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | import org.onosproject.store.AbstractStore; |
| 45 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 46 | import org.onosproject.store.service.ConsistentMap; |
| 47 | import org.onosproject.store.service.MapEvent; |
| 48 | import org.onosproject.store.service.MapEventListener; |
| 49 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.StorageService; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 51 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 54 | import java.util.Collection; |
| 55 | import java.util.HashSet; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 56 | import java.util.Iterator; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 57 | import java.util.Map; |
| 58 | import java.util.Objects; |
| 59 | import java.util.Set; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 60 | import java.util.concurrent.ConcurrentHashMap; |
| 61 | import java.util.concurrent.ScheduledExecutorService; |
| 62 | import java.util.function.Consumer; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 63 | import java.util.function.Predicate; |
| 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static com.google.common.base.Preconditions.checkNotNull; |
| 67 | import static com.google.common.base.Preconditions.checkState; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 68 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 69 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 70 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 71 | import static org.onosproject.net.host.HostEvent.Type.*; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 76 | */ |
| 77 | @Component(immediate = true) |
| 78 | @Service |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 79 | public class DistributedHostStore |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 80 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 81 | implements HostStore { |
| 82 | |
| 83 | private final Logger log = getLogger(getClass()); |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected StorageService storageService; |
| 87 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 88 | private ConsistentMap<HostId, DefaultHost> hostsConsistentMap; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 89 | private Map<HostId, DefaultHost> hosts; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 90 | private Map<IpAddress, Set<Host>> hostsByIp; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 91 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 92 | private MapEventListener<HostId, DefaultHost> hostLocationTracker = |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 93 | new HostLocationTracker(); |
| 94 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 95 | private ScheduledExecutorService executor; |
| 96 | |
| 97 | private Consumer<Status> statusChangeListener; |
| 98 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 99 | @Activate |
| 100 | public void activate() { |
| 101 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 102 | .register(KryoNamespaces.API); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 103 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 104 | hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder() |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 105 | .withName("onos-hosts") |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 106 | .withRelaxedReadConsistency() |
| 107 | .withSerializer(Serializer.using(hostSerializer.build())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 108 | .build(); |
| 109 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 110 | hosts = hostsConsistentMap.asJavaMap(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 111 | |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 112 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 113 | hostsConsistentMap.addListener(hostLocationTracker); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 114 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 115 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/hosts", "store", log)); |
| 116 | statusChangeListener = status -> { |
| 117 | if (status == Status.ACTIVE) { |
| 118 | executor.execute(this::loadHostsByIp); |
| 119 | } |
| 120 | }; |
| 121 | hostsConsistentMap.addStatusChangeListener(statusChangeListener); |
| 122 | loadHostsByIp(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 123 | log.info("Started"); |
| 124 | } |
| 125 | |
| 126 | @Deactivate |
| 127 | public void deactivate() { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 128 | hostsConsistentMap.removeListener(hostLocationTracker); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 129 | |
| 130 | log.info("Stopped"); |
| 131 | } |
| 132 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 133 | private void loadHostsByIp() { |
| 134 | hostsByIp = new ConcurrentHashMap<IpAddress, Set<Host>>(); |
| 135 | hostsConsistentMap.asJavaMap().values().forEach(host -> { |
| 136 | host.ipAddresses().forEach(ip -> { |
| 137 | Set<Host> existingHosts = hostsByIp.get(ip); |
| 138 | if (existingHosts == null) { |
| 139 | hostsByIp.put(ip, addHosts(host)); |
| 140 | } else { |
| 141 | existingHosts.add(host); |
| 142 | } |
| 143 | }); |
| 144 | }); |
| 145 | } |
| 146 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 147 | private boolean shouldUpdate(DefaultHost existingHost, |
| 148 | ProviderId providerId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 149 | HostDescription hostDescription, |
| 150 | boolean replaceIPs) { |
| 151 | if (existingHost == null) { |
| 152 | return true; |
| 153 | } |
| 154 | |
Charles Chan | 69ebcbb | 2017-04-27 14:33:21 -0700 | [diff] [blame] | 155 | // Avoid overriding configured host with learnt host |
| 156 | if (existingHost.configured() && !hostDescription.configured()) { |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 160 | if (!Objects.equals(existingHost.providerId(), providerId) || |
| 161 | !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) || |
| 162 | !Objects.equals(existingHost.vlan(), hostDescription.vlan()) || |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 163 | !Objects.equals(existingHost.location(), hostDescription.location())) { |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | if (replaceIPs) { |
| 168 | if (!Objects.equals(hostDescription.ipAddress(), |
| 169 | existingHost.ipAddresses())) { |
| 170 | return true; |
| 171 | } |
| 172 | } else { |
| 173 | if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // check to see if any of the annotations provided by hostDescription |
| 179 | // differ from those in the existing host |
| 180 | return hostDescription.annotations().keys().stream() |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 181 | .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k), |
| 182 | existingHost.annotations().value(k))); |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 183 | |
| 184 | |
| 185 | } |
| 186 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 187 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 190 | HostId hostId, |
| 191 | HostDescription hostDescription, |
| 192 | boolean replaceIPs) { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 193 | hostsConsistentMap.computeIf(hostId, |
Charles Chan | 69ebcbb | 2017-04-27 14:33:21 -0700 | [diff] [blame] | 194 | existingHost -> shouldUpdate(existingHost, providerId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 195 | hostDescription, replaceIPs), |
| 196 | (id, existingHost) -> { |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 197 | HostLocation location = hostDescription.location(); |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 198 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 199 | final Set<IpAddress> addresses; |
| 200 | if (existingHost == null || replaceIPs) { |
| 201 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 202 | } else { |
| 203 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 204 | addresses.addAll(hostDescription.ipAddress()); |
| 205 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 206 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 207 | final Annotations annotations; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 208 | final boolean configured; |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 209 | if (existingHost != null) { |
| 210 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 211 | hostDescription.annotations()); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 212 | configured = existingHost.configured(); |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 213 | } else { |
| 214 | annotations = hostDescription.annotations(); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 215 | configured = hostDescription.configured(); |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 216 | } |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 217 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 218 | return new DefaultHost(providerId, |
| 219 | hostId, |
| 220 | hostDescription.hwAddress(), |
| 221 | hostDescription.vlan(), |
| 222 | location, |
| 223 | addresses, |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 224 | configured, |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 225 | annotations); |
| 226 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 227 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 230 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 231 | @Override |
| 232 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 233 | hosts.remove(hostId); |
| 234 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 237 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 238 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 239 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 240 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 241 | if (existingHost != null) { |
| 242 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 243 | "Existing and new MAC addresses differ."); |
| 244 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 245 | "Existing and new VLANs differ."); |
| 246 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 247 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 248 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 249 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 250 | addresses.remove(ipAddress); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 251 | removeIpFromHostsByIp(existingHost, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 252 | return new DefaultHost(existingHost.providerId(), |
| 253 | hostId, |
| 254 | existingHost.mac(), |
| 255 | existingHost.vlan(), |
| 256 | existingHost.location(), |
| 257 | ImmutableSet.copyOf(addresses), |
| 258 | existingHost.annotations()); |
| 259 | } else { |
| 260 | return existingHost; |
| 261 | } |
| 262 | } |
| 263 | return null; |
| 264 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 265 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 269 | public int getHostCount() { |
| 270 | return hosts.size(); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public Iterable<Host> getHosts() { |
| 275 | return ImmutableSet.copyOf(hosts.values()); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public Host getHost(HostId hostId) { |
| 280 | return hosts.get(hostId); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public Set<Host> getHosts(VlanId vlanId) { |
| 285 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Set<Host> getHosts(MacAddress mac) { |
| 290 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public Set<Host> getHosts(IpAddress ip) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 295 | Set<Host> hosts = hostsByIp.get(ip); |
| 296 | return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 301 | Set<Host> filtered = hosts.entrySet().stream() |
| 302 | .filter(entry -> entry.getValue().location().equals(connectPoint)) |
| 303 | .map(Map.Entry::getValue) |
| 304 | .collect(Collectors.toSet()); |
| 305 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 310 | Set<Host> filtered = hosts.entrySet().stream() |
| 311 | .filter(entry -> entry.getValue().location().deviceId().equals(deviceId)) |
| 312 | .map(Map.Entry::getValue) |
| 313 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 314 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 317 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 318 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 319 | } |
| 320 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 321 | private Set<Host> addHosts(Host host) { |
| 322 | Set<Host> hosts = Sets.newConcurrentHashSet(); |
| 323 | hosts.add(host); |
| 324 | return hosts; |
| 325 | } |
| 326 | |
| 327 | private Set<Host> updateHosts(Set<Host> existingHosts, Host host) { |
| 328 | Iterator<Host> iterator = existingHosts.iterator(); |
| 329 | while (iterator.hasNext()) { |
| 330 | Host existingHost = iterator.next(); |
| 331 | if (existingHost.id().equals(host.id())) { |
| 332 | iterator.remove(); |
| 333 | } |
| 334 | } |
| 335 | existingHosts.add(host); |
| 336 | return existingHosts; |
| 337 | } |
| 338 | |
| 339 | private Set<Host> removeHosts(Set<Host> existingHosts, Host host) { |
| 340 | if (existingHosts != null) { |
| 341 | Iterator<Host> iterator = existingHosts.iterator(); |
| 342 | while (iterator.hasNext()) { |
| 343 | Host existingHost = iterator.next(); |
| 344 | if (existingHost.id().equals(host.id())) { |
| 345 | iterator.remove(); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (existingHosts.isEmpty()) { |
| 351 | return null; |
| 352 | } |
| 353 | return existingHosts; |
| 354 | } |
| 355 | |
| 356 | private void updateHostsByIp(DefaultHost host) { |
| 357 | host.ipAddresses().forEach(ip -> { |
| 358 | hostsByIp.compute(ip, (k, v) -> v == null ? addHosts(host) |
| 359 | : updateHosts(v, host)); |
| 360 | }); |
| 361 | } |
| 362 | |
| 363 | private void removeHostsByIp(DefaultHost host) { |
| 364 | host.ipAddresses().forEach(ip -> { |
| 365 | hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host)); |
| 366 | }); |
| 367 | } |
| 368 | |
| 369 | private void removeIpFromHostsByIp(DefaultHost host, IpAddress ip) { |
| 370 | hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host)); |
| 371 | } |
| 372 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 373 | private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 374 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 375 | public void event(MapEvent<HostId, DefaultHost> event) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 376 | DefaultHost host = checkNotNull(event.value().value()); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 377 | switch (event.type()) { |
| 378 | case INSERT: |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 379 | updateHostsByIp(host); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 380 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 381 | break; |
| 382 | case UPDATE: |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 383 | updateHostsByIp(host); |
| 384 | DefaultHost prevHost = checkNotNull(event.oldValue().value()); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 385 | if (!Objects.equals(prevHost.location(), host.location())) { |
| 386 | notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost)); |
| 387 | } else if (!Objects.equals(prevHost, host)) { |
| 388 | notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost)); |
| 389 | } |
| 390 | break; |
| 391 | case REMOVE: |
Charles Chan | 2172034 | 2017-05-13 00:19:09 -0700 | [diff] [blame] | 392 | removeHostsByIp(host); |
Yuta HIGUCHI | 215a7e4 | 2016-07-20 19:54:06 -0700 | [diff] [blame] | 393 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 394 | break; |
| 395 | default: |
| 396 | log.warn("Unknown map event type: {}", event.type()); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |