blob: f9c96891576369743b1e01c13d4dc3760f91bd55 [file] [log] [blame]
Madan Jampani38a88212015-09-15 11:21:27 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorf107bd72015-09-21 15:31:03 -070019import static com.google.common.base.Preconditions.checkState;
Madan Jampani38a88212015-09-15 11:21:27 -070020import static org.onosproject.net.DefaultAnnotations.merge;
21import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
22import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
Brian O'Connorf107bd72015-09-21 15:31:03 -070023import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
Madan Jampani38a88212015-09-15 11:21:27 -070024import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
25import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
26import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
27import static org.slf4j.LoggerFactory.getLogger;
28
29import java.util.Collection;
samanwita palc40e5ed2015-09-24 11:01:51 -070030import java.util.HashSet;
Madan Jampani38a88212015-09-15 11:21:27 -070031import java.util.Objects;
32import java.util.Set;
Brian O'Connorf107bd72015-09-21 15:31:03 -070033import java.util.concurrent.atomic.AtomicReference;
Madan Jampani38a88212015-09-15 11:21:27 -070034import java.util.function.Predicate;
35import java.util.stream.Collectors;
36
37import org.apache.felix.scr.annotations.Activate;
38import org.apache.felix.scr.annotations.Component;
39import org.apache.felix.scr.annotations.Deactivate;
40import org.apache.felix.scr.annotations.Reference;
41import org.apache.felix.scr.annotations.ReferenceCardinality;
42import org.apache.felix.scr.annotations.Service;
43import org.onlab.packet.IpAddress;
44import org.onlab.packet.MacAddress;
45import org.onlab.packet.VlanId;
46import org.onlab.util.KryoNamespace;
47import org.onosproject.net.Annotations;
48import org.onosproject.net.ConnectPoint;
49import org.onosproject.net.DefaultAnnotations;
50import org.onosproject.net.DefaultHost;
51import org.onosproject.net.DeviceId;
52import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
Brian O'Connorf107bd72015-09-21 15:31:03 -070054import org.onosproject.net.HostLocation;
Madan Jampani38a88212015-09-15 11:21:27 -070055import org.onosproject.net.host.HostDescription;
56import org.onosproject.net.host.HostEvent;
57import org.onosproject.net.host.HostStore;
58import org.onosproject.net.host.HostStoreDelegate;
Madan Jampani38a88212015-09-15 11:21:27 -070059import org.onosproject.net.host.HostEvent.Type;
60import org.onosproject.net.provider.ProviderId;
61import org.onosproject.store.AbstractStore;
62import org.onosproject.store.serializers.KryoNamespaces;
63import org.onosproject.store.service.EventuallyConsistentMap;
64import org.onosproject.store.service.EventuallyConsistentMapEvent;
65import org.onosproject.store.service.EventuallyConsistentMapListener;
66import org.onosproject.store.service.LogicalClockService;
67import org.onosproject.store.service.StorageService;
68import org.slf4j.Logger;
69
70import com.google.common.collect.HashMultimap;
Madan Jampani38a88212015-09-15 11:21:27 -070071import com.google.common.collect.ImmutableSet;
72import com.google.common.collect.Multimaps;
73import com.google.common.collect.SetMultimap;
74import com.google.common.collect.Sets;
75
76/**
77 * Manages the inventory of hosts using a {@code EventuallyConsistentMap}.
78 */
79@Component(immediate = true)
80@Service
81public class ECHostStore
82 extends AbstractStore<HostEvent, HostStoreDelegate>
83 implements HostStore {
84
85 private final Logger log = getLogger(getClass());
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected StorageService storageService;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected LogicalClockService clockService;
92
93 // Hosts tracked by their location
94 private final SetMultimap<ConnectPoint, Host> locations =
95 Multimaps.synchronizedSetMultimap(
96 HashMultimap.<ConnectPoint, Host>create());
97
Madan Jampani38a88212015-09-15 11:21:27 -070098 private EventuallyConsistentMap<HostId, DefaultHost> hosts;
99
100 private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker =
101 new HostLocationTracker();
102
103 @Activate
104 public void activate() {
105 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
106 .register(KryoNamespaces.API);
107
108 hosts = storageService.<HostId, DefaultHost>eventuallyConsistentMapBuilder()
109 .withName("onos-hosts")
110 .withSerializer(hostSerializer)
111 .withTimestampProvider((k, v) -> clockService.getTimestamp())
112 .build();
113
114 hosts.addListener(hostLocationTracker);
115
116 log.info("Started");
117 }
118
119 @Deactivate
120 public void deactivate() {
121 hosts.removeListener(hostLocationTracker);
122 hosts.destroy();
123 locations.clear();
Madan Jampani38a88212015-09-15 11:21:27 -0700124
125 log.info("Stopped");
126 }
127
128 @Override
129 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700130 HostId hostId,
131 HostDescription hostDescription,
132 boolean replaceIPs) {
133 // TODO: We need a way to detect conflicting changes and abort update.
134 // (BOC) Compute might do this for us.
135
136 final AtomicReference<Type> eventType = new AtomicReference<>();
137 final AtomicReference<DefaultHost> oldHost = new AtomicReference<>();
138 DefaultHost host = hosts.compute(hostId, (id, existingHost) -> {
139 if (existingHost != null) {
140 oldHost.set(existingHost);
141 checkState(Objects.equals(hostDescription.hwAddress(), existingHost.mac()),
142 "Existing and new MAC addresses differ.");
143 checkState(Objects.equals(hostDescription.vlan(), existingHost.vlan()),
144 "Existing and new VLANs differ.");
145 }
146
147 // TODO do we ever want the existing location?
148 HostLocation location = hostDescription.location();
149
150 final Set<IpAddress> addresses;
151 if (existingHost == null || replaceIPs) {
152 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
153 } else {
154 addresses = Sets.newHashSet(existingHost.ipAddresses());
155 addresses.addAll(hostDescription.ipAddress());
156 }
157
158 final Annotations annotations;
159 if (existingHost != null) {
160 annotations = merge((DefaultAnnotations) existingHost.annotations(),
161 hostDescription.annotations());
162 } else {
163 annotations = hostDescription.annotations();
164 }
165
166 if (existingHost == null) {
167 eventType.set(HOST_ADDED);
168 } else if (!Objects.equals(existingHost.location(), hostDescription.location())) {
169 eventType.set(HOST_MOVED);
170 } else if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress()) ||
171 !hostDescription.annotations().keys().isEmpty()) {
172 eventType.set(HOST_UPDATED);
173 } // else, eventType == null; this means we don't send an event
174
175 return new DefaultHost(providerId,
176 hostId,
177 hostDescription.hwAddress(),
178 hostDescription.vlan(),
179 location,
180 addresses,
181 annotations);
182 });
183
184 if (oldHost.get() != null) {
185 DefaultHost old = oldHost.get();
186 locations.remove(old.location(), old);
Madan Jampani38a88212015-09-15 11:21:27 -0700187 }
Brian O'Connorf107bd72015-09-21 15:31:03 -0700188 locations.put(host.location(), host);
189
190 return eventType.get() != null ? new HostEvent(eventType.get(), host) : null;
Madan Jampani38a88212015-09-15 11:21:27 -0700191 }
192
193 @Override
194 public HostEvent removeHost(HostId hostId) {
195 Host host = hosts.remove(hostId);
196 return host != null ? new HostEvent(HOST_REMOVED, host) : null;
197 }
198
199 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700200 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
201 DefaultHost host = hosts.compute(hostId, (id, existingHost) -> {
202 if (existingHost != null) {
203 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
204 "Existing and new MAC addresses differ.");
205 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
206 "Existing and new VLANs differ.");
207
samanwita pale7c08de2015-09-24 21:59:49 -0700208 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700209 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700210 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700211 addresses.remove(ipAddress);
212 return new DefaultHost(existingHost.providerId(),
213 hostId,
214 existingHost.mac(),
215 existingHost.vlan(),
216 existingHost.location(),
217 ImmutableSet.copyOf(addresses),
218 existingHost.annotations());
219 } else {
220 return existingHost;
221 }
222 }
223 return null;
224 });
225 return host != null ? new HostEvent(HOST_UPDATED, host) : null;
226 }
227
228 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700229 public int getHostCount() {
230 return hosts.size();
231 }
232
233 @Override
234 public Iterable<Host> getHosts() {
235 return ImmutableSet.copyOf(hosts.values());
236 }
237
238 @Override
239 public Host getHost(HostId hostId) {
240 return hosts.get(hostId);
241 }
242
243 @Override
244 public Set<Host> getHosts(VlanId vlanId) {
245 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
246 }
247
248 @Override
249 public Set<Host> getHosts(MacAddress mac) {
250 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
251 }
252
253 @Override
254 public Set<Host> getHosts(IpAddress ip) {
255 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
256 }
257
258 @Override
259 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700260 synchronized (locations) {
261 return ImmutableSet.copyOf(locations.get(connectPoint));
262 }
Madan Jampani38a88212015-09-15 11:21:27 -0700263 }
264
265 @Override
266 public Set<Host> getConnectedHosts(DeviceId deviceId) {
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700267 Set<Host> filtered;
268 synchronized (locations) {
269 filtered = locations
270 .entries()
271 .stream()
272 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
273 .map(entry -> entry.getValue())
274 .collect(Collectors.toSet());
275 }
276 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
Madan Jampani38a88212015-09-15 11:21:27 -0700283 private class HostLocationTracker implements EventuallyConsistentMapListener<HostId, DefaultHost> {
284 @Override
285 public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) {
286 DefaultHost host = checkNotNull(event.value());
287 if (event.type() == PUT) {
288 boolean isNew = locations.put(host.location(), host);
289 notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host));
290 } else if (event.type() == REMOVE) {
291 if (locations.remove(host.location(), host)) {
292 notifyDelegate(new HostEvent(HOST_REMOVED, host));
293 }
294
295 }
296 }
297 }
298}