blob: bc0dd002c128e930f0393d969d0492c16d0bfbcc [file] [log] [blame]
Madan Jampani38a88212015-09-15 11:21:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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,
Brian O'Connordab09742015-12-07 20:06:29 -0800149 HostDescription hostDescription,
150 boolean replaceIPs) {
151 if (existingHost == null) {
152 return true;
153 }
154
Charles Chan69ebcbb2017-04-27 14:33:21 -0700155 // Avoid overriding configured host with learnt host
156 if (existingHost.configured() && !hostDescription.configured()) {
Charles Chan29ecdee2017-02-22 18:46:56 -0800157 return false;
158 }
159
Brian O'Connordab09742015-12-07 20:06:29 -0800160 if (!Objects.equals(existingHost.providerId(), providerId) ||
161 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
162 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
Charles Chancd06c692017-04-27 20:46:06 -0700163 !Objects.equals(existingHost.locations(), hostDescription.locations())) {
Brian O'Connordab09742015-12-07 20:06:29 -0800164 return true;
165 }
166
167 if (replaceIPs) {
168 if (!Objects.equals(hostDescription.ipAddress(),
169 existingHost.ipAddresses())) {
170 return true;
171 }
172 } else {
173 if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
174 return true;
175 }
176 }
177
178 // check to see if any of the annotations provided by hostDescription
179 // differ from those in the existing host
180 return hostDescription.annotations().keys().stream()
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530181 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
182 existingHost.annotations().value(k)));
Brian O'Connordab09742015-12-07 20:06:29 -0800183
184
185 }
186
Charles Chan009c3082015-11-10 14:18:04 -0800187 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700188 @Override
189 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700190 HostId hostId,
191 HostDescription hostDescription,
192 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700193 hostsConsistentMap.computeIf(hostId,
Charles Chan69ebcbb2017-04-27 14:33:21 -0700194 existingHost -> shouldUpdate(existingHost, providerId,
Brian O'Connordab09742015-12-07 20:06:29 -0800195 hostDescription, replaceIPs),
196 (id, existingHost) -> {
Brian O'Connorf107bd72015-09-21 15:31:03 -0700197
Madan Jampanic7f49f92015-12-10 11:35:06 -0800198 final Set<IpAddress> addresses;
199 if (existingHost == null || replaceIPs) {
200 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
201 } else {
202 addresses = Sets.newHashSet(existingHost.ipAddresses());
203 addresses.addAll(hostDescription.ipAddress());
204 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700205
Madan Jampanic7f49f92015-12-10 11:35:06 -0800206 final Annotations annotations;
207 if (existingHost != null) {
208 annotations = merge((DefaultAnnotations) existingHost.annotations(),
209 hostDescription.annotations());
210 } else {
211 annotations = hostDescription.annotations();
212 }
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000213
Madan Jampanic7f49f92015-12-10 11:35:06 -0800214 return new DefaultHost(providerId,
215 hostId,
216 hostDescription.hwAddress(),
217 hostDescription.vlan(),
Charles Chancd06c692017-04-27 20:46:06 -0700218 hostDescription.locations(),
Madan Jampanic7f49f92015-12-10 11:35:06 -0800219 addresses,
Charles Chanb1e99242017-07-07 14:11:09 -0700220 hostDescription.configured(),
Madan Jampanic7f49f92015-12-10 11:35:06 -0800221 annotations);
222 });
Charles Chan009c3082015-11-10 14:18:04 -0800223 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700224 }
225
Charles Chan009c3082015-11-10 14:18:04 -0800226 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700227 @Override
228 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800229 hosts.remove(hostId);
230 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700231 }
232
Charles Chan009c3082015-11-10 14:18:04 -0800233 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700234 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700235 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800236 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700237 if (existingHost != null) {
238 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
239 "Existing and new MAC addresses differ.");
240 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
241 "Existing and new VLANs differ.");
242
samanwita pale7c08de2015-09-24 21:59:49 -0700243 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700244 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700245 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700246 addresses.remove(ipAddress);
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530247 removeIpFromHostsByIp(existingHost, ipAddress);
samanwita palc40e5ed2015-09-24 11:01:51 -0700248 return new DefaultHost(existingHost.providerId(),
249 hostId,
250 existingHost.mac(),
251 existingHost.vlan(),
Charles Chancd06c692017-04-27 20:46:06 -0700252 existingHost.locations(),
samanwita palc40e5ed2015-09-24 11:01:51 -0700253 ImmutableSet.copyOf(addresses),
Charles Chancd06c692017-04-27 20:46:06 -0700254 existingHost.configured(),
samanwita palc40e5ed2015-09-24 11:01:51 -0700255 existingHost.annotations());
256 } else {
257 return existingHost;
258 }
259 }
260 return null;
261 });
Charles Chan009c3082015-11-10 14:18:04 -0800262 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700263 }
264
265 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700266 public void removeLocation(HostId hostId, HostLocation location) {
267 hosts.compute(hostId, (id, existingHost) -> {
268 if (existingHost != null) {
269 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
270 "Existing and new MAC addresses differ.");
271 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
272 "Existing and new VLANs differ.");
273
274 Set<HostLocation> locations = new HashSet<>(existingHost.locations());
275 locations.remove(location);
276
277 // Remove entire host if we are removing the last location
278 return locations.isEmpty() ? null :
279 new DefaultHost(existingHost.providerId(),
280 hostId, existingHost.mac(), existingHost.vlan(),
281 locations, existingHost.ipAddresses(),
282 existingHost.configured(), existingHost.annotations());
283 }
284 return null;
285 });
286 }
287
288 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700289 public int getHostCount() {
290 return hosts.size();
291 }
292
293 @Override
294 public Iterable<Host> getHosts() {
295 return ImmutableSet.copyOf(hosts.values());
296 }
297
298 @Override
299 public Host getHost(HostId hostId) {
300 return hosts.get(hostId);
301 }
302
303 @Override
304 public Set<Host> getHosts(VlanId vlanId) {
305 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
306 }
307
308 @Override
309 public Set<Host> getHosts(MacAddress mac) {
310 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
311 }
312
313 @Override
314 public Set<Host> getHosts(IpAddress ip) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530315 Set<Host> hosts = hostsByIp.get(ip);
316 return hosts != null ? ImmutableSet.copyOf(hosts) : ImmutableSet.of();
Madan Jampani38a88212015-09-15 11:21:27 -0700317 }
318
319 @Override
320 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800321 Set<Host> filtered = hosts.entrySet().stream()
Charles Chancd06c692017-04-27 20:46:06 -0700322 .filter(entry -> entry.getValue().locations().contains(connectPoint))
Charles Chan009c3082015-11-10 14:18:04 -0800323 .map(Map.Entry::getValue)
324 .collect(Collectors.toSet());
325 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700326 }
327
328 @Override
329 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800330 Set<Host> filtered = hosts.entrySet().stream()
Charles Chancd06c692017-04-27 20:46:06 -0700331 .filter(entry -> entry.getValue().locations().stream()
332 .map(HostLocation::deviceId).anyMatch(dpid -> dpid.equals(deviceId)))
Charles Chan009c3082015-11-10 14:18:04 -0800333 .map(Map.Entry::getValue)
334 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700335 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700336 }
337
Madan Jampani38a88212015-09-15 11:21:27 -0700338 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
339 return collection.stream().filter(predicate).collect(Collectors.toSet());
340 }
341
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530342 private Set<Host> addHosts(Host host) {
343 Set<Host> hosts = Sets.newConcurrentHashSet();
344 hosts.add(host);
345 return hosts;
346 }
347
348 private Set<Host> updateHosts(Set<Host> existingHosts, Host host) {
349 Iterator<Host> iterator = existingHosts.iterator();
350 while (iterator.hasNext()) {
351 Host existingHost = iterator.next();
352 if (existingHost.id().equals(host.id())) {
353 iterator.remove();
354 }
355 }
356 existingHosts.add(host);
357 return existingHosts;
358 }
359
360 private Set<Host> removeHosts(Set<Host> existingHosts, Host host) {
361 if (existingHosts != null) {
362 Iterator<Host> iterator = existingHosts.iterator();
363 while (iterator.hasNext()) {
364 Host existingHost = iterator.next();
365 if (existingHost.id().equals(host.id())) {
366 iterator.remove();
367 }
368 }
369 }
370
371 if (existingHosts.isEmpty()) {
372 return null;
373 }
374 return existingHosts;
375 }
376
377 private void updateHostsByIp(DefaultHost host) {
378 host.ipAddresses().forEach(ip -> {
379 hostsByIp.compute(ip, (k, v) -> v == null ? addHosts(host)
380 : updateHosts(v, host));
381 });
382 }
383
384 private void removeHostsByIp(DefaultHost host) {
385 host.ipAddresses().forEach(ip -> {
386 hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host));
387 });
388 }
389
390 private void removeIpFromHostsByIp(DefaultHost host, IpAddress ip) {
391 hostsByIp.computeIfPresent(ip, (k, v) -> removeHosts(v, host));
392 }
393
alshabib8a4a6002015-11-25 14:31:16 -0800394 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700395 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800396 public void event(MapEvent<HostId, DefaultHost> event) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530397 DefaultHost host = checkNotNull(event.value().value());
alshabib1400ce92015-12-16 15:05:47 -0800398 switch (event.type()) {
399 case INSERT:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530400 updateHostsByIp(host);
Charles Chan009c3082015-11-10 14:18:04 -0800401 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800402 break;
403 case UPDATE:
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530404 updateHostsByIp(host);
405 DefaultHost prevHost = checkNotNull(event.oldValue().value());
Charles Chancd06c692017-04-27 20:46:06 -0700406 if (!Objects.equals(prevHost.locations(), host.locations())) {
alshabib1400ce92015-12-16 15:05:47 -0800407 notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost));
408 } else if (!Objects.equals(prevHost, host)) {
409 notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost));
410 }
411 break;
412 case REMOVE:
Charles Chan21720342017-05-13 00:19:09 -0700413 removeHostsByIp(host);
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700414 notifyDelegate(new HostEvent(HOST_REMOVED, host));
alshabib1400ce92015-12-16 15:05:47 -0800415 break;
416 default:
417 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700418 }
419 }
420 }
421}