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