blob: 437474faae00025a682e6f2e050b6ac0d18098c0 [file] [log] [blame]
Madan Jampani38a88212015-09-15 11:21:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani38a88212015-09-15 11:21:27 -07003 *
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 */
16package org.onosproject.store.host.impl;
17
alshabib8a4a6002015-11-25 14:31:16 -080018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
Madan Jampanic7f49f92015-12-10 11:35:06 -080020
Madan Jampani38a88212015-09-15 11:21:27 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
30import org.onlab.util.KryoNamespace;
31import org.onosproject.net.Annotations;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DefaultHost;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.Host;
37import org.onosproject.net.HostId;
Brian O'Connorf107bd72015-09-21 15:31:03 -070038import org.onosproject.net.HostLocation;
Madan Jampani38a88212015-09-15 11:21:27 -070039import org.onosproject.net.host.HostDescription;
40import org.onosproject.net.host.HostEvent;
41import org.onosproject.net.host.HostStore;
42import org.onosproject.net.host.HostStoreDelegate;
Madan Jampani38a88212015-09-15 11:21:27 -070043import org.onosproject.net.provider.ProviderId;
44import org.onosproject.store.AbstractStore;
45import org.onosproject.store.serializers.KryoNamespaces;
alshabib8a4a6002015-11-25 14:31:16 -080046import org.onosproject.store.service.ConsistentMap;
47import org.onosproject.store.service.MapEvent;
48import org.onosproject.store.service.MapEventListener;
49import org.onosproject.store.service.Serializer;
Madan Jampani38a88212015-09-15 11:21:27 -070050import org.onosproject.store.service.StorageService;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053051import org.onosproject.store.service.DistributedPrimitive.Status;
Madan Jampani38a88212015-09-15 11:21:27 -070052import org.slf4j.Logger;
53
alshabib8a4a6002015-11-25 14:31:16 -080054import java.util.Collection;
55import java.util.HashSet;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053056import java.util.Iterator;
alshabib8a4a6002015-11-25 14:31:16 -080057import java.util.Map;
58import java.util.Objects;
59import java.util.Set;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053060import java.util.concurrent.ConcurrentHashMap;
61import java.util.concurrent.ScheduledExecutorService;
62import java.util.function.Consumer;
alshabib8a4a6002015-11-25 14:31:16 -080063import java.util.function.Predicate;
64import java.util.stream.Collectors;
65
66import static com.google.common.base.Preconditions.checkNotNull;
67import static com.google.common.base.Preconditions.checkState;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053068import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
69import static org.onlab.util.Tools.groupedThreads;
alshabib8a4a6002015-11-25 14:31:16 -080070import static org.onosproject.net.DefaultAnnotations.merge;
71import static org.onosproject.net.host.HostEvent.Type.*;
72import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani38a88212015-09-15 11:21:27 -070073
74/**
75 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
76 */
77@Component(immediate = true)
78@Service
alshabib8a4a6002015-11-25 14:31:16 -080079public class DistributedHostStore
Madan Jampani38a88212015-09-15 11:21:27 -070080 extends AbstractStore<HostEvent, HostStoreDelegate>
81 implements HostStore {
82
83 private final Logger log = getLogger(getClass());
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected StorageService storageService;
87
Madan Jampanic6371882016-06-03 21:30:17 -070088 private ConsistentMap<HostId, DefaultHost> hostsConsistentMap;
alshabib8a4a6002015-11-25 14:31:16 -080089 private Map<HostId, DefaultHost> hosts;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053090 private Map<IpAddress, Set<Host>> hostsByIp;
Madan Jampani38a88212015-09-15 11:21:27 -070091
alshabib8a4a6002015-11-25 14:31:16 -080092 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070093 new HostLocationTracker();
94
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053095 private ScheduledExecutorService executor;
96
97 private Consumer<Status> statusChangeListener;
98
Madan Jampani38a88212015-09-15 11:21:27 -070099 @Activate
100 public void activate() {
101 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000102 .register(KryoNamespaces.API);
Madan Jampani38a88212015-09-15 11:21:27 -0700103
Madan Jampanic6371882016-06-03 21:30:17 -0700104 hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -0700105 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -0800106 .withRelaxedReadConsistency()
107 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -0700108 .build();
109
Madan Jampanic6371882016-06-03 21:30:17 -0700110 hosts = hostsConsistentMap.asJavaMap();
alshabib8a4a6002015-11-25 14:31:16 -0800111
alshabib1400ce92015-12-16 15:05:47 -0800112
Madan Jampanic6371882016-06-03 21:30:17 -0700113 hostsConsistentMap.addListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700114
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530115 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/hosts", "store", log));
116 statusChangeListener = status -> {
117 if (status == Status.ACTIVE) {
118 executor.execute(this::loadHostsByIp);
119 }
120 };
121 hostsConsistentMap.addStatusChangeListener(statusChangeListener);
122 loadHostsByIp();
Madan Jampani38a88212015-09-15 11:21:27 -0700123 log.info("Started");
124 }
125
126 @Deactivate
127 public void deactivate() {
Madan Jampanic6371882016-06-03 21:30:17 -0700128 hostsConsistentMap.removeListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700129
130 log.info("Stopped");
131 }
132
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530133 private void loadHostsByIp() {
134 hostsByIp = new ConcurrentHashMap<IpAddress, Set<Host>>();
135 hostsConsistentMap.asJavaMap().values().forEach(host -> {
136 host.ipAddresses().forEach(ip -> {
137 Set<Host> existingHosts = hostsByIp.get(ip);
138 if (existingHosts == null) {
139 hostsByIp.put(ip, addHosts(host));
140 } else {
141 existingHosts.add(host);
142 }
143 });
144 });
145 }
146
Brian O'Connordab09742015-12-07 20:06:29 -0800147 private boolean shouldUpdate(DefaultHost existingHost,
148 ProviderId providerId,
149 HostId hostId,
150 HostDescription hostDescription,
151 boolean replaceIPs) {
152 if (existingHost == null) {
153 return true;
154 }
155
Charles Chan29ecdee2017-02-22 18:46:56 -0800156 // Avoid overriding configured hosts
157 if (existingHost.configured()) {
158 return false;
159 }
160
Brian O'Connordab09742015-12-07 20:06:29 -0800161 if (!Objects.equals(existingHost.providerId(), providerId) ||
162 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
163 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000164 !Objects.equals(existingHost.location(), hostDescription.location())) {
Brian O'Connordab09742015-12-07 20:06:29 -0800165 return true;
166 }
167
168 if (replaceIPs) {
169 if (!Objects.equals(hostDescription.ipAddress(),
170 existingHost.ipAddresses())) {
171 return true;
172 }
173 } else {
174 if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
175 return true;
176 }
177 }
178
179 // check to see if any of the annotations provided by hostDescription
180 // differ from those in the existing host
181 return hostDescription.annotations().keys().stream()
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530182 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
183 existingHost.annotations().value(k)));
Brian O'Connordab09742015-12-07 20:06:29 -0800184
185
186 }
187
Charles Chan009c3082015-11-10 14:18:04 -0800188 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700189 @Override
190 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700191 HostId hostId,
192 HostDescription hostDescription,
193 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700194 hostsConsistentMap.computeIf(hostId,
Brian O'Connordab09742015-12-07 20:06:29 -0800195 existingHost -> shouldUpdate(existingHost, providerId, hostId,
196 hostDescription, replaceIPs),
197 (id, existingHost) -> {
Madan Jampanic7f49f92015-12-10 11:35:06 -0800198 HostLocation location = hostDescription.location();
Brian O'Connorf107bd72015-09-21 15:31:03 -0700199
Madan Jampanic7f49f92015-12-10 11:35:06 -0800200 final Set<IpAddress> addresses;
201 if (existingHost == null || replaceIPs) {
202 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
203 } else {
204 addresses = Sets.newHashSet(existingHost.ipAddresses());
205 addresses.addAll(hostDescription.ipAddress());
206 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700207
Madan Jampanic7f49f92015-12-10 11:35:06 -0800208 final Annotations annotations;
sdn5e935452016-08-30 04:12:54 -0700209 final boolean configured;
Madan Jampanic7f49f92015-12-10 11:35:06 -0800210 if (existingHost != null) {
211 annotations = merge((DefaultAnnotations) existingHost.annotations(),
212 hostDescription.annotations());
sdn5e935452016-08-30 04:12:54 -0700213 configured = existingHost.configured();
Madan Jampanic7f49f92015-12-10 11:35:06 -0800214 } else {
215 annotations = hostDescription.annotations();
sdn5e935452016-08-30 04:12:54 -0700216 configured = hostDescription.configured();
Madan Jampanic7f49f92015-12-10 11:35:06 -0800217 }
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000218
Madan Jampanic7f49f92015-12-10 11:35:06 -0800219 return new DefaultHost(providerId,
220 hostId,
221 hostDescription.hwAddress(),
222 hostDescription.vlan(),
223 location,
224 addresses,
sdn5e935452016-08-30 04:12:54 -0700225 configured,
Madan Jampanic7f49f92015-12-10 11:35:06 -0800226 annotations);
227 });
Charles Chan009c3082015-11-10 14:18:04 -0800228 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700229 }
230
Charles Chan009c3082015-11-10 14:18:04 -0800231 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700232 @Override
233 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800234 hosts.remove(hostId);
235 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700236 }
237
Charles Chan009c3082015-11-10 14:18:04 -0800238 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700239 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700240 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800241 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700242 if (existingHost != null) {
243 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
244 "Existing and new MAC addresses differ.");
245 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
246 "Existing and new VLANs differ.");
247
samanwita pale7c08de2015-09-24 21:59:49 -0700248 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700249 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700250 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700251 addresses.remove(ipAddress);
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530252 removeIpFromHostsByIp(existingHost, ipAddress);
samanwita palc40e5ed2015-09-24 11:01:51 -0700253 return new DefaultHost(existingHost.providerId(),
254 hostId,
255 existingHost.mac(),
256 existingHost.vlan(),
257 existingHost.location(),
258 ImmutableSet.copyOf(addresses),
259 existingHost.annotations());
260 } else {
261 return existingHost;
262 }
263 }
264 return null;
265 });
Charles Chan009c3082015-11-10 14:18:04 -0800266 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700267 }
268
269 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700270 public int getHostCount() {
271 return hosts.size();
272 }
273
274 @Override
275 public Iterable<Host> getHosts() {
276 return ImmutableSet.copyOf(hosts.values());
277 }
278
279 @Override
280 public Host getHost(HostId hostId) {
281 return hosts.get(hostId);
282 }
283
284 @Override
285 public Set<Host> getHosts(VlanId vlanId) {
286 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
287 }
288
289 @Override
290 public Set<Host> getHosts(MacAddress mac) {
291 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
292 }
293
294 @Override
295 public Set<Host> getHosts(IpAddress ip) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530296 Set<Host> hosts = hostsByIp.get(ip);
297 return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of();
Madan Jampani38a88212015-09-15 11:21:27 -0700298 }
299
300 @Override
301 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800302 Set<Host> filtered = hosts.entrySet().stream()
303 .filter(entry -> entry.getValue().location().equals(connectPoint))
304 .map(Map.Entry::getValue)
305 .collect(Collectors.toSet());
306 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700307 }
308
309 @Override
310 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800311 Set<Host> filtered = hosts.entrySet().stream()
312 .filter(entry -> entry.getValue().location().deviceId().equals(deviceId))
313 .map(Map.Entry::getValue)
314 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700315 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700316 }
317
Madan Jampani38a88212015-09-15 11:21:27 -0700318 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
319 return collection.stream().filter(predicate).collect(Collectors.toSet());
320 }
321
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530322 private Set<Host> addHosts(Host host) {
323 Set<Host> hosts = Sets.newConcurrentHashSet();
324 hosts.add(host);
325 return hosts;
326 }
327
328 private Set<Host> updateHosts(Set<Host> existingHosts, Host host) {
329 Iterator<Host> iterator = existingHosts.iterator();
330 while (iterator.hasNext()) {
331 Host existingHost = iterator.next();
332 if (existingHost.id().equals(host.id())) {
333 iterator.remove();
334 }
335 }
336 existingHosts.add(host);
337 return existingHosts;
338 }
339
340 private Set<Host> removeHosts(Set<Host> existingHosts, Host host) {
341 if (existingHosts != null) {
342 Iterator<Host> iterator = existingHosts.iterator();
343 while (iterator.hasNext()) {
344 Host existingHost = iterator.next();
345 if (existingHost.id().equals(host.id())) {
346 iterator.remove();
347 }
348 }
349 }
350
351 if (existingHosts.isEmpty()) {
352 return null;
353 }
354 return existingHosts;
355 }
356
357 private void updateHostsByIp(DefaultHost host) {
358 host.ipAddresses().forEach(ip -> {
359 hostsByIp.compute(ip, (k, v) -> v == null ? addHosts(host)
360 : updateHosts(v, host));
361 });
362 }
363
364 private void removeHostsByIp(DefaultHost host) {
365 host.ipAddresses().forEach(ip -> {
366 hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host));
367 });
368 }
369
370 private void removeIpFromHostsByIp(DefaultHost host, IpAddress ip) {
371 hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host));
372 }
373
alshabib8a4a6002015-11-25 14:31:16 -0800374 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700375 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800376 public void event(MapEvent<HostId, DefaultHost> event) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530377 DefaultHost host = checkNotNull(event.value().value());
alshabib1400ce92015-12-16 15:05:47 -0800378 switch (event.type()) {
379 case INSERT:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530380 updateHostsByIp(host);
Charles Chan009c3082015-11-10 14:18:04 -0800381 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800382 break;
383 case UPDATE:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530384 updateHostsByIp(host);
385 DefaultHost prevHost = checkNotNull(event.oldValue().value());
alshabib1400ce92015-12-16 15:05:47 -0800386 if (!Objects.equals(prevHost.location(), host.location())) {
387 notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost));
388 } else if (!Objects.equals(prevHost, host)) {
389 notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost));
390 }
391 break;
392 case REMOVE:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530393 updateHostsByIp(host);
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700394 notifyDelegate(new HostEvent(HOST_REMOVED, host));
alshabib1400ce92015-12-16 15:05:47 -0800395 break;
396 default:
397 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700398 }
399 }
400 }
401}