blob: 752ec9ecc12a5ae09bc4db5e9ac19b6ab4b0d31d [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;
Jonathan Hartac60c082014-09-23 08:55:17 -070027import org.onlab.onos.net.host.PortAddresses;
tom7869ad92014-09-09 14:32:08 -070028import org.onlab.onos.net.provider.ProviderId;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070029import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070030import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
Jonathan Hartac60c082014-09-23 08:55:17 -070032import org.slf4j.Logger;
tom7869ad92014-09-09 14:32:08 -070033
Ayaka Koshibee5652752014-09-10 23:27:34 -070034import com.google.common.collect.HashMultimap;
35import com.google.common.collect.ImmutableSet;
36import com.google.common.collect.Multimap;
37
tom7869ad92014-09-09 14:32:08 -070038/**
39 * Manages inventory of end-station hosts using trivial in-memory
40 * implementation.
41 */
tom5bcc9462014-09-19 10:11:31 -070042@Component(immediate = true)
43@Service
44public class SimpleHostStore implements HostStore {
tom7869ad92014-09-09 14:32:08 -070045
tom5bcc9462014-09-19 10:11:31 -070046 private final Logger log = getLogger(getClass());
47
48 // Host inventory
tom7869ad92014-09-09 14:32:08 -070049 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
50
tom5bcc9462014-09-19 10:11:31 -070051 // Hosts tracked by their location
Ayaka Koshibee5652752014-09-10 23:27:34 -070052 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070053
tom5bcc9462014-09-19 10:11:31 -070054 @Activate
55 public void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
61 log.info("Stopped");
62 }
63
64 @Override
65 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
66 HostDescription hostDescription) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070067 Host host = hosts.get(hostId);
68 if (host == null) {
69 return createHost(providerId, hostId, hostDescription);
70 }
71 return updateHost(providerId, host, hostDescription);
72 }
73
74 // creates a new host and sends HOST_ADDED
75 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -070076 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070077 DefaultHost newhost = new DefaultHost(providerId, hostId,
toma56d5fe2014-09-17 11:05:47 -070078 descr.hwAddress(),
79 descr.vlan(),
80 descr.location(),
81 descr.ipAddresses());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070082 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070083 hosts.put(hostId, newhost);
84 locations.put(descr.location(), newhost);
85 }
86 return new HostEvent(HOST_ADDED, newhost);
87 }
88
89 // checks for type of update to host, sends appropriate event
90 private HostEvent updateHost(ProviderId providerId, Host host,
toma56d5fe2014-09-17 11:05:47 -070091 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070092 DefaultHost updated;
93 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -070094 if (!host.location().equals(descr.location())) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070095 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -070096 host.mac(),
97 host.vlan(),
98 descr.location(),
99 host.ipAddresses());
Ayaka Koshibee5652752014-09-10 23:27:34 -0700100 event = new HostEvent(HOST_MOVED, updated);
toma56d5fe2014-09-17 11:05:47 -0700101
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700102 } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) {
103 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -0700104 host.mac(),
105 host.vlan(),
106 descr.location(),
107 descr.ipAddresses());
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700108 event = new HostEvent(HOST_UPDATED, updated);
109 } else {
110 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700111 }
112 synchronized (this) {
113 hosts.put(host.id(), updated);
114 locations.remove(host.location(), host);
115 locations.put(updated.location(), updated);
116 }
117 return event;
tom7869ad92014-09-09 14:32:08 -0700118 }
119
tom5bcc9462014-09-19 10:11:31 -0700120 @Override
121 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700122 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700123 Host host = hosts.remove(hostId);
124 if (host != null) {
125 locations.remove((host.location()), host);
126 return new HostEvent(HOST_REMOVED, host);
127 }
128 return null;
129 }
tom7869ad92014-09-09 14:32:08 -0700130 }
131
tom5bcc9462014-09-19 10:11:31 -0700132 @Override
133 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700134 return hosts.size();
135 }
136
tom5bcc9462014-09-19 10:11:31 -0700137 @Override
138 public Iterable<Host> getHosts() {
toma56d5fe2014-09-17 11:05:47 -0700139 return Collections.unmodifiableSet(new HashSet<>(hosts.values()));
tom7869ad92014-09-09 14:32:08 -0700140 }
141
tom5bcc9462014-09-19 10:11:31 -0700142 @Override
143 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700144 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700145 }
146
tom5bcc9462014-09-19 10:11:31 -0700147 @Override
148 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700149 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700150 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700151 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700152 vlanset.add(h);
153 }
154 }
155 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700156 }
157
tom5bcc9462014-09-19 10:11:31 -0700158 @Override
159 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700160 Set<Host> macset = new HashSet<>();
161 for (Host h : hosts.values()) {
162 if (h.mac().equals(mac)) {
163 macset.add(h);
164 }
165 }
166 return macset;
tom7869ad92014-09-09 14:32:08 -0700167 }
168
tom5bcc9462014-09-19 10:11:31 -0700169 @Override
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700170 public Set<Host> getHosts(IpPrefix ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700171 Set<Host> ipset = new HashSet<>();
172 for (Host h : hosts.values()) {
173 if (h.ipAddresses().contains(ip)) {
174 ipset.add(h);
175 }
176 }
177 return ipset;
tom7869ad92014-09-09 14:32:08 -0700178 }
179
tom5bcc9462014-09-19 10:11:31 -0700180 @Override
181 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700182 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700183 }
184
tom5bcc9462014-09-19 10:11:31 -0700185 @Override
tom7869ad92014-09-09 14:32:08 -0700186 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700187 Set<Host> hostset = new HashSet<>();
188 for (ConnectPoint p : locations.keySet()) {
189 if (p.deviceId().equals(deviceId)) {
190 hostset.addAll(locations.get(p));
191 }
192 }
193 return hostset;
tom7869ad92014-09-09 14:32:08 -0700194 }
195
Jonathan Hartac60c082014-09-23 08:55:17 -0700196 @Override
197 public void updateAddressBindings(PortAddresses addresses) {
198 // TODO Auto-generated method stub
199
200 }
201
202 @Override
203 public void removeAddressBindings(ConnectPoint connectPoint) {
204 // TODO Auto-generated method stub
205
206 }
207
208 @Override
209 public Set<PortAddresses> getAddressBindings() {
210 // TODO Auto-generated method stub
211 return null;
212 }
213
214 @Override
215 public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) {
216 // TODO Auto-generated method stub
217 return null;
218 }
219
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700220}