Committed by
Gerrit Code Review
ONOS-2624
Fix host annotations of host description Change-Id: I4822741e8cdef5c9620f5f2b8cf260fc0f523bf9
Showing
2 changed files
with
415 additions
and
413 deletions
| 1 | -package org.onosproject.store.host.impl; | 1 | +package org.onosproject.store.host.impl; |
| 2 | - | 2 | + |
| 3 | -import static com.google.common.base.Preconditions.checkNotNull; | 3 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | -import static org.onosproject.net.DefaultAnnotations.merge; | 4 | +import static org.onosproject.net.DefaultAnnotations.merge; |
| 5 | -import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; | 5 | +import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 6 | -import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; | 6 | +import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
| 7 | -import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; | 7 | +import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; |
| 8 | -import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; | 8 | +import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 9 | -import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; | 9 | +import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 10 | -import static org.slf4j.LoggerFactory.getLogger; | 10 | +import static org.slf4j.LoggerFactory.getLogger; |
| 11 | - | 11 | + |
| 12 | -import java.util.Collection; | 12 | +import java.util.Collection; |
| 13 | -import java.util.Collections; | 13 | +import java.util.Collections; |
| 14 | -import java.util.Objects; | 14 | +import java.util.Objects; |
| 15 | -import java.util.Set; | 15 | +import java.util.Set; |
| 16 | -import java.util.function.Predicate; | 16 | +import java.util.function.Predicate; |
| 17 | -import java.util.stream.Collectors; | 17 | +import java.util.stream.Collectors; |
| 18 | - | 18 | + |
| 19 | -import org.apache.felix.scr.annotations.Activate; | 19 | +import org.apache.felix.scr.annotations.Activate; |
| 20 | -import org.apache.felix.scr.annotations.Component; | 20 | +import org.apache.felix.scr.annotations.Component; |
| 21 | -import org.apache.felix.scr.annotations.Deactivate; | 21 | +import org.apache.felix.scr.annotations.Deactivate; |
| 22 | -import org.apache.felix.scr.annotations.Reference; | 22 | +import org.apache.felix.scr.annotations.Reference; |
| 23 | -import org.apache.felix.scr.annotations.ReferenceCardinality; | 23 | +import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | -import org.apache.felix.scr.annotations.Service; | 24 | +import org.apache.felix.scr.annotations.Service; |
| 25 | -import org.onlab.packet.IpAddress; | 25 | +import org.onlab.packet.IpAddress; |
| 26 | -import org.onlab.packet.MacAddress; | 26 | +import org.onlab.packet.MacAddress; |
| 27 | -import org.onlab.packet.VlanId; | 27 | +import org.onlab.packet.VlanId; |
| 28 | -import org.onlab.util.KryoNamespace; | 28 | +import org.onlab.util.KryoNamespace; |
| 29 | -import org.onosproject.net.Annotations; | 29 | +import org.onosproject.net.Annotations; |
| 30 | -import org.onosproject.net.ConnectPoint; | 30 | +import org.onosproject.net.ConnectPoint; |
| 31 | -import org.onosproject.net.DefaultAnnotations; | 31 | +import org.onosproject.net.DefaultAnnotations; |
| 32 | -import org.onosproject.net.DefaultHost; | 32 | +import org.onosproject.net.DefaultHost; |
| 33 | -import org.onosproject.net.DeviceId; | 33 | +import org.onosproject.net.DeviceId; |
| 34 | -import org.onosproject.net.Host; | 34 | +import org.onosproject.net.Host; |
| 35 | -import org.onosproject.net.HostId; | 35 | +import org.onosproject.net.HostId; |
| 36 | -import org.onosproject.net.host.HostDescription; | 36 | +import org.onosproject.net.host.HostDescription; |
| 37 | -import org.onosproject.net.host.HostEvent; | 37 | +import org.onosproject.net.host.HostEvent; |
| 38 | -import org.onosproject.net.host.HostStore; | 38 | +import org.onosproject.net.host.HostStore; |
| 39 | -import org.onosproject.net.host.HostStoreDelegate; | 39 | +import org.onosproject.net.host.HostStoreDelegate; |
| 40 | -import org.onosproject.net.host.PortAddresses; | 40 | +import org.onosproject.net.host.PortAddresses; |
| 41 | -import org.onosproject.net.host.HostEvent.Type; | 41 | +import org.onosproject.net.host.HostEvent.Type; |
| 42 | -import org.onosproject.net.provider.ProviderId; | 42 | +import org.onosproject.net.provider.ProviderId; |
| 43 | -import org.onosproject.store.AbstractStore; | 43 | +import org.onosproject.store.AbstractStore; |
| 44 | -import org.onosproject.store.serializers.KryoNamespaces; | 44 | +import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | -import org.onosproject.store.service.EventuallyConsistentMap; | 45 | +import org.onosproject.store.service.EventuallyConsistentMap; |
| 46 | -import org.onosproject.store.service.EventuallyConsistentMapEvent; | 46 | +import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 47 | -import org.onosproject.store.service.EventuallyConsistentMapListener; | 47 | +import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 48 | -import org.onosproject.store.service.LogicalClockService; | 48 | +import org.onosproject.store.service.LogicalClockService; |
| 49 | -import org.onosproject.store.service.StorageService; | 49 | +import org.onosproject.store.service.StorageService; |
| 50 | -import org.slf4j.Logger; | 50 | +import org.slf4j.Logger; |
| 51 | - | 51 | + |
| 52 | -import com.google.common.collect.HashMultimap; | 52 | +import com.google.common.collect.HashMultimap; |
| 53 | -import com.google.common.collect.ImmutableMultimap; | 53 | +import com.google.common.collect.ImmutableMultimap; |
| 54 | -import com.google.common.collect.ImmutableSet; | 54 | +import com.google.common.collect.ImmutableSet; |
| 55 | -import com.google.common.collect.Multimaps; | 55 | +import com.google.common.collect.Multimaps; |
| 56 | -import com.google.common.collect.SetMultimap; | 56 | +import com.google.common.collect.SetMultimap; |
| 57 | -import com.google.common.collect.Sets; | 57 | +import com.google.common.collect.Sets; |
| 58 | - | 58 | + |
| 59 | -/** | 59 | +/** |
| 60 | - * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. | 60 | + * Manages the inventory of hosts using a {@code EventuallyConsistentMap}. |
| 61 | - */ | 61 | + */ |
| 62 | -@Component(immediate = true) | 62 | +@Component(immediate = true) |
| 63 | -@Service | 63 | +@Service |
| 64 | -public class ECHostStore | 64 | +public class ECHostStore |
| 65 | - extends AbstractStore<HostEvent, HostStoreDelegate> | 65 | + extends AbstractStore<HostEvent, HostStoreDelegate> |
| 66 | - implements HostStore { | 66 | + implements HostStore { |
| 67 | - | 67 | + |
| 68 | - private final Logger log = getLogger(getClass()); | 68 | + private final Logger log = getLogger(getClass()); |
| 69 | - | 69 | + |
| 70 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 70 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | - protected StorageService storageService; | 71 | + protected StorageService storageService; |
| 72 | - | 72 | + |
| 73 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 73 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | - protected LogicalClockService clockService; | 74 | + protected LogicalClockService clockService; |
| 75 | - | 75 | + |
| 76 | - // Hosts tracked by their location | 76 | + // Hosts tracked by their location |
| 77 | - private final SetMultimap<ConnectPoint, Host> locations = | 77 | + private final SetMultimap<ConnectPoint, Host> locations = |
| 78 | - Multimaps.synchronizedSetMultimap( | 78 | + Multimaps.synchronizedSetMultimap( |
| 79 | - HashMultimap.<ConnectPoint, Host>create()); | 79 | + HashMultimap.<ConnectPoint, Host>create()); |
| 80 | - | 80 | + |
| 81 | - private final SetMultimap<ConnectPoint, PortAddresses> portAddresses = | 81 | + private final SetMultimap<ConnectPoint, PortAddresses> portAddresses = |
| 82 | - Multimaps.synchronizedSetMultimap( | 82 | + Multimaps.synchronizedSetMultimap( |
| 83 | - HashMultimap.<ConnectPoint, PortAddresses>create()); | 83 | + HashMultimap.<ConnectPoint, PortAddresses>create()); |
| 84 | - | 84 | + |
| 85 | - private EventuallyConsistentMap<HostId, DefaultHost> hosts; | 85 | + private EventuallyConsistentMap<HostId, DefaultHost> hosts; |
| 86 | - | 86 | + |
| 87 | - private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker = | 87 | + private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker = |
| 88 | - new HostLocationTracker(); | 88 | + new HostLocationTracker(); |
| 89 | - | 89 | + |
| 90 | - @Activate | 90 | + @Activate |
| 91 | - public void activate() { | 91 | + public void activate() { |
| 92 | - KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() | 92 | + KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 93 | - .register(KryoNamespaces.API); | 93 | + .register(KryoNamespaces.API); |
| 94 | - | 94 | + |
| 95 | - hosts = storageService.<HostId, DefaultHost>eventuallyConsistentMapBuilder() | 95 | + hosts = storageService.<HostId, DefaultHost>eventuallyConsistentMapBuilder() |
| 96 | - .withName("onos-hosts") | 96 | + .withName("onos-hosts") |
| 97 | - .withSerializer(hostSerializer) | 97 | + .withSerializer(hostSerializer) |
| 98 | - .withTimestampProvider((k, v) -> clockService.getTimestamp()) | 98 | + .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 99 | - .build(); | 99 | + .build(); |
| 100 | - | 100 | + |
| 101 | - hosts.addListener(hostLocationTracker); | 101 | + hosts.addListener(hostLocationTracker); |
| 102 | - | 102 | + |
| 103 | - log.info("Started"); | 103 | + log.info("Started"); |
| 104 | - } | 104 | + } |
| 105 | - | 105 | + |
| 106 | - @Deactivate | 106 | + @Deactivate |
| 107 | - public void deactivate() { | 107 | + public void deactivate() { |
| 108 | - hosts.removeListener(hostLocationTracker); | 108 | + hosts.removeListener(hostLocationTracker); |
| 109 | - hosts.destroy(); | 109 | + hosts.destroy(); |
| 110 | - locations.clear(); | 110 | + locations.clear(); |
| 111 | - portAddresses.clear(); | 111 | + portAddresses.clear(); |
| 112 | - | 112 | + |
| 113 | - log.info("Stopped"); | 113 | + log.info("Stopped"); |
| 114 | - } | 114 | + } |
| 115 | - | 115 | + |
| 116 | - @Override | 116 | + @Override |
| 117 | - public HostEvent createOrUpdateHost(ProviderId providerId, | 117 | + public HostEvent createOrUpdateHost(ProviderId providerId, |
| 118 | - HostId hostId, | 118 | + HostId hostId, |
| 119 | - HostDescription hostDescription) { | 119 | + HostDescription hostDescription) { |
| 120 | - DefaultHost currentHost = hosts.get(hostId); | 120 | + DefaultHost currentHost = hosts.get(hostId); |
| 121 | - if (currentHost == null) { | 121 | + if (currentHost == null) { |
| 122 | - DefaultHost newhost = new DefaultHost( | 122 | + DefaultHost newhost = new DefaultHost( |
| 123 | - providerId, | 123 | + providerId, |
| 124 | - hostId, | 124 | + hostId, |
| 125 | - hostDescription.hwAddress(), | 125 | + hostDescription.hwAddress(), |
| 126 | - hostDescription.vlan(), | 126 | + hostDescription.vlan(), |
| 127 | - hostDescription.location(), | 127 | + hostDescription.location(), |
| 128 | - ImmutableSet.copyOf(hostDescription.ipAddress())); | 128 | + ImmutableSet.copyOf(hostDescription.ipAddress()), |
| 129 | - hosts.put(hostId, newhost); | 129 | + hostDescription.annotations()); |
| 130 | - return new HostEvent(HOST_ADDED, newhost); | 130 | + hosts.put(hostId, newhost); |
| 131 | - } | 131 | + return new HostEvent(HOST_ADDED, newhost); |
| 132 | - return updateHost(providerId, hostId, hostDescription, currentHost); | 132 | + } |
| 133 | - } | 133 | + return updateHost(providerId, hostId, hostDescription, currentHost); |
| 134 | - | 134 | + } |
| 135 | - @Override | 135 | + |
| 136 | - public HostEvent removeHost(HostId hostId) { | 136 | + @Override |
| 137 | - Host host = hosts.remove(hostId); | 137 | + public HostEvent removeHost(HostId hostId) { |
| 138 | - return host != null ? new HostEvent(HOST_REMOVED, host) : null; | 138 | + Host host = hosts.remove(hostId); |
| 139 | - } | 139 | + return host != null ? new HostEvent(HOST_REMOVED, host) : null; |
| 140 | - | 140 | + } |
| 141 | - @Override | 141 | + |
| 142 | - public int getHostCount() { | 142 | + @Override |
| 143 | - return hosts.size(); | 143 | + public int getHostCount() { |
| 144 | - } | 144 | + return hosts.size(); |
| 145 | - | 145 | + } |
| 146 | - @Override | 146 | + |
| 147 | - public Iterable<Host> getHosts() { | 147 | + @Override |
| 148 | - return ImmutableSet.copyOf(hosts.values()); | 148 | + public Iterable<Host> getHosts() { |
| 149 | - } | 149 | + return ImmutableSet.copyOf(hosts.values()); |
| 150 | - | 150 | + } |
| 151 | - @Override | 151 | + |
| 152 | - public Host getHost(HostId hostId) { | 152 | + @Override |
| 153 | - return hosts.get(hostId); | 153 | + public Host getHost(HostId hostId) { |
| 154 | - } | 154 | + return hosts.get(hostId); |
| 155 | - | 155 | + } |
| 156 | - @Override | 156 | + |
| 157 | - public Set<Host> getHosts(VlanId vlanId) { | 157 | + @Override |
| 158 | - return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); | 158 | + public Set<Host> getHosts(VlanId vlanId) { |
| 159 | - } | 159 | + return filter(hosts.values(), host -> Objects.equals(host.vlan(), vlanId)); |
| 160 | - | 160 | + } |
| 161 | - @Override | 161 | + |
| 162 | - public Set<Host> getHosts(MacAddress mac) { | 162 | + @Override |
| 163 | - return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); | 163 | + public Set<Host> getHosts(MacAddress mac) { |
| 164 | - } | 164 | + return filter(hosts.values(), host -> Objects.equals(host.mac(), mac)); |
| 165 | - | 165 | + } |
| 166 | - @Override | 166 | + |
| 167 | - public Set<Host> getHosts(IpAddress ip) { | 167 | + @Override |
| 168 | - return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); | 168 | + public Set<Host> getHosts(IpAddress ip) { |
| 169 | - } | 169 | + return filter(hosts.values(), host -> host.ipAddresses().contains(ip)); |
| 170 | - | 170 | + } |
| 171 | - @Override | 171 | + |
| 172 | - public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { | 172 | + @Override |
| 173 | - return ImmutableSet.copyOf(locations.get(connectPoint)); | 173 | + public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 174 | - } | 174 | + return ImmutableSet.copyOf(locations.get(connectPoint)); |
| 175 | - | 175 | + } |
| 176 | - @Override | 176 | + |
| 177 | - public Set<Host> getConnectedHosts(DeviceId deviceId) { | 177 | + @Override |
| 178 | - return ImmutableMultimap.copyOf(locations) | 178 | + public Set<Host> getConnectedHosts(DeviceId deviceId) { |
| 179 | - .entries() | 179 | + return ImmutableMultimap.copyOf(locations) |
| 180 | - .stream() | 180 | + .entries() |
| 181 | - .filter(entry -> entry.getKey().deviceId().equals(deviceId)) | 181 | + .stream() |
| 182 | - .map(entry -> entry.getValue()) | 182 | + .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 183 | - .collect(Collectors.toSet()); | 183 | + .map(entry -> entry.getValue()) |
| 184 | - } | 184 | + .collect(Collectors.toSet()); |
| 185 | - | 185 | + } |
| 186 | - @Override | 186 | + |
| 187 | - public void updateAddressBindings(PortAddresses addresses) { | 187 | + @Override |
| 188 | - portAddresses.put(addresses.connectPoint(), addresses); | 188 | + public void updateAddressBindings(PortAddresses addresses) { |
| 189 | - } | 189 | + portAddresses.put(addresses.connectPoint(), addresses); |
| 190 | - | 190 | + } |
| 191 | - @Override | 191 | + |
| 192 | - public void removeAddressBindings(PortAddresses addresses) { | 192 | + @Override |
| 193 | - portAddresses.remove(addresses.connectPoint(), addresses); | 193 | + public void removeAddressBindings(PortAddresses addresses) { |
| 194 | - } | 194 | + portAddresses.remove(addresses.connectPoint(), addresses); |
| 195 | - | 195 | + } |
| 196 | - @Override | 196 | + |
| 197 | - public void clearAddressBindings(ConnectPoint connectPoint) { | 197 | + @Override |
| 198 | - portAddresses.removeAll(connectPoint); | 198 | + public void clearAddressBindings(ConnectPoint connectPoint) { |
| 199 | - } | 199 | + portAddresses.removeAll(connectPoint); |
| 200 | - | 200 | + } |
| 201 | - @Override | 201 | + |
| 202 | - public Set<PortAddresses> getAddressBindings() { | 202 | + @Override |
| 203 | - return ImmutableSet.copyOf(portAddresses.values()); | 203 | + public Set<PortAddresses> getAddressBindings() { |
| 204 | - } | 204 | + return ImmutableSet.copyOf(portAddresses.values()); |
| 205 | - | 205 | + } |
| 206 | - @Override | 206 | + |
| 207 | - public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) { | 207 | + @Override |
| 208 | - synchronized (portAddresses) { | 208 | + public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) { |
| 209 | - Set<PortAddresses> addresses = portAddresses.get(connectPoint); | 209 | + synchronized (portAddresses) { |
| 210 | - return addresses == null ? Collections.emptySet() : ImmutableSet.copyOf(addresses); | 210 | + Set<PortAddresses> addresses = portAddresses.get(connectPoint); |
| 211 | - } | 211 | + return addresses == null ? Collections.emptySet() : ImmutableSet.copyOf(addresses); |
| 212 | - } | 212 | + } |
| 213 | - | 213 | + } |
| 214 | - private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { | 214 | + |
| 215 | - return collection.stream().filter(predicate).collect(Collectors.toSet()); | 215 | + private Set<Host> filter(Collection<DefaultHost> collection, Predicate<DefaultHost> predicate) { |
| 216 | - } | 216 | + return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 217 | - | 217 | + } |
| 218 | - // checks for type of update to host, sends appropriate event | 218 | + |
| 219 | - private HostEvent updateHost(ProviderId providerId, | 219 | + // checks for type of update to host, sends appropriate event |
| 220 | - HostId hostId, | 220 | + private HostEvent updateHost(ProviderId providerId, |
| 221 | - HostDescription descr, | 221 | + HostId hostId, |
| 222 | - DefaultHost currentHost) { | 222 | + HostDescription descr, |
| 223 | - | 223 | + DefaultHost currentHost) { |
| 224 | - final boolean hostMoved = !currentHost.location().equals(descr.location()); | 224 | + |
| 225 | - if (hostMoved || | 225 | + final boolean hostMoved = !currentHost.location().equals(descr.location()); |
| 226 | - !currentHost.ipAddresses().containsAll(descr.ipAddress()) || | 226 | + if (hostMoved || |
| 227 | - !descr.annotations().keys().isEmpty()) { | 227 | + !currentHost.ipAddresses().containsAll(descr.ipAddress()) || |
| 228 | - | 228 | + !descr.annotations().keys().isEmpty()) { |
| 229 | - Set<IpAddress> addresses = Sets.newHashSet(currentHost.ipAddresses()); | 229 | + |
| 230 | - addresses.addAll(descr.ipAddress()); | 230 | + Set<IpAddress> addresses = Sets.newHashSet(currentHost.ipAddresses()); |
| 231 | - Annotations annotations = merge((DefaultAnnotations) currentHost.annotations(), | 231 | + addresses.addAll(descr.ipAddress()); |
| 232 | - descr.annotations()); | 232 | + Annotations annotations = merge((DefaultAnnotations) currentHost.annotations(), |
| 233 | - | 233 | + descr.annotations()); |
| 234 | - DefaultHost updatedHost = new DefaultHost(providerId, currentHost.id(), | 234 | + |
| 235 | - currentHost.mac(), currentHost.vlan(), | 235 | + DefaultHost updatedHost = new DefaultHost(providerId, currentHost.id(), |
| 236 | - descr.location(), | 236 | + currentHost.mac(), currentHost.vlan(), |
| 237 | - addresses, | 237 | + descr.location(), |
| 238 | - annotations); | 238 | + addresses, |
| 239 | - | 239 | + annotations); |
| 240 | - // TODO: We need a way to detect conflicting changes and abort update. | 240 | + |
| 241 | - hosts.put(hostId, updatedHost); | 241 | + // TODO: We need a way to detect conflicting changes and abort update. |
| 242 | - locations.remove(currentHost.location(), currentHost); | 242 | + hosts.put(hostId, updatedHost); |
| 243 | - locations.put(updatedHost.location(), updatedHost); | 243 | + locations.remove(currentHost.location(), currentHost); |
| 244 | - | 244 | + locations.put(updatedHost.location(), updatedHost); |
| 245 | - HostEvent.Type eventType = hostMoved ? Type.HOST_MOVED : Type.HOST_UPDATED; | 245 | + |
| 246 | - return new HostEvent(eventType, updatedHost); | 246 | + HostEvent.Type eventType = hostMoved ? Type.HOST_MOVED : Type.HOST_UPDATED; |
| 247 | - } | 247 | + return new HostEvent(eventType, updatedHost); |
| 248 | - return null; | 248 | + } |
| 249 | - } | 249 | + return null; |
| 250 | - | 250 | + } |
| 251 | - private class HostLocationTracker implements EventuallyConsistentMapListener<HostId, DefaultHost> { | 251 | + |
| 252 | - @Override | 252 | + private class HostLocationTracker implements EventuallyConsistentMapListener<HostId, DefaultHost> { |
| 253 | - public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) { | 253 | + @Override |
| 254 | - DefaultHost host = checkNotNull(event.value()); | 254 | + public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) { |
| 255 | - if (event.type() == PUT) { | 255 | + DefaultHost host = checkNotNull(event.value()); |
| 256 | - boolean isNew = locations.put(host.location(), host); | 256 | + if (event.type() == PUT) { |
| 257 | - notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host)); | 257 | + boolean isNew = locations.put(host.location(), host); |
| 258 | - } else if (event.type() == REMOVE) { | 258 | + notifyDelegate(new HostEvent(isNew ? HOST_ADDED : HOST_UPDATED, host)); |
| 259 | - if (locations.remove(host.location(), host)) { | 259 | + } else if (event.type() == REMOVE) { |
| 260 | - notifyDelegate(new HostEvent(HOST_REMOVED, host)); | 260 | + if (locations.remove(host.location(), host)) { |
| 261 | - } | 261 | + notifyDelegate(new HostEvent(HOST_REMOVED, host)); |
| 262 | - | 262 | + } |
| 263 | - } | 263 | + |
| 264 | - } | 264 | + } |
| 265 | - } | 265 | + } |
| 266 | -} | 266 | + } |
| 267 | +} | ... | ... |
| 1 | -/* | 1 | +/* |
| 2 | - * Copyright 2015 Open Networking Laboratory | 2 | + * Copyright 2015 Open Networking Laboratory |
| 3 | - * | 3 | + * |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | - * you may not use this file except in compliance with the License. | 5 | + * you may not use this file except in compliance with the License. |
| 6 | - * You may obtain a copy of the License at | 6 | + * You may obtain a copy of the License at |
| 7 | - * | 7 | + * |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | - * | 9 | + * |
| 10 | - * Unless required by applicable law or agreed to in writing, software | 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | - * See the License for the specific language governing permissions and | 13 | + * See the License for the specific language governing permissions and |
| 14 | - * limitations under the License. | 14 | + * limitations under the License. |
| 15 | - */ | 15 | + */ |
| 16 | -package org.onosproject.ovsdb.provider.host; | 16 | +package org.onosproject.ovsdb.provider.host; |
| 17 | - | 17 | + |
| 18 | -import static com.google.common.base.Preconditions.checkNotNull; | 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | -import static org.onlab.util.Tools.toHex; | 19 | +import static org.onlab.util.Tools.toHex; |
| 20 | -import static org.slf4j.LoggerFactory.getLogger; | 20 | +import static org.slf4j.LoggerFactory.getLogger; |
| 21 | - | 21 | + |
| 22 | -import java.net.URI; | 22 | +import java.net.URI; |
| 23 | -import java.net.URISyntaxException; | 23 | +import java.net.URISyntaxException; |
| 24 | - | 24 | + |
| 25 | -import org.apache.felix.scr.annotations.Activate; | 25 | +import org.apache.felix.scr.annotations.Activate; |
| 26 | -import org.apache.felix.scr.annotations.Component; | 26 | +import org.apache.felix.scr.annotations.Component; |
| 27 | -import org.apache.felix.scr.annotations.Deactivate; | 27 | +import org.apache.felix.scr.annotations.Deactivate; |
| 28 | -import org.apache.felix.scr.annotations.Reference; | 28 | +import org.apache.felix.scr.annotations.Reference; |
| 29 | -import org.apache.felix.scr.annotations.ReferenceCardinality; | 29 | +import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | -import org.apache.felix.scr.annotations.Service; | 30 | +import org.apache.felix.scr.annotations.Service; |
| 31 | -import org.onlab.packet.VlanId; | 31 | +import org.onlab.packet.VlanId; |
| 32 | -import org.onosproject.core.CoreService; | 32 | +import org.onosproject.core.CoreService; |
| 33 | -import org.onosproject.net.DefaultAnnotations; | 33 | +import org.onosproject.net.DefaultAnnotations; |
| 34 | -import org.onosproject.net.DeviceId; | 34 | +import org.onosproject.net.DeviceId; |
| 35 | -import org.onosproject.net.Host; | 35 | +import org.onosproject.net.Host; |
| 36 | -import org.onosproject.net.HostId; | 36 | +import org.onosproject.net.HostId; |
| 37 | -import org.onosproject.net.HostLocation; | 37 | +import org.onosproject.net.HostLocation; |
| 38 | -import org.onosproject.net.PortNumber; | 38 | +import org.onosproject.net.PortNumber; |
| 39 | -import org.onosproject.net.host.DefaultHostDescription; | 39 | +import org.onosproject.net.SparseAnnotations; |
| 40 | -import org.onosproject.net.host.HostDescription; | 40 | +import org.onosproject.net.host.DefaultHostDescription; |
| 41 | -import org.onosproject.net.host.HostProvider; | 41 | +import org.onosproject.net.host.HostDescription; |
| 42 | -import org.onosproject.net.host.HostProviderRegistry; | 42 | +import org.onosproject.net.host.HostProvider; |
| 43 | -import org.onosproject.net.host.HostProviderService; | 43 | +import org.onosproject.net.host.HostProviderRegistry; |
| 44 | -import org.onosproject.net.host.HostService; | 44 | +import org.onosproject.net.host.HostProviderService; |
| 45 | -import org.onosproject.net.provider.AbstractProvider; | 45 | +import org.onosproject.net.host.HostService; |
| 46 | -import org.onosproject.net.provider.ProviderId; | 46 | +import org.onosproject.net.provider.AbstractProvider; |
| 47 | -import org.onosproject.ovsdb.controller.EventSubject; | 47 | +import org.onosproject.net.provider.ProviderId; |
| 48 | -import org.onosproject.ovsdb.controller.OvsdbController; | 48 | +import org.onosproject.ovsdb.controller.EventSubject; |
| 49 | -import org.onosproject.ovsdb.controller.OvsdbEvent; | 49 | +import org.onosproject.ovsdb.controller.OvsdbController; |
| 50 | -import org.onosproject.ovsdb.controller.OvsdbEventListener; | 50 | +import org.onosproject.ovsdb.controller.OvsdbEvent; |
| 51 | -import org.onosproject.ovsdb.controller.OvsdbEventSubject; | 51 | +import org.onosproject.ovsdb.controller.OvsdbEventListener; |
| 52 | -import org.slf4j.Logger; | 52 | +import org.onosproject.ovsdb.controller.OvsdbEventSubject; |
| 53 | - | 53 | +import org.slf4j.Logger; |
| 54 | -/** | 54 | + |
| 55 | - * Provider which uses an ovsdb controller to detect host. | 55 | +/** |
| 56 | - */ | 56 | + * Provider which uses an ovsdb controller to detect host. |
| 57 | -@Component(immediate = true) | 57 | + */ |
| 58 | -@Service | 58 | +@Component(immediate = true) |
| 59 | -public class OvsdbHostProvider extends AbstractProvider implements HostProvider { | 59 | +@Service |
| 60 | - private final Logger log = getLogger(getClass()); | 60 | +public class OvsdbHostProvider extends AbstractProvider implements HostProvider { |
| 61 | - | 61 | + private final Logger log = getLogger(getClass()); |
| 62 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 62 | + |
| 63 | - protected HostProviderRegistry providerRegistry; | 63 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | - | 64 | + protected HostProviderRegistry providerRegistry; |
| 65 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 65 | + |
| 66 | - protected HostService hostService; | 66 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 67 | + protected HostService hostService; |
| 68 | - protected CoreService coreService; | 68 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 69 | + protected CoreService coreService; |
| 70 | - protected OvsdbController controller; | 70 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | - | 71 | + protected OvsdbController controller; |
| 72 | - private HostProviderService providerService; | 72 | + |
| 73 | - private OvsdbEventListener innerEventListener = new InnerOvsdbEventListener(); | 73 | + private HostProviderService providerService; |
| 74 | - | 74 | + private OvsdbEventListener innerEventListener = new InnerOvsdbEventListener(); |
| 75 | - @Activate | 75 | + |
| 76 | - public void activate() { | 76 | + @Activate |
| 77 | - providerService = providerRegistry.register(this); | 77 | + public void activate() { |
| 78 | - controller.addOvsdbEventListener(innerEventListener); | 78 | + providerService = providerRegistry.register(this); |
| 79 | - log.info("Started"); | 79 | + controller.addOvsdbEventListener(innerEventListener); |
| 80 | - } | 80 | + log.info("Started"); |
| 81 | - | 81 | + } |
| 82 | - @Deactivate | 82 | + |
| 83 | - public void deactivate() { | 83 | + @Deactivate |
| 84 | - providerRegistry.unregister(this); | 84 | + public void deactivate() { |
| 85 | - providerService = null; | 85 | + providerRegistry.unregister(this); |
| 86 | - log.info("Stopped"); | 86 | + providerService = null; |
| 87 | - } | 87 | + log.info("Stopped"); |
| 88 | - | 88 | + } |
| 89 | - public OvsdbHostProvider() { | 89 | + |
| 90 | - super(new ProviderId("ovsdb", "org.onosproject.ovsdb.provider.host")); | 90 | + public OvsdbHostProvider() { |
| 91 | - } | 91 | + super(new ProviderId("ovsdb", "org.onosproject.ovsdb.provider.host")); |
| 92 | - | 92 | + } |
| 93 | - @Override | 93 | + |
| 94 | - public void triggerProbe(Host host) { | 94 | + @Override |
| 95 | - log.info("Triggering probe on host {}", host); | 95 | + public void triggerProbe(Host host) { |
| 96 | - } | 96 | + log.info("Triggering probe on host {}", host); |
| 97 | - | 97 | + } |
| 98 | - private class InnerOvsdbEventListener implements OvsdbEventListener { | 98 | + |
| 99 | - | 99 | + private class InnerOvsdbEventListener implements OvsdbEventListener { |
| 100 | - @Override | 100 | + |
| 101 | - public void handle(OvsdbEvent<EventSubject> event) { | 101 | + @Override |
| 102 | - OvsdbEventSubject subject = null; | 102 | + public void handle(OvsdbEvent<EventSubject> event) { |
| 103 | - if (event.subject() instanceof OvsdbEventSubject) { | 103 | + OvsdbEventSubject subject = null; |
| 104 | - subject = (OvsdbEventSubject) event.subject(); | 104 | + if (event.subject() instanceof OvsdbEventSubject) { |
| 105 | - } | 105 | + subject = (OvsdbEventSubject) event.subject(); |
| 106 | - checkNotNull(subject, "EventSubject is not null"); | 106 | + } |
| 107 | - // If ifaceid is null,it indicates this is not a vm port. | 107 | + checkNotNull(subject, "EventSubject is not null"); |
| 108 | - if (subject.ifaceid() == null) { | 108 | + // If ifaceid is null,it indicates this is not a vm port. |
| 109 | - return; | 109 | + if (subject.ifaceid() == null) { |
| 110 | - } | 110 | + return; |
| 111 | - switch (event.type()) { | 111 | + } |
| 112 | - case PORT_ADDED: | 112 | + switch (event.type()) { |
| 113 | - HostId hostId = HostId.hostId(subject.hwAddress(), null); | 113 | + case PORT_ADDED: |
| 114 | - DeviceId deviceId = DeviceId.deviceId(uri(subject.dpid().value())); | 114 | + HostId hostId = HostId.hostId(subject.hwAddress(), null); |
| 115 | - PortNumber portNumber = PortNumber.portNumber(subject | 115 | + DeviceId deviceId = DeviceId.deviceId(uri(subject.dpid().value())); |
| 116 | - .portNumber().value(), subject.portName().value()); | 116 | + PortNumber portNumber = PortNumber.portNumber(subject |
| 117 | - HostLocation loaction = new HostLocation(deviceId, portNumber, | 117 | + .portNumber().value(), subject.portName().value()); |
| 118 | - 0L); | 118 | + HostLocation loaction = new HostLocation(deviceId, portNumber, |
| 119 | - DefaultAnnotations annotations = DefaultAnnotations.builder() | 119 | + 0L); |
| 120 | - .set("ifaceid", subject.ifaceid().value()).build(); | 120 | + SparseAnnotations annotations = DefaultAnnotations.builder() |
| 121 | - HostDescription hostDescription = new DefaultHostDescription( | 121 | + .set("ifaceid", subject.ifaceid().value()).build(); |
| 122 | - subject.hwAddress(), | 122 | + HostDescription hostDescription = new DefaultHostDescription( |
| 123 | - VlanId.vlanId(), | 123 | + subject.hwAddress(), |
| 124 | - loaction, | 124 | + VlanId.vlanId(), |
| 125 | - annotations); | 125 | + loaction, |
| 126 | - providerService.hostDetected(hostId, hostDescription); | 126 | + annotations); |
| 127 | - break; | 127 | + providerService.hostDetected(hostId, hostDescription); |
| 128 | - case PORT_REMOVED: | 128 | + break; |
| 129 | - HostId host = HostId.hostId(subject.hwAddress(), null); | 129 | + case PORT_REMOVED: |
| 130 | - providerService.hostVanished(host); | 130 | + HostId host = HostId.hostId(subject.hwAddress(), null); |
| 131 | - break; | 131 | + providerService.hostVanished(host); |
| 132 | - default: | 132 | + break; |
| 133 | - break; | 133 | + default: |
| 134 | - } | 134 | + break; |
| 135 | - | 135 | + } |
| 136 | - } | 136 | + |
| 137 | - | 137 | + } |
| 138 | - } | 138 | + |
| 139 | - | 139 | + } |
| 140 | - public URI uri(String value) { | 140 | + |
| 141 | - try { | 141 | + public URI uri(String value) { |
| 142 | - return new URI("of", toHex(Long.valueOf(value)), null); | 142 | + try { |
| 143 | - } catch (URISyntaxException e) { | 143 | + return new URI("of", toHex(Long.valueOf(value)), null); |
| 144 | - return null; | 144 | + } catch (URISyntaxException e) { |
| 145 | - } | 145 | + return null; |
| 146 | - } | 146 | + } |
| 147 | -} | 147 | + } |
| 148 | +} | ... | ... |
-
Please register or login to post a comment