Yuta HIGUCHI

Fixing DeviceManagerTest

Change-Id: I9431f40e0cb733f59041790af1ee54d1813f117d
1 package org.onlab.onos.net.device.impl; 1 package org.onlab.onos.net.device.impl;
2 2
3 import com.google.common.collect.Sets; 3 import com.google.common.collect.Sets;
4 +
4 import org.junit.After; 5 import org.junit.After;
5 import org.junit.Before; 6 import org.junit.Before;
6 import org.junit.Ignore; 7 import org.junit.Ignore;
7 import org.junit.Test; 8 import org.junit.Test;
9 +import org.onlab.onos.cluster.ClusterEventListener;
10 +import org.onlab.onos.cluster.ClusterService;
11 +import org.onlab.onos.cluster.ControllerNode;
12 +import org.onlab.onos.cluster.DefaultControllerNode;
8 import org.onlab.onos.cluster.MastershipServiceAdapter; 13 import org.onlab.onos.cluster.MastershipServiceAdapter;
14 +import org.onlab.onos.cluster.MastershipTerm;
15 +import org.onlab.onos.cluster.MastershipTermService;
9 import org.onlab.onos.cluster.NodeId; 16 import org.onlab.onos.cluster.NodeId;
17 +import org.onlab.onos.cluster.ControllerNode.State;
10 import org.onlab.onos.event.Event; 18 import org.onlab.onos.event.Event;
11 import org.onlab.onos.event.impl.TestEventDispatcher; 19 import org.onlab.onos.event.impl.TestEventDispatcher;
12 import org.onlab.onos.net.Device; 20 import org.onlab.onos.net.Device;
...@@ -27,7 +35,9 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -27,7 +35,9 @@ import org.onlab.onos.net.device.DeviceService;
27 import org.onlab.onos.net.device.PortDescription; 35 import org.onlab.onos.net.device.PortDescription;
28 import org.onlab.onos.net.provider.AbstractProvider; 36 import org.onlab.onos.net.provider.AbstractProvider;
29 import org.onlab.onos.net.provider.ProviderId; 37 import org.onlab.onos.net.provider.ProviderId;
38 +import org.onlab.onos.store.ClockProviderService;
30 import org.onlab.onos.store.trivial.impl.SimpleDeviceStore; 39 import org.onlab.onos.store.trivial.impl.SimpleDeviceStore;
40 +import org.onlab.packet.IpPrefix;
31 41
32 import java.util.ArrayList; 42 import java.util.ArrayList;
33 import java.util.Iterator; 43 import java.util.Iterator;
...@@ -56,6 +66,8 @@ public class DeviceManagerTest { ...@@ -56,6 +66,8 @@ public class DeviceManagerTest {
56 private static final PortNumber P1 = PortNumber.portNumber(1); 66 private static final PortNumber P1 = PortNumber.portNumber(1);
57 private static final PortNumber P2 = PortNumber.portNumber(2); 67 private static final PortNumber P2 = PortNumber.portNumber(2);
58 private static final PortNumber P3 = PortNumber.portNumber(3); 68 private static final PortNumber P3 = PortNumber.portNumber(3);
69 + private static final NodeId NID_LOCAL = new NodeId("local");
70 + private static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1");
59 71
60 private DeviceManager mgr; 72 private DeviceManager mgr;
61 73
...@@ -75,6 +87,8 @@ public class DeviceManagerTest { ...@@ -75,6 +87,8 @@ public class DeviceManagerTest {
75 mgr.store = new SimpleDeviceStore(); 87 mgr.store = new SimpleDeviceStore();
76 mgr.eventDispatcher = new TestEventDispatcher(); 88 mgr.eventDispatcher = new TestEventDispatcher();
77 mgr.mastershipService = new TestMastershipService(); 89 mgr.mastershipService = new TestMastershipService();
90 + mgr.clusterService = new TestClusterService();
91 + mgr.clockProviderService = new TestClockProviderService();
78 mgr.activate(); 92 mgr.activate();
79 93
80 service.addListener(listener); 94 service.addListener(listener);
...@@ -273,6 +287,59 @@ public class DeviceManagerTest { ...@@ -273,6 +287,59 @@ public class DeviceManagerTest {
273 public MastershipRole requestRoleFor(DeviceId deviceId) { 287 public MastershipRole requestRoleFor(DeviceId deviceId) {
274 return MastershipRole.MASTER; 288 return MastershipRole.MASTER;
275 } 289 }
290 +
291 + @Override
292 + public MastershipTermService requestTermService() {
293 + return new MastershipTermService() {
294 + @Override
295 + public MastershipTerm getMastershipTerm(DeviceId deviceId) {
296 + // FIXME: just returning something not null
297 + return MastershipTerm.of(NID_LOCAL, 1);
298 + }
299 + };
300 + }
301 + }
302 +
303 + // code clone
304 + private final class TestClusterService implements ClusterService {
305 +
306 + ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
307 +
308 + @Override
309 + public ControllerNode getLocalNode() {
310 + return local;
311 + }
312 +
313 + @Override
314 + public Set<ControllerNode> getNodes() {
315 + return null;
276 } 316 }
277 317
318 + @Override
319 + public ControllerNode getNode(NodeId nodeId) {
320 + return null;
321 + }
322 +
323 + @Override
324 + public State getState(NodeId nodeId) {
325 + return null;
326 + }
327 +
328 + @Override
329 + public void addListener(ClusterEventListener listener) {
330 + }
331 +
332 + @Override
333 + public void removeListener(ClusterEventListener listener) {
334 + }
335 + }
336 +
337 + private final class TestClockProviderService implements
338 + ClockProviderService {
339 +
340 + @Override
341 + public void setMastershipTerm(DeviceId deviceId, MastershipTerm term) {
342 + // TODO Auto-generated method stub
343 + }
344 + }
278 } 345 }
......
1 -package org.onlab.onos.net.device.impl;
2 -
3 -import com.google.common.collect.Iterables;
4 -import com.google.common.collect.Sets;
5 -import com.hazelcast.config.Config;
6 -import com.hazelcast.core.Hazelcast;
7 -
8 -import org.junit.After;
9 -import org.junit.Before;
10 -import org.junit.Test;
11 -import org.onlab.onos.cluster.DefaultControllerNode;
12 -import org.onlab.onos.cluster.MastershipServiceAdapter;
13 -import org.onlab.onos.cluster.NodeId;
14 -import org.onlab.onos.event.Event;
15 -import org.onlab.onos.event.EventDeliveryService;
16 -import org.onlab.onos.event.impl.TestEventDispatcher;
17 -import org.onlab.onos.net.Device;
18 -import org.onlab.onos.net.DeviceId;
19 -import org.onlab.onos.net.MastershipRole;
20 -import org.onlab.onos.net.Port;
21 -import org.onlab.onos.net.PortNumber;
22 -import org.onlab.onos.net.device.DefaultDeviceDescription;
23 -import org.onlab.onos.net.device.DefaultPortDescription;
24 -import org.onlab.onos.net.device.DeviceAdminService;
25 -import org.onlab.onos.net.device.DeviceDescription;
26 -import org.onlab.onos.net.device.DeviceEvent;
27 -import org.onlab.onos.net.device.DeviceListener;
28 -import org.onlab.onos.net.device.DeviceProvider;
29 -import org.onlab.onos.net.device.DeviceProviderRegistry;
30 -import org.onlab.onos.net.device.DeviceProviderService;
31 -import org.onlab.onos.net.device.DeviceService;
32 -import org.onlab.onos.net.device.PortDescription;
33 -import org.onlab.onos.net.provider.AbstractProvider;
34 -import org.onlab.onos.net.provider.ProviderId;
35 -import org.onlab.onos.store.common.StoreManager;
36 -import org.onlab.onos.store.common.StoreService;
37 -import org.onlab.onos.store.common.TestStoreManager;
38 -import org.onlab.onos.store.device.impl.DistributedDeviceStore;
39 -import org.onlab.packet.IpPrefix;
40 -
41 -import java.util.ArrayList;
42 -import java.util.HashSet;
43 -import java.util.Iterator;
44 -import java.util.List;
45 -import java.util.Map.Entry;
46 -import java.util.Set;
47 -import java.util.concurrent.BlockingQueue;
48 -import java.util.concurrent.ConcurrentHashMap;
49 -import java.util.concurrent.ConcurrentMap;
50 -import java.util.concurrent.LinkedBlockingQueue;
51 -import java.util.concurrent.TimeUnit;
52 -
53 -import static org.junit.Assert.*;
54 -import static org.onlab.onos.net.Device.Type.SWITCH;
55 -import static org.onlab.onos.net.DeviceId.deviceId;
56 -import static org.onlab.onos.net.device.DeviceEvent.Type.*;
57 -
58 -// FIXME This test is slow starting up Hazelcast on each test cases.
59 -// FIXME DistributedDeviceStore should have it's own test cases.
60 -
61 -/**
62 - * Test codifying the device service & device provider service contracts.
63 - */
64 -public class DistributedDeviceManagerTest {
65 -
66 - private static final ProviderId PID = new ProviderId("of", "foo");
67 - private static final DeviceId DID1 = deviceId("of:foo");
68 - private static final DeviceId DID2 = deviceId("of:bar");
69 - private static final String MFR = "whitebox";
70 - private static final String HW = "1.1.x";
71 - private static final String SW1 = "3.8.1";
72 - private static final String SW2 = "3.9.5";
73 - private static final String SN = "43311-12345";
74 -
75 - private static final PortNumber P1 = PortNumber.portNumber(1);
76 - private static final PortNumber P2 = PortNumber.portNumber(2);
77 - private static final PortNumber P3 = PortNumber.portNumber(3);
78 -
79 - private static final DefaultControllerNode SELF
80 - = new DefaultControllerNode(new NodeId("foobar"),
81 - IpPrefix.valueOf("127.0.0.1"));
82 -
83 -
84 - private DeviceManager mgr;
85 -
86 - protected StoreManager storeManager;
87 - protected DeviceService service;
88 - protected DeviceAdminService admin;
89 - protected DeviceProviderRegistry registry;
90 - protected DeviceProviderService providerService;
91 - protected TestProvider provider;
92 - protected TestListener listener = new TestListener();
93 - private DistributedDeviceStore dstore;
94 - private TestMastershipManager masterManager;
95 - private EventDeliveryService eventService;
96 -
97 - @Before
98 - public void setUp() {
99 - mgr = new DeviceManager();
100 - service = mgr;
101 - admin = mgr;
102 - registry = mgr;
103 - // TODO should find a way to clean Hazelcast instance without shutdown.
104 - Config config = TestStoreManager.getTestConfig();
105 -
106 - masterManager = new TestMastershipManager();
107 -
108 - storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
109 - storeManager.activate();
110 -
111 - dstore = new TestDistributedDeviceStore(storeManager);
112 - dstore.activate();
113 -
114 - mgr.store = dstore;
115 - eventService = new TestEventDispatcher();
116 - mgr.eventDispatcher = eventService;
117 - mgr.mastershipService = masterManager;
118 - mgr.activate();
119 -
120 - service.addListener(listener);
121 -
122 - provider = new TestProvider();
123 - providerService = registry.register(provider);
124 - assertTrue("provider should be registered",
125 - registry.getProviders().contains(provider.id()));
126 - }
127 -
128 - @After
129 - public void tearDown() {
130 - registry.unregister(provider);
131 - assertFalse("provider should not be registered",
132 - registry.getProviders().contains(provider.id()));
133 - service.removeListener(listener);
134 - mgr.deactivate();
135 -
136 - dstore.deactivate();
137 - storeManager.deactivate();
138 - }
139 -
140 - private void connectDevice(DeviceId deviceId, String swVersion) {
141 - DeviceDescription description =
142 - new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
143 - HW, swVersion, SN);
144 - providerService.deviceConnected(deviceId, description);
145 - assertNotNull("device should be found", service.getDevice(DID1));
146 - }
147 -
148 - @Test
149 - public void deviceConnected() {
150 - assertNull("device should not be found", service.getDevice(DID1));
151 - connectDevice(DID1, SW1);
152 - validateEvents(DEVICE_ADDED);
153 -
154 - assertEquals("only one device expected", 1, Iterables.size(service.getDevices()));
155 - Iterator<Device> it = service.getDevices().iterator();
156 - assertNotNull("one device expected", it.next());
157 - assertFalse("only one device expected", it.hasNext());
158 -
159 - assertEquals("incorrect device count", 1, service.getDeviceCount());
160 - assertTrue("device should be available", service.isAvailable(DID1));
161 - }
162 -
163 - @Test
164 - public void deviceDisconnected() {
165 - connectDevice(DID1, SW1);
166 - connectDevice(DID2, SW1);
167 - validateEvents(DEVICE_ADDED, DEVICE_ADDED);
168 - assertTrue("device should be available", service.isAvailable(DID1));
169 -
170 - // Disconnect
171 - providerService.deviceDisconnected(DID1);
172 - assertNotNull("device should not be found", service.getDevice(DID1));
173 - assertFalse("device should not be available", service.isAvailable(DID1));
174 - validateEvents(DEVICE_AVAILABILITY_CHANGED);
175 -
176 - // Reconnect
177 - connectDevice(DID1, SW1);
178 - validateEvents(DEVICE_AVAILABILITY_CHANGED);
179 -
180 - assertEquals("incorrect device count", 2, service.getDeviceCount());
181 - }
182 -
183 - @Test
184 - public void deviceUpdated() {
185 - connectDevice(DID1, SW1);
186 - validateEvents(DEVICE_ADDED);
187 -
188 - connectDevice(DID1, SW2);
189 - validateEvents(DEVICE_UPDATED);
190 - }
191 -
192 - @Test
193 - public void getRole() {
194 - connectDevice(DID1, SW1);
195 - assertEquals("incorrect role", MastershipRole.MASTER, service.getRole(DID1));
196 - }
197 -
198 - @Test
199 - public void updatePorts() {
200 - connectDevice(DID1, SW1);
201 - List<PortDescription> pds = new ArrayList<>();
202 - pds.add(new DefaultPortDescription(P1, true));
203 - pds.add(new DefaultPortDescription(P2, true));
204 - pds.add(new DefaultPortDescription(P3, true));
205 - providerService.updatePorts(DID1, pds);
206 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED);
207 - pds.clear();
208 -
209 - pds.add(new DefaultPortDescription(P1, false));
210 - pds.add(new DefaultPortDescription(P3, true));
211 - providerService.updatePorts(DID1, pds);
212 - validateEvents(PORT_UPDATED, PORT_REMOVED);
213 - }
214 -
215 - @Test
216 - public void updatePortStatus() {
217 - connectDevice(DID1, SW1);
218 - List<PortDescription> pds = new ArrayList<>();
219 - pds.add(new DefaultPortDescription(P1, true));
220 - pds.add(new DefaultPortDescription(P2, true));
221 - providerService.updatePorts(DID1, pds);
222 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
223 -
224 - providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
225 - validateEvents(PORT_UPDATED);
226 - providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
227 - assertTrue("no events expected", listener.events.isEmpty());
228 - }
229 -
230 - @Test
231 - public void getPorts() {
232 - connectDevice(DID1, SW1);
233 - List<PortDescription> pds = new ArrayList<>();
234 - pds.add(new DefaultPortDescription(P1, true));
235 - pds.add(new DefaultPortDescription(P2, true));
236 - providerService.updatePorts(DID1, pds);
237 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
238 - assertEquals("wrong port count", 2, service.getPorts(DID1).size());
239 -
240 - Port port = service.getPort(DID1, P1);
241 - assertEquals("incorrect port", P1, service.getPort(DID1, P1).number());
242 - assertEquals("incorrect state", true, service.getPort(DID1, P1).isEnabled());
243 - }
244 -
245 - @Test
246 - public void removeDevice() {
247 - connectDevice(DID1, SW1);
248 - connectDevice(DID2, SW2);
249 - assertEquals("incorrect device count", 2, service.getDeviceCount());
250 - admin.removeDevice(DID1);
251 - validateEvents(DEVICE_ADDED, DEVICE_ADDED, DEVICE_REMOVED);
252 - assertNull("device should not be found", service.getDevice(DID1));
253 - assertNotNull("device should be found", service.getDevice(DID2));
254 - assertEquals("incorrect device count", 1, service.getDeviceCount());
255 - }
256 -
257 - protected void validateEvents(Enum... types) {
258 - for (Enum type : types) {
259 - try {
260 - Event event = listener.events.poll(1, TimeUnit.SECONDS);
261 - assertNotNull("Timed out waiting for " + event, event);
262 - assertEquals("incorrect event type", type, event.type());
263 - } catch (InterruptedException e) {
264 - fail("Unexpected interrupt");
265 - }
266 - }
267 - assertTrue("Unexpected events left", listener.events.isEmpty());
268 - listener.events.clear();
269 - }
270 -
271 -
272 - private class TestProvider extends AbstractProvider implements DeviceProvider {
273 - private Device deviceReceived;
274 - private MastershipRole roleReceived;
275 -
276 - public TestProvider() {
277 - super(PID);
278 - }
279 -
280 - @Override
281 - public void triggerProbe(Device device) {
282 - }
283 -
284 - @Override
285 - public void roleChanged(Device device, MastershipRole newRole) {
286 - deviceReceived = device;
287 - roleReceived = newRole;
288 - }
289 - }
290 -
291 - private static class TestListener implements DeviceListener {
292 - final BlockingQueue<DeviceEvent> events = new LinkedBlockingQueue<>();
293 -
294 - @Override
295 - public void event(DeviceEvent event) {
296 - events.add(event);
297 - }
298 - }
299 -
300 - private class TestDistributedDeviceStore extends DistributedDeviceStore {
301 -
302 - public TestDistributedDeviceStore(StoreService storeService) {
303 - this.storeService = storeService;
304 - }
305 - }
306 -
307 - private static class TestMastershipManager extends MastershipServiceAdapter {
308 -
309 - private ConcurrentMap<DeviceId, NodeId> masters = new ConcurrentHashMap<>();
310 -
311 - public TestMastershipManager() {
312 - // SELF master of all initially
313 - masters.put(DID1, SELF.id());
314 - masters.put(DID1, SELF.id());
315 - }
316 - @Override
317 - public MastershipRole getLocalRole(DeviceId deviceId) {
318 - return MastershipRole.MASTER;
319 - }
320 -
321 - @Override
322 - public Set<DeviceId> getDevicesOf(NodeId nodeId) {
323 - HashSet<DeviceId> set = Sets.newHashSet();
324 - for (Entry<DeviceId, NodeId> e : masters.entrySet()) {
325 - if (e.getValue().equals(nodeId)) {
326 - set.add(e.getKey());
327 - }
328 - }
329 - return set;
330 - }
331 -
332 - @Override
333 - public MastershipRole requestRoleFor(DeviceId deviceId) {
334 - if (SELF.id().equals(masters.get(deviceId))) {
335 - return MastershipRole.MASTER;
336 - } else {
337 - return MastershipRole.STANDBY;
338 - }
339 - }
340 -
341 - @Override
342 - public void relinquishMastership(DeviceId deviceId) {
343 - masters.remove(deviceId, SELF.id());
344 - }
345 - }
346 -}