blob: ee8570d49f5b9e96dcb0eb8d23596310d807ae80 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
tom7869ad92014-09-09 14:32:08 -07002
tom093340b2014-10-10 00:15:36 -07003import com.google.common.collect.HashMultimap;
4import com.google.common.collect.ImmutableSet;
5import com.google.common.collect.Multimap;
6import com.google.common.collect.Sets;
tom5bcc9462014-09-19 10:11:31 -07007import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Service;
tom093340b2014-10-10 00:15:36 -070011import org.onlab.onos.net.Annotations;
tom7869ad92014-09-09 14:32:08 -070012import org.onlab.onos.net.ConnectPoint;
Ayaka Koshibee5652752014-09-10 23:27:34 -070013import org.onlab.onos.net.DefaultHost;
tom7869ad92014-09-09 14:32:08 -070014import org.onlab.onos.net.DeviceId;
15import org.onlab.onos.net.Host;
16import org.onlab.onos.net.HostId;
tom093340b2014-10-10 00:15:36 -070017import org.onlab.onos.net.HostLocation;
tom7869ad92014-09-09 14:32:08 -070018import org.onlab.onos.net.host.HostDescription;
19import org.onlab.onos.net.host.HostEvent;
tom5bcc9462014-09-19 10:11:31 -070020import org.onlab.onos.net.host.HostStore;
tomf80c9722014-09-24 14:49:18 -070021import org.onlab.onos.net.host.HostStoreDelegate;
Jonathan Hartac60c082014-09-23 08:55:17 -070022import org.onlab.onos.net.host.PortAddresses;
tom7869ad92014-09-09 14:32:08 -070023import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070024import org.onlab.onos.store.AbstractStore;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070025import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
Jonathan Hartac60c082014-09-23 08:55:17 -070028import org.slf4j.Logger;
tom7869ad92014-09-09 14:32:08 -070029
tom093340b2014-10-10 00:15:36 -070030import java.util.HashSet;
31import java.util.Map;
32import java.util.Set;
33import java.util.concurrent.ConcurrentHashMap;
34
35import static org.onlab.onos.net.host.HostEvent.Type.*;
36import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibee5652752014-09-10 23:27:34 -070037
Yuta HIGUCHIa2639152014-10-14 15:08:10 -070038// TODO: multi-provider, annotation not supported.
tom7869ad92014-09-09 14:32:08 -070039/**
40 * Manages inventory of end-station hosts using trivial in-memory
41 * implementation.
42 */
tom5bcc9462014-09-19 10:11:31 -070043@Component(immediate = true)
44@Service
tomf80c9722014-09-24 14:49:18 -070045public class SimpleHostStore
46 extends AbstractStore<HostEvent, HostStoreDelegate>
47 implements HostStore {
tom7869ad92014-09-09 14:32:08 -070048
tom5bcc9462014-09-19 10:11:31 -070049 private final Logger log = getLogger(getClass());
50
51 // Host inventory
tom093340b2014-10-10 00:15:36 -070052 private final Map<HostId, StoredHost> hosts = new ConcurrentHashMap<>(2000000, 0.75f, 16);
tom7869ad92014-09-09 14:32:08 -070053
tom5bcc9462014-09-19 10:11:31 -070054 // Hosts tracked by their location
Ayaka Koshibee5652752014-09-10 23:27:34 -070055 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070056
Jonathan Hart43c182c2014-09-23 11:13:42 -070057 private final Map<ConnectPoint, PortAddresses> portAddresses =
58 new ConcurrentHashMap<>();
59
tom5bcc9462014-09-19 10:11:31 -070060 @Activate
61 public void activate() {
62 log.info("Started");
63 }
64
65 @Deactivate
66 public void deactivate() {
67 log.info("Stopped");
68 }
69
70 @Override
71 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
72 HostDescription hostDescription) {
tom093340b2014-10-10 00:15:36 -070073 StoredHost host = hosts.get(hostId);
Ayaka Koshibee5652752014-09-10 23:27:34 -070074 if (host == null) {
75 return createHost(providerId, hostId, hostDescription);
76 }
77 return updateHost(providerId, host, hostDescription);
78 }
79
80 // creates a new host and sends HOST_ADDED
81 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -070082 HostDescription descr) {
tom093340b2014-10-10 00:15:36 -070083 StoredHost newhost = new StoredHost(providerId, hostId,
84 descr.hwAddress(),
85 descr.vlan(),
86 descr.location(),
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070087 ImmutableSet.copyOf(descr.ipAddress()));
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070088 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070089 hosts.put(hostId, newhost);
90 locations.put(descr.location(), newhost);
91 }
92 return new HostEvent(HOST_ADDED, newhost);
93 }
94
95 // checks for type of update to host, sends appropriate event
tom093340b2014-10-10 00:15:36 -070096 private HostEvent updateHost(ProviderId providerId, StoredHost host,
toma56d5fe2014-09-17 11:05:47 -070097 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070098 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -070099 if (!host.location().equals(descr.location())) {
tom093340b2014-10-10 00:15:36 -0700100 host.setLocation(descr.location());
101 return new HostEvent(HOST_MOVED, host);
102 }
toma56d5fe2014-09-17 11:05:47 -0700103
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -0700104 if (host.ipAddresses().containsAll(descr.ipAddress())) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700105 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700106 }
tom093340b2014-10-10 00:15:36 -0700107
108 Set<IpPrefix> addresses = new HashSet<>(host.ipAddresses());
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -0700109 addresses.addAll(descr.ipAddress());
tom093340b2014-10-10 00:15:36 -0700110 StoredHost updated = new StoredHost(providerId, host.id(),
111 host.mac(), host.vlan(),
112 descr.location(), addresses);
113 event = new HostEvent(HOST_UPDATED, updated);
Ayaka Koshibee5652752014-09-10 23:27:34 -0700114 synchronized (this) {
115 hosts.put(host.id(), updated);
116 locations.remove(host.location(), host);
117 locations.put(updated.location(), updated);
118 }
119 return event;
tom7869ad92014-09-09 14:32:08 -0700120 }
121
tom5bcc9462014-09-19 10:11:31 -0700122 @Override
123 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700124 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700125 Host host = hosts.remove(hostId);
126 if (host != null) {
127 locations.remove((host.location()), host);
128 return new HostEvent(HOST_REMOVED, host);
129 }
130 return null;
131 }
tom7869ad92014-09-09 14:32:08 -0700132 }
133
tom5bcc9462014-09-19 10:11:31 -0700134 @Override
135 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700136 return hosts.size();
137 }
138
tom5bcc9462014-09-19 10:11:31 -0700139 @Override
140 public Iterable<Host> getHosts() {
tom093340b2014-10-10 00:15:36 -0700141 return ImmutableSet.<Host>copyOf(hosts.values());
tom7869ad92014-09-09 14:32:08 -0700142 }
143
tom5bcc9462014-09-19 10:11:31 -0700144 @Override
145 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700146 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700147 }
148
tom5bcc9462014-09-19 10:11:31 -0700149 @Override
150 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700151 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700152 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700153 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700154 vlanset.add(h);
155 }
156 }
157 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700158 }
159
tom5bcc9462014-09-19 10:11:31 -0700160 @Override
161 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700162 Set<Host> macset = new HashSet<>();
163 for (Host h : hosts.values()) {
164 if (h.mac().equals(mac)) {
165 macset.add(h);
166 }
167 }
168 return macset;
tom7869ad92014-09-09 14:32:08 -0700169 }
170
tom5bcc9462014-09-19 10:11:31 -0700171 @Override
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700172 public Set<Host> getHosts(IpPrefix ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700173 Set<Host> ipset = new HashSet<>();
174 for (Host h : hosts.values()) {
175 if (h.ipAddresses().contains(ip)) {
176 ipset.add(h);
177 }
178 }
179 return ipset;
tom7869ad92014-09-09 14:32:08 -0700180 }
181
tom5bcc9462014-09-19 10:11:31 -0700182 @Override
183 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700184 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700185 }
186
tom5bcc9462014-09-19 10:11:31 -0700187 @Override
tom7869ad92014-09-09 14:32:08 -0700188 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700189 Set<Host> hostset = new HashSet<>();
190 for (ConnectPoint p : locations.keySet()) {
191 if (p.deviceId().equals(deviceId)) {
192 hostset.addAll(locations.get(p));
193 }
194 }
195 return hostset;
tom7869ad92014-09-09 14:32:08 -0700196 }
197
Jonathan Hartac60c082014-09-23 08:55:17 -0700198 @Override
199 public void updateAddressBindings(PortAddresses addresses) {
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700200 synchronized (portAddresses) {
201 PortAddresses existing = portAddresses.get(addresses.connectPoint());
202 if (existing == null) {
203 portAddresses.put(addresses.connectPoint(), addresses);
204 } else {
205 Set<IpPrefix> union = Sets.union(existing.ips(), addresses.ips())
206 .immutableCopy();
207
208 MacAddress newMac = (addresses.mac() == null) ? existing.mac()
209 : addresses.mac();
210
211 PortAddresses newAddresses =
212 new PortAddresses(addresses.connectPoint(), union, newMac);
213
214 portAddresses.put(newAddresses.connectPoint(), newAddresses);
215 }
216 }
Jonathan Hartac60c082014-09-23 08:55:17 -0700217 }
218
219 @Override
Jonathan Hart09585c62014-09-23 16:58:04 -0700220 public void removeAddressBindings(PortAddresses addresses) {
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700221 synchronized (portAddresses) {
222 PortAddresses existing = portAddresses.get(addresses.connectPoint());
223 if (existing != null) {
224 Set<IpPrefix> difference =
225 Sets.difference(existing.ips(), addresses.ips()).immutableCopy();
Jonathan Hart09585c62014-09-23 16:58:04 -0700226
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700227 // If they removed the existing mac, set the new mac to null.
228 // Otherwise, keep the existing mac.
229 MacAddress newMac = existing.mac();
230 if (addresses.mac() != null && addresses.mac().equals(existing.mac())) {
231 newMac = null;
232 }
233
234 PortAddresses newAddresses =
235 new PortAddresses(addresses.connectPoint(), difference, newMac);
236
237 portAddresses.put(newAddresses.connectPoint(), newAddresses);
238 }
239 }
Jonathan Hart09585c62014-09-23 16:58:04 -0700240 }
241
242 @Override
243 public void clearAddressBindings(ConnectPoint connectPoint) {
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700244 synchronized (portAddresses) {
245 portAddresses.remove(connectPoint);
246 }
Jonathan Hartac60c082014-09-23 08:55:17 -0700247 }
248
249 @Override
250 public Set<PortAddresses> getAddressBindings() {
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700251 synchronized (portAddresses) {
252 return new HashSet<>(portAddresses.values());
253 }
Jonathan Hartac60c082014-09-23 08:55:17 -0700254 }
255
256 @Override
257 public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) {
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700258 PortAddresses addresses;
259
260 synchronized (portAddresses) {
261 addresses = portAddresses.get(connectPoint);
262 }
263
264 if (addresses == null) {
265 addresses = new PortAddresses(connectPoint, null, null);
266 }
267
268 return addresses;
Jonathan Hartac60c082014-09-23 08:55:17 -0700269 }
270
tom093340b2014-10-10 00:15:36 -0700271 // Auxiliary extension to allow location to mutate.
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -0700272 private static final class StoredHost extends DefaultHost {
tom093340b2014-10-10 00:15:36 -0700273 private HostLocation location;
274
275 /**
276 * Creates an end-station host using the supplied information.
277 *
278 * @param providerId provider identity
279 * @param id host identifier
280 * @param mac host MAC address
281 * @param vlan host VLAN identifier
282 * @param location host location
283 * @param ips host IP addresses
284 * @param annotations optional key/value annotations
285 */
286 public StoredHost(ProviderId providerId, HostId id,
287 MacAddress mac, VlanId vlan, HostLocation location,
288 Set<IpPrefix> ips, Annotations... annotations) {
289 super(providerId, id, mac, vlan, location, ips, annotations);
290 this.location = location;
291 }
292
293 void setLocation(HostLocation location) {
294 this.location = location;
295 }
296
297 @Override
298 public HostLocation location() {
299 return location;
300 }
301 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700302}