blob: 356df9c779667936f340987b29d897dcf1be2b56 [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;
51import org.slf4j.Logger;
52
alshabib8a4a6002015-11-25 14:31:16 -080053import java.util.Collection;
54import java.util.HashSet;
55import java.util.Map;
56import java.util.Objects;
57import java.util.Set;
alshabib8a4a6002015-11-25 14:31:16 -080058import java.util.function.Predicate;
59import java.util.stream.Collectors;
60
61import static com.google.common.base.Preconditions.checkNotNull;
62import static com.google.common.base.Preconditions.checkState;
63import static org.onosproject.net.DefaultAnnotations.merge;
64import static org.onosproject.net.host.HostEvent.Type.*;
65import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani38a88212015-09-15 11:21:27 -070066
67/**
68 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
69 */
70@Component(immediate = true)
71@Service
alshabib8a4a6002015-11-25 14:31:16 -080072public class DistributedHostStore
Madan Jampani38a88212015-09-15 11:21:27 -070073 extends AbstractStore<HostEvent, HostStoreDelegate>
74 implements HostStore {
75
76 private final Logger log = getLogger(getClass());
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected StorageService storageService;
80
Madan Jampanic6371882016-06-03 21:30:17 -070081 private ConsistentMap<HostId, DefaultHost> hostsConsistentMap;
alshabib8a4a6002015-11-25 14:31:16 -080082 private Map<HostId, DefaultHost> hosts;
Madan Jampani38a88212015-09-15 11:21:27 -070083
alshabib8a4a6002015-11-25 14:31:16 -080084 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070085 new HostLocationTracker();
86
87 @Activate
88 public void activate() {
89 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
Jonathan Hart38feb6e2016-08-29 22:54:16 +000090 .register(KryoNamespaces.API);
Madan Jampani38a88212015-09-15 11:21:27 -070091
Madan Jampanic6371882016-06-03 21:30:17 -070092 hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -070093 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -080094 .withRelaxedReadConsistency()
95 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -070096 .build();
97
Madan Jampanic6371882016-06-03 21:30:17 -070098 hosts = hostsConsistentMap.asJavaMap();
alshabib8a4a6002015-11-25 14:31:16 -080099
alshabib1400ce92015-12-16 15:05:47 -0800100
Madan Jampanic6371882016-06-03 21:30:17 -0700101 hostsConsistentMap.addListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700102
103 log.info("Started");
104 }
105
106 @Deactivate
107 public void deactivate() {
Madan Jampanic6371882016-06-03 21:30:17 -0700108 hostsConsistentMap.removeListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700109
110 log.info("Stopped");
111 }
112
Brian O'Connordab09742015-12-07 20:06:29 -0800113 private boolean shouldUpdate(DefaultHost existingHost,
114 ProviderId providerId,
115 HostId hostId,
116 HostDescription hostDescription,
117 boolean replaceIPs) {
118 if (existingHost == null) {
119 return true;
120 }
121
122 if (!Objects.equals(existingHost.providerId(), providerId) ||
123 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
124 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000125 !Objects.equals(existingHost.location(), hostDescription.location())) {
Brian O'Connordab09742015-12-07 20:06:29 -0800126 return true;
127 }
128
129 if (replaceIPs) {
130 if (!Objects.equals(hostDescription.ipAddress(),
131 existingHost.ipAddresses())) {
132 return true;
133 }
134 } else {
135 if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
136 return true;
137 }
138 }
139
140 // check to see if any of the annotations provided by hostDescription
141 // differ from those in the existing host
142 return hostDescription.annotations().keys().stream()
143 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
144 existingHost.annotations().value(k)));
145
146
147 }
148
Charles Chan009c3082015-11-10 14:18:04 -0800149 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700150 @Override
151 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700152 HostId hostId,
153 HostDescription hostDescription,
154 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700155 hostsConsistentMap.computeIf(hostId,
Brian O'Connordab09742015-12-07 20:06:29 -0800156 existingHost -> shouldUpdate(existingHost, providerId, hostId,
157 hostDescription, replaceIPs),
158 (id, existingHost) -> {
Madan Jampanic7f49f92015-12-10 11:35:06 -0800159 HostLocation location = hostDescription.location();
Brian O'Connorf107bd72015-09-21 15:31:03 -0700160
Madan Jampanic7f49f92015-12-10 11:35:06 -0800161 final Set<IpAddress> addresses;
162 if (existingHost == null || replaceIPs) {
163 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
164 } else {
165 addresses = Sets.newHashSet(existingHost.ipAddresses());
166 addresses.addAll(hostDescription.ipAddress());
167 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700168
Madan Jampanic7f49f92015-12-10 11:35:06 -0800169 final Annotations annotations;
sdn5e935452016-08-30 04:12:54 -0700170 final boolean configured;
Madan Jampanic7f49f92015-12-10 11:35:06 -0800171 if (existingHost != null) {
172 annotations = merge((DefaultAnnotations) existingHost.annotations(),
173 hostDescription.annotations());
sdn5e935452016-08-30 04:12:54 -0700174 configured = existingHost.configured();
Madan Jampanic7f49f92015-12-10 11:35:06 -0800175 } else {
176 annotations = hostDescription.annotations();
sdn5e935452016-08-30 04:12:54 -0700177 configured = hostDescription.configured();
Madan Jampanic7f49f92015-12-10 11:35:06 -0800178 }
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000179
Madan Jampanic7f49f92015-12-10 11:35:06 -0800180 return new DefaultHost(providerId,
181 hostId,
182 hostDescription.hwAddress(),
183 hostDescription.vlan(),
184 location,
185 addresses,
sdn5e935452016-08-30 04:12:54 -0700186 configured,
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}