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; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.host.PortAddresses; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.provider.ProviderId; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 33 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 34 | import com.google.common.collect.HashMultimap; |
| 35 | import com.google.common.collect.ImmutableSet; |
| 36 | import com.google.common.collect.Multimap; |
| 37 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Manages inventory of end-station hosts using trivial in-memory |
| 40 | * implementation. |
| 41 | */ |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public class SimpleHostStore implements HostStore { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 45 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | // Host inventory |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 49 | private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); |
| 50 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 51 | // Hosts tracked by their location |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 52 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
tom | e615ee4 | 2014-09-11 10:52:10 -0700 | [diff] [blame] | 53 | |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 54 | private final Map<ConnectPoint, PortAddresses> portAddresses = |
| 55 | new ConcurrentHashMap<>(); |
| 56 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 57 | @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 Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 70 | 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, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 79 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 80 | DefaultHost newhost = new DefaultHost(providerId, hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 81 | descr.hwAddress(), |
| 82 | descr.vlan(), |
| 83 | descr.location(), |
| 84 | descr.ipAddresses()); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 85 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 86 | 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, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 94 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 95 | DefaultHost updated; |
| 96 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 97 | if (!host.location().equals(descr.location())) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 98 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 99 | host.mac(), |
| 100 | host.vlan(), |
| 101 | descr.location(), |
| 102 | host.ipAddresses()); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 103 | event = new HostEvent(HOST_MOVED, updated); |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 104 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 105 | } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) { |
| 106 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 107 | host.mac(), |
| 108 | host.vlan(), |
| 109 | descr.location(), |
| 110 | descr.ipAddresses()); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 111 | event = new HostEvent(HOST_UPDATED, updated); |
| 112 | } else { |
| 113 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 114 | } |
| 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 125 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 126 | 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 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 137 | return hosts.size(); |
| 138 | } |
| 139 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public Iterable<Host> getHosts() { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 142 | return Collections.unmodifiableSet(new HashSet<>(hosts.values())); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 147 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 148 | } |
| 149 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 152 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 153 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 154 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 155 | vlanset.add(h); |
| 156 | } |
| 157 | } |
| 158 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 159 | } |
| 160 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 163 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 170 | } |
| 171 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 172 | @Override |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 173 | public Set<Host> getHosts(IpPrefix ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 174 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 181 | } |
| 182 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 185 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 186 | } |
| 187 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 188 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 189 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 190 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public void updateAddressBindings(PortAddresses addresses) { |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame^] | 201 | // TODO portAddresses.put(addresses.connectPoint(), addresses); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame^] | 205 | 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 Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Set<PortAddresses> getAddressBindings() { |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 218 | return new HashSet<>(portAddresses.values()); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) { |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 223 | return portAddresses.get(connectPoint); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 226 | } |