blob: 0e69e8e10af2215400de411e73c9069ba476cbdc [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;
58import java.util.concurrent.ConcurrentHashMap;
59import 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
Charles Chan33f28a92015-11-13 13:12:38 -080085 private final ConcurrentHashMap<HostId, DefaultHost> prevHosts =
Charles Chan009c3082015-11-10 14:18:04 -080086 new ConcurrentHashMap<>();
87
alshabib8a4a6002015-11-25 14:31:16 -080088 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070089 new HostLocationTracker();
90
91 @Activate
92 public void activate() {
93 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
94 .register(KryoNamespaces.API);
95
Madan Jampanic6371882016-06-03 21:30:17 -070096 hostsConsistentMap = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -070097 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -080098 .withRelaxedReadConsistency()
99 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -0700100 .build();
101
Madan Jampanic6371882016-06-03 21:30:17 -0700102 hosts = hostsConsistentMap.asJavaMap();
alshabib8a4a6002015-11-25 14:31:16 -0800103
alshabib1400ce92015-12-16 15:05:47 -0800104 prevHosts.putAll(hosts);
105
Madan Jampanic6371882016-06-03 21:30:17 -0700106 hostsConsistentMap.addListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700107
108 log.info("Started");
109 }
110
111 @Deactivate
112 public void deactivate() {
Madan Jampanic6371882016-06-03 21:30:17 -0700113 hostsConsistentMap.removeListener(hostLocationTracker);
Charles Chan33f28a92015-11-13 13:12:38 -0800114 prevHosts.clear();
Madan Jampani38a88212015-09-15 11:21:27 -0700115
116 log.info("Stopped");
117 }
118
Brian O'Connordab09742015-12-07 20:06:29 -0800119 private boolean shouldUpdate(DefaultHost existingHost,
120 ProviderId providerId,
121 HostId hostId,
122 HostDescription hostDescription,
123 boolean replaceIPs) {
124 if (existingHost == null) {
125 return true;
126 }
127
128 if (!Objects.equals(existingHost.providerId(), providerId) ||
129 !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
130 !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
131 !Objects.equals(existingHost.location(), hostDescription.location())) {
132 return true;
133 }
134
135 if (replaceIPs) {
136 if (!Objects.equals(hostDescription.ipAddress(),
137 existingHost.ipAddresses())) {
138 return true;
139 }
140 } else {
141 if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
142 return true;
143 }
144 }
145
146 // check to see if any of the annotations provided by hostDescription
147 // differ from those in the existing host
148 return hostDescription.annotations().keys().stream()
149 .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
150 existingHost.annotations().value(k)));
151
152
153 }
154
Charles Chan009c3082015-11-10 14:18:04 -0800155 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700156 @Override
157 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700158 HostId hostId,
159 HostDescription hostDescription,
160 boolean replaceIPs) {
Madan Jampanic6371882016-06-03 21:30:17 -0700161 hostsConsistentMap.computeIf(hostId,
Brian O'Connordab09742015-12-07 20:06:29 -0800162 existingHost -> shouldUpdate(existingHost, providerId, hostId,
163 hostDescription, replaceIPs),
164 (id, existingHost) -> {
Madan Jampanic7f49f92015-12-10 11:35:06 -0800165 HostLocation location = hostDescription.location();
Brian O'Connorf107bd72015-09-21 15:31:03 -0700166
Madan Jampanic7f49f92015-12-10 11:35:06 -0800167 final Set<IpAddress> addresses;
168 if (existingHost == null || replaceIPs) {
169 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
170 } else {
171 addresses = Sets.newHashSet(existingHost.ipAddresses());
172 addresses.addAll(hostDescription.ipAddress());
173 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700174
Madan Jampanic7f49f92015-12-10 11:35:06 -0800175 final Annotations annotations;
176 if (existingHost != null) {
177 annotations = merge((DefaultAnnotations) existingHost.annotations(),
178 hostDescription.annotations());
179 } else {
180 annotations = hostDescription.annotations();
181 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700182
Madan Jampanic7f49f92015-12-10 11:35:06 -0800183 return new DefaultHost(providerId,
184 hostId,
185 hostDescription.hwAddress(),
186 hostDescription.vlan(),
187 location,
188 addresses,
189 annotations);
190 });
Charles Chan009c3082015-11-10 14:18:04 -0800191 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700192 }
193
Charles Chan009c3082015-11-10 14:18:04 -0800194 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700195 @Override
196 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800197 hosts.remove(hostId);
198 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700199 }
200
Charles Chan009c3082015-11-10 14:18:04 -0800201 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700202 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700203 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800204 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700205 if (existingHost != null) {
206 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
207 "Existing and new MAC addresses differ.");
208 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
209 "Existing and new VLANs differ.");
210
samanwita pale7c08de2015-09-24 21:59:49 -0700211 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700212 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700213 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700214 addresses.remove(ipAddress);
215 return new DefaultHost(existingHost.providerId(),
216 hostId,
217 existingHost.mac(),
218 existingHost.vlan(),
219 existingHost.location(),
220 ImmutableSet.copyOf(addresses),
221 existingHost.annotations());
222 } else {
223 return existingHost;
224 }
225 }
226 return null;
227 });
Charles Chan009c3082015-11-10 14:18:04 -0800228 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700229 }
230
231 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700232 public int getHostCount() {
233 return hosts.size();
234 }
235
236 @Override
237 public Iterable<Host> getHosts() {
238 return ImmutableSet.copyOf(hosts.values());
239 }
240
241 @Override
242 public Host getHost(HostId hostId) {
243 return hosts.get(hostId);
244 }
245
246 @Override
247 public Set<Host> getHosts(VlanId vlanId) {
248 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
249 }
250
251 @Override
252 public Set<Host> getHosts(MacAddress mac) {
253 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
254 }
255
256 @Override
257 public Set<Host> getHosts(IpAddress ip) {
258 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
259 }
260
261 @Override
262 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800263 Set<Host> filtered = hosts.entrySet().stream()
264 .filter(entry -> entry.getValue().location().equals(connectPoint))
265 .map(Map.Entry::getValue)
266 .collect(Collectors.toSet());
267 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700268 }
269
270 @Override
271 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800272 Set<Host> filtered = hosts.entrySet().stream()
273 .filter(entry -> entry.getValue().location().deviceId().equals(deviceId))
274 .map(Map.Entry::getValue)
275 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700276 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700277 }
278
Madan Jampani38a88212015-09-15 11:21:27 -0700279 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
280 return collection.stream().filter(predicate).collect(Collectors.toSet());
281 }
282
alshabib8a4a6002015-11-25 14:31:16 -0800283 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700284 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800285 public void event(MapEvent<HostId, DefaultHost> event) {
286 DefaultHost host = checkNotNull(event.value().value());
alshabib1400ce92015-12-16 15:05:47 -0800287 Host prevHost = prevHosts.put(host.id(), host);
288 switch (event.type()) {
289 case INSERT:
Charles Chan009c3082015-11-10 14:18:04 -0800290 notifyDelegate(new HostEvent(HOST_ADDED, host));
alshabib1400ce92015-12-16 15:05:47 -0800291 break;
292 case UPDATE:
293 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:
300 if (prevHosts.remove(host.id()) != null) {
301 notifyDelegate(new HostEvent(HOST_REMOVED, host));
302 }
303 break;
304 default:
305 log.warn("Unknown map event type: {}", event.type());
Madan Jampani38a88212015-09-15 11:21:27 -0700306 }
307 }
308 }
309}