Jonathan Hart

Host monitor test

...@@ -36,6 +36,12 @@ ...@@ -36,6 +36,12 @@
36 <scope>test</scope> 36 <scope>test</scope>
37 </dependency> 37 </dependency>
38 38
39 + <dependency>
40 + <groupId>org.easymock</groupId>
41 + <artifactId>easymock</artifactId>
42 + <scope>test</scope>
43 + </dependency>
44 +
39 <!-- TODO Consider removing store dependency. 45 <!-- TODO Consider removing store dependency.
40 Currently required for DistributedDeviceManagerTest. --> 46 Currently required for DistributedDeviceManagerTest. -->
41 <dependency> 47 <dependency>
......
...@@ -60,14 +60,15 @@ public class HostMonitor implements TimerTask { ...@@ -60,14 +60,15 @@ public class HostMonitor implements TimerTask {
60 * 60 *
61 * @param deviceService device service used to find edge ports 61 * @param deviceService device service used to find edge ports
62 * @param packetService packet service used to send packets on the data plane 62 * @param packetService packet service used to send packets on the data plane
63 - * @param hostService host service used to look up host information 63 + * @param hostManager host manager used to look up host information and
64 + * probe existing hosts
64 */ 65 */
65 public HostMonitor(DeviceService deviceService, PacketService packetService, 66 public HostMonitor(DeviceService deviceService, PacketService packetService,
66 - HostManager hostService) { 67 + HostManager hostManager) {
67 68
68 this.deviceService = deviceService; 69 this.deviceService = deviceService;
69 this.packetService = packetService; 70 this.packetService = packetService;
70 - this.hostManager = hostService; 71 + this.hostManager = hostManager;
71 72
72 monitoredAddresses = Collections.newSetFromMap( 73 monitoredAddresses = Collections.newSetFromMap(
73 new ConcurrentHashMap<IpAddress, Boolean>()); 74 new ConcurrentHashMap<IpAddress, Boolean>());
......
1 +package org.onlab.onos.net.host.impl;
2 +
3 +import static org.easymock.EasyMock.createMock;
4 +import static org.easymock.EasyMock.expect;
5 +import static org.easymock.EasyMock.expectLastCall;
6 +import static org.easymock.EasyMock.replay;
7 +import static org.easymock.EasyMock.verify;
8 +import static org.junit.Assert.assertTrue;
9 +
10 +import java.util.ArrayList;
11 +import java.util.Arrays;
12 +import java.util.Collections;
13 +import java.util.List;
14 +import java.util.Set;
15 +
16 +import org.junit.Test;
17 +import org.onlab.onos.net.ConnectPoint;
18 +import org.onlab.onos.net.Device;
19 +import org.onlab.onos.net.DeviceId;
20 +import org.onlab.onos.net.Host;
21 +import org.onlab.onos.net.MastershipRole;
22 +import org.onlab.onos.net.Port;
23 +import org.onlab.onos.net.PortNumber;
24 +import org.onlab.onos.net.device.DeviceListener;
25 +import org.onlab.onos.net.device.DeviceService;
26 +import org.onlab.onos.net.flow.instructions.Instruction;
27 +import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
28 +import org.onlab.onos.net.host.HostProvider;
29 +import org.onlab.onos.net.host.PortAddresses;
30 +import org.onlab.onos.net.packet.OutboundPacket;
31 +import org.onlab.onos.net.packet.PacketProcessor;
32 +import org.onlab.onos.net.packet.PacketService;
33 +import org.onlab.onos.net.provider.ProviderId;
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 +
40 +import com.google.common.collect.HashMultimap;
41 +import com.google.common.collect.Lists;
42 +import com.google.common.collect.Multimap;
43 +
44 +public class HostMonitorTest {
45 +
46 + private IpAddress targetIpAddress = IpAddress.valueOf("10.0.0.1");
47 + private IpPrefix targetIpPrefix = IpPrefix.valueOf(targetIpAddress.toOctets());
48 +
49 + private IpPrefix sourcePrefix = IpPrefix.valueOf("10.0.0.99/24");
50 + private MacAddress sourceMac = MacAddress.valueOf(1L);
51 +
52 + private HostMonitor hostMonitor;
53 +
54 + @Test
55 + public void testMonitorHostExists() throws Exception {
56 + ProviderId id = new ProviderId("fake://", "id");
57 +
58 + Host host = createMock(Host.class);
59 + expect(host.providerId()).andReturn(id);
60 + replay(host);
61 +
62 + HostManager hostManager = createMock(HostManager.class);
63 + expect(hostManager.getHostsByIp(targetIpPrefix))
64 + .andReturn(Collections.singleton(host));
65 + replay(hostManager);
66 +
67 + HostProvider hostProvider = createMock(HostProvider.class);
68 + expect(hostProvider.id()).andReturn(id).anyTimes();
69 + hostProvider.triggerProbe(host);
70 + expectLastCall().once();
71 + replay(hostProvider);
72 +
73 + hostMonitor = new HostMonitor(null, null, hostManager);
74 +
75 + hostMonitor.registerHostProvider(hostProvider);
76 + hostMonitor.addMonitoringFor(targetIpAddress);
77 +
78 + hostMonitor.run(null);
79 +
80 + verify(hostProvider);
81 + }
82 +
83 + @Test
84 + public void testMonitorHostDoesNotExist() throws Exception {
85 + HostManager hostManager = createMock(HostManager.class);
86 +
87 + DeviceId devId = DeviceId.deviceId("fake");
88 +
89 + Device device = createMock(Device.class);
90 + expect(device.id()).andReturn(devId).anyTimes();
91 + replay(device);
92 +
93 + PortNumber portNum = PortNumber.portNumber(1L);
94 +
95 + Port port = createMock(Port.class);
96 + expect(port.number()).andReturn(portNum).anyTimes();
97 + replay(port);
98 +
99 + TestDeviceService deviceService = new TestDeviceService();
100 + deviceService.addDevice(device, Collections.singleton(port));
101 +
102 + ConnectPoint cp = new ConnectPoint(devId, portNum);
103 + PortAddresses pa = new PortAddresses(cp, Collections.singleton(sourcePrefix),
104 + sourceMac);
105 +
106 + expect(hostManager.getHostsByIp(targetIpPrefix))
107 + .andReturn(Collections.<Host>emptySet()).anyTimes();
108 + expect(hostManager.getAddressBindingsForPort(cp))
109 + .andReturn(pa).anyTimes();
110 + replay(hostManager);
111 +
112 + TestPacketService packetService = new TestPacketService();
113 +
114 +
115 + // Run the test
116 + hostMonitor = new HostMonitor(deviceService, packetService, hostManager);
117 +
118 + hostMonitor.addMonitoringFor(targetIpAddress);
119 + hostMonitor.run(null);
120 +
121 +
122 + // Check that a packet was sent to our PacketService and that it has
123 + // the properties we expect
124 + assertTrue(packetService.packets.size() == 1);
125 + OutboundPacket packet = packetService.packets.get(0);
126 +
127 + // Check the output port is correct
128 + assertTrue(packet.treatment().instructions().size() == 1);
129 + Instruction instruction = packet.treatment().instructions().get(0);
130 + assertTrue(instruction instanceof OutputInstruction);
131 + OutputInstruction oi = (OutputInstruction) instruction;
132 + assertTrue(oi.port().equals(portNum));
133 +
134 + // Check the output packet is correct (well the important bits anyway)
135 + Ethernet eth = new Ethernet();
136 + eth.deserialize(packet.data().array(), 0, packet.data().array().length);
137 + ARP arp = (ARP) eth.getPayload();
138 + assertTrue(Arrays.equals(arp.getSenderProtocolAddress(), sourcePrefix.toOctets()));
139 + assertTrue(Arrays.equals(arp.getSenderHardwareAddress(), sourceMac.toBytes()));
140 + assertTrue(Arrays.equals(arp.getTargetProtocolAddress(), targetIpPrefix.toOctets()));
141 + }
142 +
143 + class TestPacketService implements PacketService {
144 +
145 + List<OutboundPacket> packets = new ArrayList<>();
146 +
147 + @Override
148 + public void addProcessor(PacketProcessor processor, int priority) {
149 + }
150 +
151 + @Override
152 + public void removeProcessor(PacketProcessor processor) {
153 + }
154 +
155 + @Override
156 + public void emit(OutboundPacket packet) {
157 + packets.add(packet);
158 + }
159 + }
160 +
161 + class TestDeviceService implements DeviceService {
162 +
163 + List<Device> devices = Lists.newArrayList();
164 + Multimap<DeviceId, Port> devicePorts = HashMultimap.create();
165 +
166 + void addDevice(Device device, Set<Port> ports) {
167 + devices.add(device);
168 + for (Port p : ports) {
169 + devicePorts.put(device.id(), p);
170 + }
171 + }
172 +
173 + @Override
174 + public int getDeviceCount() {
175 + return 0;
176 + }
177 +
178 + @Override
179 + public Iterable<Device> getDevices() {
180 + return devices;
181 + }
182 +
183 + @Override
184 + public Device getDevice(DeviceId deviceId) {
185 + return null;
186 + }
187 +
188 + @Override
189 + public MastershipRole getRole(DeviceId deviceId) {
190 + return null;
191 + }
192 +
193 + @Override
194 + public List<Port> getPorts(DeviceId deviceId) {
195 + List<Port> ports = Lists.newArrayList();
196 + for (Port p : devicePorts.get(deviceId)) {
197 + ports.add(p);
198 + }
199 + return ports;
200 + }
201 +
202 + @Override
203 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
204 + return null;
205 + }
206 +
207 + @Override
208 + public boolean isAvailable(DeviceId deviceId) {
209 + return false;
210 + }
211 +
212 + @Override
213 + public void addListener(DeviceListener listener) {
214 + }
215 +
216 + @Override
217 + public void removeListener(DeviceListener listener) {
218 + }
219 + }
220 +}
...@@ -122,6 +122,12 @@ ...@@ -122,6 +122,12 @@
122 <version>1.9.13</version> 122 <version>1.9.13</version>
123 </dependency> 123 </dependency>
124 124
125 + <dependency>
126 + <groupId>org.easymock</groupId>
127 + <artifactId>easymock</artifactId>
128 + <version>3.2</version>
129 + <scope>test</scope>
130 + </dependency>
125 131
126 <!-- Web related --> 132 <!-- Web related -->
127 <dependency> 133 <dependency>
......