Committed by
Gerrit Code Review
Added ability to remove host by CLI and by the provider on device/port down events.
Change-Id: I28de4b6b5bbfb5a00f35e1808bcd916369d7d1a4
Showing
7 changed files
with
341 additions
and
27 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2014 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (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 | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onlab.onos.cli.net; | ||
| 17 | + | ||
| 18 | +import org.apache.karaf.shell.commands.Argument; | ||
| 19 | +import org.apache.karaf.shell.commands.Command; | ||
| 20 | +import org.onlab.onos.cli.AbstractShellCommand; | ||
| 21 | +import org.onlab.onos.net.HostId; | ||
| 22 | +import org.onlab.onos.net.host.HostAdminService; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Removes an end-station host. | ||
| 26 | + */ | ||
| 27 | +@Command(scope = "onos", name = "host-remove", | ||
| 28 | + description = "Removes an end-station host") | ||
| 29 | +public class HostRemoveCommand extends AbstractShellCommand { | ||
| 30 | + | ||
| 31 | + @Argument(index = 0, name = "id", description = "Host ID", | ||
| 32 | + required = true, multiValued = false) | ||
| 33 | + String id = null; | ||
| 34 | + | ||
| 35 | + @Override | ||
| 36 | + protected void execute() { | ||
| 37 | + get(HostAdminService.class).removeHost(HostId.hostId(id)); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | +} |
| ... | @@ -207,6 +207,13 @@ | ... | @@ -207,6 +207,13 @@ |
| 207 | <action class="org.onlab.onos.cli.net.HostsListCommand"/> | 207 | <action class="org.onlab.onos.cli.net.HostsListCommand"/> |
| 208 | </command> | 208 | </command> |
| 209 | <command> | 209 | <command> |
| 210 | + <action class="org.onlab.onos.cli.net.HostRemoveCommand"/> | ||
| 211 | + <completers> | ||
| 212 | + <ref component-id="hostIdCompleter"/> | ||
| 213 | + <null/> | ||
| 214 | + </completers> | ||
| 215 | + </command> | ||
| 216 | + <command> | ||
| 210 | <action class="org.onlab.onos.cli.net.AddressBindingsListCommand"/> | 217 | <action class="org.onlab.onos.cli.net.AddressBindingsListCommand"/> |
| 211 | </command> | 218 | </command> |
| 212 | 219 | ... | ... |
| ... | @@ -26,7 +26,6 @@ | ... | @@ -26,7 +26,6 @@ |
| 26 | <relativePath>../pom.xml</relativePath> | 26 | <relativePath>../pom.xml</relativePath> |
| 27 | </parent> | 27 | </parent> |
| 28 | 28 | ||
| 29 | - | ||
| 30 | <artifactId>onos-host-provider</artifactId> | 29 | <artifactId>onos-host-provider</artifactId> |
| 31 | <packaging>bundle</packaging> | 30 | <packaging>bundle</packaging> |
| 32 | 31 | ||
| ... | @@ -38,8 +37,17 @@ | ... | @@ -38,8 +37,17 @@ |
| 38 | <classifier>tests</classifier> | 37 | <classifier>tests</classifier> |
| 39 | <scope>test</scope> | 38 | <scope>test</scope> |
| 40 | </dependency> | 39 | </dependency> |
| 40 | + <dependency> | ||
| 41 | + <groupId>org.osgi</groupId> | ||
| 42 | + <artifactId>org.osgi.compendium</artifactId> | ||
| 43 | + </dependency> | ||
| 44 | + <dependency> | ||
| 45 | + <groupId>org.onlab.onos</groupId> | ||
| 46 | + <artifactId>onlab-osgi</artifactId> | ||
| 47 | + <version>${project.version}</version> | ||
| 48 | + <classifier>tests</classifier> | ||
| 49 | + <scope>test</scope> | ||
| 50 | + </dependency> | ||
| 41 | </dependencies> | 51 | </dependencies> |
| 42 | 52 | ||
| 43 | - | ||
| 44 | - | ||
| 45 | </project> | 53 | </project> | ... | ... |
| ... | @@ -15,22 +15,27 @@ | ... | @@ -15,22 +15,27 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onlab.onos.provider.host.impl; | 16 | package org.onlab.onos.provider.host.impl; |
| 17 | 17 | ||
| 18 | -import static org.slf4j.LoggerFactory.getLogger; | ||
| 19 | - | ||
| 20 | import org.apache.felix.scr.annotations.Activate; | 18 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; | 19 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; | 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | +import org.apache.felix.scr.annotations.Modified; | ||
| 22 | +import org.apache.felix.scr.annotations.Property; | ||
| 23 | import org.apache.felix.scr.annotations.Reference; | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onlab.onos.net.ConnectPoint; | 25 | import org.onlab.onos.net.ConnectPoint; |
| 26 | +import org.onlab.onos.net.DeviceId; | ||
| 26 | import org.onlab.onos.net.Host; | 27 | import org.onlab.onos.net.Host; |
| 27 | import org.onlab.onos.net.HostId; | 28 | import org.onlab.onos.net.HostId; |
| 28 | import org.onlab.onos.net.HostLocation; | 29 | import org.onlab.onos.net.HostLocation; |
| 30 | +import org.onlab.onos.net.device.DeviceEvent; | ||
| 31 | +import org.onlab.onos.net.device.DeviceListener; | ||
| 32 | +import org.onlab.onos.net.device.DeviceService; | ||
| 29 | import org.onlab.onos.net.host.DefaultHostDescription; | 33 | import org.onlab.onos.net.host.DefaultHostDescription; |
| 30 | import org.onlab.onos.net.host.HostDescription; | 34 | import org.onlab.onos.net.host.HostDescription; |
| 31 | import org.onlab.onos.net.host.HostProvider; | 35 | import org.onlab.onos.net.host.HostProvider; |
| 32 | import org.onlab.onos.net.host.HostProviderRegistry; | 36 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 33 | import org.onlab.onos.net.host.HostProviderService; | 37 | import org.onlab.onos.net.host.HostProviderService; |
| 38 | +import org.onlab.onos.net.host.HostService; | ||
| 34 | import org.onlab.onos.net.packet.PacketContext; | 39 | import org.onlab.onos.net.packet.PacketContext; |
| 35 | import org.onlab.onos.net.packet.PacketProcessor; | 40 | import org.onlab.onos.net.packet.PacketProcessor; |
| 36 | import org.onlab.onos.net.packet.PacketService; | 41 | import org.onlab.onos.net.packet.PacketService; |
| ... | @@ -42,8 +47,14 @@ import org.onlab.packet.ARP; | ... | @@ -42,8 +47,14 @@ import org.onlab.packet.ARP; |
| 42 | import org.onlab.packet.Ethernet; | 47 | import org.onlab.packet.Ethernet; |
| 43 | import org.onlab.packet.IpAddress; | 48 | import org.onlab.packet.IpAddress; |
| 44 | import org.onlab.packet.VlanId; | 49 | import org.onlab.packet.VlanId; |
| 50 | +import org.osgi.service.component.ComponentContext; | ||
| 45 | import org.slf4j.Logger; | 51 | import org.slf4j.Logger; |
| 46 | 52 | ||
| 53 | +import java.util.Dictionary; | ||
| 54 | +import java.util.Set; | ||
| 55 | + | ||
| 56 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 57 | + | ||
| 47 | /** | 58 | /** |
| 48 | * Provider which uses an OpenFlow controller to detect network | 59 | * Provider which uses an OpenFlow controller to detect network |
| 49 | * end-station hosts. | 60 | * end-station hosts. |
| ... | @@ -62,9 +73,20 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid | ... | @@ -62,9 +73,20 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected TopologyService topologyService; | 74 | protected TopologyService topologyService; |
| 64 | 75 | ||
| 76 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 77 | + protected HostService hostService; | ||
| 78 | + | ||
| 79 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 80 | + protected DeviceService deviceService; | ||
| 81 | + | ||
| 65 | private HostProviderService providerService; | 82 | private HostProviderService providerService; |
| 66 | 83 | ||
| 67 | private final InternalHostProvider processor = new InternalHostProvider(); | 84 | private final InternalHostProvider processor = new InternalHostProvider(); |
| 85 | + private final DeviceListener deviceListener = new InternalDeviceListener(); | ||
| 86 | + | ||
| 87 | + @Property(name = "hostRemovalEnabled", boolValue = true, | ||
| 88 | + label = "Enable host removal on port/device down events") | ||
| 89 | + private boolean hostRemovalEnabled = true; | ||
| 68 | 90 | ||
| 69 | 91 | ||
| 70 | /** | 92 | /** |
| ... | @@ -75,9 +97,11 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid | ... | @@ -75,9 +97,11 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid |
| 75 | } | 97 | } |
| 76 | 98 | ||
| 77 | @Activate | 99 | @Activate |
| 78 | - public void activate() { | 100 | + public void activate(ComponentContext context) { |
| 101 | + modified(context); | ||
| 79 | providerService = providerRegistry.register(this); | 102 | providerService = providerRegistry.register(this); |
| 80 | pktService.addProcessor(processor, 1); | 103 | pktService.addProcessor(processor, 1); |
| 104 | + deviceService.addListener(deviceListener); | ||
| 81 | log.info("Started"); | 105 | log.info("Started"); |
| 82 | } | 106 | } |
| 83 | 107 | ||
| ... | @@ -89,6 +113,20 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid | ... | @@ -89,6 +113,20 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid |
| 89 | log.info("Stopped"); | 113 | log.info("Stopped"); |
| 90 | } | 114 | } |
| 91 | 115 | ||
| 116 | + @Modified | ||
| 117 | + public void modified(ComponentContext context) { | ||
| 118 | + Dictionary properties = context.getProperties(); | ||
| 119 | + try { | ||
| 120 | + String flag = (String) properties.get("hostRemovalEnabled"); | ||
| 121 | + if (flag != null) { | ||
| 122 | + hostRemovalEnabled = flag.equals("true"); | ||
| 123 | + } | ||
| 124 | + } catch (Exception e) { | ||
| 125 | + hostRemovalEnabled = true; | ||
| 126 | + } | ||
| 127 | + log.info("Host removal is {}", hostRemovalEnabled ? "enabled" : "disabled"); | ||
| 128 | + } | ||
| 129 | + | ||
| 92 | @Override | 130 | @Override |
| 93 | public void triggerProbe(Host host) { | 131 | public void triggerProbe(Host host) { |
| 94 | log.info("Triggering probe on device {}", host); | 132 | log.info("Triggering probe on device {}", host); |
| ... | @@ -135,4 +173,37 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid | ... | @@ -135,4 +173,37 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid |
| 135 | } | 173 | } |
| 136 | } | 174 | } |
| 137 | } | 175 | } |
| 176 | + | ||
| 177 | + // Auxiliary listener to device events. | ||
| 178 | + private class InternalDeviceListener implements DeviceListener { | ||
| 179 | + @Override | ||
| 180 | + public void event(DeviceEvent event) { | ||
| 181 | + if (!hostRemovalEnabled) { | ||
| 182 | + return; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + DeviceEvent.Type type = event.type(); | ||
| 186 | + DeviceId deviceId = event.subject().id(); | ||
| 187 | + if (type == DeviceEvent.Type.PORT_UPDATED) { | ||
| 188 | + ConnectPoint point = new ConnectPoint(deviceId, event.port().number()); | ||
| 189 | + removeHosts(hostService.getConnectedHosts(point)); | ||
| 190 | + | ||
| 191 | + } else if (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) { | ||
| 192 | + if (!deviceService.isAvailable(deviceId)) { | ||
| 193 | + removeHosts(hostService.getConnectedHosts(deviceId)); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + } else if (type == DeviceEvent.Type.DEVICE_REMOVED) { | ||
| 197 | + removeHosts(hostService.getConnectedHosts(deviceId)); | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + // Signals host vanish for all specified hosts. | ||
| 203 | + private void removeHosts(Set<Host> hosts) { | ||
| 204 | + for (Host host : hosts) { | ||
| 205 | + providerService.hostVanished(host.id()); | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + | ||
| 138 | } | 209 | } | ... | ... |
| ... | @@ -15,25 +15,28 @@ | ... | @@ -15,25 +15,28 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onlab.onos.provider.host.impl; | 16 | package org.onlab.onos.provider.host.impl; |
| 17 | 17 | ||
| 18 | -import static org.junit.Assert.assertEquals; | 18 | +import com.google.common.collect.ImmutableSet; |
| 19 | -import static org.junit.Assert.assertNotNull; | ||
| 20 | -import static org.junit.Assert.assertNull; | ||
| 21 | - | ||
| 22 | -import java.nio.ByteBuffer; | ||
| 23 | -import java.util.Set; | ||
| 24 | - | ||
| 25 | import org.junit.After; | 19 | import org.junit.After; |
| 26 | import org.junit.Before; | 20 | import org.junit.Before; |
| 27 | import org.junit.Test; | 21 | import org.junit.Test; |
| 28 | import org.onlab.onos.net.ConnectPoint; | 22 | import org.onlab.onos.net.ConnectPoint; |
| 23 | +import org.onlab.onos.net.DefaultDevice; | ||
| 24 | +import org.onlab.onos.net.DefaultHost; | ||
| 25 | +import org.onlab.onos.net.DefaultPort; | ||
| 26 | +import org.onlab.onos.net.Device; | ||
| 29 | import org.onlab.onos.net.DeviceId; | 27 | import org.onlab.onos.net.DeviceId; |
| 28 | +import org.onlab.onos.net.Host; | ||
| 30 | import org.onlab.onos.net.HostId; | 29 | import org.onlab.onos.net.HostId; |
| 31 | -import org.onlab.onos.net.PortNumber; | 30 | +import org.onlab.onos.net.HostLocation; |
| 31 | +import org.onlab.onos.net.device.DeviceEvent; | ||
| 32 | +import org.onlab.onos.net.device.DeviceListener; | ||
| 33 | +import org.onlab.onos.net.device.DeviceServiceAdapter; | ||
| 32 | import org.onlab.onos.net.flow.TrafficTreatment; | 34 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 33 | import org.onlab.onos.net.host.HostDescription; | 35 | import org.onlab.onos.net.host.HostDescription; |
| 34 | import org.onlab.onos.net.host.HostProvider; | 36 | import org.onlab.onos.net.host.HostProvider; |
| 35 | import org.onlab.onos.net.host.HostProviderRegistry; | 37 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 36 | import org.onlab.onos.net.host.HostProviderService; | 38 | import org.onlab.onos.net.host.HostProviderService; |
| 39 | +import org.onlab.onos.net.host.HostServiceAdapter; | ||
| 37 | import org.onlab.onos.net.packet.DefaultInboundPacket; | 40 | import org.onlab.onos.net.packet.DefaultInboundPacket; |
| 38 | import org.onlab.onos.net.packet.InboundPacket; | 41 | import org.onlab.onos.net.packet.InboundPacket; |
| 39 | import org.onlab.onos.net.packet.OutboundPacket; | 42 | import org.onlab.onos.net.packet.OutboundPacket; |
| ... | @@ -43,13 +46,30 @@ import org.onlab.onos.net.packet.PacketService; | ... | @@ -43,13 +46,30 @@ import org.onlab.onos.net.packet.PacketService; |
| 43 | import org.onlab.onos.net.provider.AbstractProviderService; | 46 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 44 | import org.onlab.onos.net.provider.ProviderId; | 47 | import org.onlab.onos.net.provider.ProviderId; |
| 45 | import org.onlab.onos.net.topology.Topology; | 48 | import org.onlab.onos.net.topology.Topology; |
| 46 | - | ||
| 47 | import org.onlab.onos.net.topology.TopologyServiceAdapter; | 49 | import org.onlab.onos.net.topology.TopologyServiceAdapter; |
| 50 | +import org.onlab.osgi.ComponentContextAdapter; | ||
| 48 | import org.onlab.packet.ARP; | 51 | import org.onlab.packet.ARP; |
| 52 | +import org.onlab.packet.ChassisId; | ||
| 49 | import org.onlab.packet.Ethernet; | 53 | import org.onlab.packet.Ethernet; |
| 54 | +import org.onlab.packet.IpAddress; | ||
| 50 | import org.onlab.packet.MacAddress; | 55 | import org.onlab.packet.MacAddress; |
| 51 | import org.onlab.packet.VlanId; | 56 | import org.onlab.packet.VlanId; |
| 52 | 57 | ||
| 58 | +import java.nio.ByteBuffer; | ||
| 59 | +import java.util.Dictionary; | ||
| 60 | +import java.util.Hashtable; | ||
| 61 | +import java.util.Set; | ||
| 62 | + | ||
| 63 | +import static org.junit.Assert.*; | ||
| 64 | +import static org.onlab.onos.net.Device.Type.SWITCH; | ||
| 65 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
| 66 | +import static org.onlab.onos.net.HostId.hostId; | ||
| 67 | +import static org.onlab.onos.net.PortNumber.portNumber; | ||
| 68 | +import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; | ||
| 69 | +import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED; | ||
| 70 | +import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_UPDATED; | ||
| 71 | +import static org.onlab.packet.VlanId.vlanId; | ||
| 72 | + | ||
| 53 | public class HostLocationProviderTest { | 73 | public class HostLocationProviderTest { |
| 54 | 74 | ||
| 55 | private static final Integer INPORT = 10; | 75 | private static final Integer INPORT = 10; |
| ... | @@ -57,14 +77,44 @@ public class HostLocationProviderTest { | ... | @@ -57,14 +77,44 @@ public class HostLocationProviderTest { |
| 57 | private static final String DEV2 = "of:2"; | 77 | private static final String DEV2 = "of:2"; |
| 58 | private static final String DEV3 = "of:3"; | 78 | private static final String DEV3 = "of:3"; |
| 59 | 79 | ||
| 60 | - private static final VlanId VLAN = VlanId.vlanId(); | 80 | + private static final VlanId VLAN = vlanId(); |
| 61 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); | 81 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 62 | private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); | 82 | private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); |
| 63 | private static final byte[] IP = new byte[]{10, 0, 0, 1}; | 83 | private static final byte[] IP = new byte[]{10, 0, 0, 1}; |
| 64 | 84 | ||
| 85 | + private static final IpAddress IP_ADDRESS = | ||
| 86 | + IpAddress.valueOf(IpAddress.Version.INET, IP); | ||
| 87 | + private static final HostLocation LOCATION = | ||
| 88 | + new HostLocation(deviceId(DEV1), portNumber(INPORT), 0L); | ||
| 89 | + | ||
| 90 | + private static final DefaultHost HOST = | ||
| 91 | + new DefaultHost(ProviderId.NONE, hostId(MAC), MAC, | ||
| 92 | + vlanId(VlanId.UNTAGGED), LOCATION, | ||
| 93 | + ImmutableSet.of(IP_ADDRESS)); | ||
| 94 | + | ||
| 95 | + private static final ComponentContextAdapter CTX_FOR_REMOVE = | ||
| 96 | + new ComponentContextAdapter() { | ||
| 97 | + @Override | ||
| 98 | + public Dictionary getProperties() { | ||
| 99 | + Hashtable<String, String> props = new Hashtable<>(); | ||
| 100 | + props.put("hostRemovalEnabled", "true"); | ||
| 101 | + return props; | ||
| 102 | + } | ||
| 103 | + }; | ||
| 104 | + | ||
| 105 | + public static final ComponentContextAdapter CTX_FOR_NO_REMOVE = | ||
| 106 | + new ComponentContextAdapter() { | ||
| 107 | + @Override | ||
| 108 | + public Dictionary getProperties() { | ||
| 109 | + return new Hashtable(); | ||
| 110 | + } | ||
| 111 | + }; | ||
| 112 | + | ||
| 65 | private final HostLocationProvider provider = new HostLocationProvider(); | 113 | private final HostLocationProvider provider = new HostLocationProvider(); |
| 66 | - private final TestHostRegistry hostService = new TestHostRegistry(); | 114 | + private final TestHostRegistry hostRegistry = new TestHostRegistry(); |
| 67 | private final TestTopologyService topoService = new TestTopologyService(); | 115 | private final TestTopologyService topoService = new TestTopologyService(); |
| 116 | + private final TestDeviceService deviceService = new TestDeviceService(); | ||
| 117 | + private final TestHostService hostService = new TestHostService(); | ||
| 68 | private final TestPacketService packetService = new TestPacketService(); | 118 | private final TestPacketService packetService = new TestPacketService(); |
| 69 | 119 | ||
| 70 | private PacketProcessor testProcessor; | 120 | private PacketProcessor testProcessor; |
| ... | @@ -72,12 +122,13 @@ public class HostLocationProviderTest { | ... | @@ -72,12 +122,13 @@ public class HostLocationProviderTest { |
| 72 | 122 | ||
| 73 | @Before | 123 | @Before |
| 74 | public void setUp() { | 124 | public void setUp() { |
| 75 | - provider.providerRegistry = hostService; | 125 | + provider.providerRegistry = hostRegistry; |
| 76 | provider.topologyService = topoService; | 126 | provider.topologyService = topoService; |
| 77 | provider.pktService = packetService; | 127 | provider.pktService = packetService; |
| 128 | + provider.deviceService = deviceService; | ||
| 129 | + provider.hostService = hostService; | ||
| 78 | 130 | ||
| 79 | - provider.activate(); | 131 | + provider.activate(CTX_FOR_NO_REMOVE); |
| 80 | - | ||
| 81 | } | 132 | } |
| 82 | 133 | ||
| 83 | @Test | 134 | @Test |
| ... | @@ -89,8 +140,6 @@ public class HostLocationProviderTest { | ... | @@ -89,8 +140,6 @@ public class HostLocationProviderTest { |
| 89 | @Test | 140 | @Test |
| 90 | public void events() { | 141 | public void events() { |
| 91 | // new host | 142 | // new host |
| 92 | - | ||
| 93 | - | ||
| 94 | testProcessor.process(new TestPacketContext(DEV1)); | 143 | testProcessor.process(new TestPacketContext(DEV1)); |
| 95 | assertNotNull("new host expected", providerService.added); | 144 | assertNotNull("new host expected", providerService.added); |
| 96 | assertNull("host motion unexpected", providerService.moved); | 145 | assertNull("host motion unexpected", providerService.moved); |
| ... | @@ -104,6 +153,39 @@ public class HostLocationProviderTest { | ... | @@ -104,6 +153,39 @@ public class HostLocationProviderTest { |
| 104 | assertNull("host misheard on spine switch", providerService.spine); | 153 | assertNull("host misheard on spine switch", providerService.spine); |
| 105 | } | 154 | } |
| 106 | 155 | ||
| 156 | + @Test | ||
| 157 | + public void removeHostByDeviceRemove() { | ||
| 158 | + provider.modified(CTX_FOR_REMOVE); | ||
| 159 | + testProcessor.process(new TestPacketContext(DEV1)); | ||
| 160 | + Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, | ||
| 161 | + "m", "h", "s", "n", new ChassisId(0L)); | ||
| 162 | + deviceService.listener.event(new DeviceEvent(DEVICE_REMOVED, device)); | ||
| 163 | + assertEquals("incorrect remove count", 1, providerService.removeCount); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + @Test | ||
| 167 | + public void removeHostByDeviceOffline() { | ||
| 168 | + provider.modified(CTX_FOR_REMOVE); | ||
| 169 | + testProcessor.process(new TestPacketContext(DEV1)); | ||
| 170 | + Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, | ||
| 171 | + "m", "h", "s", "n", new ChassisId(0L)); | ||
| 172 | + deviceService.listener.event(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device)); | ||
| 173 | + assertEquals("incorrect remove count", 1, providerService.removeCount); | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + @Test | ||
| 177 | + public void removeHostByDevicePortDown() { | ||
| 178 | + provider.modified(CTX_FOR_REMOVE); | ||
| 179 | + testProcessor.process(new TestPacketContext(DEV1)); | ||
| 180 | + Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, | ||
| 181 | + "m", "h", "s", "n", new ChassisId(0L)); | ||
| 182 | + deviceService.listener.event(new DeviceEvent(PORT_UPDATED, device, | ||
| 183 | + new DefaultPort(device, portNumber(INPORT), | ||
| 184 | + false))); | ||
| 185 | + assertEquals("incorrect remove count", 1, providerService.removeCount); | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + | ||
| 107 | @After | 189 | @After |
| 108 | public void tearDown() { | 190 | public void tearDown() { |
| 109 | provider.deactivate(); | 191 | provider.deactivate(); |
| ... | @@ -137,6 +219,7 @@ public class HostLocationProviderTest { | ... | @@ -137,6 +219,7 @@ public class HostLocationProviderTest { |
| 137 | DeviceId added = null; | 219 | DeviceId added = null; |
| 138 | DeviceId moved = null; | 220 | DeviceId moved = null; |
| 139 | DeviceId spine = null; | 221 | DeviceId spine = null; |
| 222 | + public int removeCount; | ||
| 140 | 223 | ||
| 141 | protected TestHostProviderService(HostProvider provider) { | 224 | protected TestHostProviderService(HostProvider provider) { |
| 142 | super(provider); | 225 | super(provider); |
| ... | @@ -156,6 +239,7 @@ public class HostLocationProviderTest { | ... | @@ -156,6 +239,7 @@ public class HostLocationProviderTest { |
| 156 | 239 | ||
| 157 | @Override | 240 | @Override |
| 158 | public void hostVanished(HostId hostId) { | 241 | public void hostVanished(HostId hostId) { |
| 242 | + removeCount++; | ||
| 159 | } | 243 | } |
| 160 | 244 | ||
| 161 | } | 245 | } |
| ... | @@ -169,12 +253,10 @@ public class HostLocationProviderTest { | ... | @@ -169,12 +253,10 @@ public class HostLocationProviderTest { |
| 169 | 253 | ||
| 170 | @Override | 254 | @Override |
| 171 | public void removeProcessor(PacketProcessor processor) { | 255 | public void removeProcessor(PacketProcessor processor) { |
| 172 | - | ||
| 173 | } | 256 | } |
| 174 | 257 | ||
| 175 | @Override | 258 | @Override |
| 176 | public void emit(OutboundPacket packet) { | 259 | public void emit(OutboundPacket packet) { |
| 177 | - | ||
| 178 | } | 260 | } |
| 179 | } | 261 | } |
| 180 | 262 | ||
| ... | @@ -184,7 +266,7 @@ public class HostLocationProviderTest { | ... | @@ -184,7 +266,7 @@ public class HostLocationProviderTest { |
| 184 | public boolean isInfrastructure(Topology topology, | 266 | public boolean isInfrastructure(Topology topology, |
| 185 | ConnectPoint connectPoint) { | 267 | ConnectPoint connectPoint) { |
| 186 | //simulate DPID3 as an infrastructure switch | 268 | //simulate DPID3 as an infrastructure switch |
| 187 | - if ((connectPoint.deviceId()).equals(DeviceId.deviceId(DEV3))) { | 269 | + if ((connectPoint.deviceId()).equals(deviceId(DEV3))) { |
| 188 | return true; | 270 | return true; |
| 189 | } | 271 | } |
| 190 | return false; | 272 | return false; |
| ... | @@ -218,8 +300,8 @@ public class HostLocationProviderTest { | ... | @@ -218,8 +300,8 @@ public class HostLocationProviderTest { |
| 218 | .setSourceMACAddress(MAC.toBytes()) | 300 | .setSourceMACAddress(MAC.toBytes()) |
| 219 | .setDestinationMACAddress(BCMAC) | 301 | .setDestinationMACAddress(BCMAC) |
| 220 | .setPayload(arp); | 302 | .setPayload(arp); |
| 221 | - ConnectPoint receivedFrom = new ConnectPoint(DeviceId.deviceId(deviceId), | 303 | + ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 222 | - PortNumber.portNumber(INPORT)); | 304 | + portNumber(INPORT)); |
| 223 | return new DefaultInboundPacket(receivedFrom, eth, | 305 | return new DefaultInboundPacket(receivedFrom, eth, |
| 224 | ByteBuffer.wrap(eth.serialize())); | 306 | ByteBuffer.wrap(eth.serialize())); |
| 225 | } | 307 | } |
| ... | @@ -249,4 +331,26 @@ public class HostLocationProviderTest { | ... | @@ -249,4 +331,26 @@ public class HostLocationProviderTest { |
| 249 | return false; | 331 | return false; |
| 250 | } | 332 | } |
| 251 | } | 333 | } |
| 334 | + | ||
| 335 | + private class TestDeviceService extends DeviceServiceAdapter { | ||
| 336 | + private DeviceListener listener; | ||
| 337 | + | ||
| 338 | + @Override | ||
| 339 | + public void addListener(DeviceListener listener) { | ||
| 340 | + this.listener = listener; | ||
| 341 | + } | ||
| 342 | + } | ||
| 343 | + | ||
| 344 | + private class TestHostService extends HostServiceAdapter { | ||
| 345 | + @Override | ||
| 346 | + public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { | ||
| 347 | + return ImmutableSet.of(HOST); | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + @Override | ||
| 351 | + public Set<Host> getConnectedHosts(DeviceId deviceId) { | ||
| 352 | + return ImmutableSet.of(HOST); | ||
| 353 | + } | ||
| 354 | + | ||
| 355 | + } | ||
| 252 | } | 356 | } | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2014 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (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 | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onlab.osgi; | ||
| 17 | + | ||
| 18 | +import org.osgi.framework.Bundle; | ||
| 19 | +import org.osgi.framework.BundleContext; | ||
| 20 | +import org.osgi.framework.ServiceReference; | ||
| 21 | +import org.osgi.service.component.ComponentContext; | ||
| 22 | +import org.osgi.service.component.ComponentInstance; | ||
| 23 | + | ||
| 24 | +import java.util.Dictionary; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Adapter implementation of OSGI component context. | ||
| 28 | + */ | ||
| 29 | +public class ComponentContextAdapter implements ComponentContext { | ||
| 30 | + @Override | ||
| 31 | + public Dictionary getProperties() { | ||
| 32 | + return null; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @Override | ||
| 36 | + public Object locateService(String name) { | ||
| 37 | + return null; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + @Override | ||
| 41 | + public Object locateService(String name, ServiceReference reference) { | ||
| 42 | + return null; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Override | ||
| 46 | + public Object[] locateServices(String name) { | ||
| 47 | + return new Object[0]; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + @Override | ||
| 51 | + public BundleContext getBundleContext() { | ||
| 52 | + return null; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public Bundle getUsingBundle() { | ||
| 57 | + return null; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Override | ||
| 61 | + public ComponentInstance getComponentInstance() { | ||
| 62 | + return null; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Override | ||
| 66 | + public void enableComponent(String name) { | ||
| 67 | + | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Override | ||
| 71 | + public void disableComponent(String name) { | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Override | ||
| 76 | + public ServiceReference getServiceReference() { | ||
| 77 | + return null; | ||
| 78 | + } | ||
| 79 | +} |
| ... | @@ -46,6 +46,11 @@ | ... | @@ -46,6 +46,11 @@ |
| 46 | <groupId>com.google.guava</groupId> | 46 | <groupId>com.google.guava</groupId> |
| 47 | <artifactId>guava</artifactId> | 47 | <artifactId>guava</artifactId> |
| 48 | </dependency> | 48 | </dependency> |
| 49 | + <dependency> | ||
| 50 | + <groupId>org.osgi</groupId> | ||
| 51 | + <artifactId>org.osgi.compendium</artifactId> | ||
| 52 | + <scope>test</scope> | ||
| 53 | + </dependency> | ||
| 49 | </dependencies> | 54 | </dependencies> |
| 50 | 55 | ||
| 51 | <build> | 56 | <build> | ... | ... |
-
Please register or login to post a comment