blob: d8b9daca8dacb232daeb862ac59605f2fc6daa92 [file] [log] [blame]
CNluciusb786e612015-08-20 14:36:29 +08001package org.onosproject.store.host.impl;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4import static org.onosproject.net.DefaultAnnotations.merge;
5import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
6import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
7import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
8import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
9import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
10import static org.slf4j.LoggerFactory.getLogger;
11
12import java.util.Collection;
13import java.util.Collections;
14import java.util.Objects;
15import java.util.Set;
16import java.util.function.Predicate;
17import java.util.stream.Collectors;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onlab.util.KryoNamespace;
29import org.onosproject.net.Annotations;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultAnnotations;
32import org.onosproject.net.DefaultHost;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Host;
35import org.onosproject.net.HostId;
36import org.onosproject.net.host.HostDescription;
37import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostStore;
39import org.onosproject.net.host.HostStoreDelegate;
40import org.onosproject.net.host.PortAddresses;
41import org.onosproject.net.host.HostEvent.Type;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.AbstractStore;
44import org.onosproject.store.serializers.KryoNamespaces;
45import org.onosproject.store.service.EventuallyConsistentMap;
46import org.onosproject.store.service.EventuallyConsistentMapEvent;
47import org.onosproject.store.service.EventuallyConsistentMapListener;
48import org.onosproject.store.service.LogicalClockService;
49import org.onosproject.store.service.StorageService;
50import org.slf4j.Logger;
51
52import com.google.common.collect.HashMultimap;
53import com.google.common.collect.ImmutableMultimap;
54import com.google.common.collect.ImmutableSet;
55import com.google.common.collect.Multimaps;
56import com.google.common.collect.SetMultimap;
57import com.google.common.collect.Sets;
58
59/**
60 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
61 */
62@Component(immediate = true)
63@Service
64public 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
77 private final SetMultimap<ConnectPoint, Host> locations =
78 Multimaps.synchronizedSetMultimap(
79 HashMultimap.<ConnectPoint, Host>create());
80
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 hostDescription.annotations());
130 hosts.put(hostId, newhost);
131 return new HostEvent(HOST_ADDED, newhost);
132 }
133 return updateHost(providerId, hostId, hostDescription, currentHost);
134 }
135
136 @Override
137 public HostEvent removeHost(HostId hostId) {
138 Host host = hosts.remove(hostId);
139 return host != null ? new HostEvent(HOST_REMOVED, host) : null;
140 }
141
142 @Override
143 public int getHostCount() {
144 return hosts.size();
145 }
146
147 @Override
148 public Iterable<Host> getHosts() {
149 return ImmutableSet.copyOf(hosts.values());
150 }
151
152 @Override
153 public Host getHost(HostId hostId) {
154 return hosts.get(hostId);
155 }
156
157 @Override
158 public Set<Host> getHosts(VlanId vlanId) {
159 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
160 }
161
162 @Override
163 public Set<Host> getHosts(MacAddress mac) {
164 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
165 }
166
167 @Override
168 public Set<Host> getHosts(IpAddress ip) {
169 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
170 }
171
172 @Override
173 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
174 return ImmutableSet.copyOf(locations.get(connectPoint));
175 }
176
177 @Override
178 public Set<Host> getConnectedHosts(DeviceId deviceId) {
179 return ImmutableMultimap.copyOf(locations)
180 .entries()
181 .stream()
182 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
183 .map(entry -> entry.getValue())
184 .collect(Collectors.toSet());
185 }
186
187 @Override
188 public void updateAddressBindings(PortAddresses addresses) {
189 portAddresses.put(addresses.connectPoint(), addresses);
190 }
191
192 @Override
193 public void removeAddressBindings(PortAddresses addresses) {
194 portAddresses.remove(addresses.connectPoint(), addresses);
195 }
196
197 @Override
198 public void clearAddressBindings(ConnectPoint connectPoint) {
199 portAddresses.removeAll(connectPoint);
200 }
201
202 @Override
203 public Set<PortAddresses> getAddressBindings() {
204 return ImmutableSet.copyOf(portAddresses.values());
205 }
206
207 @Override
208 public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) {
209 synchronized (portAddresses) {
210 Set<PortAddresses> addresses = portAddresses.get(connectPoint);
211 return addresses == null ? Collections.emptySet() : ImmutableSet.copyOf(addresses);
212 }
213 }
214
215 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
216 return collection.stream().filter(predicate).collect(Collectors.toSet());
217 }
218
219 // checks for type of update to host, sends appropriate event
220 private HostEvent updateHost(ProviderId providerId,
221 HostId hostId,
222 HostDescription descr,
223 DefaultHost currentHost) {
224
225 final boolean hostMoved = !currentHost.location().equals(descr.location());
226 if (hostMoved ||
227 !currentHost.ipAddresses().containsAll(descr.ipAddress()) ||
228 !descr.annotations().keys().isEmpty()) {
229
230 Set<IpAddress> addresses = Sets.newHashSet(currentHost.ipAddresses());
231 addresses.addAll(descr.ipAddress());
232 Annotations annotations = merge((DefaultAnnotations) currentHost.annotations(),
233 descr.annotations());
234
235 DefaultHost updatedHost = new DefaultHost(providerId, currentHost.id(),
236 currentHost.mac(), currentHost.vlan(),
237 descr.location(),
238 addresses,
239 annotations);
240
241 // TODO: We need a way to detect conflicting changes and abort update.
242 hosts.put(hostId, updatedHost);
243 locations.remove(currentHost.location(), currentHost);
244 locations.put(updatedHost.location(), updatedHost);
245
246 HostEvent.Type eventType = hostMoved ? Type.HOST_MOVED : Type.HOST_UPDATED;
247 return new HostEvent(eventType, updatedHost);
248 }
249 return null;
250 }
251
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) {
257 boolean isNew = locations.put(host.location(), host);
258 notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host));
259 } else if (event.type() == REMOVE) {
260 if (locations.remove(host.location(), host)) {
261 notifyDelegate(new HostEvent(HOST_REMOVED, host));
262 }
263
264 }
265 }
266 }
267}