blob: b4449ae85d06e3a4ff1cd68110003cfea61dc1d2 [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()
90 .register(KryoNamespaces.API);
91
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()) ||
125 !Objects.equals(existingHost.location(), hostDescription.location())) {
126 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;
170 if (existingHost != null) {
171 annotations = merge((DefaultAnnotations) existingHost.annotations(),
172 hostDescription.annotations());
173 } else {
174 annotations = hostDescription.annotations();
175 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700176
Madan Jampanic7f49f92015-12-10 11:35:06 -0800177 return new DefaultHost(providerId,
178 hostId,
179 hostDescription.hwAddress(),
180 hostDescription.vlan(),
181 location,
182 addresses,
183 annotations);
184 });
Charles Chan009c3082015-11-10 14:18:04 -0800185 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700186 }
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 removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800191 hosts.remove(hostId);
192 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700193 }
194
Charles Chan009c3082015-11-10 14:18:04 -0800195 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700196 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700197 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800198 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700199 if (existingHost != null) {
200 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
201 "Existing and new MAC addresses differ.");
202 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
203 "Existing and new VLANs differ.");
204
samanwita pale7c08de2015-09-24 21:59:49 -0700205 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700206 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700207 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700208 addresses.remove(ipAddress);
209 return new DefaultHost(existingHost.providerId(),
210 hostId,
211 existingHost.mac(),
212 existingHost.vlan(),
213 existingHost.location(),
214 ImmutableSet.copyOf(addresses),
215 existingHost.annotations());
216 } else {
217 return existingHost;
218 }
219 }
220 return null;
221 });
Charles Chan009c3082015-11-10 14:18:04 -0800222 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700223 }
224
225 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700226 public int getHostCount() {
227 return hosts.size();
228 }
229
230 @Override
231 public Iterable<Host> getHosts() {
232 return ImmutableSet.copyOf(hosts.values());
233 }
234
235 @Override
236 public Host getHost(HostId hostId) {
237 return hosts.get(hostId);
238 }
239
240 @Override
241 public Set<Host> getHosts(VlanId vlanId) {
242 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
243 }
244
245 @Override
246 public Set<Host> getHosts(MacAddress mac) {
247 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
248 }
249
250 @Override
251 public Set<Host> getHosts(IpAddress ip) {
252 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
253 }
254
255 @Override
256 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800257 Set<Host> filtered = hosts.entrySet().stream()
258 .filter(entry -> entry.getValue().location().equals(connectPoint))
259 .map(Map.Entry::getValue)
260 .collect(Collectors.toSet());
261 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700262 }
263
264 @Override
265 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800266 Set<Host> filtered = hosts.entrySet().stream()
267 .filter(entry -> entry.getValue().location().deviceId().equals(deviceId))
268 .map(Map.Entry::getValue)
269 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700270 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700271 }
272
Madan Jampani38a88212015-09-15 11:21:27 -0700273 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
274 return collection.stream().filter(predicate).collect(Collectors.toSet());
275 }
276
alshabib8a4a6002015-11-25 14:31:16 -0800277 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700278 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800279 public void event(MapEvent<HostId, DefaultHost> event) {
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700280 Host host;
alshabib1400ce92015-12-16 15:05:47 -0800281 switch (event.type()) {
282 case INSERT:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700283 host = checkNotNull(event.newValue().value());
Charles Chan009c3082015-11-10 14:18:04 -0800284 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800285 break;
286 case UPDATE:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700287 host = checkNotNull(event.newValue().value());
288 Host prevHost = checkNotNull(event.oldValue().value());
alshabib1400ce92015-12-16 15:05:47 -0800289 if (!Objects.equals(prevHost.location(), host.location())) {
290 notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost));
291 } else if (!Objects.equals(prevHost, host)) {
292 notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost));
293 }
294 break;
295 case REMOVE:
Yuta HIGUCHI215a7e42016-07-20 19:54:06 -0700296 host = checkNotNull(event.oldValue().value());
297 notifyDelegate(new HostEvent(HOST_REMOVED, host));
alshabib1400ce92015-12-16 15:05:47 -0800298 break;
299 default:
300 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700301 }
302 }
303 }
304}