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