blob: 93fa3f00122d1ebef5ad868634ca50b71e2fd132 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
tom7869ad92014-09-09 14:32:08 -070017
Jonathan Hart46080b62015-08-31 11:10:21 +020018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Multimap;
tom5bcc9462014-09-19 10:11:31 -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.Service;
Jonathan Hart46080b62015-08-31 11:10:21 +020025import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Annotations;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DefaultAnnotations;
31import org.onosproject.net.DefaultHost;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Host;
34import org.onosproject.net.HostId;
35import org.onosproject.net.HostLocation;
36import org.onosproject.net.host.HostDescription;
37import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostStore;
39import org.onosproject.net.host.HostStoreDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.provider.ProviderId;
41import org.onosproject.store.AbstractStore;
Jonathan Hartac60c082014-09-23 08:55:17 -070042import org.slf4j.Logger;
tom7869ad92014-09-09 14:32:08 -070043
Jonathan Hart46080b62015-08-31 11:10:21 +020044import java.util.HashSet;
45import java.util.Map;
46import java.util.Set;
47import java.util.concurrent.ConcurrentHashMap;
48
49import static org.onosproject.net.DefaultAnnotations.merge;
50import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
51import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
52import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
53import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
54import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibee5652752014-09-10 23:27:34 -070055
Yuta HIGUCHIa2639152014-10-14 15:08:10 -070056// TODO: multi-provider, annotation not supported.
tom7869ad92014-09-09 14:32:08 -070057/**
58 * Manages inventory of end-station hosts using trivial in-memory
59 * implementation.
60 */
tom5bcc9462014-09-19 10:11:31 -070061@Component(immediate = true)
62@Service
tomf80c9722014-09-24 14:49:18 -070063public class SimpleHostStore
64 extends AbstractStore<HostEvent, HostStoreDelegate>
65 implements HostStore {
tom7869ad92014-09-09 14:32:08 -070066
tom5bcc9462014-09-19 10:11:31 -070067 private final Logger log = getLogger(getClass());
68
69 // Host inventory
tom093340b2014-10-10 00:15:36 -070070 private final Map<HostId, StoredHost> hosts = new ConcurrentHashMap<>(2000000, 0.75f, 16);
tom7869ad92014-09-09 14:32:08 -070071
tom5bcc9462014-09-19 10:11:31 -070072 // Hosts tracked by their location
Ayaka Koshibee5652752014-09-10 23:27:34 -070073 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070074
tom5bcc9462014-09-19 10:11:31 -070075 @Activate
76 public void activate() {
77 log.info("Started");
78 }
79
80 @Deactivate
81 public void deactivate() {
82 log.info("Stopped");
83 }
84
85 @Override
86 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
Brian O'Connorf107bd72015-09-21 15:31:03 -070087 HostDescription hostDescription,
88 boolean replaceIps) {
89 //TODO We need a way to detect conflicting changes and abort update.
tom093340b2014-10-10 00:15:36 -070090 StoredHost host = hosts.get(hostId);
Charles Chan009c3082015-11-10 14:18:04 -080091 HostEvent hostEvent;
Ayaka Koshibee5652752014-09-10 23:27:34 -070092 if (host == null) {
Charles Chan009c3082015-11-10 14:18:04 -080093 hostEvent = createHost(providerId, hostId, hostDescription);
94 } else {
95 hostEvent = updateHost(providerId, host, hostDescription, replaceIps);
Ayaka Koshibee5652752014-09-10 23:27:34 -070096 }
Charles Chan009c3082015-11-10 14:18:04 -080097 notifyDelegate(hostEvent);
98 return hostEvent;
Ayaka Koshibee5652752014-09-10 23:27:34 -070099 }
100
101 // creates a new host and sends HOST_ADDED
102 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -0700103 HostDescription descr) {
tom093340b2014-10-10 00:15:36 -0700104 StoredHost newhost = new StoredHost(providerId, hostId,
105 descr.hwAddress(),
106 descr.vlan(),
107 descr.location(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800108 ImmutableSet.copyOf(descr.ipAddress()),
109 descr.annotations());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700110 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700111 hosts.put(hostId, newhost);
112 locations.put(descr.location(), newhost);
113 }
114 return new HostEvent(HOST_ADDED, newhost);
115 }
116
117 // checks for type of update to host, sends appropriate event
tom093340b2014-10-10 00:15:36 -0700118 private HostEvent updateHost(ProviderId providerId, StoredHost host,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700119 HostDescription descr, boolean replaceIps) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700120 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -0700121 if (!host.location().equals(descr.location())) {
tom093340b2014-10-10 00:15:36 -0700122 host.setLocation(descr.location());
123 return new HostEvent(HOST_MOVED, host);
124 }
toma56d5fe2014-09-17 11:05:47 -0700125
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800126 if (host.ipAddresses().containsAll(descr.ipAddress()) &&
127 descr.annotations().keys().isEmpty()) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700128 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700129 }
tom093340b2014-10-10 00:15:36 -0700130
Brian O'Connorf107bd72015-09-21 15:31:03 -0700131 final Set<IpAddress> addresses;
132 if (replaceIps) {
133 addresses = ImmutableSet.copyOf(descr.ipAddress());
134 } else {
135 addresses = new HashSet<>(host.ipAddresses());
136 addresses.addAll(descr.ipAddress());
137 }
138
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800139 Annotations annotations = merge((DefaultAnnotations) host.annotations(),
140 descr.annotations());
tom093340b2014-10-10 00:15:36 -0700141 StoredHost updated = new StoredHost(providerId, host.id(),
142 host.mac(), host.vlan(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800143 descr.location(), addresses,
144 annotations);
tom093340b2014-10-10 00:15:36 -0700145 event = new HostEvent(HOST_UPDATED, updated);
Ayaka Koshibee5652752014-09-10 23:27:34 -0700146 synchronized (this) {
147 hosts.put(host.id(), updated);
148 locations.remove(host.location(), host);
149 locations.put(updated.location(), updated);
150 }
151 return event;
tom7869ad92014-09-09 14:32:08 -0700152 }
153
tom5bcc9462014-09-19 10:11:31 -0700154 @Override
155 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700156 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700157 Host host = hosts.remove(hostId);
158 if (host != null) {
159 locations.remove((host.location()), host);
Charles Chan009c3082015-11-10 14:18:04 -0800160 HostEvent hostEvent = new HostEvent(HOST_REMOVED, host);
161 notifyDelegate(hostEvent);
162 return hostEvent;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700163 }
164 return null;
165 }
tom7869ad92014-09-09 14:32:08 -0700166 }
167
tom5bcc9462014-09-19 10:11:31 -0700168 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700169 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan888e20a2017-05-01 15:44:23 -0700170 // TODO implement this
samanwita palc40e5ed2015-09-24 11:01:51 -0700171 return null;
172 }
173
174 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700175 public void removeLocation(HostId hostId, HostLocation location) {
176 hosts.get(hostId).locations().remove(location);
177 }
178
179 @Override
tom5bcc9462014-09-19 10:11:31 -0700180 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700181 return hosts.size();
182 }
183
tom5bcc9462014-09-19 10:11:31 -0700184 @Override
185 public Iterable<Host> getHosts() {
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700186 return ImmutableSet.copyOf(hosts.values());
tom7869ad92014-09-09 14:32:08 -0700187 }
188
tom5bcc9462014-09-19 10:11:31 -0700189 @Override
190 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700191 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700192 }
193
tom5bcc9462014-09-19 10:11:31 -0700194 @Override
195 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700196 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700197 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700198 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700199 vlanset.add(h);
200 }
201 }
202 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700203 }
204
tom5bcc9462014-09-19 10:11:31 -0700205 @Override
206 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700207 Set<Host> macset = new HashSet<>();
208 for (Host h : hosts.values()) {
209 if (h.mac().equals(mac)) {
210 macset.add(h);
211 }
212 }
213 return macset;
tom7869ad92014-09-09 14:32:08 -0700214 }
215
tom5bcc9462014-09-19 10:11:31 -0700216 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700217 public Set<Host> getHosts(IpAddress ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700218 Set<Host> ipset = new HashSet<>();
219 for (Host h : hosts.values()) {
220 if (h.ipAddresses().contains(ip)) {
221 ipset.add(h);
222 }
223 }
224 return ipset;
tom7869ad92014-09-09 14:32:08 -0700225 }
226
tom5bcc9462014-09-19 10:11:31 -0700227 @Override
228 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700229 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700230 }
231
tom5bcc9462014-09-19 10:11:31 -0700232 @Override
tom7869ad92014-09-09 14:32:08 -0700233 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700234 Set<Host> hostset = new HashSet<>();
235 for (ConnectPoint p : locations.keySet()) {
236 if (p.deviceId().equals(deviceId)) {
237 hostset.addAll(locations.get(p));
238 }
239 }
240 return hostset;
tom7869ad92014-09-09 14:32:08 -0700241 }
242
tom093340b2014-10-10 00:15:36 -0700243 // Auxiliary extension to allow location to mutate.
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -0700244 private static final class StoredHost extends DefaultHost {
tom093340b2014-10-10 00:15:36 -0700245 private HostLocation location;
246
247 /**
248 * Creates an end-station host using the supplied information.
249 *
250 * @param providerId provider identity
251 * @param id host identifier
252 * @param mac host MAC address
253 * @param vlan host VLAN identifier
254 * @param location host location
255 * @param ips host IP addresses
256 * @param annotations optional key/value annotations
257 */
258 public StoredHost(ProviderId providerId, HostId id,
259 MacAddress mac, VlanId vlan, HostLocation location,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700260 Set<IpAddress> ips, Annotations... annotations) {
tom093340b2014-10-10 00:15:36 -0700261 super(providerId, id, mac, vlan, location, ips, annotations);
262 this.location = location;
263 }
264
265 void setLocation(HostLocation location) {
266 this.location = location;
267 }
268
269 @Override
270 public HostLocation location() {
271 return location;
272 }
273 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700274}