tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
| 2 | |
| 3 | import org.onlab.onos.net.ConnectPoint; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 4 | import org.onlab.onos.net.DefaultHost; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.DeviceId; |
| 6 | import org.onlab.onos.net.Host; |
| 7 | import org.onlab.onos.net.HostId; |
| 8 | import org.onlab.onos.net.host.HostDescription; |
| 9 | import org.onlab.onos.net.host.HostEvent; |
| 10 | import org.onlab.onos.net.provider.ProviderId; |
| 11 | import org.onlab.packet.IPv4; |
| 12 | import org.onlab.packet.MACAddress; |
| 13 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 14 | import com.google.common.collect.HashMultimap; |
| 15 | import com.google.common.collect.ImmutableSet; |
| 16 | import com.google.common.collect.Multimap; |
| 17 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.HashSet; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 24 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED; |
| 25 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
| 26 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED; |
| 27 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED; |
| 28 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Manages inventory of end-station hosts using trivial in-memory |
| 31 | * implementation. |
| 32 | */ |
| 33 | public class SimpleHostStore { |
| 34 | |
| 35 | private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); |
| 36 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 37 | // hosts sorted based on their location |
| 38 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Creates a new host or updates the existing one based on the specified |
| 41 | * description. |
| 42 | * |
| 43 | * @param providerId provider identification |
| 44 | * @param hostId host identification |
| 45 | * @param hostDescription host description data |
| 46 | * @return appropriate event or null if no change resulted |
| 47 | */ |
| 48 | HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 49 | HostDescription hostDescription) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 50 | Host host = hosts.get(hostId); |
| 51 | if (host == null) { |
| 52 | return createHost(providerId, hostId, hostDescription); |
| 53 | } |
| 54 | return updateHost(providerId, host, hostDescription); |
| 55 | } |
| 56 | |
| 57 | // creates a new host and sends HOST_ADDED |
| 58 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
| 59 | HostDescription descr) { |
| 60 | DefaultHost newhost = new DefaultHost(providerId, hostId, |
| 61 | descr.hwAddress(), |
| 62 | descr.vlan(), |
| 63 | descr.location(), |
| 64 | descr.ipAddresses()); |
| 65 | synchronized(this) { |
| 66 | hosts.put(hostId, newhost); |
| 67 | locations.put(descr.location(), newhost); |
| 68 | } |
| 69 | return new HostEvent(HOST_ADDED, newhost); |
| 70 | } |
| 71 | |
| 72 | // checks for type of update to host, sends appropriate event |
| 73 | private HostEvent updateHost(ProviderId providerId, Host host, |
| 74 | HostDescription descr) { |
| 75 | DefaultHost updated; |
| 76 | HostEvent event; |
| 77 | if (host.location().equals(descr.location())) { |
| 78 | updated = new DefaultHost(providerId, host.id(), |
| 79 | host.mac(), |
| 80 | host.vlan(), |
| 81 | host.location(), |
| 82 | descr.ipAddresses()); |
| 83 | event = new HostEvent(HOST_UPDATED, updated); |
| 84 | } else { |
| 85 | updated = new DefaultHost(providerId, host.id(), |
| 86 | host.mac(), |
| 87 | host.vlan(), |
| 88 | descr.location(), |
| 89 | host.ipAddresses()); |
| 90 | event = new HostEvent(HOST_MOVED, updated); |
| 91 | } |
| 92 | synchronized (this) { |
| 93 | hosts.put(host.id(), updated); |
| 94 | locations.remove(host.location(), host); |
| 95 | locations.put(updated.location(), updated); |
| 96 | } |
| 97 | return event; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Removes the specified host from the inventory. |
| 102 | * |
| 103 | * @param hostId host identification |
| 104 | * @return remove even or null if host was not found |
| 105 | */ |
| 106 | HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 107 | synchronized(this) { |
| 108 | Host host = hosts.remove(hostId); |
| 109 | if (host != null) { |
| 110 | locations.remove((host.location()), host); |
| 111 | return new HostEvent(HOST_REMOVED, host); |
| 112 | } |
| 113 | return null; |
| 114 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the number of hosts in the store. |
| 119 | * |
| 120 | * @return host count |
| 121 | */ |
| 122 | int getHostCount() { |
| 123 | return hosts.size(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns a collection of all hosts in the store. |
| 128 | * |
| 129 | * @return iterable collection of all hosts |
| 130 | */ |
| 131 | Iterable<Host> getHosts() { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 132 | return Collections.unmodifiableSet(new HashSet<Host>(hosts.values())); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the host with the specified identifer. |
| 137 | * |
| 138 | * @param hostId host identification |
| 139 | * @return host or null if not found |
| 140 | */ |
| 141 | Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 142 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the set of all hosts within the specified VLAN. |
| 147 | * |
| 148 | * @param vlanId vlan id |
| 149 | * @return set of hosts in the vlan |
| 150 | */ |
| 151 | Set<Host> getHosts(long vlanId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 152 | Set<Host> vlanset = new HashSet<Host>(); |
| 153 | for (Host h : hosts.values()) { |
| 154 | if (h.vlan() == vlanId) { |
| 155 | vlanset.add(h); |
| 156 | } |
| 157 | } |
| 158 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the set of hosts with the specified MAC address. |
| 163 | * |
| 164 | * @param mac mac address |
| 165 | * @return set of hosts with the given mac |
| 166 | */ |
| 167 | Set<Host> getHosts(MACAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 168 | Set<Host> macset = new HashSet<>(); |
| 169 | for (Host h : hosts.values()) { |
| 170 | if (h.mac().equals(mac)) { |
| 171 | macset.add(h); |
| 172 | } |
| 173 | } |
| 174 | return macset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the set of hosts with the specified IP address. |
| 179 | * |
| 180 | * @param ip ip address |
| 181 | * @return set of hosts with the given IP |
| 182 | */ |
| 183 | Set<Host> getHosts(IPv4 ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 184 | Set<Host> ipset = new HashSet<>(); |
| 185 | for (Host h : hosts.values()) { |
| 186 | if (h.ipAddresses().contains(ip)) { |
| 187 | ipset.add(h); |
| 188 | } |
| 189 | } |
| 190 | return ipset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns the set of hosts whose location falls on the given connection point. |
| 195 | * |
| 196 | * @param connectPoint connection point |
| 197 | * @return set of hosts |
| 198 | */ |
| 199 | Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 200 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns the set of hosts whose location falls on the given device. |
| 205 | * |
| 206 | * @param deviceId infrastructure device identifier |
| 207 | * @return set of hosts |
| 208 | */ |
| 209 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 210 | Set<Host> hostset = new HashSet<>(); |
| 211 | for (ConnectPoint p : locations.keySet()) { |
| 212 | if (p.deviceId().equals(deviceId)) { |
| 213 | hostset.addAll(locations.get(p)); |
| 214 | } |
| 215 | } |
| 216 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame^] | 219 | } |