blob: 2f9eb29360f26ca5e3124269dadfdc46d4186272 [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;
7
8import java.util.Collections;
9import java.util.HashSet;
10import java.util.Map;
11import java.util.Set;
12import java.util.concurrent.ConcurrentHashMap;
tom7869ad92014-09-09 14:32:08 -070013
14import org.onlab.onos.net.ConnectPoint;
Ayaka Koshibee5652752014-09-10 23:27:34 -070015import org.onlab.onos.net.DefaultHost;
tom7869ad92014-09-09 14:32:08 -070016import org.onlab.onos.net.DeviceId;
17import org.onlab.onos.net.Host;
18import org.onlab.onos.net.HostId;
19import org.onlab.onos.net.host.HostDescription;
20import org.onlab.onos.net.host.HostEvent;
21import org.onlab.onos.net.provider.ProviderId;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070022import org.onlab.packet.IPAddress;
tom7869ad92014-09-09 14:32:08 -070023import org.onlab.packet.MACAddress;
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070024import org.onlab.packet.VLANID;
tom7869ad92014-09-09 14:32:08 -070025
Ayaka Koshibee5652752014-09-10 23:27:34 -070026import com.google.common.collect.HashMultimap;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Multimap;
29
tom7869ad92014-09-09 14:32:08 -070030/**
31 * Manages inventory of end-station hosts using trivial in-memory
32 * implementation.
33 */
34public class SimpleHostStore {
35
36 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
37
Ayaka Koshibee5652752014-09-10 23:27:34 -070038 // hosts sorted based on their location
39 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
tome615ee42014-09-11 10:52:10 -070040
tom7869ad92014-09-09 14:32:08 -070041 /**
42 * Creates a new host or updates the existing one based on the specified
43 * description.
44 *
45 * @param providerId provider identification
46 * @param hostId host identification
47 * @param hostDescription host description data
48 * @return appropriate event or null if no change resulted
49 */
50 HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
alshabib51622f72014-09-11 11:22:33 -070051 HostDescription hostDescription) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070052 Host host = hosts.get(hostId);
53 if (host == null) {
54 return createHost(providerId, hostId, hostDescription);
55 }
56 return updateHost(providerId, host, hostDescription);
57 }
58
59 // creates a new host and sends HOST_ADDED
60 private HostEvent createHost(ProviderId providerId, HostId hostId,
61 HostDescription descr) {
62 DefaultHost newhost = new DefaultHost(providerId, hostId,
63 descr.hwAddress(),
64 descr.vlan(),
65 descr.location(),
66 descr.ipAddresses());
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070067 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -070068 hosts.put(hostId, newhost);
69 locations.put(descr.location(), newhost);
70 }
71 return new HostEvent(HOST_ADDED, newhost);
72 }
73
74 // checks for type of update to host, sends appropriate event
75 private HostEvent updateHost(ProviderId providerId, Host host,
76 HostDescription descr) {
77 DefaultHost updated;
78 HostEvent event;
79 if (host.location().equals(descr.location())) {
80 updated = new DefaultHost(providerId, host.id(),
81 host.mac(),
82 host.vlan(),
83 host.location(),
84 descr.ipAddresses());
85 event = new HostEvent(HOST_UPDATED, updated);
86 } else {
87 updated = new DefaultHost(providerId, host.id(),
88 host.mac(),
89 host.vlan(),
90 descr.location(),
91 host.ipAddresses());
92 event = new HostEvent(HOST_MOVED, updated);
93 }
94 synchronized (this) {
95 hosts.put(host.id(), updated);
96 locations.remove(host.location(), host);
97 locations.put(updated.location(), updated);
98 }
99 return event;
tom7869ad92014-09-09 14:32:08 -0700100 }
101
102 /**
103 * Removes the specified host from the inventory.
104 *
105 * @param hostId host identification
106 * @return remove even or null if host was not found
107 */
108 HostEvent removeHost(HostId hostId) {
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700109 synchronized (this) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700110 Host host = hosts.remove(hostId);
111 if (host != null) {
112 locations.remove((host.location()), host);
113 return new HostEvent(HOST_REMOVED, host);
114 }
115 return null;
116 }
tom7869ad92014-09-09 14:32:08 -0700117 }
118
119 /**
120 * Returns the number of hosts in the store.
121 *
122 * @return host count
123 */
124 int getHostCount() {
125 return hosts.size();
126 }
127
128 /**
129 * Returns a collection of all hosts in the store.
130 *
131 * @return iterable collection of all hosts
132 */
133 Iterable<Host> getHosts() {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700134 return Collections.unmodifiableSet(new HashSet<Host>(hosts.values()));
tom7869ad92014-09-09 14:32:08 -0700135 }
136
137 /**
138 * Returns the host with the specified identifer.
139 *
140 * @param hostId host identification
141 * @return host or null if not found
142 */
143 Host getHost(HostId hostId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700144 return hosts.get(hostId);
tom7869ad92014-09-09 14:32:08 -0700145 }
146
147 /**
148 * Returns the set of all hosts within the specified VLAN.
149 *
150 * @param vlanId vlan id
151 * @return set of hosts in the vlan
152 */
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700153 Set<Host> getHosts(VLANID vlanId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700154 Set<Host> vlanset = new HashSet<Host>();
155 for (Host h : hosts.values()) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700156 if (h.vlan().equals(vlanId)) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700157 vlanset.add(h);
158 }
159 }
160 return vlanset;
tom7869ad92014-09-09 14:32:08 -0700161 }
162
163 /**
164 * Returns the set of hosts with the specified MAC address.
165 *
166 * @param mac mac address
167 * @return set of hosts with the given mac
168 */
169 Set<Host> getHosts(MACAddress mac) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700170 Set<Host> macset = new HashSet<>();
171 for (Host h : hosts.values()) {
172 if (h.mac().equals(mac)) {
173 macset.add(h);
174 }
175 }
176 return macset;
tom7869ad92014-09-09 14:32:08 -0700177 }
178
179 /**
180 * Returns the set of hosts with the specified IP address.
181 *
182 * @param ip ip address
183 * @return set of hosts with the given IP
184 */
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700185 Set<Host> getHosts(IPAddress ip) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700186 Set<Host> ipset = new HashSet<>();
187 for (Host h : hosts.values()) {
188 if (h.ipAddresses().contains(ip)) {
189 ipset.add(h);
190 }
191 }
192 return ipset;
tom7869ad92014-09-09 14:32:08 -0700193 }
194
195 /**
196 * Returns the set of hosts whose location falls on the given connection point.
197 *
198 * @param connectPoint connection point
199 * @return set of hosts
200 */
201 Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700202 return ImmutableSet.copyOf(locations.get(connectPoint));
tom7869ad92014-09-09 14:32:08 -0700203 }
204
205 /**
206 * Returns the set of hosts whose location falls on the given device.
207 *
208 * @param deviceId infrastructure device identifier
209 * @return set of hosts
210 */
211 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Ayaka Koshibee5652752014-09-10 23:27:34 -0700212 Set<Host> hostset = new HashSet<>();
213 for (ConnectPoint p : locations.keySet()) {
214 if (p.deviceId().equals(deviceId)) {
215 hostset.addAll(locations.get(p));
216 }
217 }
218 return hostset;
tom7869ad92014-09-09 14:32:08 -0700219 }
220
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700221}