blob: a635742fc05f56e6c44e91a5cf391d7a29639921 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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) {
170 return null;
171 }
172
173 @Override
tom5bcc9462014-09-19 10:11:31 -0700174 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700175 return hosts.size();
176 }
177
tom5bcc9462014-09-19 10:11:31 -0700178 @Override
179 public Iterable<Host> getHosts() {
tom093340b2014-10-10 00:15:36 -0700180 return ImmutableSet.<Host>copyOf(hosts.values());
tom7869ad92014-09-09 14:32:08 -0700181 }
182
tom5bcc9462014-09-19 10:11:31 -0700183 @Override
184 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700185 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700186 }
187
tom5bcc9462014-09-19 10:11:31 -0700188 @Override
189 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700190 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700191 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700192 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700193 vlanset.add(h);
194 }
195 }
196 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700197 }
198
tom5bcc9462014-09-19 10:11:31 -0700199 @Override
200 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700201 Set<Host> macset = new HashSet<>();
202 for (Host h : hosts.values()) {
203 if (h.mac().equals(mac)) {
204 macset.add(h);
205 }
206 }
207 return macset;
tom7869ad92014-09-09 14:32:08 -0700208 }
209
tom5bcc9462014-09-19 10:11:31 -0700210 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700211 public Set<Host> getHosts(IpAddress ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700212 Set<Host> ipset = new HashSet<>();
213 for (Host h : hosts.values()) {
214 if (h.ipAddresses().contains(ip)) {
215 ipset.add(h);
216 }
217 }
218 return ipset;
tom7869ad92014-09-09 14:32:08 -0700219 }
220
tom5bcc9462014-09-19 10:11:31 -0700221 @Override
222 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700223 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700224 }
225
tom5bcc9462014-09-19 10:11:31 -0700226 @Override
tom7869ad92014-09-09 14:32:08 -0700227 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700228 Set<Host> hostset = new HashSet<>();
229 for (ConnectPoint p : locations.keySet()) {
230 if (p.deviceId().equals(deviceId)) {
231 hostset.addAll(locations.get(p));
232 }
233 }
234 return hostset;
tom7869ad92014-09-09 14:32:08 -0700235 }
236
tom093340b2014-10-10 00:15:36 -0700237 // Auxiliary extension to allow location to mutate.
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -0700238 private static final class StoredHost extends DefaultHost {
tom093340b2014-10-10 00:15:36 -0700239 private HostLocation location;
240
241 /**
242 * Creates an end-station host using the supplied information.
243 *
244 * @param providerId provider identity
245 * @param id host identifier
246 * @param mac host MAC address
247 * @param vlan host VLAN identifier
248 * @param location host location
249 * @param ips host IP addresses
250 * @param annotations optional key/value annotations
251 */
252 public StoredHost(ProviderId providerId, HostId id,
253 MacAddress mac, VlanId vlan, HostLocation location,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700254 Set<IpAddress> ips, Annotations... annotations) {
tom093340b2014-10-10 00:15:36 -0700255 super(providerId, id, mac, vlan, location, ips, annotations);
256 this.location = location;
257 }
258
259 void setLocation(HostLocation location) {
260 this.location = location;
261 }
262
263 @Override
264 public HostLocation location() {
265 return location;
266 }
267 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700268}