blob: e4f6c8cd35114a8c41147fc8cdefed7b66a65988 [file] [log] [blame]
weibitf7c31a42014-06-23 16:51:01 -07001package net.onrc.onos.core.topology;
2
3import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.eq;
6import static org.easymock.EasyMock.expect;
7import static org.easymock.EasyMock.replay;
8import static org.easymock.EasyMock.verify;
9
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.List;
13import java.util.concurrent.CopyOnWriteArrayList;
14
15import net.floodlightcontroller.util.MACAddress;
16import net.onrc.onos.core.datagrid.IDatagridService;
17import net.onrc.onos.core.datagrid.IEventChannel;
18import net.onrc.onos.core.datagrid.IEventChannelListener;
19import net.onrc.onos.core.registry.IControllerRegistryService;
20import net.onrc.onos.core.util.Dpid;
21import net.onrc.onos.core.util.PortNumber;
22import net.onrc.onos.core.util.SwitchPort;
23
24import org.easymock.EasyMock;
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28
29/**
30 * Unit tests for the TopologyManager class in the Topology module.
31 * These test cases only check the sanity of functions in the TopologyManager.
32 * Note that we do not test the eventHandler functions in the TopologyManager class.
33 * DatagridService, DataStoreService, eventChannel, and controllerRegistryService are mocked out.
34 */
35public class TopologyManagerTest {
36 private TopologyManager theTopologyManager;
37 private final String eventChannelName = "onos.topology";
38 private IEventChannel<byte[], TopologyEvent> eventChannel;
39 private IDatagridService datagridService;
40 private TopologyDatastore dataStoreService;
41 private IControllerRegistryService registryService;
42 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
43 private Collection<TopologyEvent> allTopologyEvents;
44
45 @SuppressWarnings("unchecked")
46 @Before
47 public void setUp() throws Exception {
48 // Mock objects for testing
49 datagridService = EasyMock.createNiceMock(IDatagridService.class);
50 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
51 registryService = createMock(IControllerRegistryService.class);
52 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
53
54 expect(datagridService.createChannel(
55 eq(eventChannelName),
56 eq(byte[].class),
57 eq(TopologyEvent.class)))
58 .andReturn(eventChannel).once();
59
60 expect(datagridService.addListener(
61 eq(eventChannelName),
62 anyObject(IEventChannelListener.class),
63 eq(byte[].class),
64 eq(TopologyEvent.class)))
65 .andReturn(eventChannel).once();
66
67 expect(dataStoreService.addSwitch(
68 anyObject(SwitchEvent.class),
69 anyObject(Collection.class)))
70 .andReturn(true).anyTimes();
71
72 expect(dataStoreService.deactivateSwitch(
73 anyObject(SwitchEvent.class),
74 anyObject(Collection.class)))
75 .andReturn(true).anyTimes();
76
77 expect(dataStoreService.addPort(
78 anyObject(PortEvent.class)))
79 .andReturn(true).anyTimes();
80
81 expect(dataStoreService.deactivatePort(
82 anyObject(PortEvent.class)))
83 .andReturn(true).anyTimes();
84
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070085 expect(dataStoreService.addHost(
86 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070087 .andReturn(true).anyTimes();
88
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070089 expect(dataStoreService.removeHost(
90 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070091 .andReturn(true).anyTimes();
92
93 expect(dataStoreService.addLink(
94 anyObject(LinkEvent.class)))
95 .andReturn(true).anyTimes();
96
97 expect(dataStoreService.removeLink(
98 anyObject(LinkEvent.class)))
99 .andReturn(true).anyTimes();
100
101 replay(datagridService);
102 replay(registryService);
103 replay(dataStoreService);
104
105 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700106 expect(eventChannel.getAllEntries())
107 .andReturn(allTopologyEvents).anyTimes();
108 }
weibitf7c31a42014-06-23 16:51:01 -0700109
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700110 private void setupTopologyManager() {
weibitf7c31a42014-06-23 16:51:01 -0700111 // Create a topologyManager object for testing
112 topologyListeners = new CopyOnWriteArrayList<>();
113 theTopologyManager = new TopologyManager(registryService, topologyListeners);
114 theTopologyManager.startup(datagridService);
115 theTopologyManager.debugReplaceDataStore(dataStoreService);
116 }
117
118 @After
119 public void tearDown() throws Exception {
120
121 }
122
123 /**
124 * Test the Switch discovered and Port discovered functions.
125 */
126 @Test
127 public void testPutSwitchAndPortDiscoveryEvent() {
128 // Mock the eventChannel functions first
129 eventChannel.addEntry(anyObject(byte[].class),
130 anyObject(TopologyEvent.class));
131 EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port
132 replay(eventChannel);
133
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700134 setupTopologyManager();
135
weibitf7c31a42014-06-23 16:51:01 -0700136 // mockSwitch has one port
137 Dpid swDPId = new Dpid(100L);
138 PortNumber portId = new PortNumber((short) 1);
139
140 // Generate a new switch event along with a port event
141 SwitchEvent switchEvent = new SwitchEvent(swDPId);
142
143 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
144 portEvents.add(new PortEvent(swDPId, portId));
145
146 // Call the topologyManager function for adding a switch
147 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
148
149 for (PortEvent portEvent : portEvents) {
150 // Call the topologyManager function for adding a port
151 theTopologyManager.putPortDiscoveryEvent(portEvent);
152 }
153
154 // Verify the function calls
155 verify(eventChannel);
156
157 }
158
159 /**
160 * Test the switch and port removed functions.
161 */
162 @Test
163 public void testRemoveSwitchAndPortDiscoveryEvent() {
164 // Mock the eventChannel functions first
165 eventChannel.removeEntry(anyObject(byte[].class));
166 EasyMock.expectLastCall().times(2, 2); //1 switch, 1 port
167 replay(eventChannel);
168
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700169 setupTopologyManager();
170
weibitf7c31a42014-06-23 16:51:01 -0700171 Dpid swDPId = new Dpid(100L);
172 PortNumber portId = new PortNumber((short) 1);
173
174 // Generate a port event
175 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
176 portEvents.add(new PortEvent(swDPId, portId));
177
178 // Call the topologyManager function for removing a port
179 for (PortEvent portEvent : portEvents) {
180 theTopologyManager.removePortDiscoveryEvent(portEvent);
181 }
182
183 // Call the topologyManager function for removing a switch
184 SwitchEvent switchEvent = new SwitchEvent(swDPId);
185 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
186
187 // Verify the function calls
188 verify(eventChannel);
189
190 }
191
192 /**
193 * Test the device discovered function.
194 */
195 @Test
196 public void testPutDeviceDiscoveryEvent() {
197 // Mock the eventChannel functions first
198 eventChannel.addEntry(anyObject(byte[].class),
199 anyObject(TopologyEvent.class));
200 EasyMock.expectLastCall().times(1, 1); // 1 device
201 replay(eventChannel);
202
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700203 setupTopologyManager();
204
weibitf7c31a42014-06-23 16:51:01 -0700205 long swDPId = 100L;
206 long portId = 1L;
207
208 // Generate a new device event
209 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
210 SwitchPort sp = new SwitchPort(swDPId, portId);
211 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
212 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700213 HostEvent hostEvent = new HostEvent(devMac);
214 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700215
216 // Call the topologyManager function for adding a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700217 theTopologyManager.putHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700218
219 // Verify the function calls
220 verify(eventChannel);
221 }
222
223 /**
224 * Test the device removed function.
225 */
226 @Test
227 public void testRemoveDeviceDiscoveryEvent() {
228 // Mock the eventChannel functions first
229 eventChannel.removeEntry(anyObject(byte[].class));
230 EasyMock.expectLastCall().times(1, 1); // 1 device
231 replay(eventChannel);
232
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700233 setupTopologyManager();
234
weibitf7c31a42014-06-23 16:51:01 -0700235 long swDPId = 100L;
236 long portId = 1L;
237
238 // Generate a new device event
239 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
240 SwitchPort sp = new SwitchPort(swDPId, portId);
241 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
242 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700243 HostEvent hostEvent = new HostEvent(devMac);
244 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700245
246 // Call the topologyManager function for removing a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700247 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700248
249 // Verify the function calls
250 verify(eventChannel);
251 }
252
253 /**
254 * Test the link discovered function.
255 */
256 @Test
257 public void testPutLinkDiscoveryEvent() {
258 // Mock the eventChannel functions first
259 eventChannel.addEntry(anyObject(byte[].class),
260 anyObject(TopologyEvent.class));
261 EasyMock.expectLastCall().times(5, 5); // (2 switch + 2 port + 1 link)
262 replay(eventChannel);
263
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700264 setupTopologyManager();
265
weibitf7c31a42014-06-23 16:51:01 -0700266 // Assign the switch and port IDs
267 Dpid sw1DPId = new Dpid(100L);
268 PortNumber port1Id = new PortNumber((short) 1);
269 Dpid sw2DPId = new Dpid(200L);
270 PortNumber port2Id = new PortNumber((short) 2);
271
272 // Generate the switch and port events
273 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
274 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
275 portEvents1.add(new PortEvent(sw1DPId, port1Id));
276
277 // Call the topologyManager function for adding a switch
278 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
279
280 // Generate the switch and port events
281 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
282 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
283 portEvents2.add(new PortEvent(sw2DPId, port2Id));
284
285 // Call the topologyManager function for adding a switch
286 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
287
288 // Create the link
289 LinkEvent linkEvent = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
290 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
291
292 // Verify the function calls
293 verify(eventChannel);
294 }
295
296 /**
297 * Test the link removed function.
298 */
299 @Test
300 public void testRemoveLinkDiscoveryEvent() {
301 // Mock the eventChannel functions first
302 eventChannel.removeEntry(anyObject(byte[].class));
303 EasyMock.expectLastCall().times(1, 1); // (1 link)
304 replay(eventChannel);
305
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700306 setupTopologyManager();
307
weibitf7c31a42014-06-23 16:51:01 -0700308 // Assign the switch and port IDs
309 Dpid sw1DPId = new Dpid(100L);
310 PortNumber port1Id = new PortNumber((short) 1);
311 Dpid sw2DPId = new Dpid(200L);
312 PortNumber port2Id = new PortNumber((short) 2);
313
314 // Generate the switch and port events
315 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
316 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
317 portEvents1.add(new PortEvent(sw1DPId, port1Id));
318
319 // Call the topologyManager function for adding a switch
320 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
321
322 // Generate the switch and port events
323 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
324 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
325 portEvents2.add(new PortEvent(sw2DPId, port2Id));
326
327 // Call the topologyManager function for adding a switch
328 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
329
330 // Remove the link
331 LinkEvent linkEventRemove = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
332 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
333
334 // Verify the function calls
335 verify(eventChannel);
336 }
337
338}