blob: 2ad4bd2476f221e008843214efa7bb9d02ad80fc [file] [log] [blame]
sunish vk1857c422016-02-17 18:05:52 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
sunish vk1857c422016-02-17 18:05:52 +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.impl;
17
18import org.easymock.EasyMock;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.buffer.ChannelBuffers;
sunish vk1857c422016-02-17 18:05:52 +053021import org.jboss.netty.channel.Channel;
sunish vk1857c422016-02-17 18:05:52 +053022import org.jboss.netty.channel.ChannelFuture;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.jboss.netty.channel.ChannelStateEvent;
25import org.jboss.netty.channel.ExceptionEvent;
26import org.jboss.netty.channel.MessageEvent;
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
30import org.onlab.packet.Ip4Address;
sunishvkf7c56552016-07-18 16:02:39 +053031import org.onosproject.ospf.controller.OspfArea;
sunish vk1857c422016-02-17 18:05:52 +053032import org.onosproject.ospf.controller.OspfAreaAddressRange;
33import org.onosproject.ospf.controller.OspfInterface;
sunishvkf7c56552016-07-18 16:02:39 +053034import org.onosproject.ospf.controller.OspfLsaType;
sunish vk1857c422016-02-17 18:05:52 +053035import org.onosproject.ospf.controller.OspfNeighborState;
sunishvkf7c56552016-07-18 16:02:39 +053036import org.onosproject.ospf.controller.OspfProcess;
sunish vk1857c422016-02-17 18:05:52 +053037import 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;
sunishvkf7c56552016-07-18 16:02:39 +053041import org.onosproject.ospf.controller.area.OspfProcessImpl;
sunish vk1857c422016-02-17 18:05:52 +053042import org.onosproject.ospf.exceptions.OspfParseException;
43import org.onosproject.ospf.protocol.lsa.LsaHeader;
sunish vk1857c422016-02-17 18:05:52 +053044import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
sunish vk1857c422016-02-17 18:05:52 +053045import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
46import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
sunish vk1857c422016-02-17 18:05:52 +053047import org.onosproject.ospf.protocol.util.ChecksumCalculator;
sunish vk1857c422016-02-17 18:05:52 +053048
49import java.net.SocketAddress;
50import java.net.UnknownHostException;
51import java.util.ArrayList;
sunish vk1857c422016-02-17 18:05:52 +053052import java.util.List;
53
54import static org.hamcrest.CoreMatchers.is;
55import static org.hamcrest.CoreMatchers.notNullValue;
56import static org.hamcrest.MatcherAssert.assertThat;
57
58
59/**
60 * Unit test class for OspfInterfaceChannelHandler.
61 */
62public class OspfInterfaceChannelHandlerTest {
sunishvkf7c56552016-07-18 16:02:39 +053063 private final String string1 = "2.2.2.2";
64 private List<OspfAreaAddressRange> addressRanges = new ArrayList();
65 private List<OspfInterface> ospfInterfaces = new ArrayList<>();
sunish vk1857c422016-02-17 18:05:52 +053066 private Controller controller;
67 private OspfAreaImpl ospfArea;
68 private OspfInterfaceImpl ospfInterface;
69 private OspfInterfaceChannelHandler ospfInterfaceChannelHandler;
sunish vk1857c422016-02-17 18:05:52 +053070 private OspfNbrImpl ospfNbr;
sunish vk1857c422016-02-17 18:05:52 +053071 private ChannelHandlerContext channelHandlerContext;
72 private ChannelStateEvent channelStateEvent;
sunishvkf7c56552016-07-18 16:02:39 +053073 private TopologyForDeviceAndLink topologyForDeviceAndLink;
74 private List<OspfProcess> ospfProcesses = new ArrayList<>();
75 private OspfProcess ospfProcess;
76 private Ip4Address ip4Address1 = Ip4Address.valueOf("10.10.10.10");
77 private Ip4Address ip4Address2 = Ip4Address.valueOf("2.2.2.2");
78 private Ip4Address ip4Address3 = Ip4Address.valueOf("13.13.13.13");
79 private Ip4Address ip4Address4 = Ip4Address.valueOf("111.111.111.111");
80 private Ip4Address ip4Address5 = Ip4Address.valueOf("10.226.165.164");
81 private Ip4Address ip4Address6 = Ip4Address.valueOf("1.1.1.1");
82 private Ip4Address ip4Address7 = Ip4Address.valueOf("10.226.165.100");
83 private Ip4Address subnetAddress = Ip4Address.valueOf("255.255.255.255");
sunish vk1857c422016-02-17 18:05:52 +053084 private byte[] byteArray;
85 private byte[] checkArray;
sunishvkf7c56552016-07-18 16:02:39 +053086 private HelloPacket helloPacket;
87 private ChecksumCalculator checksumCalculator;
sunish vk1857c422016-02-17 18:05:52 +053088 private ChannelBuffer buf;
sunishvkf7c56552016-07-18 16:02:39 +053089 private List<OspfArea> ospfAreas = new ArrayList<>();
sunish vk1857c422016-02-17 18:05:52 +053090
91 @Before
92 public void setUp() throws Exception {
sunishvkf7c56552016-07-18 16:02:39 +053093 ospfProcess = new OspfProcessImpl();
sunish vk1857c422016-02-17 18:05:52 +053094 ospfArea = createOspfArea();
sunishvkf7c56552016-07-18 16:02:39 +053095 ospfAreas.add(ospfArea);
96 ospfProcess.setAreas(ospfAreas);
97 ospfProcesses.add(ospfProcess);
sunish vk1857c422016-02-17 18:05:52 +053098 controller = new Controller();
sunishvkf7c56552016-07-18 16:02:39 +053099 topologyForDeviceAndLink = new TopologyForDeviceAndLinkImpl();
100 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, ip4Address1,
101 ip4Address2, 2, topologyForDeviceAndLink);
102 ospfNbr.setNeighborId(ip4Address1);
103 ospfNbr.setRouterPriority(0);
104 ospfNbr.setNeighborDr(ip4Address3);
105 ospfInterface.addNeighbouringRouter(ospfNbr);
106 ospfInterfaceChannelHandler = new OspfInterfaceChannelHandler(controller, ospfProcesses);
sunish vk1857c422016-02-17 18:05:52 +0530107 }
108
109 @After
110 public void tearDown() throws Exception {
111 ospfInterfaceChannelHandler = null;
sunish vk1857c422016-02-17 18:05:52 +0530112 ospfInterfaceChannelHandler = null;
113 ospfInterface = null;
sunish vk1857c422016-02-17 18:05:52 +0530114 channelHandlerContext = null;
115 channelStateEvent = null;
sunish vk1857c422016-02-17 18:05:52 +0530116 }
117
118 /**
119 * Tests channelConnected() method.
120 */
121 @Test(expected = Exception.class)
122 public void testChannelConnected() throws Exception {
123 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
124 channelStateEvent = EasyMock.createMock(ChannelStateEvent.class);
125 ospfInterfaceChannelHandler.channelConnected(channelHandlerContext, channelStateEvent);
sunish vk1857c422016-02-17 18:05:52 +0530126 }
127
128 /**
129 * Tests exceptionCaught() method.
130 */
131 @Test(expected = Exception.class)
132 public void testExceptionCaught() throws Exception {
133 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
134 ExceptionEvent exception = EasyMock.createMock(ExceptionEvent.class);
135 ospfInterfaceChannelHandler.exceptionCaught(channelHandlerContext, exception);
136 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
137 }
138
139 /**
140 * Tests channelDisconnected() method.
141 */
sunishvkf7c56552016-07-18 16:02:39 +0530142 @Test
sunish vk1857c422016-02-17 18:05:52 +0530143 public void testChannelDisconnected() throws Exception {
144 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
145 channelStateEvent = EasyMock.createMock(ChannelStateEvent.class);
sunish vk1857c422016-02-17 18:05:52 +0530146 ospfInterfaceChannelHandler.channelDisconnected(channelHandlerContext, channelStateEvent);
sunishvkf7c56552016-07-18 16:02:39 +0530147 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
148 }
sunish vk1857c422016-02-17 18:05:52 +0530149
sunishvkf7c56552016-07-18 16:02:39 +0530150 /**
151 * Tests initializeInterfaceMap() method.
152 */
153 @Test
154 public void testInitializeInterfaceMap() throws Exception {
155 ospfInterfaceChannelHandler.initializeInterfaceMap();
156 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
157 }
158
159 /**
160 * Tests updateInterfaceMap() method.
161 */
162 @Test
163 public void testUpdateInterfaceMap() throws Exception {
164 ospfInterfaceChannelHandler.updateInterfaceMap(ospfProcesses);
165 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
166 }
167
168 /**
169 * Utility for test method.
170 */
171 private OspfAreaImpl createOspfArea() throws Exception {
172 OspfAreaAddressRangeImpl ospfAreaAddressRange;
173 ospfAreaAddressRange = createOspfAreaAddressRange();
174 addressRanges.add(ospfAreaAddressRange);
175 OspfAreaImpl ospfArea = new OspfAreaImpl();
176 ospfArea.setAreaId(ip4Address5);
177 ospfArea.setExternalRoutingCapability(true);
178 OspfInterfaceImpl ospfInterface = createOspfInterface();
179 ospfInterfaces.add(ospfInterface);
180 ospfArea.setOspfInterfaceList(ospfInterfaces);
181 RouterLsa routerLsa = new RouterLsa();
182 routerLsa.setLsType(1);
183 routerLsa.setLinkStateId(string1);
184 routerLsa.setAdvertisingRouter(ip4Address2);
185 ospfArea.addLsa(routerLsa, false, ospfInterface);
186 ospfArea.setRouterId(ip4Address4);
187
188 return ospfArea;
189 }
190
191 /**
192 * Utility for test method.
193 */
194 private OspfAreaAddressRangeImpl createOspfAreaAddressRange() {
195 OspfAreaAddressRangeImpl ospfAreaAddressRange = new OspfAreaAddressRangeImpl();
196 ospfAreaAddressRange.setIpAddress(ip4Address5);
197 ospfAreaAddressRange.setAdvertise(true);
198 ospfAreaAddressRange.setMask("mask");
199 return ospfAreaAddressRange;
sunish vk1857c422016-02-17 18:05:52 +0530200 }
201
202 /**
203 * Tests messageReceived() method.
204 */
205 @Test
206 public void testMessageReceived() throws Exception {
207 ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
208 ospfInterface.setInterfaceType(2);
209 ospfArea.setAreaId(Ip4Address.valueOf("13.13.13.13"));
210 channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
211 MessageEvent messageEvent = new MessageEvent() {
212 @Override
213 public Object getMessage() {
214 helloPacket = new HelloPacket();
215 helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
216 helloPacket.setRouterId(Ip4Address.valueOf("10.10.10.10"));
217 helloPacket.setOspfVer(2);
218 helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
219 helloPacket.setOptions(2);
220 helloPacket.setAreaId(Ip4Address.valueOf("5.5.5.5"));
221 helloPacket.setNetworkMask(Ip4Address.valueOf("3.3.3.3"));
222 helloPacket.setOspftype(1);
223 helloPacket.setAuthType(0);
224 helloPacket.setAuthentication(0);
225 checksumCalculator = new ChecksumCalculator();
226 byteArray = helloPacket.asBytes();
227 helloPacket.setOspfPacLength(byteArray.length);
228 checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
229 buf = ChannelBuffers.copiedBuffer(checkArray);
230 helloPacket.setChecksum(buf.readUnsignedShort());
231 List<HelloPacket> messPackets = new ArrayList<>();
232 messPackets.add(helloPacket);
233 return messPackets;
234 }
235
236 @Override
237 public SocketAddress getRemoteAddress() {
238 return null;
239 }
240
241 @Override
242 public Channel getChannel() {
243 return null;
244 }
245
246 @Override
247 public ChannelFuture getFuture() {
248 return null;
249 }
250 };
251 ospfInterfaceChannelHandler.messageReceived(channelHandlerContext, messageEvent);
252 assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
253 }
254
255 /**
sunishvkf7c56552016-07-18 16:02:39 +0530256 * Utility for test method.
sunish vk1857c422016-02-17 18:05:52 +0530257 */
sunishvkf7c56552016-07-18 16:02:39 +0530258 private OspfInterfaceImpl createOspfInterface1() throws UnknownHostException {
259 ospfInterface = new OspfInterfaceImpl();
260 OspfAreaImpl ospfArea = new OspfAreaImpl();
261 OspfInterfaceChannelHandler ospfInterfaceChannelHandler = EasyMock.createMock(
262 OspfInterfaceChannelHandler.class);
263 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, ip4Address5,
264 ip4Address6, 2, topologyForDeviceAndLink);
265 ospfNbr.setState(OspfNeighborState.FULL);
266 ospfNbr.setNeighborId(ip4Address7);
267 ospfInterface = new OspfInterfaceImpl();
268 ospfInterface.setIpAddress(ip4Address5);
269 ospfInterface.setIpNetworkMask(subnetAddress);
270 ospfInterface.setBdr(ip4Address4);
271 ospfInterface.setDr(ip4Address4);
272 ospfInterface.setHelloIntervalTime(20);
sunish vk1857c422016-02-17 18:05:52 +0530273 ospfInterface.setInterfaceType(2);
sunishvkf7c56552016-07-18 16:02:39 +0530274 ospfInterface.setReTransmitInterval(2000);
275 ospfInterface.setMtu(6500);
276 ospfInterface.setRouterDeadIntervalTime(1000);
sunish vk1857c422016-02-17 18:05:52 +0530277 ospfInterface.setRouterPriority(1);
sunishvkf7c56552016-07-18 16:02:39 +0530278 ospfInterface.setInterfaceType(1);
sunish vk1857c422016-02-17 18:05:52 +0530279 ospfInterface.addNeighbouringRouter(ospfNbr);
sunishvkf7c56552016-07-18 16:02:39 +0530280 return ospfInterface;
sunish vk1857c422016-02-17 18:05:52 +0530281 }
282
283 /**
sunishvkf7c56552016-07-18 16:02:39 +0530284 * Utility for test method.
sunish vk1857c422016-02-17 18:05:52 +0530285 */
sunishvkf7c56552016-07-18 16:02:39 +0530286 private OspfInterfaceImpl createOspfInterface() throws Exception {
287 ospfInterface = new OspfInterfaceImpl();
sunish vk1857c422016-02-17 18:05:52 +0530288 LsaHeader lsaHeader = new LsaHeader();
sunishvkf7c56552016-07-18 16:02:39 +0530289 lsaHeader.setLsType(OspfLsaType.ROUTER.value());
290 RouterLsa routerLsa = new RouterLsa();
291 OspfAreaImpl ospfArea = new OspfAreaImpl();
292 ospfArea.addLsa(routerLsa, true, ospfInterface);
293 ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, ip4Address5,
294 ip4Address6, 2, topologyForDeviceAndLink);
295 ospfNbr.setState(OspfNeighborState.EXSTART);
296 ospfNbr.setNeighborId(ip4Address7);
297 this.ospfInterface = new OspfInterfaceImpl();
298 this.ospfInterface.setIpAddress(ip4Address5);
299 this.ospfInterface.setIpNetworkMask(subnetAddress);
300 this.ospfInterface.setBdr(ip4Address4);
301 this.ospfInterface.setDr(ip4Address4);
302 this.ospfInterface.setHelloIntervalTime(20);
303 this.ospfInterface.setInterfaceType(2);
304 this.ospfInterface.setReTransmitInterval(2000);
305 this.ospfInterface.setMtu(6500);
306 this.ospfInterface.setRouterDeadIntervalTime(1000);
307 this.ospfInterface.setRouterPriority(1);
308 this.ospfInterface.setInterfaceType(1);
309 this.ospfInterface.setInterfaceIndex(1);
310 this.ospfInterface.addNeighbouringRouter(ospfNbr);
311 this.ospfInterface.setOspfArea(ospfArea);
312 return this.ospfInterface;
sunish vk1857c422016-02-17 18:05:52 +0530313 }
314
315 /**
316 * Utility for test method.
317 */
318 private DdPacket createDdPacket() throws OspfParseException {
319 byte[] ddPacket = {2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82,
320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119, -87, 126};
321 DdPacket ddPacket1 = new DdPacket();
322 ChannelBuffer buf = ChannelBuffers.buffer(ddPacket.length);
323 buf.writeBytes(ddPacket);
324 ddPacket1.readFrom(buf);
325 return ddPacket1;
326 }
sunish vk1857c422016-02-17 18:05:52 +0530327}