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; |
| 51 | import org.slf4j.Logger; |
| 52 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 53 | import java.util.Collection; |
| 54 | import java.util.HashSet; |
| 55 | import java.util.Map; |
| 56 | import java.util.Objects; |
| 57 | import java.util.Set; |
| 58 | import java.util.concurrent.ConcurrentHashMap; |
| 59 | import java.util.function.Predicate; |
| 60 | import java.util.stream.Collectors; |
| 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
| 63 | import static com.google.common.base.Preconditions.checkState; |
| 64 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 65 | import static org.onosproject.net.host.HostEvent.Type.*; |
| 66 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 70 | */ |
| 71 | @Component(immediate = true) |
| 72 | @Service |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 73 | public class DistributedHostStore |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 74 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 75 | implements HostStore { |
| 76 | |
| 77 | private final Logger log = getLogger(getClass()); |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected StorageService storageService; |
| 81 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 82 | private ConsistentMap<HostId, DefaultHost> hostsConsistentMap; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 83 | private Map<HostId, DefaultHost> hosts; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 84 | |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 85 | private final ConcurrentHashMap<HostId, DefaultHost> prevHosts = |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 86 | new ConcurrentHashMap<>(); |
| 87 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 88 | private MapEventListener<HostId, DefaultHost> hostLocationTracker = |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 89 | new HostLocationTracker(); |
| 90 | |
| 91 | @Activate |
| 92 | public void activate() { |
| 93 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 94 | .register(KryoNamespaces.API); |
| 95 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 96 | hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder() |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 97 | .withName("onos-hosts") |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 98 | .withRelaxedReadConsistency() |
| 99 | .withSerializer(Serializer.using(hostSerializer.build())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 100 | .build(); |
| 101 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 102 | hosts = hostsConsistentMap.asJavaMap(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 103 | |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 104 | prevHosts.putAll(hosts); |
| 105 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 106 | hostsConsistentMap.addListener(hostLocationTracker); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 107 | |
| 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | public void deactivate() { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 113 | hostsConsistentMap.removeListener(hostLocationTracker); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 114 | prevHosts.clear(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 115 | |
| 116 | log.info("Stopped"); |
| 117 | } |
| 118 | |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 119 | private boolean shouldUpdate(DefaultHost existingHost, |
| 120 | ProviderId providerId, |
| 121 | HostId hostId, |
| 122 | HostDescription hostDescription, |
| 123 | boolean replaceIPs) { |
| 124 | if (existingHost == null) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (!Objects.equals(existingHost.providerId(), providerId) || |
| 129 | !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) || |
| 130 | !Objects.equals(existingHost.vlan(), hostDescription.vlan()) || |
| 131 | !Objects.equals(existingHost.location(), hostDescription.location())) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | if (replaceIPs) { |
| 136 | if (!Objects.equals(hostDescription.ipAddress(), |
| 137 | existingHost.ipAddresses())) { |
| 138 | return true; |
| 139 | } |
| 140 | } else { |
| 141 | if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // check to see if any of the annotations provided by hostDescription |
| 147 | // differ from those in the existing host |
| 148 | return hostDescription.annotations().keys().stream() |
| 149 | .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k), |
| 150 | existingHost.annotations().value(k))); |
| 151 | |
| 152 | |
| 153 | } |
| 154 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 155 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 158 | HostId hostId, |
| 159 | HostDescription hostDescription, |
| 160 | boolean replaceIPs) { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 161 | hostsConsistentMap.computeIf(hostId, |
Brian O'Connor | dab0974 | 2015-12-07 20:06:29 -0800 | [diff] [blame] | 162 | existingHost -> shouldUpdate(existingHost, providerId, hostId, |
| 163 | hostDescription, replaceIPs), |
| 164 | (id, existingHost) -> { |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 165 | HostLocation location = hostDescription.location(); |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 166 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 167 | final Set<IpAddress> addresses; |
| 168 | if (existingHost == null || replaceIPs) { |
| 169 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 170 | } else { |
| 171 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 172 | addresses.addAll(hostDescription.ipAddress()); |
| 173 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 174 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 175 | final Annotations annotations; |
| 176 | if (existingHost != null) { |
| 177 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 178 | hostDescription.annotations()); |
| 179 | } else { |
| 180 | annotations = hostDescription.annotations(); |
| 181 | } |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 182 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 183 | return new DefaultHost(providerId, |
| 184 | hostId, |
| 185 | hostDescription.hwAddress(), |
| 186 | hostDescription.vlan(), |
| 187 | location, |
| 188 | addresses, |
| 189 | annotations); |
| 190 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 191 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 194 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 197 | hosts.remove(hostId); |
| 198 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 201 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 202 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 203 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 204 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 205 | if (existingHost != null) { |
| 206 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 207 | "Existing and new MAC addresses differ."); |
| 208 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 209 | "Existing and new VLANs differ."); |
| 210 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 211 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 212 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 213 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 214 | addresses.remove(ipAddress); |
| 215 | return new DefaultHost(existingHost.providerId(), |
| 216 | hostId, |
| 217 | existingHost.mac(), |
| 218 | existingHost.vlan(), |
| 219 | existingHost.location(), |
| 220 | ImmutableSet.copyOf(addresses), |
| 221 | existingHost.annotations()); |
| 222 | } else { |
| 223 | return existingHost; |
| 224 | } |
| 225 | } |
| 226 | return null; |
| 227 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 228 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 232 | public int getHostCount() { |
| 233 | return hosts.size(); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Iterable<Host> getHosts() { |
| 238 | return ImmutableSet.copyOf(hosts.values()); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Host getHost(HostId hostId) { |
| 243 | return hosts.get(hostId); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public Set<Host> getHosts(VlanId vlanId) { |
| 248 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public Set<Host> getHosts(MacAddress mac) { |
| 253 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public Set<Host> getHosts(IpAddress ip) { |
| 258 | return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 263 | Set<Host> filtered = hosts.entrySet().stream() |
| 264 | .filter(entry -> entry.getValue().location().equals(connectPoint)) |
| 265 | .map(Map.Entry::getValue) |
| 266 | .collect(Collectors.toSet()); |
| 267 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 272 | Set<Host> filtered = hosts.entrySet().stream() |
| 273 | .filter(entry -> entry.getValue().location().deviceId().equals(deviceId)) |
| 274 | .map(Map.Entry::getValue) |
| 275 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 276 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 279 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 280 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 281 | } |
| 282 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 283 | private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 284 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 285 | public void event(MapEvent<HostId, DefaultHost> event) { |
| 286 | DefaultHost host = checkNotNull(event.value().value()); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 287 | Host prevHost = prevHosts.put(host.id(), host); |
| 288 | switch (event.type()) { |
| 289 | case INSERT: |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 290 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
alshabib | 1400ce9 | 2015-12-16 15:05:47 -0800 | [diff] [blame] | 291 | break; |
| 292 | case UPDATE: |
| 293 | if (!Objects.equals(prevHost.location(), host.location())) { |
| 294 | notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost)); |
| 295 | } else if (!Objects.equals(prevHost, host)) { |
| 296 | notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost)); |
| 297 | } |
| 298 | break; |
| 299 | case REMOVE: |
| 300 | if (prevHosts.remove(host.id()) != null) { |
| 301 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
| 302 | } |
| 303 | break; |
| 304 | default: |
| 305 | log.warn("Unknown map event type: {}", event.type()); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |