blob: 172380b80920243773ddc5a5c97cbc19c23551b8 [file] [log] [blame]
xueliang5b3d34442016-09-23 10:37:33 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.fujitsu;
18
19import org.junit.Before;
20import org.junit.Test;
21
xueliang5b3d34442016-09-23 10:37:33 +090022import static org.junit.Assert.assertTrue;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertNotNull;
25import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
26
27
28/**
29 * Unit tests for methods of FujitsuVoltOnuConfig.
30 */
31public class FujitsuVoltOnuConfigTest {
32
xueliang5b3d34442016-09-23 10:37:33 +090033 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
34
35 private static final String TEST_VOLT_ONUS = "volt-onus";
36 private static final String TEST_ONUS_PERLINK = "onus-perlink";
37 private static final String TEST_ONUS_LIST = "onus-list";
38 private static final String TEST_ONU_INFO = "onu-info";
39 private static final String TEST_ONU_SET_CONFIG = "onu-set-config";
40 private static final String TEST_CONFIG_INFO = "config-info";
41 private static final String TEST_VOLT_STATISTICS = "volt-statistics";
42 private static final String TEST_ONU_STATISTICS = "onu-statistics";
43 private static final String TEST_ONU_ETH_STATS = "onu-eth-stats";
44 private static final String TEST_ETH_STATS = "eth-stats";
45 private static final String TEST_ONU_GEM_STATS = "onu-gem-stats";
46 private static final String TEST_GEM_STATS = "gem-stats";
47
48 private static final String TEST_ONU_SET_CONFIG_WITH_NAMESPACE =
49 TEST_ANGLE_LEFT + TEST_ONU_SET_CONFIG + TEST_SPACE +
50 TEST_VOLT_NE_NAMESPACE;
51
xueliangc6e47e22016-10-20 16:37:24 +090052 private static final String[] INVALID_GET_TCS = {
53 "a-b",
54 "--1-2",
55 "s-1",
56 "16-1-1",
57 "1 A-1",
58 "1*A-1",
xueliang5b3d34442016-09-23 10:37:33 +090059 };
xueliangc6e47e22016-10-20 16:37:24 +090060 private static final String[] VALID_GET_TCS = {
61 "1",
62 "1-2",
63 null,
xueliang5b3d34442016-09-23 10:37:33 +090064 };
xueliangc6e47e22016-10-20 16:37:24 +090065 private static final String[] INVALID_SET_TCS = {
66 "-11-3:admin-state:enable",
67 "1-2:admin-state:false",
68 "1-2:pm-enable:123",
69 "^1-2:pm-enable:false",
70 "1-2:fec-enable:xyz",
71 "1-2:security-enable:123abc",
72 "2-3:password:-1&",
73 "2:admin-state:disable",
xueliang5b3d34442016-09-23 10:37:33 +090074 };
xueliangc6e47e22016-10-20 16:37:24 +090075 private static final String[] VALID_SET_TCS = {
76 "1-11:admin-state:disable",
77 "8-1:pm-enable:true",
78 "1-1:fec-enable:true",
79 "1-21:security-enable:false",
80 "3-2:password:abc123",
xueliang5b3d34442016-09-23 10:37:33 +090081 };
xueliangc6e47e22016-10-20 16:37:24 +090082 private static final String[] INVALID_GET_STATS_TCS = {
83 "1-a",
84 "1:1",
85 "a-1",
86 "1-1-1",
87 "2 A-1",
88 "2/A-1",
xueliang5b3d34442016-09-23 10:37:33 +090089 };
xueliangc6e47e22016-10-20 16:37:24 +090090 private static final String[] VALID_GET_STATS_TCS = {
91 "1",
92 "3-12",
93 null,
xueliang5b3d34442016-09-23 10:37:33 +090094 };
95 private Integer currentKey;
xueliangc6e47e22016-10-20 16:37:24 +090096 private FujitsuNetconfControllerMock controller;
97 private FujitsuDriverHandlerAdapter driverHandler;
98 private FujitsuVoltOnuConfig voltConfig;
xueliang5b3d34442016-09-23 10:37:33 +090099
100 @Before
101 public void setUp() throws Exception {
102 controller = new FujitsuNetconfControllerMock();
103 driverHandler = controller.setUp(listener);
104 voltConfig = new FujitsuVoltOnuConfig();
105 voltConfig.setHandler(driverHandler);
106 }
107
108 /**
109 * Run to verify handling of invalid input for get operation.
110 */
111 @Test
112 public void testInvalidGetOnusInput() throws Exception {
113 String reply;
114 String target;
115
xueliangc6e47e22016-10-20 16:37:24 +0900116 for (int i = ZERO; i < INVALID_GET_TCS.length; i++) {
117 target = INVALID_GET_TCS[i];
xueliang5b3d34442016-09-23 10:37:33 +0900118 reply = voltConfig.getOnus(target);
xueliangc6e47e22016-10-20 16:37:24 +0900119 assertNull("Incorrect response for INVALID_GET_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900120 }
121 }
122
123 /**
124 * Run to verify handling of valid input for get operation.
125 */
126 @Test
127 public void testValidGetOnus() throws Exception {
128 String reply;
129 String target;
130
xueliangc6e47e22016-10-20 16:37:24 +0900131 for (int i = ZERO; i < VALID_GET_TCS.length; i++) {
132 target = VALID_GET_TCS[i];
133 currentKey = i;
xueliang5b3d34442016-09-23 10:37:33 +0900134 reply = voltConfig.getOnus(target);
xueliangc6e47e22016-10-20 16:37:24 +0900135 assertNotNull("Incorrect response for VALID_GET_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900136 }
137 }
138
139 /**
140 * Run to verify handling of invalid input for set operation.
141 */
142 @Test
143 public void testInvalidSetOnuInput() throws Exception {
144 String target;
145 String reply;
146
xueliangc6e47e22016-10-20 16:37:24 +0900147 for (int i = ZERO; i < INVALID_SET_TCS.length; i++) {
148 target = INVALID_SET_TCS[i];
xueliang5b3d34442016-09-23 10:37:33 +0900149 reply = voltConfig.setOnu(target);
xueliangc6e47e22016-10-20 16:37:24 +0900150 assertNull("Incorrect response for INVALID_SET_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900151 }
152 }
153
154 /**
155 * Run to verify handling of valid input for set operation.
156 */
157 @Test
158 public void testValidSetOnu() throws Exception {
159 String target;
160 String reply;
161
xueliangc6e47e22016-10-20 16:37:24 +0900162 for (int i = ZERO; i < VALID_SET_TCS.length; i++) {
163 target = VALID_SET_TCS[i];
164 currentKey = i;
xueliang5b3d34442016-09-23 10:37:33 +0900165 reply = voltConfig.setOnu(target);
xueliangc6e47e22016-10-20 16:37:24 +0900166 assertNotNull("Incorrect response for VALID_SET_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900167 }
168 }
169
170 /**
171 * Run to verify handling of invalid input for get statistics operation.
172 */
173 @Test
174 public void testInvalidGetOnuStatsInput() throws Exception {
175 String reply;
176 String target;
177
xueliangc6e47e22016-10-20 16:37:24 +0900178 for (int i = ZERO; i < INVALID_GET_STATS_TCS.length; i++) {
179 target = INVALID_GET_STATS_TCS[i];
xueliang5b3d34442016-09-23 10:37:33 +0900180 reply = voltConfig.getOnuStatistics(target);
xueliangc6e47e22016-10-20 16:37:24 +0900181 assertNull("Incorrect response for INVALID_GET_STATS_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900182 }
183 }
184
185 /**
186 * Run to verify handling of valid input for get statistics operation.
187 */
188 @Test
189 public void testValidGetOnuStats() throws Exception {
190 String reply;
191 String target;
192
xueliangc6e47e22016-10-20 16:37:24 +0900193 for (int i = ZERO; i < VALID_GET_STATS_TCS.length; i++) {
194 target = VALID_GET_STATS_TCS[i];
195 currentKey = i;
xueliang5b3d34442016-09-23 10:37:33 +0900196 reply = voltConfig.getOnuStatistics(target);
xueliangc6e47e22016-10-20 16:37:24 +0900197 assertNotNull("Incorrect response for VALID_GET_STATS_TCS", reply);
xueliang5b3d34442016-09-23 10:37:33 +0900198 }
199 }
200
201 /**
202 * Verifies XML request string by comparing with generated string.
203 *
204 * @param request XML string for get operation
205 * @return true if XML string matches with generated
206 */
207 private boolean verifyGetRequest(String request) {
208 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900209 String target = VALID_GET_TCS[currentKey];
xueliang5b3d34442016-09-23 10:37:33 +0900210 String[] onuId = null;
211
212 if (target != null) {
213 onuId = target.split(TEST_HYPHEN);
214 }
215
xueliangc6e47e22016-10-20 16:37:24 +0900216 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
217 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
xueliang5b3d34442016-09-23 10:37:33 +0900218 if (onuId != null) {
xueliangc6e47e22016-10-20 16:37:24 +0900219 rpc.append(startTag(TEST_VOLT_ONUS))
220 .append(startTag(TEST_ONUS_PERLINK))
221 .append(startTag(TEST_PONLINK_ID, false))
222 .append(onuId[FIRST_PART])
223 .append(endTag(TEST_PONLINK_ID));
xueliang5b3d34442016-09-23 10:37:33 +0900224 if (onuId.length > ONE) {
xueliangc6e47e22016-10-20 16:37:24 +0900225 rpc.append(startTag(TEST_ONUS_LIST))
226 .append(startTag(TEST_ONU_INFO))
227 .append(startTag(TEST_ONU_ID, false))
228 .append(onuId[SECOND_PART])
229 .append(endTag(TEST_ONU_ID))
230 .append(endTag(TEST_ONU_INFO))
231 .append(endTag(TEST_ONUS_LIST));
xueliang5b3d34442016-09-23 10:37:33 +0900232 }
xueliangc6e47e22016-10-20 16:37:24 +0900233 rpc.append(endTag(TEST_ONUS_PERLINK))
234 .append(endTag(TEST_VOLT_ONUS));
xueliang5b3d34442016-09-23 10:37:33 +0900235 } else {
236 rpc.append(emptyTag(TEST_VOLT_ONUS));
237 }
238 rpc.append(TEST_VOLT_NE_CLOSE);
239
240 String testRequest = rpc.toString();
xueliang5b3d34442016-09-23 10:37:33 +0900241 boolean result = request.equals(testRequest);
242 assertTrue("Does not match with generated string", result);
243 return result;
244 }
245
246 /**
247 * Verifies XML request string by comparing with generated string.
248 *
249 * @param request XML string for set operation
250 * @return true or false
251 */
252 private boolean verifyWrappedRpcRequest(String request) {
253 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900254 String target = VALID_SET_TCS[currentKey];
xueliang5b3d34442016-09-23 10:37:33 +0900255 String[] data = target.split(TEST_COLON);
256 String[] onuId = data[FIRST_PART].split(TEST_HYPHEN);
257
xueliangc6e47e22016-10-20 16:37:24 +0900258 rpc.append(TEST_ANGLE_LEFT + TEST_ONU_SET_CONFIG + TEST_SPACE);
259 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
260 rpc.append(startTag(TEST_PONLINK_ID, false))
261 .append(onuId[FIRST_PART])
262 .append(endTag(TEST_PONLINK_ID))
263 .append(startTag(TEST_ONU_ID, false))
264 .append(onuId[SECOND_PART])
265 .append(endTag(TEST_ONU_ID))
266 .append(startTag(TEST_CONFIG_INFO))
267 .append(startTag(data[SECOND_PART], false))
268 .append(data[THIRD_PART])
269 .append(endTag(data[SECOND_PART]))
270 .append(endTag(TEST_CONFIG_INFO))
271 .append(endTag(TEST_ONU_SET_CONFIG));
xueliang5b3d34442016-09-23 10:37:33 +0900272
273 String testRequest = rpc.toString();
xueliang5b3d34442016-09-23 10:37:33 +0900274 boolean result = request.equals(testRequest);
275 assertTrue("Does not match with generated string", result);
276 return result;
277 }
278
279 /**
280 * Verifies XML request string by comparing with generated string (statistics).
281 *
282 * @param request XML string for get operation
283 * @return true if XML string matches with generated
284 */
285 private boolean verifyGetRequestForStats(String request) {
286 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900287 String target = VALID_GET_STATS_TCS[currentKey];
xueliang5b3d34442016-09-23 10:37:33 +0900288 String[] onuId = null;
289
290 if (target != null) {
291 onuId = target.split(TEST_HYPHEN);
292 }
293
xueliangc6e47e22016-10-20 16:37:24 +0900294 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
295 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
xueliang5b3d34442016-09-23 10:37:33 +0900296 rpc.append(startTag(TEST_VOLT_STATISTICS));
297 if (onuId != null) {
xueliangc6e47e22016-10-20 16:37:24 +0900298 rpc.append(startTag(TEST_ONU_STATISTICS))
299 .append(startTag(TEST_ONU_GEM_STATS))
300 .append(startTag(TEST_GEM_STATS))
301 .append(startTag(TEST_PONLINK_ID, false))
302 .append(onuId[FIRST_PART])
303 .append(endTag(TEST_PONLINK_ID));
xueliang5b3d34442016-09-23 10:37:33 +0900304 if (onuId.length > ONE) {
xueliangc6e47e22016-10-20 16:37:24 +0900305 rpc.append(startTag(TEST_ONU_ID, false))
306 .append(onuId[SECOND_PART])
307 .append(endTag(TEST_ONU_ID));
xueliang5b3d34442016-09-23 10:37:33 +0900308 }
xueliangc6e47e22016-10-20 16:37:24 +0900309 rpc.append(endTag(TEST_GEM_STATS))
310 .append(endTag(TEST_ONU_GEM_STATS));
xueliang5b3d34442016-09-23 10:37:33 +0900311
xueliangc6e47e22016-10-20 16:37:24 +0900312 rpc.append(startTag(TEST_ONU_ETH_STATS))
313 .append(startTag(TEST_ETH_STATS))
314 .append(startTag(TEST_PONLINK_ID, false))
315 .append(onuId[FIRST_PART])
316 .append(endTag(TEST_PONLINK_ID));
xueliang5b3d34442016-09-23 10:37:33 +0900317 if (onuId.length > ONE) {
xueliangc6e47e22016-10-20 16:37:24 +0900318 rpc.append(startTag(TEST_ONU_ID, false))
319 .append(onuId[SECOND_PART])
320 .append(endTag(TEST_ONU_ID));
xueliang5b3d34442016-09-23 10:37:33 +0900321 }
xueliangc6e47e22016-10-20 16:37:24 +0900322 rpc.append(endTag(TEST_ETH_STATS))
323 .append(endTag(TEST_ONU_ETH_STATS))
324 .append(endTag(TEST_ONU_STATISTICS));
xueliang5b3d34442016-09-23 10:37:33 +0900325 } else {
326 rpc.append(emptyTag(TEST_ONU_STATISTICS));
327 }
xueliangc6e47e22016-10-20 16:37:24 +0900328 rpc.append(endTag(TEST_VOLT_STATISTICS))
329 .append(TEST_VOLT_NE_CLOSE);
xueliang5b3d34442016-09-23 10:37:33 +0900330
331 String testRequest = rpc.toString();
xueliang5b3d34442016-09-23 10:37:33 +0900332 boolean result = request.equals(testRequest);
333 assertTrue("Does not match with generated string", result);
334 return result;
335 }
336
337 /**
338 * Internal listener for device service events.
339 */
340 private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
341 @Override
342 public boolean verifyEditConfig(String request) {
343 return false;
344 }
345
346 @Override
347 public boolean verifyEditConfig(String target, String mode, String request) {
348 return false;
349 }
350
351 @Override
352 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
353 boolean result;
354 boolean forStats;
355
356 assertTrue("Incorrect withDefaultsMode", withDefaultsMode.equals(TEST_REPORT_ALL));
357 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
358 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
359 filterSchema.contains(TEST_VOLT_NAMESPACE));
360
361 forStats = filterSchema.contains(TEST_VOLT_STATISTICS);
362 if (forStats) {
363 result = verifyGetRequestForStats(filterSchema);
364 } else {
365 result = verifyGetRequest(filterSchema);
366 }
367 assertTrue("XML verification failure", result);
368 return result;
369 }
370
371 @Override
372 public String buildGetReply() {
373 return null;
374 }
375
376 @Override
377 public boolean verifyWrappedRpc(String request) {
378 boolean result;
379
380 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
381 assertTrue("Does not contain:" + TEST_ONU_SET_CONFIG_WITH_NAMESPACE,
382 request.contains(TEST_ONU_SET_CONFIG_WITH_NAMESPACE));
383
384 result = verifyWrappedRpcRequest(request);
385 assertTrue("XML verification failure", result);
386 return result;
387 }
388
389 @Override
390 public void verifyStartSubscription(String filterSchema) {
391 }
392 }
393
394}