blob: d6980116d070adcd460d418d5841b4ca0e4bfee8 [file] [log] [blame]
Madan Jampani70583972015-06-30 11:25:05 -07001package org.onosproject.store.host.impl;
2
Madan Jampanid13f3b82015-07-01 17:37:50 -07003import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani70583972015-06-30 11:25:05 -07004import 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;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -07007import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
Madan Jampani70583972015-06-30 11:25:05 -07008import 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;
Madan Jampani70583972015-06-30 11:25:05 -070016import 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;
Madan Jampanid33b0772015-08-14 10:30:55 -070053import com.google.common.collect.ImmutableMultimap;
Madan Jampani70583972015-06-30 11:25:05 -070054import com.google.common.collect.ImmutableSet;
Madan Jampani70583972015-06-30 11:25:05 -070055import com.google.common.collect.Multimaps;
56import com.google.common.collect.SetMultimap;
57import com.google.common.collect.Sets;
Madan Jampani70583972015-06-30 11:25:05 -070058
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
Madan Jampanid13f3b82015-07-01 17:37:50 -070077 private final SetMultimap<ConnectPoint, Host> locations =
78 Multimaps.synchronizedSetMultimap(
79 HashMultimap.<ConnectPoint, Host>create());
80
Madan Jampani70583972015-06-30 11:25:05 -070081 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 Jampanid33b0772015-08-14 10:30:55 -0700178 return ImmutableMultimap.copyOf(locations)
179 .entries()
Madan Jampani70583972015-06-30 11:25:05 -0700180 .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 Jampani70583972015-06-30 11:25:05 -0700252 @Override
253 public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) {
Madan Jampanid13f3b82015-07-01 17:37:50 -0700254 DefaultHost host = checkNotNull(event.value());
Madan Jampani70583972015-06-30 11:25:05 -0700255 if (event.type() == PUT) {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700256 boolean isNew = locations.put(host.location(), host);
257 notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host));
Madan Jampani70583972015-06-30 11:25:05 -0700258 } else if (event.type() == REMOVE) {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700259 if (locations.remove(host.location(), host)) {
260 notifyDelegate(new HostEvent(HOST_REMOVED, host));
261 }
262
Madan Jampani70583972015-06-30 11:25:05 -0700263 }
264 }
265 }
266}