blob: aeb713104ebfad02c7804c25eb50f1e9c5e6371e [file] [log] [blame]
sunish vk1857c422016-02-17 18:05:52 +05301/*
2 * Copyright 2016 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 */
16package org.onosproject.ospf.controller.impl;
17
18import org.easymock.EasyMock;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.buffer.ChannelBuffers;
21import org.jboss.netty.buffer.HeapChannelBufferFactory;
22import org.jboss.netty.channel.Channel;
23import org.jboss.netty.channel.ChannelConfig;
24import org.jboss.netty.channel.ChannelFuture;
25import org.jboss.netty.channel.ChannelHandlerContext;
26import org.jboss.netty.channel.ChannelStateEvent;
27import org.jboss.netty.channel.ExceptionEvent;
28import org.jboss.netty.channel.MessageEvent;
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32import org.onlab.packet.Ip4Address;
33import org.onosproject.ospf.controller.OspfAreaAddressRange;
34import org.onosproject.ospf.controller.OspfInterface;
35import org.onosproject.ospf.controller.OspfNbr;
36import org.onosproject.ospf.controller.OspfNeighborState;
37import org.onosproject.ospf.controller.TopologyForDeviceAndLink;
38import org.onosproject.ospf.controller.area.OspfAreaAddressRangeImpl;
39import org.onosproject.ospf.controller.area.OspfAreaImpl;
40import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
41import org.onosproject.ospf.controller.util.OspfEligibleRouter;
42import org.onosproject.ospf.exceptions.OspfParseException;
43import org.onosproject.ospf.protocol.lsa.LsaHeader;
44import org.onosproject.ospf.protocol.lsa.TlvHeader;
45import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
46import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
47import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
48import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
49import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
50import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
51import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
52import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
53import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
54import org.onosproject.ospf.protocol.util.ChecksumCalculator;
55import org.onosproject.ospf.protocol.util.OspfInterfaceState;
56
57import java.net.SocketAddress;
58import java.net.UnknownHostException;
59import java.util.ArrayList;
60import java.util.HashMap;
61import java.util.List;
62
63import static org.hamcrest.CoreMatchers.is;
64import static org.hamcrest.CoreMatchers.notNullValue;
65import static org.hamcrest.MatcherAssert.assertThat;
66
67
68/**
69 * Unit test class for OspfInterfaceChannelHandler.
70 */
71public class OspfInterfaceChannelHandlerTest {
72
73 private List<OspfAreaAddressRange> addressRanges;
74 private List<OspfInterface> ospfInterfaces;
75 private Controller controller;
76 private OspfAreaImpl ospfArea;
77 private OspfInterfaceImpl ospfInterface;
78 private OspfInterfaceChannelHandler ospfInterfaceChannelHandler;
79 private HashMap<String, OspfNbr> ospfNbrHashMap;
80 private OspfNbrImpl ospfNbr;
81 private Channel channel;
82 private ChannelHandlerContext channelHandlerContext;
83 private ChannelStateEvent channelStateEvent;
84 private HelloPacket helloPacket;
85 private DdPacket ddPacket;
86 private ChecksumCalculator checksumCalculator;
87 private byte[] byteArray;
88 private byte[] checkArray;
89 private ChannelBuffer buf;
90 private OspfEligibleRouter ospfEligibleRouter;
91 private LsUpdate lsUpdate;
92 private LsAcknowledge lsAck;
93 private LsRequest lsRequest;
94 private TopologyForDeviceAndLink topologyForDeviceAndLink;
95
96 @Before
97 public void setUp() throws Exception {
98 addressRanges = new ArrayList();
99 ospfInterfaces = new ArrayList<>();
100 ospfArea = createOspfArea();
101 ospfInterface = createOspfInterface();
102 ospfNbrHashMap = new HashMap();
103 topologyForDeviceAndLink = new TopologyForDeviceAndLinkImpl();
104 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
105 Ip4Address.valueOf("2.2.2.2"), 2,
106 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530107 ospfInterface),
108 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530109 ospfNbr.setNeighborId(Ip4Address.valueOf("10.10.10.10"));
110 ospfNbr.setRouterPriority(0);
111 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
112 ospfInterface.addNeighbouringRouter(ospfNbr);
113 controller = new Controller();
114 ospfInterfaceChannelHandler = new OspfInterfaceChannelHandler();
115 ospfInterfaceChannelHandler = new OspfInterfaceChannelHandler(controller, ospfArea,
116 ospfInterface);
117 }
118
119 @After
120 public void tearDown() throws Exception {
121 ospfInterfaceChannelHandler = null;
122 addressRanges = null;
123 ospfInterfaces = null;
124 controller = null;
125 ospfArea = null;
126 ospfInterfaceChannelHandler = null;
127 ospfInterface = null;
128 ospfNbrHashMap = null;
129 channel = null;
130 channelHandlerContext = null;
131 channelStateEvent = null;
132 helloPacket = null;
133 ddPacket = null;
134 checksumCalculator = null;
135 byteArray = null;
136 checkArray = null;
137 ospfEligibleRouter = null;
138 lsUpdate = null;
139 lsAck = null;
140 lsRequest = null;
141 }
142
143 /**
144 * Tests interfaceUp() method.
145 */
146 @Test
147 public void testInterfaceUp() throws Exception {
148 ospfInterface.setInterfaceType(2);
149 ospfInterface.setRouterPriority(0);
150 ospfInterfaceChannelHandler.interfaceUp();
151 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
152 }
153
154 /**
155 * Tests interfaceUp() method.
156 */
157 @Test
158 public void testInterfaceUp1() throws Exception {
159
160 ospfInterface.setInterfaceType(2);
161 ospfInterface.setRouterPriority(0);
162 ospfInterfaceChannelHandler.interfaceUp();
163 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
164 }
165
166 /**
167 * Tests interfaceUp() method.
168 */
169 @Test
170 public void testInterfaceUp2() throws Exception {
171 ospfInterface.setInterfaceType(1);
172 ospfInterface.setRouterPriority(1);
173 ospfInterfaceChannelHandler.interfaceUp();
174 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
175 }
176
177 /**
178 * Tests interfaceUp() method.
179 */
180 @Test
181 public void testInterfaceUp3() throws Exception {
182 ospfInterface.setInterfaceType(2);
183 ospfInterface.setRouterPriority(1);
184 ospfInterfaceChannelHandler.interfaceUp();
185 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
186 }
187
188 /**
189 * Tests backupSeen() method.
190 */
191 @Test
192 public void testBackupSeen() throws Exception {
193 channel = EasyMock.createMock(Channel.class);
194 ospfInterface.setState(OspfInterfaceState.WAITING);
195 ospfInterfaceChannelHandler.backupSeen(channel);
196 assertThat(ospfInterface.dr(), is(notNullValue()));
197 }
198
199 /**
200 * Tests waitTimer() method.
201 */
202 @Test
203 public void testWaitTimer() throws Exception {
204 channel = EasyMock.createMock(Channel.class);
205 ospfInterface.setState(OspfInterfaceState.WAITING);
206 ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.10"));
207 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
208 ospfInterfaceChannelHandler.waitTimer(channel);
209 assertThat(ospfInterface.dr(), is(notNullValue()));
210 }
211
212 /**
213 * Tests neighborChange() method.
214 */
215 @Test
216 public void testNeighborChange() throws Exception {
217 ospfNbrHashMap = new HashMap();
218 ospfNbr = new OspfNbrImpl(new OspfAreaImpl(), new OspfInterfaceImpl(),
219 Ip4Address.valueOf("1.1.1.1"), Ip4Address.valueOf("2.2.2.2"), 2,
220 new OspfInterfaceChannelHandler(new Controller(),
221 new OspfAreaImpl(),
sunish vkaa48da82016-03-02 23:17:06 +0530222 new OspfInterfaceImpl()),
223 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530224 ospfNbr.setNeighborId(Ip4Address.valueOf("111.111.111.111"));
225 ospfNbrHashMap.put("111.111.111.111", ospfNbr);
226 ospfNbr.setState(OspfNeighborState.EXCHANGE);
227 ospfInterface.setListOfNeighbors(ospfNbrHashMap);
228 ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.10"));
229 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
230 ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.10"));
231 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
232 channel = EasyMock.createMock(Channel.class);
233 ospfInterface.setState(OspfInterfaceState.DR);
234 ospfInterfaceChannelHandler.waitTimer(channel);
235 assertThat(ospfInterface.dr(), is(Ip4Address.valueOf("0.0.0.0")));
236 }
237
238 /**
239 * Tests interfaceDown() method.
240 */
241 @Test(expected = Exception.class)
242 public void testInterfaceDown() throws Exception {
243 ospfInterfaceChannelHandler.interfaceDown();
244 assertThat(ospfInterface.state(), is(OspfInterfaceState.DOWN));
245 }
246
247 /**
248 * Tests channelConnected() method.
249 */
250 @Test(expected = Exception.class)
251 public void testChannelConnected() throws Exception {
252 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
253 channelStateEvent = EasyMock.createMock(ChannelStateEvent.class);
254 ospfInterfaceChannelHandler.channelConnected(channelHandlerContext, channelStateEvent);
255 assertThat(ospfInterface.state(), is(notNullValue()));
256 }
257
258 /**
259 * Tests exceptionCaught() method.
260 */
261 @Test(expected = Exception.class)
262 public void testExceptionCaught() throws Exception {
263 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
264 ExceptionEvent exception = EasyMock.createMock(ExceptionEvent.class);
265 ospfInterfaceChannelHandler.exceptionCaught(channelHandlerContext, exception);
266 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
267 }
268
269 /**
270 * Tests channelDisconnected() method.
271 */
272 @Test(expected = Exception.class)
273 public void testChannelDisconnected() throws Exception {
274 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
275 channelStateEvent = EasyMock.createMock(ChannelStateEvent.class);
276
277 ospfInterfaceChannelHandler.channelDisconnected(channelHandlerContext, channelStateEvent);
278
279 assertThat(ospfInterface.state(), is(notNullValue()));
280 }
281
282 /**
283 * Tests messageReceived() method.
284 */
285 @Test
286 public void testMessageReceived() throws Exception {
287 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
288 ospfInterface.setInterfaceType(2);
289 ospfArea.setAreaId(Ip4Address.valueOf("13.13.13.13"));
290 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
291 MessageEvent messageEvent = new MessageEvent() {
292 @Override
293 public Object getMessage() {
294 helloPacket = new HelloPacket();
295 helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
296 helloPacket.setRouterId(Ip4Address.valueOf("10.10.10.10"));
297 helloPacket.setOspfVer(2);
298 helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
299 helloPacket.setOptions(2);
300 helloPacket.setAreaId(Ip4Address.valueOf("5.5.5.5"));
301 helloPacket.setNetworkMask(Ip4Address.valueOf("3.3.3.3"));
302 helloPacket.setOspftype(1);
303 helloPacket.setAuthType(0);
304 helloPacket.setAuthentication(0);
305 checksumCalculator = new ChecksumCalculator();
306 byteArray = helloPacket.asBytes();
307 helloPacket.setOspfPacLength(byteArray.length);
308 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
309 buf = ChannelBuffers.copiedBuffer(checkArray);
310 helloPacket.setChecksum(buf.readUnsignedShort());
311 List<HelloPacket> messPackets = new ArrayList<>();
312 messPackets.add(helloPacket);
313 return messPackets;
314 }
315
316 @Override
317 public SocketAddress getRemoteAddress() {
318 return null;
319 }
320
321 @Override
322 public Channel getChannel() {
323 return null;
324 }
325
326 @Override
327 public ChannelFuture getFuture() {
328 return null;
329 }
330 };
331 ospfInterfaceChannelHandler.messageReceived(channelHandlerContext, messageEvent);
332 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
333 }
334
335 /**
sunish vkaa48da82016-03-02 23:17:06 +0530336 * Tests processOspfMessage() method.
sunish vk1857c422016-02-17 18:05:52 +0530337 */
338 @Test
sunish vkaa48da82016-03-02 23:17:06 +0530339 public void testProcessOspfMessage() throws Exception {
sunish vk1857c422016-02-17 18:05:52 +0530340 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
341 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("225.225.225.225"));
342 ospfInterface.setInterfaceType(2);
343 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
344 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
345 OspfMessage message;
346 helloPacket = new HelloPacket();
347 helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
348 helloPacket.setRouterId(Ip4Address.valueOf("10.10.10.10"));
349 helloPacket.setOspfVer(2);
350 helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
351 helloPacket.setOptions(2);
352 helloPacket.setNetworkMask(Ip4Address.valueOf("3.3.3.3"));
353 helloPacket.setOspftype(1);
354 helloPacket.setAuthType(0);
355 helloPacket.setHelloInterval(60);
356 helloPacket.setRouterDeadInterval(60);
357 helloPacket.setAuthentication(0);
358 checksumCalculator = new ChecksumCalculator();
359 byteArray = helloPacket.asBytes();
360 helloPacket.setOspfPacLength(byteArray.length);
361 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
362 checkArray[0] = -53;
363 checkArray[1] = 37;
364 buf = ChannelBuffers.copiedBuffer(checkArray);
365 helloPacket.setChecksum(buf.readUnsignedShort());
366 message = helloPacket;
sunish vkaa48da82016-03-02 23:17:06 +0530367 ospfInterfaceChannelHandler.processOspfMessage(message, channelHandlerContext);
sunish vk1857c422016-02-17 18:05:52 +0530368 ddPacket = new DdPacket();
369 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
370 ddPacket.setRouterId(Ip4Address.valueOf("10.10.10.10"));
371 ddPacket.setOspfVer(2);
372 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
373 ddPacket.setOptions(2);
374 ddPacket.setOspftype(2);
375 ddPacket.setAuthType(0);
376 ddPacket.setAuthentication(0);
377 checksumCalculator = new ChecksumCalculator();
378 byteArray = ddPacket.asBytes();
379 ddPacket.setOspfPacLength(byteArray.length);
380 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
381 checkArray[0] = -49;
382 checkArray[1] = -79;
383 buf = ChannelBuffers.copiedBuffer(checkArray);
384 ddPacket.setChecksum(buf.readUnsignedShort());
385 message = ddPacket;
sunish vkaa48da82016-03-02 23:17:06 +0530386 ospfInterfaceChannelHandler.processOspfMessage(message, channelHandlerContext);
sunish vk1857c422016-02-17 18:05:52 +0530387 lsRequest = new LsRequest();
388 lsRequest.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
389 lsRequest.setRouterId(Ip4Address.valueOf("10.10.10.10"));
390 lsRequest.setOspfVer(2);
391 lsRequest.setAreaId(Ip4Address.valueOf("12.12.12.12"));
392 lsRequest.setOspftype(3);
393 lsRequest.setAuthType(0);
394 lsRequest.setAuthentication(0);
395 checksumCalculator = new ChecksumCalculator();
396 byteArray = lsRequest.asBytes();
397 lsRequest.setOspfPacLength(byteArray.length);
398 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
399 checkArray[0] = -33;
400 checkArray[1] = -58;
401 buf = ChannelBuffers.copiedBuffer(checkArray);
402 lsRequest.setChecksum(buf.readUnsignedShort());
403 message = lsRequest;
sunish vkaa48da82016-03-02 23:17:06 +0530404 ospfInterfaceChannelHandler.processOspfMessage(message, channelHandlerContext);
sunish vk1857c422016-02-17 18:05:52 +0530405 lsUpdate = new LsUpdate();
406 lsUpdate.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
407 lsUpdate.setRouterId(Ip4Address.valueOf("10.10.10.10"));
408 lsUpdate.setOspfVer(2);
409 lsUpdate.setAreaId(Ip4Address.valueOf("12.12.12.12"));
410 lsUpdate.setOspftype(4);
411 lsUpdate.setAuthType(0);
412 lsUpdate.setAuthentication(0);
413 checksumCalculator = new ChecksumCalculator();
414 byteArray = lsUpdate.asBytes();
415 lsUpdate.setOspfPacLength(byteArray.length);
416 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
417 checkArray[0] = -47;
418 checkArray[1] = -77;
419 buf = ChannelBuffers.copiedBuffer(checkArray);
420 lsUpdate.setChecksum(buf.readUnsignedShort());
421 message = lsUpdate;
sunish vkaa48da82016-03-02 23:17:06 +0530422 ospfInterfaceChannelHandler.processOspfMessage(message, channelHandlerContext);
sunish vk1857c422016-02-17 18:05:52 +0530423 lsAck = new LsAcknowledge();
424 lsAck.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
425 lsAck.setRouterId(Ip4Address.valueOf("10.10.10.10"));
426 lsAck.setOspfVer(2);
427 lsAck.setAreaId(Ip4Address.valueOf("12.12.12.12"));
428 lsAck.setOspftype(5);
429 lsAck.setAuthType(0);
430 lsAck.setAuthentication(0);
431 checksumCalculator = new ChecksumCalculator();
432 byteArray = lsAck.asBytes();
433 lsAck.setOspfPacLength(byteArray.length);
434 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
435 checkArray[0] = -47;
436 checkArray[1] = -74;
437 buf = ChannelBuffers.copiedBuffer(checkArray);
438 lsAck.setChecksum(buf.readUnsignedShort());
439 message = lsAck;
sunish vkaa48da82016-03-02 23:17:06 +0530440 ospfInterfaceChannelHandler.processOspfMessage(message, channelHandlerContext);
sunish vk1857c422016-02-17 18:05:52 +0530441 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
442
443 }
444
445 /**
446 * Tests processHelloMessage() method.
447 */
448 @Test
449 public void testProcessHelloMessage() throws Exception {
450 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
451 ospfInterface.setInterfaceType(1);
452 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("244.244.244.244"));
453 ospfInterface.setHelloIntervalTime(10);
454 ospfInterface.setRouterDeadIntervalTime(10);
455 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
456 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
457 OspfMessage message;
458 helloPacket = new HelloPacket();
459 helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
460 helloPacket.setOspfVer(2);
461 helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
462 helloPacket.setNetworkMask(Ip4Address.valueOf("244.244.244.244"));
463 helloPacket.setHelloInterval(10);
464 helloPacket.setRouterDeadInterval(10);
465 helloPacket.setDr(Ip4Address.valueOf("10.10.10.10"));
466 helloPacket.setBdr(Ip4Address.valueOf("11.11.11.11"));
467 helloPacket.setRouterId(Ip4Address.valueOf("111.111.111.111"));
468 message = helloPacket;
469 ospfInterfaceChannelHandler.processHelloMessage(message, channelHandlerContext);
470 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
471 }
472
473 /**
474 * Tests processHelloMessage() method.
475 */
476 @Test
477 public void testProcessHelloMessage1() throws Exception {
478 ospfInterface.setInterfaceType(2);
479 ospfInterface.setRouterPriority(1);
480 ospfInterfaceChannelHandler.interfaceUp();
481 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
482 ospfInterface.setState(OspfInterfaceState.WAITING);
483 ospfInterface.setInterfaceType(2);
484 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("244.244.244.244"));
485 ospfInterface.setHelloIntervalTime(10);
486 ospfInterface.setRouterDeadIntervalTime(10);
487 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
488 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
489 OspfMessage message;
490 helloPacket = new HelloPacket();
491 helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
492 helloPacket.setOspfVer(2);
493 helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
494 helloPacket.setNetworkMask(Ip4Address.valueOf("244.244.244.244"));
495 helloPacket.setHelloInterval(10);
496 helloPacket.setRouterDeadInterval(10);
497 helloPacket.setDr(Ip4Address.valueOf("10.10.10.10"));
498 helloPacket.setBdr(Ip4Address.valueOf("11.11.11.11"));
499 helloPacket.setRouterId(Ip4Address.valueOf("2.2.2.2"));
500 message = helloPacket;
501 ospfInterfaceChannelHandler.processHelloMessage(message, channelHandlerContext);
502 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
503 ospfNbrHashMap = new HashMap();
504 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
505 Ip4Address.valueOf("2.2.2.2"), 2,
506 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530507 ospfInterface),
508 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530509 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
510 ospfNbr.setRouterPriority(0);
511 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
512 ospfInterface.addNeighbouringRouter(ospfNbr);
513 ospfInterfaceChannelHandler.processHelloMessage(message, channelHandlerContext);
514 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
515 }
516
517 /**
518 * Tests processDdMessage() method.
519 */
520 @Test
521 public void testProcessDdMessage() throws Exception {
522 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
523 ospfInterface.setInterfaceType(2);
524 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
525 ospfInterface.setHelloIntervalTime(10);
526 ospfInterface.setRouterDeadIntervalTime(10);
527 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
528 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
529 OspfMessage message;
530 ddPacket = new DdPacket();
531 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
532 ddPacket.setOspfVer(2);
533 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
534 ddPacket.setRouterId(Ip4Address.valueOf("2.2.2.2"));
535 ddPacket.setIsOpaqueCapable(true);
536 ddPacket.setIsMore(1);
537 ddPacket.setIsInitialize(1);
538 ddPacket.setIsMaster(1);
539 ddPacket.setSequenceNo(123);
540 message = ddPacket;
541 ospfNbrHashMap = new HashMap();
542 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
543 Ip4Address.valueOf("2.2.2.2"), 2,
544 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530545 ospfInterface),
546 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530547 ospfNbr.setLastDdPacket(createDdPacket());
548 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
549 ospfNbr.setState(OspfNeighborState.EXSTART);
550 ospfNbr.setRouterPriority(0);
551 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
552 ospfNbr.setDdSeqNum(123);
553 ospfInterface.addNeighbouringRouter(ospfNbr);
554 ospfInterfaceChannelHandler.processDdMessage(message, channelHandlerContext);
555 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
556 }
557
558 /**
559 * Tests processDdMessage() method.
560 */
561 @Test(expected = Exception.class)
562 public void testProcessDdMessage3() throws Exception {
563 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
564 ospfInterface.setInterfaceType(2);
565 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
566 ospfInterface.setHelloIntervalTime(10);
567 ospfInterface.setRouterDeadIntervalTime(10);
568 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
569 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
570 OspfMessage message;
571 ddPacket = new DdPacket();
572 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
573 ddPacket.setOspfVer(2);
574 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
575 ddPacket.setRouterId(Ip4Address.valueOf("2.2.2.2"));
576 ddPacket.setIsOpaqueCapable(true);
577 ddPacket.setIsMore(1);
578 ddPacket.setIsInitialize(1);
579 ddPacket.setIsMaster(1);
580 ddPacket.setSequenceNo(123);
581 message = ddPacket;
582 ospfNbrHashMap = new HashMap();
583 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
584 Ip4Address.valueOf("2.2.2.2"), 2,
585 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530586 ospfInterface),
587 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530588 ospfNbr.setLastDdPacket(createDdPacket());
589 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
590 ospfNbr.setState(OspfNeighborState.EXSTART);
591 ospfNbr.setRouterPriority(0);
592 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
593 ospfNbr.setDdSeqNum(123);
594 ospfInterface.addNeighbouringRouter(ospfNbr);
595 ddPacket.setIsMore(1);
596 ddPacket.setIsInitialize(0);
597 ddPacket.setIsMaster(0);
598 ddPacket.setSequenceNo(123);
599 ospfInterface.addNeighbouringRouter(ospfNbr);
600 ospfInterfaceChannelHandler.processDdMessage(message, channelHandlerContext);
601 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
602 }
603
604 /**
605 * Tests processDdMessage() method.
606 */
607 @Test(expected = Exception.class)
608 public void testProcessDdMessage1() throws Exception {
609 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
610 ospfInterface.setInterfaceType(2);
611 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
612 ospfInterface.setHelloIntervalTime(10);
613 ospfInterface.setRouterDeadIntervalTime(10);
614 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
615 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
616 OspfMessage message;
617 ddPacket = new DdPacket();
618 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
619 ddPacket.setOspfVer(2);
620 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
621 ddPacket.setRouterId(Ip4Address.valueOf("2.2.2.2"));
622 ddPacket.setIsOpaqueCapable(true);
623 ddPacket.setIsMore(1);
624 ddPacket.setIsInitialize(1);
625 ddPacket.setIsMaster(1);
626 ddPacket.setSequenceNo(123);
627 message = ddPacket;
628 ospfNbrHashMap = new HashMap();
629 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
630 Ip4Address.valueOf("2.2.2.2"), 2,
631 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530632 ospfInterface),
633 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530634 ospfNbr.setLastDdPacket(createDdPacket());
635 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
636 ospfNbr.setState(OspfNeighborState.EXCHANGE);
637 ospfNbr.setRouterPriority(0);
638 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
639 ospfNbr.setDdSeqNum(123);
640 ospfInterface.addNeighbouringRouter(ospfNbr);
641 ddPacket.setIsMore(1);
642 ddPacket.setIsInitialize(0);
643 ddPacket.setIsMaster(0);
644 ddPacket.setSequenceNo(123);
645 ospfInterface.addNeighbouringRouter(ospfNbr);
646 ospfInterfaceChannelHandler.processDdMessage(message, channelHandlerContext);
647 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
648
649 }
650
651 /**
652 * Tests processDdMessage() method.
653 */
654 @Test(expected = Exception.class)
655 public void testProcessDdMessage2() throws Exception {
656 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
657 ospfInterface.setInterfaceType(2);
658 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
659 ospfInterface.setHelloIntervalTime(10);
660 ospfInterface.setRouterDeadIntervalTime(10);
661 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
662 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
663 OspfMessage message;
664 ddPacket = new DdPacket();
665 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
666 ddPacket.setOspfVer(2);
667 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
668 ddPacket.setRouterId(Ip4Address.valueOf("2.2.2.2"));
669 ddPacket.setIsOpaqueCapable(true);
670 ddPacket.setIsMore(1);
671 ddPacket.setIsInitialize(1);
672 ddPacket.setIsMaster(1);
673 ddPacket.setSequenceNo(123);
674 message = ddPacket;
675 ospfNbrHashMap = new HashMap();
676 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"),
677 Ip4Address.valueOf("2.2.2.2"), 2,
678 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530679 ospfInterface),
680 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530681 ospfNbr.setLastDdPacket(createDdPacket());
682 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
683 ospfNbr.setState(OspfNeighborState.LOADING);
684 ospfNbr.setRouterPriority(0);
685 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
686 ospfNbr.setDdSeqNum(123);
687 ospfInterface.addNeighbouringRouter(ospfNbr);
688 ospfInterfaceChannelHandler.processDdMessage(message, channelHandlerContext);
689 ddPacket.setIsMore(1);
690 ddPacket.setIsInitialize(0);
691 ddPacket.setIsMaster(0);
692 ddPacket.setSequenceNo(123);
693 ospfInterface.addNeighbouringRouter(ospfNbr);
694 ospfNbr.setState(OspfNeighborState.LOADING);
695 ospfInterface.addNeighbouringRouter(ospfNbr);
696
697 ospfInterfaceChannelHandler.processDdMessage(message, channelHandlerContext);
698
699 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
700
701 }
702
703 /**
704 * Tests processLsRequestMessage() method.
705 */
706 @Test
707 public void testProcessLSRequestMessage() throws Exception {
708 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
709 ospfInterface.setInterfaceType(2);
710 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
711 ospfInterface.setHelloIntervalTime(10);
712 ospfInterface.setRouterDeadIntervalTime(10);
713 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
714 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
715 OspfMessage message;
716 lsRequest = new LsRequest();
717 lsRequest.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
718 lsRequest.setOspfVer(2);
719 lsRequest.setAreaId(Ip4Address.valueOf("12.12.12.12"));
720 lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.100"));
721 List<LsRequestPacket> lsRequests = new ArrayList();
722 LsRequestPacket lsRequestPacket = new LsRequestPacket();
723 lsRequestPacket.setLsType(3);
724 lsRequestPacket.setLinkStateId("2.2.2.2");
725 lsRequestPacket.setOwnRouterId("2.2.2.2");
726 lsRequests.add(lsRequestPacket);
727 lsRequests.add(lsRequestPacket);
728 lsRequest.addLinkStateRequests(new LsRequestPacket());
729 lsRequest.addLinkStateRequests(new LsRequestPacket());
730 message = lsRequest;
731 ospfNbrHashMap = new HashMap();
732 ospfNbr.setState(OspfNeighborState.EXCHANGE);
733 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
734 Ip4Address.valueOf("10.226.165.100"), 2,
735 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530736 createOspfInterface1()),
737 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530738 ospfNbr.setLastDdPacket(createDdPacket());
739 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
740 ospfNbr.setState(OspfNeighborState.FULL);
741 ospfNbr.setRouterPriority(0);
742 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
743 ospfNbr.setDdSeqNum(123);
744 ospfInterface.addNeighbouringRouter(ospfNbr);
745 ospfInterface.setListOfNeighbors(ospfNbrHashMap);
746 ospfInterfaceChannelHandler.processLsRequestMessage(message, channelHandlerContext);
747 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
748
749 }
750
751 /**
752 * Tests processLsUpdateMessage() method.
753 */
754 @Test
755 public void testProcessLSUpdateMessage() throws Exception {
756 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
757 ospfInterface.setInterfaceType(2);
758 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
759 ospfInterface.setHelloIntervalTime(10);
760 ospfInterface.setRouterDeadIntervalTime(10);
761 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
762 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
763 OspfMessage message;
764 lsUpdate = new LsUpdate();
765 lsUpdate.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
766 lsUpdate.setOspfVer(2);
767 lsUpdate.setAreaId(Ip4Address.valueOf("12.12.12.12"));
768 lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.100"));
769 RouterLsa routerLsa = new RouterLsa();
770 lsUpdate.addLsa(routerLsa);
771 lsUpdate.setNumberOfLsa(1);
772 message = lsUpdate;
773 ospfNbrHashMap = new HashMap();
774 ospfNbr.setState(OspfNeighborState.FULL);
775 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
776 Ip4Address.valueOf("10.226.165.100"), 2,
777 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530778 createOspfInterface1()),
779 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530780 ospfNbr.setLastDdPacket(createDdPacket());
781 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
782 ospfNbr.setState(OspfNeighborState.FULL);
783 ospfNbr.setRouterPriority(0);
784 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
785 ospfNbr.setDdSeqNum(123);
786 ospfInterface.addNeighbouringRouter(ospfNbr);
787 ospfInterfaceChannelHandler.processLsUpdateMessage(message, channelHandlerContext);
788 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
789
790 }
791
792 @Test(expected = Exception.class)
793 public void testProcessLSAckMessage() throws Exception {
794 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
795 ospfInterface.setInterfaceType(2);
796 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
797 ospfInterface.setHelloIntervalTime(10);
798 ospfInterface.setRouterDeadIntervalTime(10);
799 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
800 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
801 OspfMessage message;
802 lsAck = new LsAcknowledge();
803 lsAck.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
804 lsAck.setOspfVer(2);
805 lsAck.setAreaId(Ip4Address.valueOf("12.12.12.12"));
806 LsaHeader lsaHeader = new LsaHeader();
807 lsAck.addLinkStateHeader(lsaHeader);
808 message = lsAck;
809 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
810 Ip4Address.valueOf("10.226.165.100"), 2,
811 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530812 createOspfInterface()),
813 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530814 ospfNbr.setLastDdPacket(createDdPacket());
815 ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2"));
816 ospfNbr.setState(OspfNeighborState.FULL);
817 ospfNbr.setRouterPriority(0);
818 ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13"));
819 ospfNbr.setDdSeqNum(123);
820 ospfInterfaceChannelHandler.processLsAckMessage(message, channelHandlerContext);
821 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
822
823 }
824
825 /**
826 * Tests compareDdPackets() method.
827 */
828 @Test
829 public void testCompareDDPackets() throws Exception {
830 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
831 ospfInterface.setInterfaceType(2);
832 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
833 ospfInterface.setHelloIntervalTime(10);
834 ospfInterface.setRouterDeadIntervalTime(10);
835 ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12"));
836 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
837 OspfMessage message;
838 ddPacket = new DdPacket();
839 ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
840 ddPacket.setOspfVer(2);
841 ddPacket.setIsInitialize(1);
842 ddPacket.setIsMaster(1);
843 ddPacket.setIsMore(1);
844 ddPacket.setOptions(2);
845 ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
846 assertThat(ospfInterfaceChannelHandler.compareDdPackets(ddPacket, ddPacket), is(true));
847 }
848
849 @Test(expected = Exception.class)
850 public void testCloseChannel() throws Exception {
851 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
852
853 ospfInterfaceChannelHandler.closeChannel(channelHandlerContext);
854
855 assertThat(ospfInterface.dr(), is(notNullValue()));
856 }
857
858 /**
859 * Tests electRouter() method.
860 */
861 @Test
862 public void testElectRouter() throws Exception {
863 ospfInterface.setDr(Ip4Address.valueOf("3.3.3.3"));
864 ospfInterface.setBdr(Ip4Address.valueOf("3.3.3.3"));
865 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
866 ChannelConfig channelConfig = EasyMock.createMock(ChannelConfig.class);
867 EasyMock.expect(channelConfig.getBufferFactory()).andReturn(new HeapChannelBufferFactory());
868 Channel channel = EasyMock.createMock(Channel.class);
869 ospfInterfaceChannelHandler.electRouter(channel);
870 assertThat(ospfInterface.dr(), is(notNullValue()));
871 }
872
873 /**
874 * Tests electBdr() method.
875 */
876 @Test
877 public void testElectBdr() throws Exception {
878 ospfEligibleRouter = new OspfEligibleRouter();
879 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
880 ospfEligibleRouter.setIsDr(true);
881 ospfEligibleRouter.setRouterPriority(10);
882 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
883 ospfEligibleRouter.setIsBdr(false);
884 OspfEligibleRouter ospfEligibleRouter1 = new OspfEligibleRouter();
885 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
886 ospfEligibleRouter.setIsDr(true);
887 ospfEligibleRouter.setRouterPriority(10);
888 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
889 ospfEligibleRouter.setIsBdr(false);
890 OspfEligibleRouter ospfEligibleRouter2 = new OspfEligibleRouter();
891 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
892 ospfEligibleRouter.setIsDr(true);
893 ospfEligibleRouter.setRouterPriority(10);
894 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
895 ospfEligibleRouter.setIsBdr(false);
896 List<OspfEligibleRouter> ospfEligibleRouters = new ArrayList<>();
897
898 ospfEligibleRouters.add(ospfEligibleRouter);
899 ospfEligibleRouters.add(ospfEligibleRouter1);
900 ospfEligibleRouters.add(ospfEligibleRouter2);
901 OspfEligibleRouter eligibleRouter = ospfInterfaceChannelHandler.electBdr(ospfEligibleRouters);
902 assertThat(ospfEligibleRouters.size(), is(3));
903 assertThat(eligibleRouter, is(notNullValue()));
904 }
905
906 /**
907 * Tests electDr() method.
908 */
909 @Test
910 public void testElectDR() throws Exception {
911 ospfEligibleRouter = new OspfEligibleRouter();
912 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
913 ospfEligibleRouter.setIsDr(true);
914 ospfEligibleRouter.setRouterPriority(10);
915 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
916 ospfEligibleRouter.setIsBdr(false);
917 OspfEligibleRouter ospfEligibleRouter1 = new OspfEligibleRouter();
918 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
919 ospfEligibleRouter.setIsDr(true);
920 ospfEligibleRouter.setRouterPriority(10);
921 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
922 ospfEligibleRouter.setIsBdr(false);
923 OspfEligibleRouter ospfEligibleRouter2 = new OspfEligibleRouter();
924 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
925 ospfEligibleRouter.setIsDr(true);
926 ospfEligibleRouter.setRouterPriority(10);
927 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
928 ospfEligibleRouter.setIsBdr(false);
929 List<OspfEligibleRouter> ospfEligibleRouters = new ArrayList<>();
930 ospfEligibleRouters.add(ospfEligibleRouter);
931 ospfEligibleRouters.add(ospfEligibleRouter1);
932 ospfEligibleRouters.add(ospfEligibleRouter2);
933 OspfEligibleRouter eligibleRouter = ospfInterfaceChannelHandler.electDr(ospfEligibleRouters,
934 ospfEligibleRouter);
935 assertThat(ospfEligibleRouters.size(), is(3));
936 assertThat(eligibleRouter, is(notNullValue()));
937 }
938
939 /**
940 * Tests selectRouterBasedOnPriority() method.
941 */
942 @Test
943 public void testSelectRouterBasedOnPriority() throws Exception {
944 ospfEligibleRouter = new OspfEligibleRouter();
945 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
946 ospfEligibleRouter.setIsDr(true);
947 ospfEligibleRouter.setRouterPriority(10);
948 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
949 ospfEligibleRouter.setIsBdr(false);
950 OspfEligibleRouter ospfEligibleRouter1 = new OspfEligibleRouter();
951 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
952 ospfEligibleRouter.setIsDr(true);
953 ospfEligibleRouter.setRouterPriority(11);
954 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
955 ospfEligibleRouter.setIsBdr(false);
956 OspfEligibleRouter ospfEligibleRouter2 = new OspfEligibleRouter();
957 ospfEligibleRouter.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
958 ospfEligibleRouter.setIsDr(true);
959 ospfEligibleRouter.setRouterPriority(12);
960 ospfEligibleRouter.setRouterId(Ip4Address.valueOf("1.1.1.1"));
961 ospfEligibleRouter.setIsBdr(false);
962 List<OspfEligibleRouter> ospfEligibleRouters = new ArrayList<>();
963 ospfEligibleRouters.add(ospfEligibleRouter);
964 ospfEligibleRouters.add(ospfEligibleRouter1);
965 ospfEligibleRouters.add(ospfEligibleRouter2);
966 OspfEligibleRouter eligibleRouter = ospfInterfaceChannelHandler.selectRouterBasedOnPriority(
967 ospfEligibleRouters);
968 assertThat(eligibleRouter, is(notNullValue()));
969 }
970
971 /**
972 * Tests addDeviceInformation() method.
973 */
974 @Test(expected = Exception.class)
975 public void testAddDeviceInformation() throws Exception {
976 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
977 Ip4Address.valueOf("10.226.165.100"), 2,
978 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530979 createOspfInterface()),
980 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530981
982 ospfInterfaceChannelHandler.addDeviceInformation(new OspfRouterImpl());
983 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
984 }
985
986 /**
987 * Tests removeDeviceInformation() method.
988 */
989 @Test(expected = Exception.class)
990 public void testRemoveDeviceInformation() throws Exception {
991 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
992 Ip4Address.valueOf("10.226.165.100"), 2,
993 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +0530994 createOspfInterface()),
995 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +0530996
997 ospfInterfaceChannelHandler.removeDeviceInformation(new OspfRouterImpl());
998 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
999 }
1000
1001 /**
1002 * Tests addLinkInformation() method.
1003 */
1004 @Test(expected = Exception.class)
1005 public void testaddLinkInformation() throws Exception {
1006 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
1007 Ip4Address.valueOf("10.226.165.100"), 2,
1008 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +05301009 createOspfInterface()),
1010 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +05301011
1012 List topTlv = new ArrayList();
1013 topTlv.add(new RouterTlv(new TlvHeader()));
1014 ospfInterfaceChannelHandler.addLinkInformation(new OspfRouterImpl(), new OspfLinkTedImpl());
1015 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
1016 }
1017
1018 /**
1019 * Tests removeLinkInformation() method.
1020 */
1021 @Test(expected = Exception.class)
1022 public void testRemoveLinkInformation() throws Exception {
1023 ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"),
1024 Ip4Address.valueOf("10.226.165.100"), 2,
1025 new OspfInterfaceChannelHandler(new Controller(), ospfArea,
sunish vkaa48da82016-03-02 23:17:06 +05301026 createOspfInterface()),
1027 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +05301028
1029 ospfInterfaceChannelHandler.removeLinkInformation(ospfNbr);
1030 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
1031 }
1032
1033 /**
1034 * Utility for test method.
1035 */
1036 private DdPacket createDdPacket() throws OspfParseException {
1037 byte[] ddPacket = {2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82,
1038 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119, -87, 126};
1039 DdPacket ddPacket1 = new DdPacket();
1040 ChannelBuffer buf = ChannelBuffers.buffer(ddPacket.length);
1041 buf.writeBytes(ddPacket);
1042 ddPacket1.readFrom(buf);
1043 return ddPacket1;
1044 }
1045
1046 /**
1047 * Utility for test method.
1048 */
1049 private OspfInterfaceImpl createOspfInterface() throws UnknownHostException {
1050 ospfInterface = new OspfInterfaceImpl();
1051 OspfAreaImpl ospfArea = new OspfAreaImpl();
1052 OspfInterfaceChannelHandler ospfInterfaceChannelHandler = EasyMock.createMock(
1053 OspfInterfaceChannelHandler.class);
1054 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.226.165.164"),
sunish vkaa48da82016-03-02 23:17:06 +05301055 Ip4Address.valueOf("1.1.1.1"), 2, ospfInterfaceChannelHandler,
1056 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +05301057 ospfNbr.setState(OspfNeighborState.EXSTART);
1058 ospfNbr.setNeighborId(Ip4Address.valueOf("10.226.165.100"));
1059 this.ospfInterface = new OspfInterfaceImpl();
1060 this.ospfInterface.setIpAddress(Ip4Address.valueOf("10.226.165.164"));
1061 this.ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
1062 this.ospfInterface.setAreaId(2);
1063 this.ospfInterface.setAuthKey("authKey");
1064 this.ospfInterface.setAuthType("AuthReq");
1065 this.ospfInterface.setBdr(Ip4Address.valueOf("111.111.111.111"));
1066 this.ospfInterface.setDr(Ip4Address.valueOf("111.111.111.111"));
1067 this.ospfInterface.setHelloIntervalTime(20);
1068 this.ospfInterface.setInterfaceCost(10);
1069 this.ospfInterface.setInterfaceType(2);
1070 this.ospfInterface.setReTransmitInterval(2000);
1071 this.ospfInterface.setMtu(6500);
1072 this.ospfInterface.setPollInterval(1000);
1073 this.ospfInterface.setRouterDeadIntervalTime(1000);
1074 this.ospfInterface.setRouterPriority(1);
1075 this.ospfInterface.setTransmitDelay(500);
1076 this.ospfInterface.setInterfaceType(1);
1077 this.ospfInterface.addNeighbouringRouter(ospfNbr);
1078 return this.ospfInterface;
1079 }
1080
1081 /**
1082 * Utility for test method.
1083 */
1084 private OspfInterfaceImpl createOspfInterface1() throws UnknownHostException {
1085 ospfInterface = new OspfInterfaceImpl();
1086 OspfAreaImpl ospfArea = new OspfAreaImpl();
1087 OspfInterfaceChannelHandler ospfInterfaceChannelHandler = EasyMock.createMock(
1088 OspfInterfaceChannelHandler.class);
1089 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.226.165.164"),
sunish vkaa48da82016-03-02 23:17:06 +05301090 Ip4Address.valueOf("1.1.1.1"), 2, ospfInterfaceChannelHandler,
1091 topologyForDeviceAndLink);
sunish vk1857c422016-02-17 18:05:52 +05301092 ospfNbr.setState(OspfNeighborState.FULL);
1093 ospfNbr.setNeighborId(Ip4Address.valueOf("10.226.165.100"));
1094 ospfInterface = new OspfInterfaceImpl();
1095 ospfInterface.setIpAddress(Ip4Address.valueOf("10.226.165.164"));
1096 ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255"));
1097 ospfInterface.setAreaId(2);
1098 ospfInterface.setAuthKey("authKey");
1099 ospfInterface.setAuthType("AuthReq");
1100 ospfInterface.setBdr(Ip4Address.valueOf("111.111.111.111"));
1101 ospfInterface.setDr(Ip4Address.valueOf("111.111.111.111"));
1102 ospfInterface.setHelloIntervalTime(20);
1103 ospfInterface.setInterfaceCost(10);
1104 ospfInterface.setInterfaceType(2);
1105 ospfInterface.setReTransmitInterval(2000);
1106 ospfInterface.setMtu(6500);
1107 ospfInterface.setPollInterval(1000);
1108 ospfInterface.setRouterDeadIntervalTime(1000);
1109 ospfInterface.setRouterPriority(1);
1110 ospfInterface.setTransmitDelay(500);
1111 ospfInterface.setInterfaceType(1);
1112 ospfInterface.addNeighbouringRouter(ospfNbr);
1113 return ospfInterface;
1114 }
1115
1116 /**
1117 * Utility for test method.
1118 */
1119 private OspfAreaImpl createOspfArea() throws UnknownHostException {
1120 OspfAreaAddressRangeImpl ospfAreaAddressRange;
1121 ospfAreaAddressRange = createOspfAreaAddressRange();
1122 addressRanges.add(ospfAreaAddressRange);
1123 OspfAreaImpl ospfArea = new OspfAreaImpl();
1124 ospfArea.setStubCost(10);
1125 ospfArea.setAreaId(Ip4Address.valueOf("10.226.165.164"));
1126 ospfArea.setExternalRoutingCapability(true);
1127 ospfArea.setTransitCapability(true);
1128 ospfArea.setAddressRanges(addressRanges);
1129 OspfInterfaceImpl ospfInterface = createOspfInterface();
1130 ospfInterfaces.add(ospfInterface);
1131 ospfArea.setInterfacesLst(ospfInterfaces);
1132 RouterLsa routerLsa = new RouterLsa();
1133 routerLsa.setLsType(1);
1134 routerLsa.setLinkStateId("2.2.2.2");
1135 routerLsa.setAdvertisingRouter(Ip4Address.valueOf("2.2.2.2"));
1136 try {
1137 ospfArea.addLsa(routerLsa, false, ospfInterface);
1138 } catch (Exception e) {
1139 System.out.println("ospfAreaImpl createOspfArea");
1140 }
1141 ospfArea.setRouterId(Ip4Address.valueOf("111.111.111.111"));
1142
1143 return ospfArea;
1144 }
1145
1146 /**
1147 * Utility for test method.
1148 */
1149 private OspfAreaAddressRangeImpl createOspfAreaAddressRange() {
1150 OspfAreaAddressRangeImpl ospfAreaAddressRange = new OspfAreaAddressRangeImpl();
1151 ospfAreaAddressRange.setIpAddress(Ip4Address.valueOf("10.226.165.164"));
1152 ospfAreaAddressRange.setAdvertise(true);
1153 ospfAreaAddressRange.setMask("mask");
1154 return ospfAreaAddressRange;
1155 }
sunish vk1857c422016-02-17 18:05:52 +05301156}