sunish vk
Committed by Thomas Vachuska

ONOS-2740,ONOS-2741,from ONOS-3032 - to ONOS 3071 , OSPF Protocol Implementation Unit Tests

Change-Id: I18972a0712fbd63798f92da7b4c48381b4a38519
1 +/*
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 +*/
16 +package org.onosproject.ospf.controller.impl;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.controller.DeviceInformation;
25 +import org.onosproject.ospf.controller.OspfLinkTed;
26 +import org.onosproject.ospf.controller.OspfLsa;
27 +import org.onosproject.ospf.controller.area.OspfAreaImpl;
28 +import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
29 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
30 +import org.onosproject.ospf.protocol.lsa.TlvHeader;
31 +import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
32 +import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
33 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
34 +import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
35 +
36 +import java.util.List;
37 +import java.util.Map;
38 +
39 +import static org.hamcrest.CoreMatchers.nullValue;
40 +import static org.hamcrest.MatcherAssert.assertThat;
41 +import static org.hamcrest.Matchers.is;
42 +import static org.hamcrest.Matchers.notNullValue;
43 +
44 +/**
45 + * Unit test class for TopologyForDeviceAndLinkImpl.
46 + */
47 +public class TopologyForDeviceAndLinkImplTest {
48 + private final byte[] packet = {0, 9, 0, 4, 0, 0, 0, 1,
49 + 0, 9, 0, 4, 0, 0, 0, 1,
50 + 0, 1, 0, 4, 0, 0, 0, 1,
51 + 0, 2, 0, 4, 0, 0, 0, 1,
52 + 0, 3, 0, 4, 0, 0, 0, 1,
53 + 0, 4, 0, 4, 0, 0, 0, 1,
54 + 0, 6, 0, 4, 0, 0, 0, 1,
55 + 0, 7, 0, 4, 0, 0, 0, 1,
56 + 0, 8, 0, 4, 0, 0, 0, 1,
57 + };
58 + private TopologyForDeviceAndLinkImpl topologyForDeviceAndLink;
59 + private Map result;
60 + private LinkTlv linkTlv;
61 + private TlvHeader header;
62 + private ChannelBuffer channelBuffer;
63 +
64 + @Before
65 + public void setUp() throws Exception {
66 + topologyForDeviceAndLink = new TopologyForDeviceAndLinkImpl();
67 + }
68 +
69 + @After
70 + public void tearDown() throws Exception {
71 + topologyForDeviceAndLink = null;
72 + }
73 +
74 + /**
75 + * Tests deviceInformationMap() method.
76 + */
77 + @Test
78 + public void testDeviceInformationMap() throws Exception {
79 + result = topologyForDeviceAndLink.deviceInformationMap();
80 + assertThat(result.size(), is(0));
81 + }
82 +
83 + /**
84 + * Tests setDeviceInformationMap() method.
85 + */
86 + @Test
87 + public void testSetDeviceInformationMap() throws Exception {
88 + topologyForDeviceAndLink.setDeviceInformationMap("1.1.1.1", new DeviceInformationImpl());
89 + result = topologyForDeviceAndLink.deviceInformationMap();
90 + assertThat(result.size(), is(1));
91 + }
92 +
93 + /**
94 + * Tests deviceInformation() method.
95 + */
96 + @Test
97 + public void testDeviceInformation() throws Exception {
98 + topologyForDeviceAndLink.setDeviceInformationMap("1.1.1.1", new DeviceInformationImpl());
99 + DeviceInformation deviceInformation = topologyForDeviceAndLink.deviceInformation("1.1.1.1");
100 + assertThat(deviceInformation, is(notNullValue()));
101 + }
102 +
103 + /**
104 + * Tests removeDeviceInformationMap() method.
105 + */
106 + @Test
107 + public void testRemoveDeviceInformationMap() throws Exception {
108 + topologyForDeviceAndLink.setDeviceInformationMap("1.1.1.1", new DeviceInformationImpl());
109 + topologyForDeviceAndLink.deviceInformation("1.1.1.1");
110 + result = topologyForDeviceAndLink.deviceInformationMap();
111 + topologyForDeviceAndLink.removeDeviceInformationMap("1.1.1.1");
112 + assertThat(result.size(), is(0));
113 + }
114 +
115 + /**
116 + * Tests linkInformationMap() method.
117 + */
118 + @Test
119 + public void testLinkInformationMap() throws Exception {
120 + result = topologyForDeviceAndLink.linkInformationMap();
121 + assertThat(result.size(), is(0));
122 + }
123 +
124 + /**
125 + * Tests setLinkInformationMap() method.
126 + */
127 + @Test
128 + public void testSetLinkInformationMap() throws Exception {
129 + topologyForDeviceAndLink.setLinkInformationMap("1.1.1.1", new LinkInformationImpl());
130 + result = topologyForDeviceAndLink.linkInformationMap();
131 + assertThat(result.size(), is(1));
132 + }
133 +
134 + /**
135 + * Tests removeLinkInformationMap() method.
136 + */
137 + @Test
138 + public void testRemoveLinkInformationMap() throws Exception {
139 + topologyForDeviceAndLink.setLinkInformationMap("1.1.1.1", new LinkInformationImpl());
140 + topologyForDeviceAndLink.removeLinkInformationMap("1.1.1.1");
141 + result = topologyForDeviceAndLink.linkInformationMap();
142 + assertThat(result.size(), is(0));
143 + }
144 +
145 + /**
146 + * Tests getOspfLinkTedHashMap() method.
147 + */
148 + @Test
149 + public void testGetOspfLinkTedHashMap() throws Exception {
150 + OspfLinkTed ospfLinkTed = topologyForDeviceAndLink.getOspfLinkTedHashMap("1.1.1.1");
151 + assertThat(ospfLinkTed, is(nullValue()));
152 + }
153 +
154 + /**
155 + * Tests addLocalDevice() method.
156 + */
157 + @Test
158 + public void testAddLocalDevice() throws Exception {
159 + OspfAreaImpl ospfArea = new OspfAreaImpl();
160 + ospfArea.setRouterId(Ip4Address.valueOf("5.5.5.5"));
161 + topologyForDeviceAndLink.addLocalDevice(createOspfLsa(), new OspfInterfaceImpl(), ospfArea);
162 + topologyForDeviceAndLink.addLocalDevice(createOspfLsa1(), new OspfInterfaceImpl(), ospfArea);
163 + assertThat(topologyForDeviceAndLink, is(notNullValue()));
164 + }
165 +
166 + /**
167 + * Tests addLocalLink() method.
168 + */
169 + @Test
170 + public void testAddLocalLink() throws Exception {
171 + Ip4Address linkData = Ip4Address.valueOf("1.1.1.1");
172 + Ip4Address linkSrc = Ip4Address.valueOf("2.2.2.2");
173 + Ip4Address linkDest = Ip4Address.valueOf("3.3.3.3");
174 + boolean opaqueEnabled = true;
175 + boolean linkSrcIdNotRouterId = true;
176 + topologyForDeviceAndLink.addLocalLink("10.10.10.10", linkData, linkSrc,
177 + linkDest, opaqueEnabled, linkSrcIdNotRouterId);
178 + assertThat(topologyForDeviceAndLink, is(notNullValue()));
179 + }
180 +
181 + /**
182 + * Tests removeLinks() method.
183 + */
184 + @Test
185 + public void testRemoveLinks() throws Exception {
186 + Ip4Address linkData = Ip4Address.valueOf("1.1.1.1");
187 + Ip4Address linkSrc = Ip4Address.valueOf("2.2.2.2");
188 + Ip4Address linkDest = Ip4Address.valueOf("3.3.3.3");
189 + boolean opaqueEnabled = true;
190 + boolean linkSrcIdNotRouterId = true;
191 + topologyForDeviceAndLink.addLocalLink("10.10.10.10", linkData, linkSrc,
192 + linkDest, opaqueEnabled, linkSrcIdNotRouterId);
193 + topologyForDeviceAndLink.removeLinks(Ip4Address.valueOf("10.10.10.10"));
194 + assertThat(topologyForDeviceAndLink, is(notNullValue()));
195 + }
196 +
197 + /**
198 + * Tests updateLinkInformation() method.
199 + */
200 + @Test
201 + public void testUpdateLinkInformation() throws Exception {
202 + OspfAreaImpl ospfArea = new OspfAreaImpl();
203 + ospfArea.setRouterId(Ip4Address.valueOf("5.5.5.5"));
204 + topologyForDeviceAndLink.updateLinkInformation(createOspfLsa(), ospfArea);
205 + assertThat(topologyForDeviceAndLink, is(notNullValue()));
206 + }
207 +
208 + /**
209 + * Tests getDeleteRouterInformation() method.
210 + */
211 + @Test
212 + public void testGetDeleteRouterInformation() throws Exception {
213 + OspfAreaImpl ospfArea = new OspfAreaImpl();
214 + ospfArea.setRouterId(Ip4Address.valueOf("5.5.5.5"));
215 + topologyForDeviceAndLink.updateLinkInformation(createOspfLsa(), ospfArea);
216 + List list = topologyForDeviceAndLink.getDeleteRouterInformation(createOspfLsa(), ospfArea);
217 + assertThat(list, is(notNullValue()));
218 + }
219 +
220 + /**
221 + * Utility for test methods.
222 + */
223 + private OspfLsa createOspfLsa() {
224 + RouterLsa routerLsa = new RouterLsa();
225 + routerLsa.setLsType(1);
226 + routerLsa.setAdvertisingRouter(Ip4Address.valueOf("6.6.6.6"));
227 + OspfLsaLink ospfLsaLink = new OspfLsaLink();
228 + ospfLsaLink.setLinkData("192.168.7.77");
229 + ospfLsaLink.setLinkId("9.9.9.9");
230 + ospfLsaLink.setLinkType(1);
231 + OspfLsaLink ospfLsaLink0 = new OspfLsaLink();
232 + ospfLsaLink0.setLinkData("7.7.7.7");
233 + ospfLsaLink0.setLinkId("7.7.7.7");
234 + ospfLsaLink0.setLinkType(2);
235 + OspfLsaLink ospfLsaLink120 = new OspfLsaLink();
236 + ospfLsaLink120.setLinkData("192.168.7.77");
237 + ospfLsaLink120.setLinkId("1.1.1.1");
238 + ospfLsaLink120.setLinkType(1);
239 + OspfLsaLink lsaLink = new OspfLsaLink();
240 + lsaLink.setLinkData("192.168.7.77");
241 + lsaLink.setLinkId("14.14.14.14");
242 + lsaLink.setLinkType(2);
243 + routerLsa.addRouterLink(lsaLink);
244 + routerLsa.addRouterLink(ospfLsaLink);
245 + routerLsa.addRouterLink(ospfLsaLink0);
246 + routerLsa.addRouterLink(ospfLsaLink120);
247 + return routerLsa;
248 + }
249 +
250 + /**
251 + * Utility for test methods.
252 + */
253 + private OspfLsa createOspfLsa1() throws Exception {
254 + OpaqueLsa10 opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
255 + opaqueLsa10.setLsType(10);
256 + header = new TlvHeader();
257 + header.setTlvLength(8);
258 + header.setTlvType(9);
259 + linkTlv = new LinkTlv(header);
260 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
261 + linkTlv.readFrom(channelBuffer);
262 + opaqueLsa10.addValue(linkTlv);
263 + return opaqueLsa10;
264 + }
265 +}