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