Committed by
Gerrit Code Review
ONOS-4361 OSPF Provider JUNIT
Change-Id: Ib02d2bd136ac8ff071aa6b562d9b5ab9011959b9
Showing
3 changed files
with
389 additions
and
0 deletions
... | @@ -46,6 +46,11 @@ | ... | @@ -46,6 +46,11 @@ |
46 | </dependency> | 46 | </dependency> |
47 | <dependency> | 47 | <dependency> |
48 | <groupId>org.onosproject</groupId> | 48 | <groupId>org.onosproject</groupId> |
49 | + <artifactId>onos-ospf-ctl</artifactId> | ||
50 | + <version>${project.version}</version> | ||
51 | + </dependency> | ||
52 | + <dependency> | ||
53 | + <groupId>org.onosproject</groupId> | ||
49 | <artifactId>onos-api</artifactId> | 54 | <artifactId>onos-api</artifactId> |
50 | <classifier>tests</classifier> | 55 | <classifier>tests</classifier> |
51 | <scope>test</scope> | 56 | <scope>test</scope> | ... | ... |
1 | COMPILE_DEPS = [ | 1 | COMPILE_DEPS = [ |
2 | '//lib:CORE_DEPS', | 2 | '//lib:CORE_DEPS', |
3 | '//protocols/ospf/api:onos-protocols-ospf-api', | 3 | '//protocols/ospf/api:onos-protocols-ospf-api', |
4 | + '//protocols/ospf/ctl:onos-protocols-ospf-ctl', | ||
5 | +] | ||
6 | + | ||
7 | +TEST_DEPS = [ | ||
8 | + '//lib:TEST_ADAPTERS', | ||
4 | ] | 9 | ] |
5 | 10 | ||
6 | osgi_jar_with_tests ( | 11 | osgi_jar_with_tests ( |
7 | deps = COMPILE_DEPS, | 12 | deps = COMPILE_DEPS, |
13 | + test_deps = TEST_DEPS, | ||
8 | ) | 14 | ) |
9 | 15 | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016-present 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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.provider.ospf.topology.impl; | ||
18 | + | ||
19 | +import com.fasterxml.jackson.databind.JsonNode; | ||
20 | +import org.junit.After; | ||
21 | +import org.junit.Before; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onlab.packet.Ip4Address; | ||
24 | +import org.onlab.util.Bandwidth; | ||
25 | +import org.onosproject.net.ConnectPoint; | ||
26 | +import org.onosproject.net.Device; | ||
27 | +import org.onosproject.net.DeviceId; | ||
28 | +import org.onosproject.net.Link; | ||
29 | +import org.onosproject.net.MastershipRole; | ||
30 | +import org.onosproject.net.PortNumber; | ||
31 | +import org.onosproject.net.device.DeviceDescription; | ||
32 | +import org.onosproject.net.device.DeviceListener; | ||
33 | +import org.onosproject.net.device.DeviceProvider; | ||
34 | +import org.onosproject.net.device.DeviceProviderRegistry; | ||
35 | +import org.onosproject.net.device.DeviceProviderService; | ||
36 | +import org.onosproject.net.device.DeviceServiceAdapter; | ||
37 | +import org.onosproject.net.device.PortDescription; | ||
38 | +import org.onosproject.net.device.PortStatistics; | ||
39 | +import org.onosproject.net.link.LinkDescription; | ||
40 | +import org.onosproject.net.link.LinkListener; | ||
41 | +import org.onosproject.net.link.LinkProvider; | ||
42 | +import org.onosproject.net.link.LinkProviderRegistry; | ||
43 | +import org.onosproject.net.link.LinkProviderService; | ||
44 | +import org.onosproject.net.link.LinkServiceAdapter; | ||
45 | +import org.onosproject.net.provider.ProviderId; | ||
46 | +import org.onosproject.ospf.controller.OspfController; | ||
47 | +import org.onosproject.ospf.controller.OspfDeviceTed; | ||
48 | +import org.onosproject.ospf.controller.OspfLinkListener; | ||
49 | +import org.onosproject.ospf.controller.OspfLinkTed; | ||
50 | +import org.onosproject.ospf.controller.OspfProcess; | ||
51 | +import org.onosproject.ospf.controller.OspfRouter; | ||
52 | +import org.onosproject.ospf.controller.OspfRouterListener; | ||
53 | +import org.onosproject.ospf.controller.impl.OspfDeviceTedImpl; | ||
54 | +import org.onosproject.ospf.controller.impl.OspfLinkTedImpl; | ||
55 | +import org.onosproject.ospf.controller.impl.OspfRouterImpl; | ||
56 | + | ||
57 | +import java.util.Collection; | ||
58 | +import java.util.Collections; | ||
59 | +import java.util.HashSet; | ||
60 | +import java.util.List; | ||
61 | +import java.util.Set; | ||
62 | +import java.util.concurrent.CopyOnWriteArraySet; | ||
63 | + | ||
64 | +import static org.junit.Assert.*; | ||
65 | + | ||
66 | +/** | ||
67 | + * Test cases for OSPF topology provider. | ||
68 | + */ | ||
69 | +public class OspfTopologyProviderTest { | ||
70 | + | ||
71 | + private final OspfTopologyProvider provider = new OspfTopologyProvider(); | ||
72 | + private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry(); | ||
73 | + private final TestLinkRegistry linkRegistry = new TestLinkRegistry(); | ||
74 | + private final TestController controller = new TestController(); | ||
75 | + | ||
76 | + @Before | ||
77 | + public void setUp() throws Exception { | ||
78 | + provider.deviceProviderRegistry = nodeRegistry; | ||
79 | + provider.linkProviderRegistry = linkRegistry; | ||
80 | + provider.controller = controller; | ||
81 | + provider.activate(); | ||
82 | + assertNotNull("provider should be registered", nodeRegistry.provider); | ||
83 | + assertNotNull("listener should be registered", controller.nodeListener); | ||
84 | + | ||
85 | + } | ||
86 | + | ||
87 | + @After | ||
88 | + public void tearDown() throws Exception { | ||
89 | + provider.deactivate(); | ||
90 | + assertNull("listener should be removed", controller.nodeListener); | ||
91 | + provider.controller = null; | ||
92 | + provider.deviceProviderRegistry = null; | ||
93 | + } | ||
94 | + | ||
95 | + @Test | ||
96 | + public void triggerProbe() { | ||
97 | + DeviceId deviceId = DeviceId.deviceId("2.2.2.2"); | ||
98 | + provider.triggerProbe(deviceId); | ||
99 | + } | ||
100 | + | ||
101 | + @Test | ||
102 | + public void roleChanged() { | ||
103 | + DeviceId deviceId = DeviceId.deviceId("2.2.2.2"); | ||
104 | + provider.roleChanged(deviceId, MastershipRole.MASTER); | ||
105 | + } | ||
106 | + | ||
107 | + @Test | ||
108 | + public void changePortState() { | ||
109 | + DeviceId deviceId = DeviceId.deviceId("2.2.2.2"); | ||
110 | + provider.changePortState(deviceId, PortNumber.portNumber(0), false); | ||
111 | + } | ||
112 | + | ||
113 | + @Test | ||
114 | + public void isReachable() { | ||
115 | + DeviceId deviceId = DeviceId.deviceId("1.1.1.1"); | ||
116 | + provider.isReachable(deviceId); | ||
117 | + } | ||
118 | + | ||
119 | + /* Validate node is added to the device validating URI, RIB should get updated properly */ | ||
120 | + @Test | ||
121 | + public void ospfTopologyProviderTestAddDevice1() { | ||
122 | + int deviceAddCount = 0; | ||
123 | + OspfRouter ospfRouter = new OspfRouterImpl(); | ||
124 | + ospfRouter.setDr(false); | ||
125 | + ospfRouter.setOpaque(false); | ||
126 | + ospfRouter.setNeighborRouterId(Ip4Address.valueOf("2.2.2.2")); | ||
127 | + ospfRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.2")); | ||
128 | + ospfRouter.setAreaIdOfInterface(Ip4Address.valueOf("5.5.5.5")); | ||
129 | + ospfRouter.setRouterIp(Ip4Address.valueOf("1.1.1.1")); | ||
130 | + OspfDeviceTed ospfDeviceTed = new OspfDeviceTedImpl(); | ||
131 | + ospfDeviceTed.setAbr(false); | ||
132 | + ospfDeviceTed.setAsbr(false); | ||
133 | + ospfRouter.setDeviceTed(ospfDeviceTed); | ||
134 | + OspfLinkTed ospfLinkTed = new OspfLinkTedImpl(); | ||
135 | + ospfLinkTed.setMaximumLink(Bandwidth.bps(10)); | ||
136 | + ospfLinkTed.setMaxReserved(Bandwidth.bps(20)); | ||
137 | + ospfLinkTed.setTeMetric(10); | ||
138 | + OspfRouter ospfRouter1 = new OspfRouterImpl(); | ||
139 | + ospfRouter1.setDr(true); | ||
140 | + ospfRouter1.setOpaque(true); | ||
141 | + ospfRouter1.setNeighborRouterId(Ip4Address.valueOf("2.2.2.2")); | ||
142 | + ospfRouter1.setInterfaceId(Ip4Address.valueOf("10.10.10.2")); | ||
143 | + ospfRouter1.setAreaIdOfInterface(Ip4Address.valueOf("5.5.5.5")); | ||
144 | + ospfRouter1.setRouterIp(Ip4Address.valueOf("1.1.1.1")); | ||
145 | + OspfDeviceTed ospfDeviceTed1 = new OspfDeviceTedImpl(); | ||
146 | + ospfDeviceTed1.setAbr(false); | ||
147 | + ospfDeviceTed1.setAsbr(false); | ||
148 | + ospfRouter.setDeviceTed(ospfDeviceTed); | ||
149 | + OspfLinkTed ospfLinkTed1 = new OspfLinkTedImpl(); | ||
150 | + ospfLinkTed1.setMaximumLink(Bandwidth.bps(10)); | ||
151 | + ospfLinkTed1.setMaxReserved(Bandwidth.bps(20)); | ||
152 | + ospfLinkTed1.setTeMetric(10); | ||
153 | + for (OspfRouterListener l : controller.nodeListener) { | ||
154 | + l.routerAdded(ospfRouter); | ||
155 | + | ||
156 | + deviceAddCount = nodeRegistry.connected.size(); | ||
157 | + assertTrue(deviceAddCount == 1); | ||
158 | + l.routerRemoved(ospfRouter); | ||
159 | + deviceAddCount = nodeRegistry.connected.size(); | ||
160 | + assertTrue(deviceAddCount == 0); | ||
161 | + } | ||
162 | + for (OspfLinkListener l : controller.linkListener) { | ||
163 | + l.addLink(ospfRouter, ospfLinkTed); | ||
164 | + l.deleteLink(ospfRouter, ospfLinkTed); | ||
165 | + | ||
166 | + } | ||
167 | + } | ||
168 | + | ||
169 | + @Test | ||
170 | + public void ospfTopologyProviderTestAddDevice2() { | ||
171 | + int deviceAddCount = 0; | ||
172 | + OspfRouter ospfRouter = new OspfRouterImpl(); | ||
173 | + ospfRouter.setDr(true); | ||
174 | + ospfRouter.setOpaque(true); | ||
175 | + ospfRouter.setNeighborRouterId(Ip4Address.valueOf("3.3.3.3")); | ||
176 | + ospfRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.3")); | ||
177 | + ospfRouter.setAreaIdOfInterface(Ip4Address.valueOf("6.6.6.6")); | ||
178 | + ospfRouter.setRouterIp(Ip4Address.valueOf("7.7.7.7")); | ||
179 | + OspfDeviceTed ospfDeviceTed = new OspfDeviceTedImpl(); | ||
180 | + ospfDeviceTed.setAbr(true); | ||
181 | + ospfDeviceTed.setAsbr(true); | ||
182 | + ospfRouter.setDeviceTed(ospfDeviceTed); | ||
183 | + OspfLinkTed ospfLinkTed = new OspfLinkTedImpl(); | ||
184 | + ospfLinkTed.setMaximumLink(Bandwidth.bps(30)); | ||
185 | + ospfLinkTed.setMaxReserved(Bandwidth.bps(40)); | ||
186 | + ospfLinkTed.setTeMetric(50); | ||
187 | + for (OspfRouterListener l : controller.nodeListener) { | ||
188 | + l.routerAdded(ospfRouter); | ||
189 | + deviceAddCount = nodeRegistry.connected.size(); | ||
190 | + assertTrue(deviceAddCount == 1); | ||
191 | + l.routerRemoved(ospfRouter); | ||
192 | + deviceAddCount = nodeRegistry.connected.size(); | ||
193 | + assertTrue(deviceAddCount == 0); | ||
194 | + } | ||
195 | + } | ||
196 | + | ||
197 | + | ||
198 | + /* Class implement device test registry */ | ||
199 | + private class TestDeviceRegistry implements DeviceProviderRegistry { | ||
200 | + DeviceProvider provider; | ||
201 | + Set<DeviceId> connected = new HashSet<>(); | ||
202 | + | ||
203 | + @Override | ||
204 | + public DeviceProviderService register(DeviceProvider provider) { | ||
205 | + this.provider = provider; | ||
206 | + return new TestProviderService(); | ||
207 | + } | ||
208 | + | ||
209 | + @Override | ||
210 | + public void unregister(DeviceProvider provider) { | ||
211 | + } | ||
212 | + | ||
213 | + @Override | ||
214 | + public Set<ProviderId> getProviders() { | ||
215 | + return null; | ||
216 | + } | ||
217 | + | ||
218 | + private class TestProviderService implements DeviceProviderService { | ||
219 | + | ||
220 | + @Override | ||
221 | + public DeviceProvider provider() { | ||
222 | + return null; | ||
223 | + } | ||
224 | + | ||
225 | + @Override | ||
226 | + public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { | ||
227 | + connected.add(deviceId); | ||
228 | + } | ||
229 | + | ||
230 | + @Override | ||
231 | + public void deviceDisconnected(DeviceId deviceId) { | ||
232 | + connected.remove(deviceId); | ||
233 | + } | ||
234 | + | ||
235 | + @Override | ||
236 | + public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { | ||
237 | + // TODO Auto-generated method stub | ||
238 | + } | ||
239 | + | ||
240 | + @Override | ||
241 | + public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { | ||
242 | + // TODO Auto-generated method stub | ||
243 | + } | ||
244 | + | ||
245 | + @Override | ||
246 | + public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) { | ||
247 | + // TODO Auto-generated method stub | ||
248 | + } | ||
249 | + | ||
250 | + @Override | ||
251 | + public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { | ||
252 | + // TODO Auto-generated method stub | ||
253 | + } | ||
254 | + } | ||
255 | + } | ||
256 | + | ||
257 | + private class TestDeviceService extends DeviceServiceAdapter { | ||
258 | + private DeviceListener listener; | ||
259 | + | ||
260 | + @Override | ||
261 | + public void addListener(DeviceListener listener) { | ||
262 | + this.listener = listener; | ||
263 | + } | ||
264 | + | ||
265 | + @Override | ||
266 | + public Iterable<Device> getDevices() { | ||
267 | + return Collections.emptyList(); | ||
268 | + } | ||
269 | + } | ||
270 | + | ||
271 | + private class TestLinkService extends LinkServiceAdapter { | ||
272 | + private LinkListener listener; | ||
273 | + | ||
274 | + @Override | ||
275 | + public void addListener(LinkListener listener) { | ||
276 | + this.listener = listener; | ||
277 | + } | ||
278 | + | ||
279 | + @Override | ||
280 | + public Iterable<Link> getLinks() { | ||
281 | + return Collections.emptyList(); | ||
282 | + } | ||
283 | + } | ||
284 | + | ||
285 | + /* Class implement device test registry */ | ||
286 | + private class TestLinkRegistry implements LinkProviderRegistry { | ||
287 | + LinkProvider provider; | ||
288 | + Set<DeviceId> connected = new HashSet<>(); | ||
289 | + | ||
290 | + @Override | ||
291 | + public LinkProviderService register(LinkProvider provider) { | ||
292 | + this.provider = provider; | ||
293 | + return new TestLinkProviderService(); | ||
294 | + } | ||
295 | + | ||
296 | + @Override | ||
297 | + public void unregister(LinkProvider provider) { | ||
298 | + } | ||
299 | + | ||
300 | + @Override | ||
301 | + public Set<ProviderId> getProviders() { | ||
302 | + return null; | ||
303 | + } | ||
304 | + | ||
305 | + private class TestLinkProviderService implements LinkProviderService { | ||
306 | + @Override | ||
307 | + public void linkDetected(LinkDescription linkDescription) { | ||
308 | + } | ||
309 | + | ||
310 | + @Override | ||
311 | + public void linkVanished(LinkDescription linkDescription) { | ||
312 | + } | ||
313 | + | ||
314 | + @Override | ||
315 | + public void linksVanished(ConnectPoint connectPoint) { | ||
316 | + } | ||
317 | + | ||
318 | + @Override | ||
319 | + public void linksVanished(DeviceId deviceId) { | ||
320 | + } | ||
321 | + | ||
322 | + @Override | ||
323 | + public LinkProvider provider() { | ||
324 | + return null; | ||
325 | + } | ||
326 | + } | ||
327 | + } | ||
328 | + | ||
329 | + /* class implement test controller */ | ||
330 | + private class TestController implements OspfController { | ||
331 | + protected Set<OspfRouterListener> nodeListener = new CopyOnWriteArraySet<>(); | ||
332 | + protected Set<OspfLinkListener> linkListener = new CopyOnWriteArraySet<>(); | ||
333 | + | ||
334 | + @Override | ||
335 | + public void addRouterListener(OspfRouterListener nodeListener) { | ||
336 | + this.nodeListener.add(nodeListener); | ||
337 | + } | ||
338 | + | ||
339 | + @Override | ||
340 | + public void removeRouterListener(OspfRouterListener nodeListener) { | ||
341 | + this.nodeListener = null; | ||
342 | + } | ||
343 | + | ||
344 | + @Override | ||
345 | + public void addLinkListener(OspfLinkListener listener) { | ||
346 | + this.linkListener.add(listener); | ||
347 | + } | ||
348 | + | ||
349 | + @Override | ||
350 | + public void removeLinkListener(OspfLinkListener listener) { | ||
351 | + this.nodeListener = null; | ||
352 | + } | ||
353 | + | ||
354 | + @Override | ||
355 | + public void updateConfig(JsonNode processesNode) { | ||
356 | + } | ||
357 | + | ||
358 | + | ||
359 | + @Override | ||
360 | + public void deleteConfig(List<OspfProcess> processes, String attribute) { | ||
361 | + } | ||
362 | + | ||
363 | + @Override | ||
364 | + public Set<OspfRouterListener> listener() { | ||
365 | + return null; | ||
366 | + } | ||
367 | + | ||
368 | + @Override | ||
369 | + public Set<OspfLinkListener> linkListener() { | ||
370 | + return null; | ||
371 | + } | ||
372 | + | ||
373 | + @Override | ||
374 | + public List<OspfProcess> getAllConfiguredProcesses() { | ||
375 | + return null; | ||
376 | + } | ||
377 | + } | ||
378 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment