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 | |
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 | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onlab.util.KryoNamespace; |
| 24 | import org.onosproject.net.Annotations; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.DefaultHost; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.HostId; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 31 | import org.onosproject.net.HostLocation; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 32 | import org.onosproject.net.host.HostDescription; |
| 33 | import org.onosproject.net.host.HostEvent; |
| 34 | import org.onosproject.net.host.HostStore; |
| 35 | import org.onosproject.net.host.HostStoreDelegate; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | import org.onosproject.store.AbstractStore; |
| 38 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 39 | import org.onosproject.store.service.ConsistentMap; |
| 40 | import org.onosproject.store.service.MapEvent; |
| 41 | import org.onosproject.store.service.MapEventListener; |
| 42 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 43 | import org.onosproject.store.service.StorageService; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 44 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Ray Milkey | d0f017f | 2018-09-21 12:52:34 -0700 | [diff] [blame] | 45 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | import org.osgi.service.component.annotations.Activate; |
| 47 | import org.osgi.service.component.annotations.Component; |
| 48 | import org.osgi.service.component.annotations.Deactivate; |
| 49 | import org.osgi.service.component.annotations.Reference; |
| 50 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 53 | import java.util.Collection; |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 54 | import java.util.Collections; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 55 | import java.util.HashSet; |
| 56 | import java.util.Map; |
| 57 | import java.util.Objects; |
| 58 | import java.util.Set; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 59 | import java.util.concurrent.ConcurrentHashMap; |
| 60 | import java.util.concurrent.ScheduledExecutorService; |
| 61 | import java.util.function.Consumer; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 62 | import java.util.function.Predicate; |
| 63 | import java.util.stream.Collectors; |
| 64 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 65 | import static com.google.common.base.Preconditions.checkState; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 66 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 67 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.DefaultAnnotations.merge; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 69 | import static org.onosproject.net.host.HostEvent.Type.*; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 70 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 74 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Component(immediate = true, service = HostStore.class) |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 76 | public class DistributedHostStore |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 77 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 78 | implements HostStore { |
| 79 | |
| 80 | private final Logger log = getLogger(getClass()); |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 83 | protected StorageService storageService; |
| 84 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 85 | private ConsistentMap<HostId, DefaultHost> hostsConsistentMap; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 86 | private Map<HostId, DefaultHost> hosts; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 87 | private Map<IpAddress, Set<Host>> hostsByIp; |
Harshada Chaundkar | c971222 | 2019-07-02 15:13:24 +0000 | [diff] [blame] | 88 | MapEventListener<HostId, DefaultHost> hostLocationTracker = |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 89 | new HostLocationTracker(); |
| 90 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 91 | private ScheduledExecutorService executor; |
| 92 | |
| 93 | private Consumer<Status> statusChangeListener; |
| 94 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 95 | @Activate |
| 96 | public void activate() { |
| 97 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 98 | .register(KryoNamespaces.API); |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 99 | hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder() |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 100 | .withName("onos-hosts") |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 101 | .withRelaxedReadConsistency() |
| 102 | .withSerializer(Serializer.using(hostSerializer.build())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 103 | .build(); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 104 | hostsConsistentMap.addListener(hostLocationTracker); |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 105 | hosts = hostsConsistentMap.asJavaMap(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 106 | |
Charles Chan | 35c06ac | 2018-04-19 15:11:23 -0700 | [diff] [blame] | 107 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/hosts", "status-listener", log)); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 108 | statusChangeListener = status -> { |
| 109 | if (status == Status.ACTIVE) { |
| 110 | executor.execute(this::loadHostsByIp); |
| 111 | } |
| 112 | }; |
| 113 | hostsConsistentMap.addStatusChangeListener(statusChangeListener); |
| 114 | loadHostsByIp(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 115 | log.info("Started"); |
| 116 | } |
| 117 | |
| 118 | @Deactivate |
| 119 | public void deactivate() { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 120 | hostsConsistentMap.removeListener(hostLocationTracker); |
Charles Chan | 35c06ac | 2018-04-19 15:11:23 -0700 | [diff] [blame] | 121 | executor.shutdown(); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 122 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 126 | private void loadHostsByIp() { |
| 127 | hostsByIp = new ConcurrentHashMap<IpAddress, Set<Host>>(); |
| 128 | hostsConsistentMap.asJavaMap().values().forEach(host -> { |
| 129 | host.ipAddresses().forEach(ip -> { |
| 130 | Set<Host> existingHosts = hostsByIp.get(ip); |
| 131 | if (existingHosts == null) { |
| 132 | hostsByIp.put(ip, addHosts(host)); |
| 133 | } else { |
| 134 | existingHosts.add(host); |
| 135 | } |
| 136 | }); |
| 137 | }); |
| 138 | } |
| 139 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 140 | private boolean shouldUpdate(DefaultHost existingHost, |
| 141 | ProviderId providerId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 142 | HostDescription hostDescription, |
| 143 | boolean replaceIPs) { |
| 144 | if (existingHost == null) { |
| 145 | return true; |
| 146 | } |
| 147 | |
Charles Chan | 69ebcbb | 2017-04-27 14:33:21 -0700 | [diff] [blame] | 148 | // Avoid overriding configured host with learnt host |
| 149 | if (existingHost.configured() && !hostDescription.configured()) { |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 153 | if (!Objects.equals(existingHost.providerId(), providerId) || |
| 154 | !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) || |
| 155 | !Objects.equals(existingHost.vlan(), hostDescription.vlan()) || |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 156 | !Objects.equals(existingHost.innerVlan(), hostDescription.innerVlan()) || |
| 157 | !Objects.equals(existingHost.tpid(), hostDescription.tpid()) || |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 158 | !Objects.equals(existingHost.locations(), hostDescription.locations())) { |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | if (replaceIPs) { |
| 163 | if (!Objects.equals(hostDescription.ipAddress(), |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 164 | existingHost.ipAddresses())) { |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | } else { |
| 168 | if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) { |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // check to see if any of the annotations provided by hostDescription |
| 174 | // differ from those in the existing host |
| 175 | return hostDescription.annotations().keys().stream() |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 176 | .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k), |
| 177 | existingHost.annotations().value(k))); |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 178 | |
| 179 | |
| 180 | } |
| 181 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 182 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 185 | HostId hostId, |
| 186 | HostDescription hostDescription, |
| 187 | boolean replaceIPs) { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 188 | hostsConsistentMap.computeIf(hostId, |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 189 | existingHost -> shouldUpdate(existingHost, providerId, |
| 190 | hostDescription, replaceIPs), |
| 191 | (id, existingHost) -> { |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 192 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 193 | final Set<IpAddress> addresses; |
| 194 | if (existingHost == null || replaceIPs) { |
| 195 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 196 | } else { |
| 197 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 198 | addresses.addAll(hostDescription.ipAddress()); |
| 199 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 200 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 201 | final Annotations annotations; |
| 202 | if (existingHost != null) { |
| 203 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 204 | hostDescription.annotations()); |
| 205 | } else { |
| 206 | annotations = hostDescription.annotations(); |
| 207 | } |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 208 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 209 | return new DefaultHost(providerId, |
| 210 | hostId, |
| 211 | hostDescription.hwAddress(), |
| 212 | hostDescription.vlan(), |
| 213 | hostDescription.locations(), |
| 214 | addresses, |
| 215 | hostDescription.innerVlan(), |
| 216 | hostDescription.tpid(), |
| 217 | hostDescription.configured(), |
| 218 | annotations); |
| 219 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 220 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 223 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 226 | hosts.remove(hostId); |
| 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 |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 232 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 233 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 234 | if (existingHost != null) { |
| 235 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 236 | "Existing and new MAC addresses differ."); |
| 237 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 238 | "Existing and new VLANs differ."); |
| 239 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 240 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 241 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 242 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 243 | addresses.remove(ipAddress); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 244 | removeIpFromHostsByIp(existingHost, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 245 | return new DefaultHost(existingHost.providerId(), |
| 246 | hostId, |
| 247 | existingHost.mac(), |
| 248 | existingHost.vlan(), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 249 | existingHost.locations(), |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 250 | ImmutableSet.copyOf(addresses), |
Charles Chan | 7993dfd | 2019-12-12 16:58:08 -0800 | [diff] [blame^] | 251 | existingHost.innerVlan(), |
| 252 | existingHost.tpid(), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 253 | existingHost.configured(), |
Charles Chan | 7993dfd | 2019-12-12 16:58:08 -0800 | [diff] [blame^] | 254 | existingHost.suspended(), |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 255 | existingHost.annotations()); |
| 256 | } else { |
| 257 | return existingHost; |
| 258 | } |
| 259 | } |
| 260 | return null; |
| 261 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 262 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 266 | public void appendLocation(HostId hostId, HostLocation location) { |
| 267 | log.debug("Appending location {} to host {}", location, hostId); |
| 268 | hosts.compute(hostId, (id, existingHost) -> { |
| 269 | if (existingHost != null) { |
| 270 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 271 | "Existing and new MAC addresses differ."); |
| 272 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 273 | "Existing and new VLANs differ."); |
| 274 | |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 275 | // Move within the same switch |
| 276 | // Simply replace old location that is on the same device |
| 277 | Set<HostLocation> newLocations = Sets.newHashSet(location); |
| 278 | existingHost.locations().stream().filter(loc -> !loc.deviceId().equals(location.deviceId())) |
| 279 | .forEach(newLocations::add); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 280 | |
| 281 | return new DefaultHost(existingHost.providerId(), |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 282 | hostId, existingHost.mac(), existingHost.vlan(), |
| 283 | newLocations, existingHost.ipAddresses(), |
Charles Chan | 7993dfd | 2019-12-12 16:58:08 -0800 | [diff] [blame^] | 284 | existingHost.innerVlan(), existingHost.tpid(), |
| 285 | existingHost.configured(), existingHost.suspended(), existingHost.annotations()); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 286 | } |
| 287 | return null; |
| 288 | }); |
| 289 | } |
| 290 | |
| 291 | @Override |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 292 | public void removeLocation(HostId hostId, HostLocation location) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 293 | log.debug("Removing location {} from host {}", location, hostId); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 294 | hosts.compute(hostId, (id, existingHost) -> { |
| 295 | if (existingHost != null) { |
| 296 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 297 | "Existing and new MAC addresses differ."); |
| 298 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 299 | "Existing and new VLANs differ."); |
| 300 | |
| 301 | Set<HostLocation> locations = new HashSet<>(existingHost.locations()); |
| 302 | locations.remove(location); |
| 303 | |
| 304 | // Remove entire host if we are removing the last location |
| 305 | return locations.isEmpty() ? null : |
| 306 | new DefaultHost(existingHost.providerId(), |
| 307 | hostId, existingHost.mac(), existingHost.vlan(), |
| 308 | locations, existingHost.ipAddresses(), |
Charles Chan | 7993dfd | 2019-12-12 16:58:08 -0800 | [diff] [blame^] | 309 | existingHost.innerVlan(), existingHost.tpid(), |
| 310 | existingHost.configured(), existingHost.suspended(), existingHost.annotations()); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 311 | } |
| 312 | return null; |
| 313 | }); |
| 314 | } |
| 315 | |
| 316 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 317 | public int getHostCount() { |
| 318 | return hosts.size(); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public Iterable<Host> getHosts() { |
| 323 | return ImmutableSet.copyOf(hosts.values()); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public Host getHost(HostId hostId) { |
| 328 | return hosts.get(hostId); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public Set<Host> getHosts(VlanId vlanId) { |
| 333 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public Set<Host> getHosts(MacAddress mac) { |
| 338 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public Set<Host> getHosts(IpAddress ip) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 343 | Set<Host> hosts = hostsByIp.get(ip); |
| 344 | return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 349 | Set<Host> filtered = hosts.entrySet().stream() |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 350 | .filter(entry -> entry.getValue().locations().contains(connectPoint)) |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 351 | .map(Map.Entry::getValue) |
| 352 | .collect(Collectors.toSet()); |
| 353 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 358 | Set<Host> filtered = hosts.entrySet().stream() |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 359 | .filter(entry -> entry.getValue().locations().stream() |
| 360 | .map(HostLocation::deviceId).anyMatch(dpid -> dpid.equals(deviceId))) |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 361 | .map(Map.Entry::getValue) |
| 362 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 363 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 364 | } |
| 365 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 366 | @Override |
| 367 | public void suspend(HostId hostId) { |
| 368 | hosts.compute(hostId, (id, existingHost) -> { |
| 369 | if (existingHost != null) { |
| 370 | if (!existingHost.suspended()) { |
| 371 | return new DefaultHost(existingHost.providerId(), |
| 372 | hostId, |
| 373 | existingHost.mac(), |
| 374 | existingHost.vlan(), |
| 375 | existingHost.locations(), |
| 376 | existingHost.ipAddresses(), |
| 377 | existingHost.innerVlan(), |
| 378 | existingHost.tpid(), |
| 379 | existingHost.configured(), |
| 380 | true, |
| 381 | existingHost.annotations()); |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | return null; |
| 386 | }); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void unsuspend(HostId hostId) { |
| 391 | hosts.compute(hostId, (id, existingHost) -> { |
| 392 | if (existingHost != null) { |
| 393 | if (existingHost.suspended()) { |
| 394 | return new DefaultHost(existingHost.providerId(), |
| 395 | hostId, |
| 396 | existingHost.mac(), |
| 397 | existingHost.vlan(), |
| 398 | existingHost.locations(), |
| 399 | existingHost.ipAddresses(), |
| 400 | existingHost.innerVlan(), |
| 401 | existingHost.tpid(), |
| 402 | existingHost.configured(), |
| 403 | false, |
| 404 | existingHost.annotations()); |
| 405 | |
| 406 | } |
| 407 | } |
| 408 | return null; |
| 409 | }); |
| 410 | } |
| 411 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 412 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 413 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 414 | } |
| 415 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 416 | private Set<Host> addHosts(Host host) { |
| 417 | Set<Host> hosts = Sets.newConcurrentHashSet(); |
| 418 | hosts.add(host); |
| 419 | return hosts; |
| 420 | } |
| 421 | |
| 422 | private Set<Host> updateHosts(Set<Host> existingHosts, Host host) { |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 423 | existingHosts.removeIf(existingHost -> existingHost.id().equals(host.id())); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 424 | existingHosts.add(host); |
| 425 | return existingHosts; |
| 426 | } |
| 427 | |
| 428 | private Set<Host> removeHosts(Set<Host> existingHosts, Host host) { |
Harshada Chaundkar | c971222 | 2019-07-02 15:13:24 +0000 | [diff] [blame] | 429 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 430 | if (existingHosts != null) { |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 431 | existingHosts.removeIf(existingHost -> existingHost.id().equals(host.id())); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 432 | } |
| 433 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 434 | if (existingHosts == null || existingHosts.isEmpty()) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 435 | return null; |
| 436 | } |
| 437 | return existingHosts; |
| 438 | } |
| 439 | |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 440 | private void updateHostsByIp(DefaultHost host, DefaultHost prevHost) { |
Harshada Chaundkar | c971222 | 2019-07-02 15:13:24 +0000 | [diff] [blame] | 441 | // Let's update first the current ips |
| 442 | host.ipAddresses().forEach( |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 443 | ip -> hostsByIp.compute(ip, (k, v) -> v == null ? addHosts(host) : updateHosts(v, host))); |
Harshada Chaundkar | c971222 | 2019-07-02 15:13:24 +0000 | [diff] [blame] | 444 | |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 445 | // Let's remove then each old ip |
Harshada Chaundkar | c971222 | 2019-07-02 15:13:24 +0000 | [diff] [blame] | 446 | Set<IpAddress> oldIps = prevHost != null ? prevHost.ipAddresses() : Collections.emptySet(); |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 447 | Sets.difference(oldIps, host.ipAddresses()).forEach( |
| 448 | ip -> hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host))); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | private void removeHostsByIp(DefaultHost host) { |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 452 | host.ipAddresses().forEach(ip -> hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host))); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | private void removeIpFromHostsByIp(DefaultHost host, IpAddress ip) { |
| 456 | hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host)); |
| 457 | } |
| 458 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 459 | private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 460 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 461 | public void event(MapEvent<HostId, DefaultHost> event) { |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 462 | DefaultHost host = Versioned.valueOrNull(event.newValue()); |
| 463 | DefaultHost prevHost = Versioned.valueOrNull(event.oldValue()); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 464 | switch (event.type()) { |
| 465 | case INSERT: |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 466 | updateHostsByIp(host, prevHost); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 467 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 468 | break; |
| 469 | case UPDATE: |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 470 | updateHostsByIp(host, prevHost); |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 471 | if (host.suspended() && !prevHost.suspended()) { |
| 472 | notifyDelegate(new HostEvent(HOST_SUSPENDED, host, prevHost)); |
| 473 | } else if (!host.suspended() && prevHost.suspended()) { |
| 474 | notifyDelegate(new HostEvent(HOST_UNSUSPENDED, host, prevHost)); |
| 475 | } else if (!Objects.equals(prevHost.locations(), host.locations())) { |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 476 | notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost)); |
| 477 | } else if (!Objects.equals(prevHost, host)) { |
| 478 | notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost)); |
| 479 | } |
| 480 | break; |
| 481 | case REMOVE: |
pier | 15743ea | 2019-04-11 11:35:55 +0200 | [diff] [blame] | 482 | removeHostsByIp(prevHost); |
| 483 | notifyDelegate(new HostEvent(HOST_REMOVED, prevHost)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 484 | break; |
| 485 | default: |
| 486 | log.warn("Unknown map event type: {}", event.type()); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | } |