blob: 79689c023a51098e232c44cfbddcd77f0d3fbc26 [file] [log] [blame]
Kalyankumar Asangi19f64492016-02-17 17:34:16 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Kalyankumar Asangi19f64492016-02-17 17:34:16 +05303 *
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 */
16package org.onosproject.ospf.controller.area;
17
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053018import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.Ip4Address;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053022import org.onosproject.ospf.controller.OspfInterface;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053023import org.onosproject.ospf.controller.OspfNeighborState;
24import org.onosproject.ospf.controller.TopologyForDeviceAndLink;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053025import org.onosproject.ospf.controller.impl.OspfNbrImpl;
26import org.onosproject.ospf.controller.impl.TopologyForDeviceAndLinkImpl;
27import org.onosproject.ospf.controller.lsdb.LsaWrapperImpl;
28import org.onosproject.ospf.protocol.lsa.LsaHeader;
sunishvkf7c56552016-07-18 16:02:39 +053029import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053030import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
sunishvkf7c56552016-07-18 16:02:39 +053031import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053032import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
33import org.onosproject.ospf.protocol.util.OspfInterfaceState;
sunishvkf7c56552016-07-18 16:02:39 +053034import org.onosproject.ospf.protocol.util.OspfParameters;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053035
36import java.util.ArrayList;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053037import java.util.List;
38
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.*;
41
42/**
43 * Unit test class for OspfAreaImpl.
44 */
45public class OspfAreaImplTest {
46
47 private OspfAreaImpl ospfArea;
48 private int result;
sunishvkf7c56552016-07-18 16:02:39 +053049 private List<OspfInterface> ospfInterfaces = new ArrayList<>();
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053050 private OspfInterfaceImpl ospfInterface1;
51 private OspfInterfaceImpl ospfInterface2;
52 private OspfInterfaceImpl ospfInterface3;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053053 private OspfNbrImpl ospfNbr;
sunishvkf7c56552016-07-18 16:02:39 +053054 private OspfNbrImpl ospfNbr1;
55 private NetworkLsa networkLsa;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053056 private LsaHeader lsaHeader;
sunishvkf7c56552016-07-18 16:02:39 +053057 private Ip4Address ip4Address = Ip4Address.valueOf("10.10.10.10");
58 private Ip4Address ip4Address1 = Ip4Address.valueOf("11.11.11.11");
59 private Ip4Address networkAddress = Ip4Address.valueOf("255.255.255.255");
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053060 private TopologyForDeviceAndLink topologyForDeviceAndLink;
sunishvkf7c56552016-07-18 16:02:39 +053061 private RouterLsa routerLsa;
62 private OpaqueLsaHeader opaqueLsaHeader;
63 private OpaqueLsa10 opaqueLsa10;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053064
65 @Before
66 public void setUp() throws Exception {
sunishvkf7c56552016-07-18 16:02:39 +053067 lsaHeader = new LsaHeader();
68 opaqueLsaHeader = new OpaqueLsaHeader();
69 opaqueLsaHeader.setAdvertisingRouter(ip4Address);
70 lsaHeader.setAdvertisingRouter(ip4Address);
71 routerLsa = new RouterLsa(lsaHeader);
72 routerLsa.setAdvertisingRouter(ip4Address);
73 opaqueLsa10 = new OpaqueLsa10(opaqueLsaHeader);
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053074 ospfArea = new OspfAreaImpl();
sunishvkf7c56552016-07-18 16:02:39 +053075 ospfInterface1 = new OspfInterfaceImpl();
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053076 topologyForDeviceAndLink = new TopologyForDeviceAndLinkImpl();
sunishvkf7c56552016-07-18 16:02:39 +053077 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface1, ip4Address, ip4Address1,
78 2, topologyForDeviceAndLink);
79 ospfNbr.setState(OspfNeighborState.EXCHANGE);
80 ospfNbr1 = new OspfNbrImpl(ospfArea, ospfInterface1, ip4Address, ip4Address1,
81 2, topologyForDeviceAndLink);
82 ospfNbr1.setState(OspfNeighborState.FULL);
83 ospfNbr1.setNeighborId(ip4Address);
84 ospfNbr.setNeighborId(ip4Address);
85 ospfNbr.setIsOpaqueCapable(true);
86 ospfInterface1.addNeighbouringRouter(ospfNbr);
87 ospfInterface1.addNeighbouringRouter(ospfNbr1);
88 ospfInterface2 = new OspfInterfaceImpl();
89 ospfInterface2.setIpAddress(ip4Address);
90 ospfInterface2.setIpNetworkMask(networkAddress);
91 ospfInterface2.setState(OspfInterfaceState.LOOPBACK);
92 ospfInterface2.addNeighbouringRouter(ospfNbr);
93 ospfInterface2.addNeighbouringRouter(ospfNbr1);
94 ospfInterfaces.add(ospfInterface2);
Kalyankumar Asangi19f64492016-02-17 17:34:16 +053095 }
96
97 @After
98 public void tearDown() throws Exception {
99 ospfArea = null;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530100 ospfInterfaces = null;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530101 lsaHeader = null;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530102 networkLsa = null;
103 ospfInterface1 = null;
104 ospfInterface2 = null;
105 ospfInterface3 = null;
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530106
107 }
108
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530109
110 /**
111 * Tests hashCode() method.
112 */
113 @Test
114 public void testHashCode() throws Exception {
115 result = ospfArea.hashCode();
116 assertThat(result, is(notNullValue()));
117 }
118
119 /**
120 * Tests routerId() getter method.
121 */
122 @Test
123 public void testGetRouterId() throws Exception {
124 ospfArea.setRouterId(Ip4Address.valueOf("1.1.1.1"));
125 assertThat(ospfArea.routerId(), is(Ip4Address.valueOf("1.1.1.1")));
126 }
127
128 /**
129 * Tests routerId() setter method.
130 */
131 @Test
132 public void testSetRouterId() throws Exception {
133 ospfArea.setRouterId(Ip4Address.valueOf("1.1.1.1"));
134 assertThat(ospfArea.routerId(), is(Ip4Address.valueOf("1.1.1.1")));
135 }
136
137 /**
138 * Tests isOpaqueEnabled() getter method.
139 */
140 @Test
141 public void testSetisOpaqueEnabled() throws Exception {
142 ospfArea.setIsOpaqueEnabled(true);
143 assertThat(ospfArea.isOpaqueEnabled(), is(true));
144 }
145
146 /**
147 * Tests isOpaqueEnabled() setter method.
148 */
149 @Test
150 public void testIsOpaqueEnabled() throws Exception {
151 ospfArea.setIsOpaqueEnabled(true);
152 assertThat(ospfArea.isOpaqueEnabled(), is(true));
153 }
154
155 /**
156 * Tests initializeDb() method.
157 */
158 @Test
159 public void testInitializeDb() throws Exception {
160 ospfArea.initializeDb();
161 assertThat(ospfArea, is(notNullValue()));
162 }
163
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530164
165 /**
166 * Tests buildNetworkLsa() method.
167 */
168 @Test
169 public void testBuildNetworkLsa() throws Exception {
170 ospfInterfaces = new ArrayList();
171 ospfInterface1 = new OspfInterfaceImpl();
172 ospfInterface1.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
173 ospfInterfaces.add(ospfInterface1);
174 ospfInterface2 = new OspfInterfaceImpl();
175 ospfInterface2.setIpAddress(Ip4Address.valueOf("2.2.2.2"));
176 ospfInterfaces.add(ospfInterface2);
177 ospfInterface3 = new OspfInterfaceImpl();
178 ospfInterface3.setIpAddress(Ip4Address.valueOf("3.3.3.3"));
179 ospfInterfaces.add(ospfInterface3);
sunishvkf7c56552016-07-18 16:02:39 +0530180 ospfArea.setOspfInterfaceList(ospfInterfaces);
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530181 ospfArea.setRouterId(Ip4Address.valueOf("111.111.111.111"));
182 networkLsa = ospfArea.buildNetworkLsa(Ip4Address.valueOf("1.1.1.1"),
183 Ip4Address.valueOf("255.255.255.255"));
184 assertThat(ospfInterfaces.size(), is(3));
185 assertThat(networkLsa, is(notNullValue()));
186 assertThat(ospfArea, is(notNullValue()));
187 }
188
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530189
190 /**
191 * Tests areaId() getter method.
192 */
193 @Test
194 public void testGetAreaId() throws Exception {
195 ospfArea.setAreaId(Ip4Address.valueOf("1.1.1.1"));
196 assertThat(ospfArea.areaId(), is(Ip4Address.valueOf("1.1.1.1")));
197 }
198
199 /**
200 * Tests areaId() setter method.
201 */
202 @Test
203 public void testSetAreaId() throws Exception {
204 ospfArea.setAreaId(Ip4Address.valueOf("1.1.1.1"));
205 assertThat(ospfArea.areaId(), is(Ip4Address.valueOf("1.1.1.1")));
206 }
207
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530208
209 /**
210 * Tests isExternalRoutingCapability() getter method.
211 */
212 @Test
213 public void testIsExternalRoutingCapability() throws Exception {
214 ospfArea.setExternalRoutingCapability(true);
215 assertThat(ospfArea.isExternalRoutingCapability(), is(true));
216 }
217
218 /**
219 * Tests isExternalRoutingCapability() setter method.
220 */
221 @Test
222 public void testSetExternalRoutingCapability() throws Exception {
223 ospfArea.setExternalRoutingCapability(true);
224 assertThat(ospfArea.isExternalRoutingCapability(), is(true));
225 }
226
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530227
228 /**
sunishvkf7c56552016-07-18 16:02:39 +0530229 * Tests ospfInterfaceList() getter method.
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530230 */
231 @Test
232 public void testGetInterfacesLst() throws Exception {
233 ospfInterfaces = new ArrayList();
234 ospfInterface1 = new OspfInterfaceImpl();
235 ospfInterface1.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
236 ospfInterfaces.add(ospfInterface1);
237 ospfInterface2 = new OspfInterfaceImpl();
238 ospfInterface2.setIpAddress(Ip4Address.valueOf("2.2.2.2"));
239 ospfInterfaces.add(ospfInterface2);
240 ospfInterface3 = new OspfInterfaceImpl();
241 ospfInterface3.setIpAddress(Ip4Address.valueOf("3.3.3.3"));
242 ospfInterfaces.add(ospfInterface3);
sunishvkf7c56552016-07-18 16:02:39 +0530243 ospfArea.setOspfInterfaceList(ospfInterfaces);
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530244 assertThat(ospfInterfaces.size(), is(3));
sunishvkf7c56552016-07-18 16:02:39 +0530245 assertThat(ospfArea.ospfInterfaceList(), is(notNullValue()));
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530246 }
247
248 /**
249 * Tests setInterfacesLst() setter method.
250 */
251 @Test
252 public void testSetInterfacesLst() throws Exception {
253 ospfInterfaces = new ArrayList();
254 ospfInterface1 = new OspfInterfaceImpl();
255 ospfInterface1.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
256 ospfInterfaces.add(ospfInterface1);
257 ospfInterface2 = new OspfInterfaceImpl();
258 ospfInterface2.setIpAddress(Ip4Address.valueOf("2.2.2.2"));
259 ospfInterfaces.add(ospfInterface2);
260 ospfInterface3 = new OspfInterfaceImpl();
261 ospfInterface3.setIpAddress(Ip4Address.valueOf("3.3.3.3"));
262 ospfInterfaces.add(ospfInterface3);
sunishvkf7c56552016-07-18 16:02:39 +0530263 ospfArea.setOspfInterfaceList(ospfInterfaces);
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530264 assertThat(ospfInterfaces.size(), is(3));
sunishvkf7c56552016-07-18 16:02:39 +0530265 assertThat(ospfArea.ospfInterfaceList(), is(notNullValue()));
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530266 }
267
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530268
269 /**
270 * Tests getLsaHeaders() method.
271 */
272 @Test
273 public void testGetLsaHeaders() throws Exception {
274 assertThat(ospfArea.getLsaHeaders(true, true).size(), is(0));
275 }
276
277 /**
278 * Tests getLsa() method.
279 */
280 @Test
281 public void testGetLsa() throws Exception {
282 assertThat(ospfArea.getLsa(1, "1.1.1.1", "1.1.1.1"), is(nullValue()));
283 assertThat(ospfArea.getLsa(10, "1.1.1.1", "1.1.1.1"), is(nullValue()));
284 }
285
286 /**
287 * Tests lsaLookup() method.
288 */
289 @Test
290 public void testLsaLookup() throws Exception {
291 assertThat(ospfArea.lsaLookup(new RouterLsa()), is(nullValue()));
292 }
293
294 /**
295 * Tests isNewerOrSameLsa() method.
296 */
297 @Test
298 public void testIsNewerOrSameLsa() throws Exception {
299 assertThat(ospfArea.isNewerOrSameLsa(new RouterLsa(), new RouterLsa()), is("same"));
300 }
301
302 /**
303 * Tests addLsa() method.
304 */
305 @Test
306 public void testAddLsa() throws Exception {
307 ospfArea.addLsa(new RouterLsa(), new OspfInterfaceImpl());
308 assertThat(ospfArea, is(notNullValue()));
309 }
310
311 /**
312 * Tests addLsa() method.
313 */
314 @Test
315 public void testAddLsa1() throws Exception {
316 ospfArea.addLsa(new RouterLsa(), false, new OspfInterfaceImpl());
317 assertThat(ospfArea, is(notNullValue()));
318 }
319
320 /**
321 * Tests addLsaToMaxAgeBin() method.
322 */
323 @Test
324 public void testAddLsaToMaxAgeBin() throws Exception {
325 ospfArea.addLsaToMaxAgeBin("111", new LsaWrapperImpl());
326 assertThat(ospfArea, is(notNullValue()));
327 }
328
329 /**
330 * Tests setDbRouterSequenceNumber() method.
331 */
332 @Test
333 public void testSetDbRouterSequenceNumber() throws Exception {
334 ospfArea.setDbRouterSequenceNumber(123456);
335 assertThat(ospfArea, is(notNullValue()));
336 }
337
338 /**
339 * Tests deleteLsa() method.
340 */
341 @Test
342 public void testDeleteLsa() throws Exception {
343 ospfArea.deleteLsa(new LsaHeader());
344 assertThat(ospfArea, is(notNullValue()));
345 }
346
347 /**
348 * Tests removeLsaFromBin() method.
349 */
350 @Test
351 public void testRemoveLsaFromBin() throws Exception {
352 ospfArea.removeLsaFromBin(new LsaWrapperImpl());
353 assertThat(ospfArea, is(notNullValue()));
354 }
355
356 /**
357 * Tests to string method.
358 */
359 @Test
360 public void testToString() throws Exception {
361 assertThat(ospfArea.toString(), is(notNullValue()));
362 }
363
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530364
365 /**
366 * Tests getLsaKey() method.
367 */
368 @Test
369 public void testGetLsaKey() throws Exception {
370 lsaHeader = new LsaHeader();
371 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("1.1.1.1"));
372 assertThat(ospfArea.getLsaKey(lsaHeader), is(notNullValue()));
373 }
374
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530375
376 /**
377 * Tests options() getter method.
378 */
379 @Test
380 public void testGetOptions() throws Exception {
381 ospfArea.setOptions(2);
382 assertThat(ospfArea.options(), is(2));
383 }
384
385 /**
386 * Tests options() setter method.
387 */
388 @Test
389 public void testSetOptions() throws Exception {
390 ospfArea.setOptions(2);
391 assertThat(ospfArea.options(), is(2));
392 }
393
394 /**
395 * Tests isOpaqueEnabled() method.
396 */
397 @Test
398 public void testGetOpaqueEnabledOptions() throws Exception {
399 ospfArea.setIsOpaqueEnabled(true);
400 assertThat(ospfArea.isOpaqueEnabled(), is(true));
401 }
402
403 /**
404 * Tests database() method.
405 */
406 @Test
407 public void testGetDatabase() throws Exception {
408 assertThat(ospfArea.database(), is(notNullValue()));
409 }
410
411 /**
412 * Tests opaqueEnabledOptions() method.
413 */
414 @Test
415 public void testOpaqueEnabledOptionsa() throws Exception {
416 assertThat(ospfArea.opaqueEnabledOptions(), is(66));
417 }
sunishvkf7c56552016-07-18 16:02:39 +0530418
419 /**
420 * Tests noNeighborInLsaExchangeProcess() method.
421 */
422 @Test
423 public void testNoNeighborInLsaExchangeProcess() throws Exception {
424 ospfArea.setOspfInterfaceList(ospfInterfaces);
425 ospfArea.noNeighborInLsaExchangeProcess();
426 assertThat(ospfArea, is(notNullValue()));
427 }
428
429 /**
430 * Tests getNeighborsInFullState() method.
431 */
432 @Test
433 public void testGetNeighborsInFullState() throws Exception {
434 ospfArea.getNeighborsInFullState(ospfInterface1);
435 assertThat(ospfArea, is(notNullValue()));
436 }
437
438 /**
439 * Tests addToOtherNeighborLsaTxList() method.
440 */
441 @Test
442 public void testAddToOtherNeighborLsaTxList() throws Exception {
443 ospfArea.setOspfInterfaceList(ospfInterfaces);
444 ospfArea.addToOtherNeighborLsaTxList(routerLsa);
445 assertThat(ospfArea, is(notNullValue()));
446
447 opaqueLsa10.setLsType(OspfParameters.LINK_LOCAL_OPAQUE_LSA);
448 ospfArea.addToOtherNeighborLsaTxList(opaqueLsa10);
449 assertThat(ospfArea, is(notNullValue()));
450 }
451
452 /**
453 * Tests buildRouterLsa() method.
454 */
455 @Test
456 public void testBuildRouterLsa() throws Exception {
457 ospfArea.setRouterId(ip4Address);
458 ospfArea.setOspfInterfaceList(ospfInterfaces);
459 ospfInterface1.setState(OspfInterfaceState.POINT2POINT);
460 ospfInterface1.setIpAddress(ip4Address);
461 ospfInterface1.setIpNetworkMask(networkAddress);
462 ospfInterfaces.add(ospfInterface1);
463 ospfArea.buildRouterLsa(ospfInterface1);
464 ospfArea.setOspfInterfaceList(ospfInterfaces);
465 assertThat(ospfArea, is(notNullValue()));
466
467 }
Kalyankumar Asangi19f64492016-02-17 17:34:16 +0530468}