tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 2 | |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
| 4 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED; |
| 5 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED; |
| 6 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 7 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 8 | |
| 9 | import java.util.Collections; |
| 10 | import java.util.HashSet; |
| 11 | import java.util.Map; |
| 12 | import java.util.Set; |
| 13 | import java.util.concurrent.ConcurrentHashMap; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 14 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 15 | import org.apache.felix.scr.annotations.Activate; |
| 16 | import org.apache.felix.scr.annotations.Component; |
| 17 | import org.apache.felix.scr.annotations.Deactivate; |
| 18 | import org.apache.felix.scr.annotations.Service; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.ConnectPoint; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.DefaultHost; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.Host; |
| 23 | import org.onlab.onos.net.HostId; |
| 24 | import org.onlab.onos.net.host.HostDescription; |
| 25 | import org.onlab.onos.net.host.HostEvent; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.host.HostStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 27 | import org.onlab.onos.net.host.HostStoreDelegate; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.host.PortAddresses; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.provider.ProviderId; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 30 | import org.onlab.onos.store.AbstractStore; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 31 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.packet.VlanId; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 36 | import com.google.common.collect.HashMultimap; |
| 37 | import com.google.common.collect.ImmutableSet; |
| 38 | import com.google.common.collect.Multimap; |
| 39 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Manages inventory of end-station hosts using trivial in-memory |
| 42 | * implementation. |
| 43 | */ |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 44 | @Component(immediate = true) |
| 45 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 46 | public class SimpleHostStore |
| 47 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 48 | implements HostStore { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 49 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | // Host inventory |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 53 | private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); |
| 54 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 55 | // Hosts tracked by their location |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 56 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
tom | e615ee4 | 2014-09-11 10:52:10 -0700 | [diff] [blame] | 57 | |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 58 | private final Map<ConnectPoint, PortAddresses> portAddresses = |
| 59 | new ConcurrentHashMap<>(); |
| 60 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 61 | @Activate |
| 62 | public void activate() { |
| 63 | log.info("Started"); |
| 64 | } |
| 65 | |
| 66 | @Deactivate |
| 67 | public void deactivate() { |
| 68 | log.info("Stopped"); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 73 | HostDescription hostDescription) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 74 | Host host = hosts.get(hostId); |
| 75 | if (host == null) { |
| 76 | return createHost(providerId, hostId, hostDescription); |
| 77 | } |
| 78 | return updateHost(providerId, host, hostDescription); |
| 79 | } |
| 80 | |
| 81 | // creates a new host and sends HOST_ADDED |
| 82 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 83 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 84 | DefaultHost newhost = new DefaultHost(providerId, hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 85 | descr.hwAddress(), |
| 86 | descr.vlan(), |
| 87 | descr.location(), |
| 88 | descr.ipAddresses()); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 89 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 90 | hosts.put(hostId, newhost); |
| 91 | locations.put(descr.location(), newhost); |
| 92 | } |
| 93 | return new HostEvent(HOST_ADDED, newhost); |
| 94 | } |
| 95 | |
| 96 | // checks for type of update to host, sends appropriate event |
| 97 | private HostEvent updateHost(ProviderId providerId, Host host, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 98 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 99 | DefaultHost updated; |
| 100 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 101 | if (!host.location().equals(descr.location())) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 102 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 103 | host.mac(), |
| 104 | host.vlan(), |
| 105 | descr.location(), |
| 106 | host.ipAddresses()); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 107 | event = new HostEvent(HOST_MOVED, updated); |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 108 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 109 | } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) { |
| 110 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 111 | host.mac(), |
| 112 | host.vlan(), |
| 113 | descr.location(), |
| 114 | descr.ipAddresses()); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 115 | event = new HostEvent(HOST_UPDATED, updated); |
| 116 | } else { |
| 117 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 118 | } |
| 119 | synchronized (this) { |
| 120 | hosts.put(host.id(), updated); |
| 121 | locations.remove(host.location(), host); |
| 122 | locations.put(updated.location(), updated); |
| 123 | } |
| 124 | return event; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 125 | } |
| 126 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 129 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 130 | Host host = hosts.remove(hostId); |
| 131 | if (host != null) { |
| 132 | locations.remove((host.location()), host); |
| 133 | return new HostEvent(HOST_REMOVED, host); |
| 134 | } |
| 135 | return null; |
| 136 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 141 | return hosts.size(); |
| 142 | } |
| 143 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public Iterable<Host> getHosts() { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 146 | return Collections.unmodifiableSet(new HashSet<>(hosts.values())); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 147 | } |
| 148 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 151 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 156 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 157 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 158 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 159 | vlanset.add(h); |
| 160 | } |
| 161 | } |
| 162 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 163 | } |
| 164 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 167 | Set<Host> macset = new HashSet<>(); |
| 168 | for (Host h : hosts.values()) { |
| 169 | if (h.mac().equals(mac)) { |
| 170 | macset.add(h); |
| 171 | } |
| 172 | } |
| 173 | return macset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 174 | } |
| 175 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 176 | @Override |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 177 | public Set<Host> getHosts(IpPrefix ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 178 | Set<Host> ipset = new HashSet<>(); |
| 179 | for (Host h : hosts.values()) { |
| 180 | if (h.ipAddresses().contains(ip)) { |
| 181 | ipset.add(h); |
| 182 | } |
| 183 | } |
| 184 | return ipset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 189 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 190 | } |
| 191 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 192 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 193 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 194 | Set<Host> hostset = new HashSet<>(); |
| 195 | for (ConnectPoint p : locations.keySet()) { |
| 196 | if (p.deviceId().equals(deviceId)) { |
| 197 | hostset.addAll(locations.get(p)); |
| 198 | } |
| 199 | } |
| 200 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 203 | @Override |
| 204 | public void updateAddressBindings(PortAddresses addresses) { |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 205 | // TODO portAddresses.put(addresses.connectPoint(), addresses); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 209 | public void removeAddressBindings(PortAddresses addresses) { |
| 210 | // TODO Auto-generated method stub |
| 211 | |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void clearAddressBindings(ConnectPoint connectPoint) { |
| 216 | // TODO Auto-generated method stub |
| 217 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Set<PortAddresses> getAddressBindings() { |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 222 | return new HashSet<>(portAddresses.values()); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) { |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 227 | return portAddresses.get(connectPoint); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 230 | } |