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 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 20 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 21 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 22 | import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 23 | import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 24 | import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; |
| 25 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 26 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | import java.util.Collection; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 30 | import java.util.HashSet; |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 31 | import java.util.Map; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 32 | import java.util.Objects; |
| 33 | import java.util.Set; |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 34 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 35 | import java.util.function.Predicate; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import org.apache.felix.scr.annotations.Activate; |
| 39 | import org.apache.felix.scr.annotations.Component; |
| 40 | import org.apache.felix.scr.annotations.Deactivate; |
| 41 | import org.apache.felix.scr.annotations.Reference; |
| 42 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 43 | import org.apache.felix.scr.annotations.Service; |
| 44 | import org.onlab.packet.IpAddress; |
| 45 | import org.onlab.packet.MacAddress; |
| 46 | import org.onlab.packet.VlanId; |
| 47 | import org.onlab.util.KryoNamespace; |
| 48 | import org.onosproject.net.Annotations; |
| 49 | import org.onosproject.net.ConnectPoint; |
| 50 | import org.onosproject.net.DefaultAnnotations; |
| 51 | import org.onosproject.net.DefaultHost; |
| 52 | import org.onosproject.net.DeviceId; |
| 53 | import org.onosproject.net.Host; |
| 54 | import org.onosproject.net.HostId; |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 55 | import org.onosproject.net.HostLocation; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 56 | import org.onosproject.net.host.HostDescription; |
| 57 | import org.onosproject.net.host.HostEvent; |
| 58 | import org.onosproject.net.host.HostStore; |
| 59 | import org.onosproject.net.host.HostStoreDelegate; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 60 | import org.onosproject.net.provider.ProviderId; |
| 61 | import org.onosproject.store.AbstractStore; |
| 62 | import org.onosproject.store.serializers.KryoNamespaces; |
| 63 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 64 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 65 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 66 | import org.onosproject.store.service.LogicalClockService; |
| 67 | import org.onosproject.store.service.StorageService; |
| 68 | import org.slf4j.Logger; |
| 69 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 70 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 71 | import com.google.common.collect.Sets; |
| 72 | |
| 73 | /** |
| 74 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 75 | */ |
| 76 | @Component(immediate = true) |
| 77 | @Service |
| 78 | public class ECHostStore |
| 79 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 80 | implements HostStore { |
| 81 | |
| 82 | private final Logger log = getLogger(getClass()); |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected StorageService storageService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected LogicalClockService clockService; |
| 89 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 90 | private EventuallyConsistentMap<HostId, DefaultHost> hosts; |
| 91 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 92 | private final ConcurrentHashMap<HostId, ConnectPoint> locations = |
| 93 | new ConcurrentHashMap<>(); |
| 94 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 95 | private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker = |
| 96 | new HostLocationTracker(); |
| 97 | |
| 98 | @Activate |
| 99 | public void activate() { |
| 100 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 101 | .register(KryoNamespaces.API); |
| 102 | |
| 103 | hosts = storageService.<HostId, DefaultHost>eventuallyConsistentMapBuilder() |
| 104 | .withName("onos-hosts") |
| 105 | .withSerializer(hostSerializer) |
| 106 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 107 | .build(); |
| 108 | |
| 109 | hosts.addListener(hostLocationTracker); |
| 110 | |
| 111 | log.info("Started"); |
| 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | public void deactivate() { |
| 116 | hosts.removeListener(hostLocationTracker); |
| 117 | hosts.destroy(); |
| 118 | locations.clear(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 119 | |
| 120 | log.info("Stopped"); |
| 121 | } |
| 122 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 123 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public HostEvent createOrUpdateHost(ProviderId providerId, |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 126 | HostId hostId, |
| 127 | HostDescription hostDescription, |
| 128 | boolean replaceIPs) { |
| 129 | // TODO: We need a way to detect conflicting changes and abort update. |
| 130 | // (BOC) Compute might do this for us. |
| 131 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 132 | hosts.compute(hostId, (id, existingHost) -> { |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 133 | HostLocation location = hostDescription.location(); |
| 134 | |
| 135 | final Set<IpAddress> addresses; |
| 136 | if (existingHost == null || replaceIPs) { |
| 137 | addresses = ImmutableSet.copyOf(hostDescription.ipAddress()); |
| 138 | } else { |
| 139 | addresses = Sets.newHashSet(existingHost.ipAddresses()); |
| 140 | addresses.addAll(hostDescription.ipAddress()); |
| 141 | } |
| 142 | |
| 143 | final Annotations annotations; |
| 144 | if (existingHost != null) { |
| 145 | annotations = merge((DefaultAnnotations) existingHost.annotations(), |
| 146 | hostDescription.annotations()); |
| 147 | } else { |
| 148 | annotations = hostDescription.annotations(); |
| 149 | } |
| 150 | |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 151 | return new DefaultHost(providerId, |
| 152 | hostId, |
| 153 | hostDescription.hwAddress(), |
| 154 | hostDescription.vlan(), |
| 155 | location, |
| 156 | addresses, |
| 157 | annotations); |
| 158 | }); |
| 159 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 160 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 163 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public HostEvent removeHost(HostId hostId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 166 | hosts.remove(hostId); |
| 167 | return null; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 170 | // TODO No longer need to return HostEvent |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 171 | @Override |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 172 | public HostEvent removeIp(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 173 | hosts.compute(hostId, (id, existingHost) -> { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 174 | if (existingHost != null) { |
| 175 | checkState(Objects.equals(hostId.mac(), existingHost.mac()), |
| 176 | "Existing and new MAC addresses differ."); |
| 177 | checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()), |
| 178 | "Existing and new VLANs differ."); |
| 179 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 180 | Set<IpAddress> addresses = existingHost.ipAddresses(); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 181 | if (addresses != null && addresses.contains(ipAddress)) { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 182 | addresses = new HashSet<>(existingHost.ipAddresses()); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 183 | addresses.remove(ipAddress); |
| 184 | return new DefaultHost(existingHost.providerId(), |
| 185 | hostId, |
| 186 | existingHost.mac(), |
| 187 | existingHost.vlan(), |
| 188 | existingHost.location(), |
| 189 | ImmutableSet.copyOf(addresses), |
| 190 | existingHost.annotations()); |
| 191 | } else { |
| 192 | return existingHost; |
| 193 | } |
| 194 | } |
| 195 | return null; |
| 196 | }); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 197 | return null; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 201 | public int getHostCount() { |
| 202 | return hosts.size(); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Iterable<Host> getHosts() { |
| 207 | return ImmutableSet.copyOf(hosts.values()); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Host getHost(HostId hostId) { |
| 212 | return hosts.get(hostId); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Set<Host> getHosts(VlanId vlanId) { |
| 217 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Set<Host> getHosts(MacAddress mac) { |
| 222 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Set<Host> getHosts(IpAddress ip) { |
| 227 | return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 232 | Set<Host> filtered = hosts.entrySet().stream() |
| 233 | .filter(entry -> entry.getValue().location().equals(connectPoint)) |
| 234 | .map(Map.Entry::getValue) |
| 235 | .collect(Collectors.toSet()); |
| 236 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 241 | Set<Host> filtered = hosts.entrySet().stream() |
| 242 | .filter(entry -> entry.getValue().location().deviceId().equals(deviceId)) |
| 243 | .map(Map.Entry::getValue) |
| 244 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | fe2122c | 2015-09-30 13:46:22 -0700 | [diff] [blame] | 245 | return ImmutableSet.copyOf(filtered); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 248 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 249 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 250 | } |
| 251 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 252 | private class HostLocationTracker implements EventuallyConsistentMapListener<HostId, DefaultHost> { |
| 253 | @Override |
| 254 | public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) { |
| 255 | DefaultHost host = checkNotNull(event.value()); |
| 256 | if (event.type() == PUT) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 257 | ConnectPoint prevLocation = locations.put(host.id(), host.location()); |
| 258 | if (prevLocation == null) { |
| 259 | notifyDelegate(new HostEvent(HOST_ADDED, host)); |
| 260 | } else if (!Objects.equals(prevLocation, host.location())) { |
| 261 | notifyDelegate(new HostEvent(HOST_MOVED, host)); |
| 262 | } else { |
| 263 | notifyDelegate(new HostEvent(HOST_UPDATED, host)); |
| 264 | } |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 265 | } else if (event.type() == REMOVE) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame^] | 266 | if (locations.remove(host.id()) != null) { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 267 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
| 268 | } |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |