blob: be609a84e433e9ed31122131b8d4abb25c566cf3 [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.trivial.impl;
tom7869ad92014-09-09 14:32:08 -07002
alshabib51622f72014-09-11 11:22:33 -07003import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
4import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED;
5import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
6import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED;
tom5bcc9462014-09-19 10:11:31 -07007import static org.slf4j.LoggerFactory.getLogger;
alshabib51622f72014-09-11 11:22:33 -07008
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.Map;
12import java.util.Set;
13import java.util.concurrent.ConcurrentHashMap;
tom7869ad92014-09-09 14:32:08 -070014
tom5bcc9462014-09-19 10:11:31 -070015import org.apache.felix.scr.annotations.Activate;
16import org.apache.felix.scr.annotations.Component;
17import org.apache.felix.scr.annotations.Deactivate;
18import org.apache.felix.scr.annotations.Service;
tom7869ad92014-09-09 14:32:08 -070019import org.onlab.onos.net.ConnectPoint;
Ayaka Koshibee5652752014-09-10 23:27:34 -070020import org.onlab.onos.net.DefaultHost;
tom7869ad92014-09-09 14:32:08 -070021import org.onlab.onos.net.DeviceId;
22import org.onlab.onos.net.Host;
23import org.onlab.onos.net.HostId;
24import org.onlab.onos.net.host.HostDescription;
25import org.onlab.onos.net.host.HostEvent;
tom5bcc9462014-09-19 10:11:31 -070026import org.onlab.onos.net.host.HostStore;
tomf80c9722014-09-24 14:49:18 -070027import org.onlab.onos.net.host.HostStoreDelegate;
Jonathan Hartac60c082014-09-23 08:55:17 -070028import org.onlab.onos.net.host.PortAddresses;
tom7869ad92014-09-09 14:32:08 -070029import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070030import org.onlab.onos.store.AbstractStore;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070031import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070032import org.onlab.packet.MacAddress;
33import org.onlab.packet.VlanId;
Jonathan Hartac60c082014-09-23 08:55:17 -070034import org.slf4j.Logger;
tom7869ad92014-09-09 14:32:08 -070035
Ayaka Koshibee5652752014-09-10 23:27:34 -070036import com.google.common.collect.HashMultimap;
37import com.google.common.collect.ImmutableSet;
38import com.google.common.collect.Multimap;
39
tom7869ad92014-09-09 14:32:08 -070040/**
41 * Manages inventory of end-station hosts using trivial in-memory
42 * implementation.
43 */
tom5bcc9462014-09-19 10:11:31 -070044@Component(immediate = true)
45@Service
tomf80c9722014-09-24 14:49:18 -070046public class SimpleHostStore
47 extends AbstractStore<HostEvent, HostStoreDelegate>
48 implements HostStore {
tom7869ad92014-09-09 14:32:08 -070049
tom5bcc9462014-09-19 10:11:31 -070050 private final Logger log = getLogger(getClass());
51
52 // Host inventory
tom7869ad92014-09-09 14:32:08 -070053 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
54
tom5bcc9462014-09-19 10:11:31 -070055 // Hosts tracked by their location
Ayaka Koshibee5652752014-09-10 23:27:34 -070056 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070057
Jonathan Hart43c182c2014-09-23 11:13:42 -070058 private final Map<ConnectPoint, PortAddresses> portAddresses =
59 new ConcurrentHashMap<>();
60
tom5bcc9462014-09-19 10:11:31 -070061 @Activate
62 public void activate() {
63 log.info("Started");
64 }
65
66 @Deactivate
67 public void deactivate() {
68 log.info("Stopped");
69 }
70
71 @Override
72 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
73 HostDescription hostDescription) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070074 Host host = hosts.get(hostId);
75 if (host == null) {
76 return createHost(providerId, hostId, hostDescription);
77 }
78 return updateHost(providerId, host, hostDescription);
79 }
80
81 // creates a new host and sends HOST_ADDED
82 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -070083 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070084 DefaultHost newhost = new DefaultHost(providerId, hostId,
toma56d5fe2014-09-17 11:05:47 -070085 descr.hwAddress(),
86 descr.vlan(),
87 descr.location(),
88 descr.ipAddresses());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070089 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070090 hosts.put(hostId, newhost);
91 locations.put(descr.location(), newhost);
92 }
93 return new HostEvent(HOST_ADDED, newhost);
94 }
95
96 // checks for type of update to host, sends appropriate event
97 private HostEvent updateHost(ProviderId providerId, Host host,
toma56d5fe2014-09-17 11:05:47 -070098 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070099 DefaultHost updated;
100 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -0700101 if (!host.location().equals(descr.location())) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700102 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -0700103 host.mac(),
104 host.vlan(),
105 descr.location(),
106 host.ipAddresses());
Ayaka Koshibee5652752014-09-10 23:27:34 -0700107 event = new HostEvent(HOST_MOVED, updated);
toma56d5fe2014-09-17 11:05:47 -0700108
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700109 } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) {
110 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -0700111 host.mac(),
112 host.vlan(),
113 descr.location(),
114 descr.ipAddresses());
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700115 event = new HostEvent(HOST_UPDATED, updated);
116 } else {
117 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700118 }
119 synchronized (this) {
120 hosts.put(host.id(), updated);
121 locations.remove(host.location(), host);
122 locations.put(updated.location(), updated);
123 }
124 return event;
tom7869ad92014-09-09 14:32:08 -0700125 }
126
tom5bcc9462014-09-19 10:11:31 -0700127 @Override
128 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700129 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700130 Host host = hosts.remove(hostId);
131 if (host != null) {
132 locations.remove((host.location()), host);
133 return new HostEvent(HOST_REMOVED, host);
134 }
135 return null;
136 }
tom7869ad92014-09-09 14:32:08 -0700137 }
138
tom5bcc9462014-09-19 10:11:31 -0700139 @Override
140 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700141 return hosts.size();
142 }
143
tom5bcc9462014-09-19 10:11:31 -0700144 @Override
145 public Iterable<Host> getHosts() {
toma56d5fe2014-09-17 11:05:47 -0700146 return Collections.unmodifiableSet(new HashSet<>(hosts.values()));
tom7869ad92014-09-09 14:32:08 -0700147 }
148
tom5bcc9462014-09-19 10:11:31 -0700149 @Override
150 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700151 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700152 }
153
tom5bcc9462014-09-19 10:11:31 -0700154 @Override
155 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700156 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700157 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700158 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700159 vlanset.add(h);
160 }
161 }
162 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700163 }
164
tom5bcc9462014-09-19 10:11:31 -0700165 @Override
166 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700167 Set<Host> macset = new HashSet<>();
168 for (Host h : hosts.values()) {
169 if (h.mac().equals(mac)) {
170 macset.add(h);
171 }
172 }
173 return macset;
tom7869ad92014-09-09 14:32:08 -0700174 }
175
tom5bcc9462014-09-19 10:11:31 -0700176 @Override
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700177 public Set<Host> getHosts(IpPrefix ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700178 Set<Host> ipset = new HashSet<>();
179 for (Host h : hosts.values()) {
180 if (h.ipAddresses().contains(ip)) {
181 ipset.add(h);
182 }
183 }
184 return ipset;
tom7869ad92014-09-09 14:32:08 -0700185 }
186
tom5bcc9462014-09-19 10:11:31 -0700187 @Override
188 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700189 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700190 }
191
tom5bcc9462014-09-19 10:11:31 -0700192 @Override
tom7869ad92014-09-09 14:32:08 -0700193 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700194 Set<Host> hostset = new HashSet<>();
195 for (ConnectPoint p : locations.keySet()) {
196 if (p.deviceId().equals(deviceId)) {
197 hostset.addAll(locations.get(p));
198 }
199 }
200 return hostset;
tom7869ad92014-09-09 14:32:08 -0700201 }
202
Jonathan Hartac60c082014-09-23 08:55:17 -0700203 @Override
204 public void updateAddressBindings(PortAddresses addresses) {
Jonathan Hart09585c62014-09-23 16:58:04 -0700205 // TODO portAddresses.put(addresses.connectPoint(), addresses);
Jonathan Hartac60c082014-09-23 08:55:17 -0700206 }
207
208 @Override
Jonathan Hart09585c62014-09-23 16:58:04 -0700209 public void removeAddressBindings(PortAddresses addresses) {
210 // TODO Auto-generated method stub
211
212 }
213
214 @Override
215 public void clearAddressBindings(ConnectPoint connectPoint) {
216 // TODO Auto-generated method stub
217
Jonathan Hartac60c082014-09-23 08:55:17 -0700218 }
219
220 @Override
221 public Set<PortAddresses> getAddressBindings() {
Jonathan Hart43c182c2014-09-23 11:13:42 -0700222 return new HashSet<>(portAddresses.values());
Jonathan Hartac60c082014-09-23 08:55:17 -0700223 }
224
225 @Override
226 public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) {
Jonathan Hart43c182c2014-09-23 11:13:42 -0700227 return portAddresses.get(connectPoint);
Jonathan Hartac60c082014-09-23 08:55:17 -0700228 }
229
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700230}