blob: e8fccde2e49e408af8b9472cdda9fe16d333943f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host.impl;
Jonathan Hartb4758a92014-09-24 10:46:45 -070017
18import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.expectLastCall;
21import static org.easymock.EasyMock.replay;
22import static org.easymock.EasyMock.verify;
Jonathan Harta887ba82014-11-03 15:20:52 -080023import static org.junit.Assert.assertArrayEquals;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertTrue;
Jonathan Hartb4758a92014-09-24 10:46:45 -070026
27import java.util.ArrayList;
Jonathan Hartb4758a92014-09-24 10:46:45 -070028import java.util.Collections;
29import java.util.List;
30import java.util.Set;
31
Jonathan Hartdc09a3b2014-10-27 11:34:26 -070032import org.junit.After;
Jonathan Hartb4758a92014-09-24 10:46:45 -070033import org.junit.Test;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080034import org.onlab.packet.ARP;
35import org.onlab.packet.Ethernet;
36import org.onlab.packet.IpAddress;
37import org.onlab.packet.IpPrefix;
38import org.onlab.packet.MacAddress;
39import org.onosproject.core.ApplicationId;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080040import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.Device;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.Host;
45import org.onosproject.net.MastershipRole;
46import org.onosproject.net.Port;
47import org.onosproject.net.PortNumber;
48import org.onosproject.net.device.DeviceListener;
49import org.onosproject.net.device.DeviceServiceAdapter;
Saurav Dasc313c402015-02-27 10:09:47 -080050import org.onosproject.net.flow.FlowRule;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080051import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.flow.instructions.Instruction;
53import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
54import org.onosproject.net.host.HostProvider;
55import org.onosproject.net.host.InterfaceIpAddress;
56import org.onosproject.net.host.PortAddresses;
57import org.onosproject.net.packet.OutboundPacket;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080058import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.packet.PacketProcessor;
60import org.onosproject.net.packet.PacketService;
61import org.onosproject.net.provider.ProviderId;
Jonathan Hartb4758a92014-09-24 10:46:45 -070062
63import com.google.common.collect.HashMultimap;
64import com.google.common.collect.Lists;
65import com.google.common.collect.Multimap;
66
67public class HostMonitorTest {
68
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070069 private static final IpAddress TARGET_IP_ADDR =
70 IpAddress.valueOf("10.0.0.1");
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070071 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 Hartb4758a92014-09-24 10:46:45 -070075 private MacAddress sourceMac = MacAddress.valueOf(1L);
76
77 private HostMonitor hostMonitor;
78
Jonathan Hartdc09a3b2014-10-27 11:34:26 -070079 @After
80 public void shutdown() {
81 hostMonitor.shutdown();
82 }
83
Jonathan Hartb4758a92014-09-24 10:46:45 -070084 @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 Radoslavov33f228a2014-10-27 19:33:16 -070093 expect(hostManager.getHostsByIp(TARGET_IP_ADDR))
Jonathan Hartb4758a92014-09-24 10:46:45 -070094 .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 Radoslavov33f228a2014-10-27 19:33:16 -0700106 hostMonitor.addMonitoringFor(TARGET_IP_ADDR);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700107
108 hostMonitor.run(null);
109
110 verify(hostProvider);
111 }
112
113 @Test
Jonathan Hartb4758a92014-09-24 10:46:45 -0700114 public void testMonitorHostDoesNotExist() throws Exception {
Jonathan Hartdc09a3b2014-10-27 11:34:26 -0700115
Jonathan Hartb4758a92014-09-24 10:46:45 -0700116 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 Radoslavov76b0ae22014-10-27 15:33:19 -0700134 PortAddresses pa =
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800135 new PortAddresses(cp, Collections.singleton(IA1), sourceMac, VlanId.NONE);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700136
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700137 expect(hostManager.getHostsByIp(TARGET_IP_ADDR))
Jonathan Hartb4758a92014-09-24 10:46:45 -0700138 .andReturn(Collections.<Host>emptySet()).anyTimes();
139 expect(hostManager.getAddressBindingsForPort(cp))
Jonathan Harta887ba82014-11-03 15:20:52 -0800140 .andReturn(Collections.singleton(pa)).anyTimes();
Jonathan Hartb4758a92014-09-24 10:46:45 -0700141 replay(hostManager);
142
143 TestPacketService packetService = new TestPacketService();
144
145
146 // Run the test
147 hostMonitor = new HostMonitor(deviceService, packetService, hostManager);
148
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700149 hostMonitor.addMonitoringFor(TARGET_IP_ADDR);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700150 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 Hartdc09a3b2014-10-27 11:34:26 -0700155 assertEquals(1, packetService.packets.size());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700156 OutboundPacket packet = packetService.packets.get(0);
157
158 // Check the output port is correct
Ray Milkey42507352015-03-20 15:16:10 -0700159 assertEquals(1, packet.treatment().immediate().size());
160 Instruction instruction = packet.treatment().immediate().get(0);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700161 assertTrue(instruction instanceof OutputInstruction);
162 OutputInstruction oi = (OutputInstruction) instruction;
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800163 assertEquals(portNum, oi.port());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700164
165 // Check the output packet is correct (well the important bits anyway)
166 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800167 final byte[] pktData = new byte[packet.data().remaining()];
168 packet.data().get(pktData);
169 eth.deserialize(pktData, 0, pktData.length);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800170 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
Ray Milkey42507352015-03-20 15:16:10 -0700228 assertEquals(1, packet.treatment().immediate().size());
229 Instruction instruction = packet.treatment().immediate().get(0);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800230 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 Hartb4758a92014-09-24 10:46:45 -0700240 ARP arp = (ARP) eth.getPayload();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800241 assertArrayEquals(SOURCE_ADDR.toOctets(),
242 arp.getSenderProtocolAddress());
243 assertArrayEquals(sourceMac.toBytes(),
244 arp.getSenderHardwareAddress());
245 assertArrayEquals(TARGET_IP_ADDR.toOctets(),
246 arp.getTargetProtocolAddress());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700247 }
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 Hart3cfce8e2015-01-14 16:43:27 -0800265
266 @Override
267 public void requestPackets(TrafficSelector selector,
268 PacketPriority priority, ApplicationId appId) {
269 }
Saurav Dasc313c402015-02-27 10:09:47 -0800270
271 @Override
272 public void requestPackets(TrafficSelector selector,
273 PacketPriority priority, ApplicationId appId,
274 FlowRule.Type tableType) {
275 }
Jonathan Hartb4758a92014-09-24 10:46:45 -0700276 }
277
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800278 class TestDeviceService extends DeviceServiceAdapter {
Jonathan Hartb4758a92014-09-24 10:46:45 -0700279
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}