Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 1 | package org.onosproject.store.host.impl; |
| 2 | |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 4 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 5 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 6 | import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 7 | import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 8 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 9 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 10 | import static org.slf4j.LoggerFactory.getLogger; |
| 11 | |
| 12 | import java.util.Collection; |
| 13 | import java.util.Collections; |
| 14 | import java.util.Objects; |
| 15 | import java.util.Set; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 16 | import java.util.function.Predicate; |
| 17 | import java.util.stream.Collectors; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onlab.util.KryoNamespace; |
| 29 | import org.onosproject.net.Annotations; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DefaultAnnotations; |
| 32 | import org.onosproject.net.DefaultHost; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.HostId; |
| 36 | import org.onosproject.net.host.HostDescription; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostStore; |
| 39 | import org.onosproject.net.host.HostStoreDelegate; |
| 40 | import org.onosproject.net.host.PortAddresses; |
| 41 | import org.onosproject.net.host.HostEvent.Type; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.onosproject.store.AbstractStore; |
| 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 46 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 47 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 48 | import org.onosproject.store.service.LogicalClockService; |
| 49 | import org.onosproject.store.service.StorageService; |
| 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | import com.google.common.collect.HashMultimap; |
Madan Jampani | d33b077 | 2015-08-14 10:30:55 -0700 | [diff] [blame] | 53 | import com.google.common.collect.ImmutableMultimap; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 54 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 55 | import com.google.common.collect.Multimaps; |
| 56 | import com.google.common.collect.SetMultimap; |
| 57 | import com.google.common.collect.Sets; |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | @Service |
| 64 | public class ECHostStore |
| 65 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 66 | implements HostStore { |
| 67 | |
| 68 | private final Logger log = getLogger(getClass()); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected StorageService storageService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected LogicalClockService clockService; |
| 75 | |
| 76 | // Hosts tracked by their location |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 77 | private final SetMultimap<ConnectPoint, Host> locations = |
| 78 | Multimaps.synchronizedSetMultimap( |
| 79 | HashMultimap.<ConnectPoint, Host>create()); |
| 80 | |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 81 | private final SetMultimap<ConnectPoint, PortAddresses> portAddresses = |
| 82 | Multimaps.synchronizedSetMultimap( |
| 83 | HashMultimap.<ConnectPoint, PortAddresses>create()); |
| 84 | |
| 85 | private EventuallyConsistentMap<HostId, DefaultHost> hosts; |
| 86 | |
| 87 | private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker = |
| 88 | new HostLocationTracker(); |
| 89 | |
| 90 | @Activate |
| 91 | public void activate() { |
| 92 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 93 | .register(KryoNamespaces.API); |
| 94 | |
| 95 | hosts = storageService.<HostId, DefaultHost>eventuallyConsistentMapBuilder() |
| 96 | .withName("onos-hosts") |
| 97 | .withSerializer(hostSerializer) |
| 98 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 99 | .build(); |
| 100 | |
| 101 | hosts.addListener(hostLocationTracker); |
| 102 | |
| 103 | log.info("Started"); |
| 104 | } |
| 105 | |
| 106 | @Deactivate |
| 107 | public void deactivate() { |
| 108 | hosts.removeListener(hostLocationTracker); |
| 109 | hosts.destroy(); |
| 110 | locations.clear(); |
| 111 | portAddresses.clear(); |
| 112 | |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public HostEvent createOrUpdateHost(ProviderId providerId, |
| 118 | HostId hostId, |
| 119 | HostDescription hostDescription) { |
| 120 | DefaultHost currentHost = hosts.get(hostId); |
| 121 | if (currentHost == null) { |
| 122 | DefaultHost newhost = new DefaultHost( |
| 123 | providerId, |
| 124 | hostId, |
| 125 | hostDescription.hwAddress(), |
| 126 | hostDescription.vlan(), |
| 127 | hostDescription.location(), |
| 128 | ImmutableSet.copyOf(hostDescription.ipAddress())); |
| 129 | hosts.put(hostId, newhost); |
| 130 | return new HostEvent(HOST_ADDED, newhost); |
| 131 | } |
| 132 | return updateHost(providerId, hostId, hostDescription, currentHost); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public HostEvent removeHost(HostId hostId) { |
| 137 | Host host = hosts.remove(hostId); |
| 138 | return host != null ? new HostEvent(HOST_REMOVED, host) : null; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int getHostCount() { |
| 143 | return hosts.size(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Iterable<Host> getHosts() { |
| 148 | return ImmutableSet.copyOf(hosts.values()); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Host getHost(HostId hostId) { |
| 153 | return hosts.get(hostId); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Set<Host> getHosts(VlanId vlanId) { |
| 158 | return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<Host> getHosts(MacAddress mac) { |
| 163 | return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Set<Host> getHosts(IpAddress ip) { |
| 168 | return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 173 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Madan Jampani | d33b077 | 2015-08-14 10:30:55 -0700 | [diff] [blame] | 178 | return ImmutableMultimap.copyOf(locations) |
| 179 | .entries() |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 180 | .stream() |
| 181 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 182 | .map(entry -> entry.getValue()) |
| 183 | .collect(Collectors.toSet()); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void updateAddressBindings(PortAddresses addresses) { |
| 188 | portAddresses.put(addresses.connectPoint(), addresses); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void removeAddressBindings(PortAddresses addresses) { |
| 193 | portAddresses.remove(addresses.connectPoint(), addresses); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void clearAddressBindings(ConnectPoint connectPoint) { |
| 198 | portAddresses.removeAll(connectPoint); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Set<PortAddresses> getAddressBindings() { |
| 203 | return ImmutableSet.copyOf(portAddresses.values()); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) { |
| 208 | synchronized (portAddresses) { |
| 209 | Set<PortAddresses> addresses = portAddresses.get(connectPoint); |
| 210 | return addresses == null ? Collections.emptySet() : ImmutableSet.copyOf(addresses); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 215 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 216 | } |
| 217 | |
| 218 | // checks for type of update to host, sends appropriate event |
| 219 | private HostEvent updateHost(ProviderId providerId, |
| 220 | HostId hostId, |
| 221 | HostDescription descr, |
| 222 | DefaultHost currentHost) { |
| 223 | |
| 224 | final boolean hostMoved = !currentHost.location().equals(descr.location()); |
| 225 | if (hostMoved || |
| 226 | !currentHost.ipAddresses().containsAll(descr.ipAddress()) || |
| 227 | !descr.annotations().keys().isEmpty()) { |
| 228 | |
| 229 | Set<IpAddress> addresses = Sets.newHashSet(currentHost.ipAddresses()); |
| 230 | addresses.addAll(descr.ipAddress()); |
| 231 | Annotations annotations = merge((DefaultAnnotations) currentHost.annotations(), |
| 232 | descr.annotations()); |
| 233 | |
| 234 | DefaultHost updatedHost = new DefaultHost(providerId, currentHost.id(), |
| 235 | currentHost.mac(), currentHost.vlan(), |
| 236 | descr.location(), |
| 237 | addresses, |
| 238 | annotations); |
| 239 | |
| 240 | // TODO: We need a way to detect conflicting changes and abort update. |
| 241 | hosts.put(hostId, updatedHost); |
| 242 | locations.remove(currentHost.location(), currentHost); |
| 243 | locations.put(updatedHost.location(), updatedHost); |
| 244 | |
| 245 | HostEvent.Type eventType = hostMoved ? Type.HOST_MOVED : Type.HOST_UPDATED; |
| 246 | return new HostEvent(eventType, updatedHost); |
| 247 | } |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | private class HostLocationTracker implements EventuallyConsistentMapListener<HostId, DefaultHost> { |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 252 | @Override |
| 253 | public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) { |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 254 | DefaultHost host = checkNotNull(event.value()); |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 255 | if (event.type() == PUT) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 256 | boolean isNew = locations.put(host.location(), host); |
| 257 | notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host)); |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 258 | } else if (event.type() == REMOVE) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 259 | if (locations.remove(host.location(), host)) { |
| 260 | notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
| 261 | } |
| 262 | |
Madan Jampani | 7058397 | 2015-06-30 11:25:05 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |