blob: bcd84dff8e09445d8cdc8cded9a7b6f0a0ca93cd [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
Jonathan Hart43c182c2014-09-23 11:13:42 -070054 private final Map<ConnectPoint, PortAddresses> portAddresses =
55 new ConcurrentHashMap<>();
56
tom5bcc9462014-09-19 10:11:31 -070057 @Activate
58 public void activate() {
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 log.info("Stopped");
65 }
66
67 @Override
68 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
69 HostDescription hostDescription) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070070 Host host = hosts.get(hostId);
71 if (host == null) {
72 return createHost(providerId, hostId, hostDescription);
73 }
74 return updateHost(providerId, host, hostDescription);
75 }
76
77 // creates a new host and sends HOST_ADDED
78 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -070079 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070080 DefaultHost newhost = new DefaultHost(providerId, hostId,
toma56d5fe2014-09-17 11:05:47 -070081 descr.hwAddress(),
82 descr.vlan(),
83 descr.location(),
84 descr.ipAddresses());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070085 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070086 hosts.put(hostId, newhost);
87 locations.put(descr.location(), newhost);
88 }
89 return new HostEvent(HOST_ADDED, newhost);
90 }
91
92 // checks for type of update to host, sends appropriate event
93 private HostEvent updateHost(ProviderId providerId, Host host,
toma56d5fe2014-09-17 11:05:47 -070094 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070095 DefaultHost updated;
96 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -070097 if (!host.location().equals(descr.location())) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070098 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -070099 host.mac(),
100 host.vlan(),
101 descr.location(),
102 host.ipAddresses());
Ayaka Koshibee5652752014-09-10 23:27:34 -0700103 event = new HostEvent(HOST_MOVED, updated);
toma56d5fe2014-09-17 11:05:47 -0700104
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700105 } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) {
106 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -0700107 host.mac(),
108 host.vlan(),
109 descr.location(),
110 descr.ipAddresses());
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700111 event = new HostEvent(HOST_UPDATED, updated);
112 } else {
113 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700114 }
115 synchronized (this) {
116 hosts.put(host.id(), updated);
117 locations.remove(host.location(), host);
118 locations.put(updated.location(), updated);
119 }
120 return event;
tom7869ad92014-09-09 14:32:08 -0700121 }
122
tom5bcc9462014-09-19 10:11:31 -0700123 @Override
124 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700125 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700126 Host host = hosts.remove(hostId);
127 if (host != null) {
128 locations.remove((host.location()), host);
129 return new HostEvent(HOST_REMOVED, host);
130 }
131 return null;
132 }
tom7869ad92014-09-09 14:32:08 -0700133 }
134
tom5bcc9462014-09-19 10:11:31 -0700135 @Override
136 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700137 return hosts.size();
138 }
139
tom5bcc9462014-09-19 10:11:31 -0700140 @Override
141 public Iterable<Host> getHosts() {
toma56d5fe2014-09-17 11:05:47 -0700142 return Collections.unmodifiableSet(new HashSet<>(hosts.values()));
tom7869ad92014-09-09 14:32:08 -0700143 }
144
tom5bcc9462014-09-19 10:11:31 -0700145 @Override
146 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700147 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700148 }
149
tom5bcc9462014-09-19 10:11:31 -0700150 @Override
151 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700152 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700153 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700154 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700155 vlanset.add(h);
156 }
157 }
158 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700159 }
160
tom5bcc9462014-09-19 10:11:31 -0700161 @Override
162 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700163 Set<Host> macset = new HashSet<>();
164 for (Host h : hosts.values()) {
165 if (h.mac().equals(mac)) {
166 macset.add(h);
167 }
168 }
169 return macset;
tom7869ad92014-09-09 14:32:08 -0700170 }
171
tom5bcc9462014-09-19 10:11:31 -0700172 @Override
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700173 public Set<Host> getHosts(IpPrefix ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700174 Set<Host> ipset = new HashSet<>();
175 for (Host h : hosts.values()) {
176 if (h.ipAddresses().contains(ip)) {
177 ipset.add(h);
178 }
179 }
180 return ipset;
tom7869ad92014-09-09 14:32:08 -0700181 }
182
tom5bcc9462014-09-19 10:11:31 -0700183 @Override
184 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700185 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700186 }
187
tom5bcc9462014-09-19 10:11:31 -0700188 @Override
tom7869ad92014-09-09 14:32:08 -0700189 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700190 Set<Host> hostset = new HashSet<>();
191 for (ConnectPoint p : locations.keySet()) {
192 if (p.deviceId().equals(deviceId)) {
193 hostset.addAll(locations.get(p));
194 }
195 }
196 return hostset;
tom7869ad92014-09-09 14:32:08 -0700197 }
198
Jonathan Hartac60c082014-09-23 08:55:17 -0700199 @Override
200 public void updateAddressBindings(PortAddresses addresses) {
Jonathan Hart09585c62014-09-23 16:58:04 -0700201 // TODO portAddresses.put(addresses.connectPoint(), addresses);
Jonathan Hartac60c082014-09-23 08:55:17 -0700202 }
203
204 @Override
Jonathan Hart09585c62014-09-23 16:58:04 -0700205 public void removeAddressBindings(PortAddresses addresses) {
206 // TODO Auto-generated method stub
207
208 }
209
210 @Override
211 public void clearAddressBindings(ConnectPoint connectPoint) {
212 // TODO Auto-generated method stub
213
Jonathan Hartac60c082014-09-23 08:55:17 -0700214 }
215
216 @Override
217 public Set<PortAddresses> getAddressBindings() {
Jonathan Hart43c182c2014-09-23 11:13:42 -0700218 return new HashSet<>(portAddresses.values());
Jonathan Hartac60c082014-09-23 08:55:17 -0700219 }
220
221 @Override
222 public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) {
Jonathan Hart43c182c2014-09-23 11:13:42 -0700223 return portAddresses.get(connectPoint);
Jonathan Hartac60c082014-09-23 08:55:17 -0700224 }
225
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700226}