blob: d540afb8619fa7837e66a187269d1d6c2310de32 [file] [log] [blame]
tom578ebdc2014-09-11 11:12:51 -07001package org.onlab.onos.net.trivial.host.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;
tom7869ad92014-09-09 14:32:08 -070027import org.onlab.onos.net.provider.ProviderId;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070028import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070029import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
tom7869ad92014-09-09 14:32:08 -070031
Ayaka Koshibee5652752014-09-10 23:27:34 -070032import com.google.common.collect.HashMultimap;
33import com.google.common.collect.ImmutableSet;
34import com.google.common.collect.Multimap;
tom5bcc9462014-09-19 10:11:31 -070035import org.slf4j.Logger;
Ayaka Koshibee5652752014-09-10 23:27:34 -070036
tom7869ad92014-09-09 14:32:08 -070037/**
38 * Manages inventory of end-station hosts using trivial in-memory
39 * implementation.
40 */
tom5bcc9462014-09-19 10:11:31 -070041@Component(immediate = true)
42@Service
43public class SimpleHostStore implements HostStore {
tom7869ad92014-09-09 14:32:08 -070044
tom5bcc9462014-09-19 10:11:31 -070045 private final Logger log = getLogger(getClass());
46
47 // Host inventory
tom7869ad92014-09-09 14:32:08 -070048 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
49
tom5bcc9462014-09-19 10:11:31 -070050 // Hosts tracked by their location
Ayaka Koshibee5652752014-09-10 23:27:34 -070051 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070052
tom5bcc9462014-09-19 10:11:31 -070053 @Activate
54 public void activate() {
55 log.info("Started");
56 }
57
58 @Deactivate
59 public void deactivate() {
60 log.info("Stopped");
61 }
62
63 @Override
64 public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
65 HostDescription hostDescription) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070066 Host host = hosts.get(hostId);
67 if (host == null) {
68 return createHost(providerId, hostId, hostDescription);
69 }
70 return updateHost(providerId, host, hostDescription);
71 }
72
73 // creates a new host and sends HOST_ADDED
74 private HostEvent createHost(ProviderId providerId, HostId hostId,
toma56d5fe2014-09-17 11:05:47 -070075 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070076 DefaultHost newhost = new DefaultHost(providerId, hostId,
toma56d5fe2014-09-17 11:05:47 -070077 descr.hwAddress(),
78 descr.vlan(),
79 descr.location(),
80 descr.ipAddresses());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070081 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070082 hosts.put(hostId, newhost);
83 locations.put(descr.location(), newhost);
84 }
85 return new HostEvent(HOST_ADDED, newhost);
86 }
87
88 // checks for type of update to host, sends appropriate event
89 private HostEvent updateHost(ProviderId providerId, Host host,
toma56d5fe2014-09-17 11:05:47 -070090 HostDescription descr) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070091 DefaultHost updated;
92 HostEvent event;
toma56d5fe2014-09-17 11:05:47 -070093 if (!host.location().equals(descr.location())) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070094 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -070095 host.mac(),
96 host.vlan(),
97 descr.location(),
98 host.ipAddresses());
Ayaka Koshibee5652752014-09-10 23:27:34 -070099 event = new HostEvent(HOST_MOVED, updated);
toma56d5fe2014-09-17 11:05:47 -0700100
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700101 } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) {
102 updated = new DefaultHost(providerId, host.id(),
toma56d5fe2014-09-17 11:05:47 -0700103 host.mac(),
104 host.vlan(),
105 descr.location(),
106 descr.ipAddresses());
Ayaka Koshibe1a100982014-09-13 19:32:19 -0700107 event = new HostEvent(HOST_UPDATED, updated);
108 } else {
109 return null;
Ayaka Koshibee5652752014-09-10 23:27:34 -0700110 }
111 synchronized (this) {
112 hosts.put(host.id(), updated);
113 locations.remove(host.location(), host);
114 locations.put(updated.location(), updated);
115 }
116 return event;
tom7869ad92014-09-09 14:32:08 -0700117 }
118
tom5bcc9462014-09-19 10:11:31 -0700119 @Override
120 public HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700121 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700122 Host host = hosts.remove(hostId);
123 if (host != null) {
124 locations.remove((host.location()), host);
125 return new HostEvent(HOST_REMOVED, host);
126 }
127 return null;
128 }
tom7869ad92014-09-09 14:32:08 -0700129 }
130
tom5bcc9462014-09-19 10:11:31 -0700131 @Override
132 public int getHostCount() {
tom7869ad92014-09-09 14:32:08 -0700133 return hosts.size();
134 }
135
tom5bcc9462014-09-19 10:11:31 -0700136 @Override
137 public Iterable<Host> getHosts() {
toma56d5fe2014-09-17 11:05:47 -0700138 return Collections.unmodifiableSet(new HashSet<>(hosts.values()));
tom7869ad92014-09-09 14:32:08 -0700139 }
140
tom5bcc9462014-09-19 10:11:31 -0700141 @Override
142 public Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700143 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700144 }
145
tom5bcc9462014-09-19 10:11:31 -0700146 @Override
147 public Set<Host> getHosts(VlanId vlanId) {
toma56d5fe2014-09-17 11:05:47 -0700148 Set<Host> vlanset = new HashSet<>();
Ayaka Koshibee5652752014-09-10 23:27:34 -0700149 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700150 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700151 vlanset.add(h);
152 }
153 }
154 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700155 }
156
tom5bcc9462014-09-19 10:11:31 -0700157 @Override
158 public Set<Host> getHosts(MacAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700159 Set<Host> macset = new HashSet<>();
160 for (Host h : hosts.values()) {
161 if (h.mac().equals(mac)) {
162 macset.add(h);
163 }
164 }
165 return macset;
tom7869ad92014-09-09 14:32:08 -0700166 }
167
tom5bcc9462014-09-19 10:11:31 -0700168 @Override
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700169 public Set<Host> getHosts(IpPrefix ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700170 Set<Host> ipset = new HashSet<>();
171 for (Host h : hosts.values()) {
172 if (h.ipAddresses().contains(ip)) {
173 ipset.add(h);
174 }
175 }
176 return ipset;
tom7869ad92014-09-09 14:32:08 -0700177 }
178
tom5bcc9462014-09-19 10:11:31 -0700179 @Override
180 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700181 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700182 }
183
tom5bcc9462014-09-19 10:11:31 -0700184 @Override
tom7869ad92014-09-09 14:32:08 -0700185 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700186 Set<Host> hostset = new HashSet<>();
187 for (ConnectPoint p : locations.keySet()) {
188 if (p.deviceId().equals(deviceId)) {
189 hostset.addAll(locations.get(p));
190 }
191 }
192 return hostset;
tom7869ad92014-09-09 14:32:08 -0700193 }
194
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700195}