blob: 836a3c229ce41633594d07667a490c0d927617d8 [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
alshabib8a4a6002015-11-25 14:31:16 -080018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
Madan Jampani38a88212015-09-15 11:21:27 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.net.Annotations;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.DefaultHost;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Host;
36import org.onosproject.net.HostId;
Brian O'Connorf107bd72015-09-21 15:31:03 -070037import org.onosproject.net.HostLocation;
Madan Jampani38a88212015-09-15 11:21:27 -070038import org.onosproject.net.host.HostDescription;
39import org.onosproject.net.host.HostEvent;
40import org.onosproject.net.host.HostStore;
41import org.onosproject.net.host.HostStoreDelegate;
Madan Jampani38a88212015-09-15 11:21:27 -070042import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.AbstractStore;
44import org.onosproject.store.serializers.KryoNamespaces;
alshabib8a4a6002015-11-25 14:31:16 -080045import org.onosproject.store.service.ConsistentMap;
46import org.onosproject.store.service.MapEvent;
47import org.onosproject.store.service.MapEventListener;
48import org.onosproject.store.service.Serializer;
Madan Jampani38a88212015-09-15 11:21:27 -070049import org.onosproject.store.service.StorageService;
50import org.slf4j.Logger;
51
alshabib8a4a6002015-11-25 14:31:16 -080052import java.util.Collection;
53import java.util.HashSet;
54import java.util.Map;
55import java.util.Objects;
56import java.util.Set;
57import java.util.concurrent.ConcurrentHashMap;
58import 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
alshabib8a4a6002015-11-25 14:31:16 -080081 private ConsistentMap<HostId, DefaultHost> host;
82 private Map<HostId, DefaultHost> hosts;
Madan Jampani38a88212015-09-15 11:21:27 -070083
Charles Chan33f28a92015-11-13 13:12:38 -080084 private final ConcurrentHashMap<HostId, DefaultHost> prevHosts =
Charles Chan009c3082015-11-10 14:18:04 -080085 new ConcurrentHashMap<>();
86
alshabib8a4a6002015-11-25 14:31:16 -080087 private MapEventListener<HostId, DefaultHost> hostLocationTracker =
Madan Jampani38a88212015-09-15 11:21:27 -070088 new HostLocationTracker();
89
90 @Activate
91 public void activate() {
92 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
93 .register(KryoNamespaces.API);
94
alshabib8a4a6002015-11-25 14:31:16 -080095 host = storageService.<HostId, DefaultHost>consistentMapBuilder()
Madan Jampani38a88212015-09-15 11:21:27 -070096 .withName("onos-hosts")
alshabib8a4a6002015-11-25 14:31:16 -080097 .withRelaxedReadConsistency()
98 .withSerializer(Serializer.using(hostSerializer.build()))
Madan Jampani38a88212015-09-15 11:21:27 -070099 .build();
100
alshabib8a4a6002015-11-25 14:31:16 -0800101 hosts = host.asJavaMap();
102
103 host.addListener(hostLocationTracker);
Madan Jampani38a88212015-09-15 11:21:27 -0700104
105 log.info("Started");
106 }
107
108 @Deactivate
109 public void deactivate() {
alshabib8a4a6002015-11-25 14:31:16 -0800110 host.removeListener(hostLocationTracker);
Charles Chan33f28a92015-11-13 13:12:38 -0800111 prevHosts.clear();
Madan Jampani38a88212015-09-15 11:21:27 -0700112
113 log.info("Stopped");
114 }
115
Charles Chan009c3082015-11-10 14:18:04 -0800116 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700117 @Override
118 public HostEvent createOrUpdateHost(ProviderId providerId,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700119 HostId hostId,
120 HostDescription hostDescription,
121 boolean replaceIPs) {
122 // TODO: We need a way to detect conflicting changes and abort update.
123 // (BOC) Compute might do this for us.
124
Charles Chan009c3082015-11-10 14:18:04 -0800125 hosts.compute(hostId, (id, existingHost) -> {
Brian O'Connorf107bd72015-09-21 15:31:03 -0700126 HostLocation location = hostDescription.location();
127
128 final Set<IpAddress> addresses;
129 if (existingHost == null || replaceIPs) {
130 addresses = ImmutableSet.copyOf(hostDescription.ipAddress());
131 } else {
132 addresses = Sets.newHashSet(existingHost.ipAddresses());
133 addresses.addAll(hostDescription.ipAddress());
134 }
135
136 final Annotations annotations;
137 if (existingHost != null) {
138 annotations = merge((DefaultAnnotations) existingHost.annotations(),
139 hostDescription.annotations());
140 } else {
141 annotations = hostDescription.annotations();
142 }
143
Brian O'Connorf107bd72015-09-21 15:31:03 -0700144 return new DefaultHost(providerId,
145 hostId,
146 hostDescription.hwAddress(),
147 hostDescription.vlan(),
148 location,
149 addresses,
150 annotations);
151 });
152
Charles Chan009c3082015-11-10 14:18:04 -0800153 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700154 }
155
Charles Chan009c3082015-11-10 14:18:04 -0800156 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700157 @Override
158 public HostEvent removeHost(HostId hostId) {
Charles Chan009c3082015-11-10 14:18:04 -0800159 hosts.remove(hostId);
160 return null;
Madan Jampani38a88212015-09-15 11:21:27 -0700161 }
162
Charles Chan009c3082015-11-10 14:18:04 -0800163 // TODO No longer need to return HostEvent
Madan Jampani38a88212015-09-15 11:21:27 -0700164 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700165 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan009c3082015-11-10 14:18:04 -0800166 hosts.compute(hostId, (id, existingHost) -> {
samanwita palc40e5ed2015-09-24 11:01:51 -0700167 if (existingHost != null) {
168 checkState(Objects.equals(hostId.mac(), existingHost.mac()),
169 "Existing and new MAC addresses differ.");
170 checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
171 "Existing and new VLANs differ.");
172
samanwita pale7c08de2015-09-24 21:59:49 -0700173 Set<IpAddress> addresses = existingHost.ipAddresses();
samanwita palc40e5ed2015-09-24 11:01:51 -0700174 if (addresses != null && addresses.contains(ipAddress)) {
samanwita pale7c08de2015-09-24 21:59:49 -0700175 addresses = new HashSet<>(existingHost.ipAddresses());
samanwita palc40e5ed2015-09-24 11:01:51 -0700176 addresses.remove(ipAddress);
177 return new DefaultHost(existingHost.providerId(),
178 hostId,
179 existingHost.mac(),
180 existingHost.vlan(),
181 existingHost.location(),
182 ImmutableSet.copyOf(addresses),
183 existingHost.annotations());
184 } else {
185 return existingHost;
186 }
187 }
188 return null;
189 });
Charles Chan009c3082015-11-10 14:18:04 -0800190 return null;
samanwita palc40e5ed2015-09-24 11:01:51 -0700191 }
192
193 @Override
Madan Jampani38a88212015-09-15 11:21:27 -0700194 public int getHostCount() {
195 return hosts.size();
196 }
197
198 @Override
199 public Iterable<Host> getHosts() {
200 return ImmutableSet.copyOf(hosts.values());
201 }
202
203 @Override
204 public Host getHost(HostId hostId) {
205 return hosts.get(hostId);
206 }
207
208 @Override
209 public Set<Host> getHosts(VlanId vlanId) {
210 return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId));
211 }
212
213 @Override
214 public Set<Host> getHosts(MacAddress mac) {
215 return filter(hosts.values(), host -> Objects.equals(host.mac(), mac));
216 }
217
218 @Override
219 public Set<Host> getHosts(IpAddress ip) {
220 return filter(hosts.values(), host -> host.ipAddresses().contains(ip));
221 }
222
223 @Override
224 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan009c3082015-11-10 14:18:04 -0800225 Set<Host> filtered = hosts.entrySet().stream()
226 .filter(entry -> entry.getValue().location().equals(connectPoint))
227 .map(Map.Entry::getValue)
228 .collect(Collectors.toSet());
229 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700230 }
231
232 @Override
233 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan009c3082015-11-10 14:18:04 -0800234 Set<Host> filtered = hosts.entrySet().stream()
235 .filter(entry -> entry.getValue().location().deviceId().equals(deviceId))
236 .map(Map.Entry::getValue)
237 .collect(Collectors.toSet());
HIGUCHI Yutafe2122c2015-09-30 13:46:22 -0700238 return ImmutableSet.copyOf(filtered);
Madan Jampani38a88212015-09-15 11:21:27 -0700239 }
240
Madan Jampani38a88212015-09-15 11:21:27 -0700241 private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) {
242 return collection.stream().filter(predicate).collect(Collectors.toSet());
243 }
244
alshabib8a4a6002015-11-25 14:31:16 -0800245 private class HostLocationTracker implements MapEventListener<HostId, DefaultHost> {
Madan Jampani38a88212015-09-15 11:21:27 -0700246 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800247 public void event(MapEvent<HostId, DefaultHost> event) {
248 DefaultHost host = checkNotNull(event.value().value());
249 if (event.type() == MapEvent.Type.INSERT) {
Charles Chan33f28a92015-11-13 13:12:38 -0800250 Host prevHost = prevHosts.put(host.id(), host);
251 if (prevHost == null) {
Charles Chan009c3082015-11-10 14:18:04 -0800252 notifyDelegate(new HostEvent(HOST_ADDED, host));
Charles Chan33f28a92015-11-13 13:12:38 -0800253 } else if (!Objects.equals(prevHost.location(), host.location())) {
254 notifyDelegate(new HostEvent(HOST_MOVED, host, prevHost));
255 } else if (!Objects.equals(prevHost, host)) {
256 notifyDelegate(new HostEvent(HOST_UPDATED, host, prevHost));
Charles Chan009c3082015-11-10 14:18:04 -0800257 }
alshabib8a4a6002015-11-25 14:31:16 -0800258 } else if (event.type() == MapEvent.Type.REMOVE) {
Charles Chan33f28a92015-11-13 13:12:38 -0800259 if (prevHosts.remove(host.id()) != null) {
Madan Jampani38a88212015-09-15 11:21:27 -0700260 notifyDelegate(new HostEvent(HOST_REMOVED, host));
261 }
Madan Jampani38a88212015-09-15 11:21:27 -0700262 }
263 }
264 }
265}