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 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 54 | @Activate |
| 55 | public void activate() { |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
| 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 66 | HostDescription hostDescription) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 67 | Host host = hosts.get(hostId); |
| 68 | if (host == null) { |
| 69 | return createHost(providerId, hostId, hostDescription); |
| 70 | } |
| 71 | return updateHost(providerId, host, hostDescription); |
| 72 | } |
| 73 | |
| 74 | // creates a new host and sends HOST_ADDED |
| 75 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 76 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 77 | DefaultHost newhost = new DefaultHost(providerId, hostId, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 78 | descr.hwAddress(), |
| 79 | descr.vlan(), |
| 80 | descr.location(), |
| 81 | descr.ipAddresses()); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 82 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 83 | hosts.put(hostId, newhost); |
| 84 | locations.put(descr.location(), newhost); |
| 85 | } |
| 86 | return new HostEvent(HOST_ADDED, newhost); |
| 87 | } |
| 88 | |
| 89 | // checks for type of update to host, sends appropriate event |
| 90 | private HostEvent updateHost(ProviderId providerId, Host host, |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 91 | HostDescription descr) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 92 | DefaultHost updated; |
| 93 | HostEvent event; |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 94 | if (!host.location().equals(descr.location())) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 95 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 96 | host.mac(), |
| 97 | host.vlan(), |
| 98 | descr.location(), |
| 99 | host.ipAddresses()); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 100 | event = new HostEvent(HOST_MOVED, updated); |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 101 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 102 | } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) { |
| 103 | updated = new DefaultHost(providerId, host.id(), |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 104 | host.mac(), |
| 105 | host.vlan(), |
| 106 | descr.location(), |
| 107 | descr.ipAddresses()); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 108 | event = new HostEvent(HOST_UPDATED, updated); |
| 109 | } else { |
| 110 | return null; |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 111 | } |
| 112 | synchronized (this) { |
| 113 | hosts.put(host.id(), updated); |
| 114 | locations.remove(host.location(), host); |
| 115 | locations.put(updated.location(), updated); |
| 116 | } |
| 117 | return event; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 118 | } |
| 119 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public HostEvent removeHost(HostId hostId) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 122 | synchronized (this) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 123 | Host host = hosts.remove(hostId); |
| 124 | if (host != null) { |
| 125 | locations.remove((host.location()), host); |
| 126 | return new HostEvent(HOST_REMOVED, host); |
| 127 | } |
| 128 | return null; |
| 129 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public int getHostCount() { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 134 | return hosts.size(); |
| 135 | } |
| 136 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public Iterable<Host> getHosts() { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 139 | return Collections.unmodifiableSet(new HashSet<>(hosts.values())); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 140 | } |
| 141 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public Host getHost(HostId hostId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 144 | return hosts.get(hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 145 | } |
| 146 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public Set<Host> getHosts(VlanId vlanId) { |
tom | a56d5fe | 2014-09-17 11:05:47 -0700 | [diff] [blame] | 149 | Set<Host> vlanset = new HashSet<>(); |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 150 | for (Host h : hosts.values()) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 151 | if (h.vlan().equals(vlanId)) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 152 | vlanset.add(h); |
| 153 | } |
| 154 | } |
| 155 | return vlanset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 156 | } |
| 157 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public Set<Host> getHosts(MacAddress mac) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 160 | Set<Host> macset = new HashSet<>(); |
| 161 | for (Host h : hosts.values()) { |
| 162 | if (h.mac().equals(mac)) { |
| 163 | macset.add(h); |
| 164 | } |
| 165 | } |
| 166 | return macset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 167 | } |
| 168 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 169 | @Override |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 170 | public Set<Host> getHosts(IpPrefix ip) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 171 | Set<Host> ipset = new HashSet<>(); |
| 172 | for (Host h : hosts.values()) { |
| 173 | if (h.ipAddresses().contains(ip)) { |
| 174 | ipset.add(h); |
| 175 | } |
| 176 | } |
| 177 | return ipset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 178 | } |
| 179 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 182 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 183 | } |
| 184 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 185 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 186 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Ayaka Koshibe | e565275 | 2014-09-10 23:27:34 -0700 | [diff] [blame] | 187 | Set<Host> hostset = new HashSet<>(); |
| 188 | for (ConnectPoint p : locations.keySet()) { |
| 189 | if (p.deviceId().equals(deviceId)) { |
| 190 | hostset.addAll(locations.get(p)); |
| 191 | } |
| 192 | } |
| 193 | return hostset; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame^] | 196 | @Override |
| 197 | public void updateAddressBindings(PortAddresses addresses) { |
| 198 | // TODO Auto-generated method stub |
| 199 | |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void removeAddressBindings(ConnectPoint connectPoint) { |
| 204 | // TODO Auto-generated method stub |
| 205 | |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<PortAddresses> getAddressBindings() { |
| 210 | // TODO Auto-generated method stub |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) { |
| 216 | // TODO Auto-generated method stub |
| 217 | return null; |
| 218 | } |
| 219 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 220 | } |