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