blob: 934ce7d282c5495e7842f0ebdde802b07ff0ea0d [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
85 expect(dataStoreService.addDevice(
86 anyObject(DeviceEvent.class)))
87 .andReturn(true).anyTimes();
88
89 expect(dataStoreService.removeDevice(
90 anyObject(DeviceEvent.class)))
91 .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<>();
106 expect(eventChannel.getAllEntries()).andReturn(allTopologyEvents);
107 EasyMock.expectLastCall().anyTimes();
108
109 // Create a topologyManager object for testing
110 topologyListeners = new CopyOnWriteArrayList<>();
111 theTopologyManager = new TopologyManager(registryService, topologyListeners);
112 theTopologyManager.startup(datagridService);
113 theTopologyManager.debugReplaceDataStore(dataStoreService);
114 }
115
116 @After
117 public void tearDown() throws Exception {
118
119 }
120
121 /**
122 * Test the Switch discovered and Port discovered functions.
123 */
124 @Test
125 public void testPutSwitchAndPortDiscoveryEvent() {
126 // Mock the eventChannel functions first
127 eventChannel.addEntry(anyObject(byte[].class),
128 anyObject(TopologyEvent.class));
129 EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port
130 replay(eventChannel);
131
132 // mockSwitch has one port
133 Dpid swDPId = new Dpid(100L);
134 PortNumber portId = new PortNumber((short) 1);
135
136 // Generate a new switch event along with a port event
137 SwitchEvent switchEvent = new SwitchEvent(swDPId);
138
139 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
140 portEvents.add(new PortEvent(swDPId, portId));
141
142 // Call the topologyManager function for adding a switch
143 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
144
145 for (PortEvent portEvent : portEvents) {
146 // Call the topologyManager function for adding a port
147 theTopologyManager.putPortDiscoveryEvent(portEvent);
148 }
149
150 // Verify the function calls
151 verify(eventChannel);
152
153 }
154
155 /**
156 * Test the switch and port removed functions.
157 */
158 @Test
159 public void testRemoveSwitchAndPortDiscoveryEvent() {
160 // Mock the eventChannel functions first
161 eventChannel.removeEntry(anyObject(byte[].class));
162 EasyMock.expectLastCall().times(2, 2); //1 switch, 1 port
163 replay(eventChannel);
164
165 Dpid swDPId = new Dpid(100L);
166 PortNumber portId = new PortNumber((short) 1);
167
168 // Generate a port event
169 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
170 portEvents.add(new PortEvent(swDPId, portId));
171
172 // Call the topologyManager function for removing a port
173 for (PortEvent portEvent : portEvents) {
174 theTopologyManager.removePortDiscoveryEvent(portEvent);
175 }
176
177 // Call the topologyManager function for removing a switch
178 SwitchEvent switchEvent = new SwitchEvent(swDPId);
179 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
180
181 // Verify the function calls
182 verify(eventChannel);
183
184 }
185
186 /**
187 * Test the device discovered function.
188 */
189 @Test
190 public void testPutDeviceDiscoveryEvent() {
191 // Mock the eventChannel functions first
192 eventChannel.addEntry(anyObject(byte[].class),
193 anyObject(TopologyEvent.class));
194 EasyMock.expectLastCall().times(1, 1); // 1 device
195 replay(eventChannel);
196
197 long swDPId = 100L;
198 long portId = 1L;
199
200 // Generate a new device event
201 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
202 SwitchPort sp = new SwitchPort(swDPId, portId);
203 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
204 spLists.add(sp);
205 DeviceEvent deviceEvent = new DeviceEvent(devMac);
206 deviceEvent.setAttachmentPoints(spLists);
207
208 // Call the topologyManager function for adding a device
209 theTopologyManager.putDeviceDiscoveryEvent(deviceEvent);
210
211 // Verify the function calls
212 verify(eventChannel);
213 }
214
215 /**
216 * Test the device removed function.
217 */
218 @Test
219 public void testRemoveDeviceDiscoveryEvent() {
220 // Mock the eventChannel functions first
221 eventChannel.removeEntry(anyObject(byte[].class));
222 EasyMock.expectLastCall().times(1, 1); // 1 device
223 replay(eventChannel);
224
225 long swDPId = 100L;
226 long portId = 1L;
227
228 // Generate a new device event
229 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
230 SwitchPort sp = new SwitchPort(swDPId, portId);
231 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
232 spLists.add(sp);
233 DeviceEvent deviceEvent = new DeviceEvent(devMac);
234 deviceEvent.setAttachmentPoints(spLists);
235
236 // Call the topologyManager function for removing a device
237 theTopologyManager.removeDeviceDiscoveryEvent(deviceEvent);
238
239 // Verify the function calls
240 verify(eventChannel);
241 }
242
243 /**
244 * Test the link discovered function.
245 */
246 @Test
247 public void testPutLinkDiscoveryEvent() {
248 // Mock the eventChannel functions first
249 eventChannel.addEntry(anyObject(byte[].class),
250 anyObject(TopologyEvent.class));
251 EasyMock.expectLastCall().times(5, 5); // (2 switch + 2 port + 1 link)
252 replay(eventChannel);
253
254 // Assign the switch and port IDs
255 Dpid sw1DPId = new Dpid(100L);
256 PortNumber port1Id = new PortNumber((short) 1);
257 Dpid sw2DPId = new Dpid(200L);
258 PortNumber port2Id = new PortNumber((short) 2);
259
260 // Generate the switch and port events
261 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
262 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
263 portEvents1.add(new PortEvent(sw1DPId, port1Id));
264
265 // Call the topologyManager function for adding a switch
266 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
267
268 // Generate the switch and port events
269 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
270 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
271 portEvents2.add(new PortEvent(sw2DPId, port2Id));
272
273 // Call the topologyManager function for adding a switch
274 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
275
276 // Create the link
277 LinkEvent linkEvent = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
278 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
279
280 // Verify the function calls
281 verify(eventChannel);
282 }
283
284 /**
285 * Test the link removed function.
286 */
287 @Test
288 public void testRemoveLinkDiscoveryEvent() {
289 // Mock the eventChannel functions first
290 eventChannel.removeEntry(anyObject(byte[].class));
291 EasyMock.expectLastCall().times(1, 1); // (1 link)
292 replay(eventChannel);
293
294 // Assign the switch and port IDs
295 Dpid sw1DPId = new Dpid(100L);
296 PortNumber port1Id = new PortNumber((short) 1);
297 Dpid sw2DPId = new Dpid(200L);
298 PortNumber port2Id = new PortNumber((short) 2);
299
300 // Generate the switch and port events
301 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
302 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
303 portEvents1.add(new PortEvent(sw1DPId, port1Id));
304
305 // Call the topologyManager function for adding a switch
306 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
307
308 // Generate the switch and port events
309 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
310 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
311 portEvents2.add(new PortEvent(sw2DPId, port2Id));
312
313 // Call the topologyManager function for adding a switch
314 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
315
316 // Remove the link
317 LinkEvent linkEventRemove = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
318 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
319
320 // Verify the function calls
321 verify(eventChannel);
322 }
323
324}