tom | 578ebdc | 2014-09-11 11:12:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.host.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 | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.provider.ProviderId; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame^] | 28 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 31 | |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 32 | import com.google.common.collect.HashMultimap; |
| 33 | import com.google.common.collect.ImmutableSet; |
| 34 | import com.google.common.collect.Multimap; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 36 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Manages inventory of end-station hosts using trivial in-memory |
| 39 | * implementation. |
| 40 | */ |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class SimpleHostStore implements HostStore { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 44 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | // Host inventory |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 48 | private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); |
| 49 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 50 | // Hosts tracked by their location |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 51 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
tom | e615ee4 | 2014-09-11 10:52:10 -0700 | [diff] [blame] | 52 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 53 | @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 Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 66 | 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, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 75 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 76 | DefaultHost newhost = new DefaultHost(providerId, hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 77 | descr.hwAddress(), |
| 78 | descr.vlan(), |
| 79 | descr.location(), |
| 80 | descr.ipAddresses()); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 81 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 82 | 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, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 90 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 91 | DefaultHost updated; |
| 92 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 93 | if (!host.location().equals(descr.location())) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 94 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 95 | host.mac(), |
| 96 | host.vlan(), |
| 97 | descr.location(), |
| 98 | host.ipAddresses()); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 99 | event = new HostEvent(HOST_MOVED, updated); |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 100 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 101 | } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) { |
| 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 | descr.ipAddresses()); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 107 | event = new HostEvent(HOST_UPDATED, updated); |
| 108 | } else { |
| 109 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 110 | } |
| 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 121 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 122 | 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 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 129 | } |
| 130 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 133 | return hosts.size(); |
| 134 | } |
| 135 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public Iterable<Host> getHosts() { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 138 | return Collections.unmodifiableSet(new HashSet<>(hosts.values())); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 139 | } |
| 140 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 143 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 148 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 149 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 150 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 151 | vlanset.add(h); |
| 152 | } |
| 153 | } |
| 154 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 159 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 166 | } |
| 167 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 168 | @Override |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame^] | 169 | public Set<Host> getHosts(IpPrefix ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 170 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 177 | } |
| 178 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 181 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 182 | } |
| 183 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 184 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 185 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 186 | 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; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 195 | } |