Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host.impl; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.easymock.EasyMock.createMock; |
| 19 | import static org.easymock.EasyMock.expect; |
| 20 | import static org.easymock.EasyMock.expectLastCall; |
| 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.easymock.EasyMock.verify; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 23 | import static org.junit.Assert.assertArrayEquals; |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.ArrayList; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 32 | import org.junit.After; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 33 | import org.junit.Test; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 34 | import org.onlab.packet.ARP; |
| 35 | import org.onlab.packet.Ethernet; |
| 36 | import org.onlab.packet.IpAddress; |
| 37 | import org.onlab.packet.IpPrefix; |
| 38 | import org.onlab.packet.MacAddress; |
| 39 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 40 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.Host; |
| 45 | import org.onosproject.net.MastershipRole; |
| 46 | import org.onosproject.net.Port; |
| 47 | import org.onosproject.net.PortNumber; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceServiceAdapter; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRule; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flow.instructions.Instruction; |
| 53 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 54 | import org.onosproject.net.host.HostProvider; |
| 55 | import org.onosproject.net.host.InterfaceIpAddress; |
| 56 | import org.onosproject.net.host.PortAddresses; |
| 57 | import org.onosproject.net.packet.OutboundPacket; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 58 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketProcessor; |
| 60 | import org.onosproject.net.packet.PacketService; |
| 61 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 62 | |
| 63 | import com.google.common.collect.HashMultimap; |
| 64 | import com.google.common.collect.Lists; |
| 65 | import com.google.common.collect.Multimap; |
| 66 | |
| 67 | public class HostMonitorTest { |
| 68 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 69 | private static final IpAddress TARGET_IP_ADDR = |
| 70 | IpAddress.valueOf("10.0.0.1"); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 71 | private static final IpAddress SOURCE_ADDR = |
| 72 | IpAddress.valueOf("10.0.0.99"); |
| 73 | private static final InterfaceIpAddress IA1 = |
| 74 | new InterfaceIpAddress(SOURCE_ADDR, IpPrefix.valueOf("10.0.0.0/24")); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 75 | private MacAddress sourceMac = MacAddress.valueOf(1L); |
| 76 | |
| 77 | private HostMonitor hostMonitor; |
| 78 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 79 | @After |
| 80 | public void shutdown() { |
| 81 | hostMonitor.shutdown(); |
| 82 | } |
| 83 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 84 | @Test |
| 85 | public void testMonitorHostExists() throws Exception { |
| 86 | ProviderId id = new ProviderId("fake://", "id"); |
| 87 | |
| 88 | Host host = createMock(Host.class); |
| 89 | expect(host.providerId()).andReturn(id); |
| 90 | replay(host); |
| 91 | |
| 92 | HostManager hostManager = createMock(HostManager.class); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 93 | expect(hostManager.getHostsByIp(TARGET_IP_ADDR)) |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 94 | .andReturn(Collections.singleton(host)); |
| 95 | replay(hostManager); |
| 96 | |
| 97 | HostProvider hostProvider = createMock(HostProvider.class); |
| 98 | expect(hostProvider.id()).andReturn(id).anyTimes(); |
| 99 | hostProvider.triggerProbe(host); |
| 100 | expectLastCall().once(); |
| 101 | replay(hostProvider); |
| 102 | |
| 103 | hostMonitor = new HostMonitor(null, null, hostManager); |
| 104 | |
| 105 | hostMonitor.registerHostProvider(hostProvider); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 106 | hostMonitor.addMonitoringFor(TARGET_IP_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 107 | |
| 108 | hostMonitor.run(null); |
| 109 | |
| 110 | verify(hostProvider); |
| 111 | } |
| 112 | |
| 113 | @Test |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 114 | public void testMonitorHostDoesNotExist() throws Exception { |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 115 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 116 | HostManager hostManager = createMock(HostManager.class); |
| 117 | |
| 118 | DeviceId devId = DeviceId.deviceId("fake"); |
| 119 | |
| 120 | Device device = createMock(Device.class); |
| 121 | expect(device.id()).andReturn(devId).anyTimes(); |
| 122 | replay(device); |
| 123 | |
| 124 | PortNumber portNum = PortNumber.portNumber(1L); |
| 125 | |
| 126 | Port port = createMock(Port.class); |
| 127 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 128 | replay(port); |
| 129 | |
| 130 | TestDeviceService deviceService = new TestDeviceService(); |
| 131 | deviceService.addDevice(device, Collections.singleton(port)); |
| 132 | |
| 133 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 134 | PortAddresses pa = |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 135 | new PortAddresses(cp, Collections.singleton(IA1), sourceMac, VlanId.NONE); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 136 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 137 | expect(hostManager.getHostsByIp(TARGET_IP_ADDR)) |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 138 | .andReturn(Collections.<Host>emptySet()).anyTimes(); |
| 139 | expect(hostManager.getAddressBindingsForPort(cp)) |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 140 | .andReturn(Collections.singleton(pa)).anyTimes(); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 141 | replay(hostManager); |
| 142 | |
| 143 | TestPacketService packetService = new TestPacketService(); |
| 144 | |
| 145 | |
| 146 | // Run the test |
| 147 | hostMonitor = new HostMonitor(deviceService, packetService, hostManager); |
| 148 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 149 | hostMonitor.addMonitoringFor(TARGET_IP_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 150 | hostMonitor.run(null); |
| 151 | |
| 152 | |
| 153 | // Check that a packet was sent to our PacketService and that it has |
| 154 | // the properties we expect |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 155 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 156 | OutboundPacket packet = packetService.packets.get(0); |
| 157 | |
| 158 | // Check the output port is correct |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 159 | assertEquals(1, packet.treatment().instructions().size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 160 | Instruction instruction = packet.treatment().instructions().get(0); |
| 161 | assertTrue(instruction instanceof OutputInstruction); |
| 162 | OutputInstruction oi = (OutputInstruction) instruction; |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 163 | assertEquals(portNum, oi.port()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 164 | |
| 165 | // Check the output packet is correct (well the important bits anyway) |
| 166 | Ethernet eth = new Ethernet(); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 167 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 168 | packet.data().get(pktData); |
| 169 | eth.deserialize(pktData, 0, pktData.length); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 170 | assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); |
| 171 | ARP arp = (ARP) eth.getPayload(); |
| 172 | assertArrayEquals(SOURCE_ADDR.toOctets(), |
| 173 | arp.getSenderProtocolAddress()); |
| 174 | assertArrayEquals(sourceMac.toBytes(), |
| 175 | arp.getSenderHardwareAddress()); |
| 176 | assertArrayEquals(TARGET_IP_ADDR.toOctets(), |
| 177 | arp.getTargetProtocolAddress()); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testMonitorHostDoesNotExistWithVlan() throws Exception { |
| 182 | |
| 183 | HostManager hostManager = createMock(HostManager.class); |
| 184 | |
| 185 | DeviceId devId = DeviceId.deviceId("fake"); |
| 186 | short vlan = 5; |
| 187 | |
| 188 | Device device = createMock(Device.class); |
| 189 | expect(device.id()).andReturn(devId).anyTimes(); |
| 190 | replay(device); |
| 191 | |
| 192 | PortNumber portNum = PortNumber.portNumber(1L); |
| 193 | |
| 194 | Port port = createMock(Port.class); |
| 195 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 196 | replay(port); |
| 197 | |
| 198 | TestDeviceService deviceService = new TestDeviceService(); |
| 199 | deviceService.addDevice(device, Collections.singleton(port)); |
| 200 | |
| 201 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
| 202 | PortAddresses pa = |
| 203 | new PortAddresses(cp, Collections.singleton(IA1), sourceMac, |
| 204 | VlanId.vlanId(vlan)); |
| 205 | |
| 206 | expect(hostManager.getHostsByIp(TARGET_IP_ADDR)) |
| 207 | .andReturn(Collections.<Host>emptySet()).anyTimes(); |
| 208 | expect(hostManager.getAddressBindingsForPort(cp)) |
| 209 | .andReturn(Collections.singleton(pa)).anyTimes(); |
| 210 | replay(hostManager); |
| 211 | |
| 212 | TestPacketService packetService = new TestPacketService(); |
| 213 | |
| 214 | |
| 215 | // Run the test |
| 216 | hostMonitor = new HostMonitor(deviceService, packetService, hostManager); |
| 217 | |
| 218 | hostMonitor.addMonitoringFor(TARGET_IP_ADDR); |
| 219 | hostMonitor.run(null); |
| 220 | |
| 221 | |
| 222 | // Check that a packet was sent to our PacketService and that it has |
| 223 | // the properties we expect |
| 224 | assertEquals(1, packetService.packets.size()); |
| 225 | OutboundPacket packet = packetService.packets.get(0); |
| 226 | |
| 227 | // Check the output port is correct |
| 228 | assertEquals(1, packet.treatment().instructions().size()); |
| 229 | Instruction instruction = packet.treatment().instructions().get(0); |
| 230 | assertTrue(instruction instanceof OutputInstruction); |
| 231 | OutputInstruction oi = (OutputInstruction) instruction; |
| 232 | assertEquals(portNum, oi.port()); |
| 233 | |
| 234 | // Check the output packet is correct (well the important bits anyway) |
| 235 | Ethernet eth = new Ethernet(); |
| 236 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 237 | packet.data().get(pktData); |
| 238 | eth.deserialize(pktData, 0, pktData.length); |
| 239 | assertEquals(vlan, eth.getVlanID()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 240 | ARP arp = (ARP) eth.getPayload(); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 241 | assertArrayEquals(SOURCE_ADDR.toOctets(), |
| 242 | arp.getSenderProtocolAddress()); |
| 243 | assertArrayEquals(sourceMac.toBytes(), |
| 244 | arp.getSenderHardwareAddress()); |
| 245 | assertArrayEquals(TARGET_IP_ADDR.toOctets(), |
| 246 | arp.getTargetProtocolAddress()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | class TestPacketService implements PacketService { |
| 250 | |
| 251 | List<OutboundPacket> packets = new ArrayList<>(); |
| 252 | |
| 253 | @Override |
| 254 | public void addProcessor(PacketProcessor processor, int priority) { |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void removeProcessor(PacketProcessor processor) { |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void emit(OutboundPacket packet) { |
| 263 | packets.add(packet); |
| 264 | } |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 265 | |
| 266 | @Override |
| 267 | public void requestPackets(TrafficSelector selector, |
| 268 | PacketPriority priority, ApplicationId appId) { |
| 269 | } |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 270 | |
| 271 | @Override |
| 272 | public void requestPackets(TrafficSelector selector, |
| 273 | PacketPriority priority, ApplicationId appId, |
| 274 | FlowRule.Type tableType) { |
| 275 | } |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 278 | class TestDeviceService extends DeviceServiceAdapter { |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 279 | |
| 280 | List<Device> devices = Lists.newArrayList(); |
| 281 | Multimap<DeviceId, Port> devicePorts = HashMultimap.create(); |
| 282 | |
| 283 | void addDevice(Device device, Set<Port> ports) { |
| 284 | devices.add(device); |
| 285 | for (Port p : ports) { |
| 286 | devicePorts.put(device.id(), p); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public int getDeviceCount() { |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public Iterable<Device> getDevices() { |
| 297 | return devices; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public Device getDevice(DeviceId deviceId) { |
| 302 | return null; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public MastershipRole getRole(DeviceId deviceId) { |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public List<Port> getPorts(DeviceId deviceId) { |
| 312 | List<Port> ports = Lists.newArrayList(); |
| 313 | for (Port p : devicePorts.get(deviceId)) { |
| 314 | ports.add(p); |
| 315 | } |
| 316 | return ports; |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 321 | return null; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public boolean isAvailable(DeviceId deviceId) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void addListener(DeviceListener listener) { |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void removeListener(DeviceListener listener) { |
| 335 | } |
| 336 | } |
| 337 | } |