blob: a3598623746ac1b0092f7eb058b9bdfe35a37560 [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 Vaddireddy50ad0802016-08-08 19:27:36 +053051import org.onosproject.store.service.WallClockTimestamp;
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;
56import java.util.Map;
57import java.util.Objects;
58import java.util.Set;
alshabib8a4a6002015-11-25 14:31:16 -080059import java.util.function.Predicate;
60import java.util.stream.Collectors;
61
62import static com.google.common.base.Preconditions.checkNotNull;
63import static com.google.common.base.Preconditions.checkState;
64import static org.onosproject.net.DefaultAnnotations.merge;
65import static org.onosproject.net.host.HostEvent.Type.*;
66import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani38a88212015-09-15 11:21:27 -070067
68/**
69 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
70 */
71@Component(immediate = true)
72@Service
alshabib8a4a6002015-11-25 14:31:16 -080073public class DistributedHostStore
Madan Jampani38a88212015-09-15 11:21:27 -070074 extends AbstractStore<HostEvent, HostStoreDelegate>
75 implements HostStore {
76
77 private final Logger log = getLogger(getClass());
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected StorageService storageService;
81
Madan Jampanic6371882016-06-03 21:30:17 -070082 private ConsistentMap<HostId, DefaultHost> hostsConsistentMap;
alshabib8a4a6002015-11-25 14:31:16 -080083 private Map<HostId, DefaultHost> hosts;
Madan Jampani38a88212015-09-15 11:21:27 -070084
alshabib8a4a6002015-11-25 14:31:16 -080085 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070086 new HostLocationTracker();
87
88 @Activate
89 public void activate() {
90 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053091 .register(KryoNamespaces.API)
92 .register(WallClockTimestamp.class);
Madan Jampani38a88212015-09-15 11:21:27 -070093
Madan Jampanic6371882016-06-03 21:30:17 -070094 hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -070095 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -080096 .withRelaxedReadConsistency()
97 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -070098 .build();
99
Madan Jampanic6371882016-06-03 21:30:17 -0700100 hosts = hostsConsistentMap.asJavaMap();
alshabib8a4a6002015-11-25 14:31:16 -0800101
alshabib1400ce92015-12-16 15:05:47 -0800102
Madan Jampanic6371882016-06-03 21:30:17 -0700103 hostsConsistentMap.addListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700104
105 log.info("Started");
106 }
107
108 @Deactivate
109 public void deactivate() {
Madan Jampanic6371882016-06-03 21:30:17 -0700110 hostsConsistentMap.removeListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700111
112 log.info("Stopped");
113 }
114
Brian O'Connordab09742015-12-07 20:06:29 -0800115 private boolean shouldUpdate(DefaultHost existingHost,
116 ProviderId providerId,
117 HostId hostId,
118 HostDescription hostDescription,
119 boolean replaceIPs) {
120 if (existingHost == null) {
121 return true;
122 }
123
124 if (!Objects.equals(existingHost.providerId(), providerId) ||
125 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
126 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530127 !Objects.equals(existingHost.location(), hostDescription.location()) ||
128 hostDescription.timestamp() == null ||
129 hostDescription.timestamp().isNewerThan(existingHost.timestamp())) {
Brian O'Connordab09742015-12-07 20:06:29 -0800130 return true;
131 }
132
133 if (replaceIPs) {
134 if (!Objects.equals(hostDescription.ipAddress(),
135 existingHost.ipAddresses())) {
136 return true;
137 }
138 } else {
139 if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
140 return true;
141 }
142 }
143
144 // check to see if any of the annotations provided by hostDescription
145 // differ from those in the existing host
146 return hostDescription.annotations().keys().stream()
147 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
148 existingHost.annotations().value(k)));
149
150
151 }
152
Charles Chan009c3082015-11-10 14:18:04 -0800153 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700154 @Override
155 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700156 HostId hostId,
157 HostDescription hostDescription,
158 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700159 hostsConsistentMap.computeIf(hostId,
Brian O'Connordab09742015-12-07 20:06:29 -0800160 existingHost -> shouldUpdate(existingHost, providerId, hostId,
161 hostDescription, replaceIPs),
162 (id, existingHost) -> {
Madan Jampanic7f49f92015-12-10 11:35:06 -0800163 HostLocation location = hostDescription.location();
Brian O'Connorf107bd72015-09-21 15:31:03 -0700164
Madan Jampanic7f49f92015-12-10 11:35:06 -0800165 final Set<IpAddress> addresses;
166 if (existingHost == null || replaceIPs) {
167 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
168 } else {
169 addresses = Sets.newHashSet(existingHost.ipAddresses());
170 addresses.addAll(hostDescription.ipAddress());
171 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700172
Madan Jampanic7f49f92015-12-10 11:35:06 -0800173 final Annotations annotations;
174 if (existingHost != null) {
175 annotations = merge((DefaultAnnotations) existingHost.annotations(),
176 hostDescription.annotations());
177 } else {
178 annotations = hostDescription.annotations();
179 }
Madan Jampanic7f49f92015-12-10 11:35:06 -0800180 return new DefaultHost(providerId,
181 hostId,
182 hostDescription.hwAddress(),
183 hostDescription.vlan(),
184 location,
185 addresses,
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530186 hostDescription.timestamp(),
Madan Jampanic7f49f92015-12-10 11:35:06 -0800187 annotations);
188 });
Charles Chan009c3082015-11-10 14:18:04 -0800189 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700190 }
191
Charles Chan009c3082015-11-10 14:18:04 -0800192 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700193 @Override
194 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800195 hosts.remove(hostId);
196 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700197 }
198
Charles Chan009c3082015-11-10 14:18:04 -0800199 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700200 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700201 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800202 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700203 if (existingHost != null) {
204 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
205 "Existing and new MAC addresses differ.");
206 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
207 "Existing and new VLANs differ.");
208
samanwita pale7c08de2015-09-24 21:59:49 -0700209 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700210 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700211 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700212 addresses.remove(ipAddress);
213 return new DefaultHost(existingHost.providerId(),
214 hostId,
215 existingHost.mac(),
216 existingHost.vlan(),
217 existingHost.location(),
218 ImmutableSet.copyOf(addresses),
219 existingHost.annotations());
220 } else {
221 return existingHost;
222 }
223 }
224 return null;
225 });
Charles Chan009c3082015-11-10 14:18:04 -0800226 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700227 }
228
229 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700230 public int getHostCount() {
231 return hosts.size();
232 }
233
234 @Override
235 public Iterable<Host> getHosts() {
236 return ImmutableSet.copyOf(hosts.values());
237 }
238
239 @Override
240 public Host getHost(HostId hostId) {
241 return hosts.get(hostId);
242 }
243
244 @Override
245 public Set<Host> getHosts(VlanId vlanId) {
246 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
247 }
248
249 @Override
250 public Set<Host> getHosts(MacAddress mac) {
251 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
252 }
253
254 @Override
255 public Set<Host> getHosts(IpAddress ip) {
256 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
257 }
258
259 @Override
260 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800261 Set<Host> filtered = hosts.entrySet().stream()
262 .filter(entry -> entry.getValue().location().equals(connectPoint))
263 .map(Map.Entry::getValue)
264 .collect(Collectors.toSet());
265 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700266 }
267
268 @Override
269 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800270 Set<Host> filtered = hosts.entrySet().stream()
271 .filter(entry -> entry.getValue().location().deviceId().equals(deviceId))
272 .map(Map.Entry::getValue)
273 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700274 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700275 }
276
Madan Jampani38a88212015-09-15 11:21:27 -0700277 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
278 return collection.stream().filter(predicate).collect(Collectors.toSet());
279 }
280
alshabib8a4a6002015-11-25 14:31:16 -0800281 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700282 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800283 public void event(MapEvent<HostId, DefaultHost> event) {
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700284 Host host;
alshabib1400ce92015-12-16 15:05:47 -0800285 switch (event.type()) {
286 case INSERT:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700287 host = checkNotNull(event.newValue().value());
Charles Chan009c3082015-11-10 14:18:04 -0800288 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800289 break;
290 case UPDATE:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700291 host = checkNotNull(event.newValue().value());
292 Host prevHost = checkNotNull(event.oldValue().value());
alshabib1400ce92015-12-16 15:05:47 -0800293 if (!Objects.equals(prevHost.location(), host.location())) {
294 notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost));
295 } else if (!Objects.equals(prevHost, host)) {
296 notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost));
297 }
298 break;
299 case REMOVE:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700300 host = checkNotNull(event.oldValue().value());
301 notifyDelegate(new HostEvent(HOST_REMOVED, host));
alshabib1400ce92015-12-16 15:05:47 -0800302 break;
303 default:
304 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700305 }
306 }
307 }
308}
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530309