blob: e1fea28ae9ca9ec6351df937cac9984451e3112a [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()),
Charles Chand0c147a2017-09-14 14:00:10 -0700109 descr.configured(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800110 descr.annotations());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700111 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700112 hosts.put(hostId, newhost);
113 locations.put(descr.location(), newhost);
114 }
115 return new HostEvent(HOST_ADDED, newhost);
116 }
117
118 // checks for type of update to host, sends appropriate event
tom093340b2014-10-10 00:15:36 -0700119 private HostEvent updateHost(ProviderId providerId, StoredHost host,
Brian O'Connorf107bd72015-09-21 15:31:03 -0700120 HostDescription descr, boolean replaceIps) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700121 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -0700122 if (!host.location().equals(descr.location())) {
tom093340b2014-10-10 00:15:36 -0700123 host.setLocation(descr.location());
124 return new HostEvent(HOST_MOVED, host);
125 }
toma56d5fe2014-09-17 11:05:47 -0700126
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800127 if (host.ipAddresses().containsAll(descr.ipAddress()) &&
128 descr.annotations().keys().isEmpty()) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700129 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700130 }
tom093340b2014-10-10 00:15:36 -0700131
Brian O'Connorf107bd72015-09-21 15:31:03 -0700132 final Set<IpAddress> addresses;
133 if (replaceIps) {
134 addresses = ImmutableSet.copyOf(descr.ipAddress());
135 } else {
136 addresses = new HashSet<>(host.ipAddresses());
137 addresses.addAll(descr.ipAddress());
138 }
139
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800140 Annotations annotations = merge((DefaultAnnotations) host.annotations(),
141 descr.annotations());
tom093340b2014-10-10 00:15:36 -0700142 StoredHost updated = new StoredHost(providerId, host.id(),
143 host.mac(), host.vlan(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800144 descr.location(), addresses,
Charles Chand0c147a2017-09-14 14:00:10 -0700145 descr.configured(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800146 annotations);
tom093340b2014-10-10 00:15:36 -0700147 event = new HostEvent(HOST_UPDATED, updated);
Ayaka Koshibee5652752014-09-10 23:27:34 -0700148 synchronized (this) {
149 hosts.put(host.id(), updated);
150 locations.remove(host.location(), host);
151 locations.put(updated.location(), updated);
152 }
153 return event;
tom7869ad92014-09-09 14:32:08 -0700154 }
155
tom5bcc9462014-09-19 10:11:31 -0700156 @Override
157 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700158 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700159 Host host = hosts.remove(hostId);
160 if (host != null) {
161 locations.remove((host.location()), host);
Charles Chan009c3082015-11-10 14:18:04 -0800162 HostEvent hostEvent = new HostEvent(HOST_REMOVED, host);
163 notifyDelegate(hostEvent);
164 return hostEvent;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700165 }
166 return null;
167 }
tom7869ad92014-09-09 14:32:08 -0700168 }
169
tom5bcc9462014-09-19 10:11:31 -0700170 @Override
samanwita palc40e5ed2015-09-24 11:01:51 -0700171 public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
Charles Chan888e20a2017-05-01 15:44:23 -0700172 // TODO implement this
samanwita palc40e5ed2015-09-24 11:01:51 -0700173 return null;
174 }
175
176 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700177 public void removeLocation(HostId hostId, HostLocation location) {
178 hosts.get(hostId).locations().remove(location);
179 }
180
181 @Override
tom5bcc9462014-09-19 10:11:31 -0700182 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700183 return hosts.size();
184 }
185
tom5bcc9462014-09-19 10:11:31 -0700186 @Override
187 public Iterable<Host> getHosts() {
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700188 return ImmutableSet.copyOf(hosts.values());
tom7869ad92014-09-09 14:32:08 -0700189 }
190
tom5bcc9462014-09-19 10:11:31 -0700191 @Override
192 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700193 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700194 }
195
tom5bcc9462014-09-19 10:11:31 -0700196 @Override
197 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700198 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700199 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700200 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700201 vlanset.add(h);
202 }
203 }
204 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700205 }
206
tom5bcc9462014-09-19 10:11:31 -0700207 @Override
208 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700209 Set<Host> macset = new HashSet<>();
210 for (Host h : hosts.values()) {
211 if (h.mac().equals(mac)) {
212 macset.add(h);
213 }
214 }
215 return macset;
tom7869ad92014-09-09 14:32:08 -0700216 }
217
tom5bcc9462014-09-19 10:11:31 -0700218 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700219 public Set<Host> getHosts(IpAddress ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700220 Set<Host> ipset = new HashSet<>();
221 for (Host h : hosts.values()) {
222 if (h.ipAddresses().contains(ip)) {
223 ipset.add(h);
224 }
225 }
226 return ipset;
tom7869ad92014-09-09 14:32:08 -0700227 }
228
tom5bcc9462014-09-19 10:11:31 -0700229 @Override
230 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700231 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700232 }
233
tom5bcc9462014-09-19 10:11:31 -0700234 @Override
tom7869ad92014-09-09 14:32:08 -0700235 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700236 Set<Host> hostset = new HashSet<>();
237 for (ConnectPoint p : locations.keySet()) {
238 if (p.deviceId().equals(deviceId)) {
239 hostset.addAll(locations.get(p));
240 }
241 }
242 return hostset;
tom7869ad92014-09-09 14:32:08 -0700243 }
244
tom093340b2014-10-10 00:15:36 -0700245 // Auxiliary extension to allow location to mutate.
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -0700246 private static final class StoredHost extends DefaultHost {
tom093340b2014-10-10 00:15:36 -0700247 private HostLocation location;
248
249 /**
250 * Creates an end-station host using the supplied information.
251 *
252 * @param providerId provider identity
253 * @param id host identifier
254 * @param mac host MAC address
255 * @param vlan host VLAN identifier
256 * @param location host location
257 * @param ips host IP addresses
258 * @param annotations optional key/value annotations
259 */
260 public StoredHost(ProviderId providerId, HostId id,
261 MacAddress mac, VlanId vlan, HostLocation location,
Charles Chand0c147a2017-09-14 14:00:10 -0700262 Set<IpAddress> ips, boolean configured, Annotations... annotations) {
263 super(providerId, id, mac, vlan, location, ips, configured, annotations);
tom093340b2014-10-10 00:15:36 -0700264 this.location = location;
265 }
266
267 void setLocation(HostLocation location) {
268 this.location = location;
269 }
270
271 @Override
272 public HostLocation location() {
273 return location;
274 }
275 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700276}