blob: 72697ee7f6403b32123bcd3edcf9dd2e9a18dfd6 [file] [log] [blame]
xueliangf83fbc62016-09-16 10:55:28 +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.apache.commons.io.IOUtils;
20import org.onlab.packet.IpAddress;
21import org.onosproject.net.Annotations;
22import org.onosproject.net.DefaultAnnotations;
23import org.onosproject.net.behaviour.ControllerInfo;
24import org.junit.Before;
25import org.junit.Test;
26
27import java.io.IOException;
28import java.util.ArrayList;
29import java.util.List;
xueliangf83fbc62016-09-16 10:55:28 +090030
31import java.nio.charset.StandardCharsets;
32import java.io.InputStream;
33
34import static org.junit.Assert.assertTrue;
35import static org.junit.Assert.fail;
36import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
37
38
39/**
40 * Unit tests for methods of FujitsuVoltControllerConfig.
41 */
42public class FujitsuVoltControllerConfigTest {
43
xueliangf83fbc62016-09-16 10:55:28 +090044 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
45
46 private static final String TEST_VOLT_OFCONFIG = "volt-ofconfig";
47 private static final String TEST_OFCONFIG_ID = "ofconfig-id";
48 private static final String TEST_END_LICENSE_HEADER = "-->";
xueliangf83fbc62016-09-16 10:55:28 +090049
xueliangc6e47e22016-10-20 16:37:24 +090050 private static final String[] GET_CONTROLLERS = {
51 "tcp:172.10.10.45:6633",
52 "tcp:100.0.0.22:5555",
xueliangf83fbc62016-09-16 10:55:28 +090053 };
xueliangc6e47e22016-10-20 16:37:24 +090054 private static final String[] SET_CONTROLLERS = {
55 "tcp:172.10.10.55:2222",
56 "tcp:172.20.33.11:6633",
xueliangf83fbc62016-09-16 10:55:28 +090057 };
58 private static final String GET_CONTROLLERS_RSP_FILE = "/getcontrollers.xml";
59 private static final String SET_CONTROLLERS_REQ_FILE = "/setcontrollers.xml";
60
xueliangc6e47e22016-10-20 16:37:24 +090061 private Integer currentKey;
62 private FujitsuNetconfControllerMock controller;
63 private FujitsuDriverHandlerAdapter driverHandler;
64 private FujitsuVoltControllerConfig voltConfig;
xueliangf83fbc62016-09-16 10:55:28 +090065
66 @Before
67 public void setUp() throws Exception {
68 controller = new FujitsuNetconfControllerMock();
69 driverHandler = controller.setUp(listener);
70 voltConfig = new FujitsuVoltControllerConfig();
71 voltConfig.setHandler(driverHandler);
72 }
73
74 /**
75 * Run to verify handling of valid get operation.
76 */
77 @Test
78 public void testGetControllers() throws Exception {
79 List<ControllerInfo> controllers;
80 List<ControllerInfo> expectedControllers = new ArrayList<>();
81
xueliangc6e47e22016-10-20 16:37:24 +090082 for (int i = ZERO; i < GET_CONTROLLERS.length; i++) {
83 String target = GET_CONTROLLERS[i];
xueliangf83fbc62016-09-16 10:55:28 +090084 String[] data = target.split(TEST_COLON);
xueliangc6e47e22016-10-20 16:37:24 +090085 currentKey = i + ONE;
xueliangf83fbc62016-09-16 10:55:28 +090086
87 Annotations annotations = DefaultAnnotations
88 .builder()
xueliangc6e47e22016-10-20 16:37:24 +090089 .set(TEST_OFCONFIG_ID, currentKey.toString())
xueliangf83fbc62016-09-16 10:55:28 +090090 .build();
91 ControllerInfo controller = new ControllerInfo(
92 IpAddress.valueOf(data[SECOND_PART]),
93 Integer.parseInt(data[THIRD_PART]),
94 data[FIRST_PART], annotations);
95 expectedControllers.add(controller);
96 }
97
98 controllers = voltConfig.getControllers();
99 assertTrue("Incorrect response", controllers.equals(expectedControllers));
100 }
101
102 /**
103 * Run to verify handling of valid set operation.
104 */
105 @Test
106 public void testSetControllers() throws Exception {
107 List<ControllerInfo> controllers = new ArrayList<>();
108
xueliangc6e47e22016-10-20 16:37:24 +0900109 for (int i = ZERO; i < SET_CONTROLLERS.length; i++) {
110 String target = SET_CONTROLLERS[i];
xueliangf83fbc62016-09-16 10:55:28 +0900111 String[] data = target.split(TEST_COLON);
xueliangc6e47e22016-10-20 16:37:24 +0900112 currentKey = i + ONE;
xueliangf83fbc62016-09-16 10:55:28 +0900113
114 Annotations annotations = DefaultAnnotations
115 .builder()
xueliangc6e47e22016-10-20 16:37:24 +0900116 .set(TEST_OFCONFIG_ID, currentKey.toString())
xueliangf83fbc62016-09-16 10:55:28 +0900117 .build();
118 ControllerInfo controller = new ControllerInfo(
119 IpAddress.valueOf(data[SECOND_PART]),
120 Integer.parseInt(data[THIRD_PART]),
121 data[FIRST_PART], annotations);
122 controllers.add(controller);
123 }
124
125 voltConfig.setControllers(controllers);
126 }
127
128 /**
129 * Verifies XML request string by comparing with generated string.
130 *
131 * @param request XML string for set operation
132 * @return true if XML string matches with generated
133 */
134 private boolean verifyGetRequest(String request) {
135 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900136 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
137 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
138 rpc.append(emptyTag(TEST_VOLT_OFCONFIG))
139 .append(TEST_VOLT_NE_CLOSE);
xueliangf83fbc62016-09-16 10:55:28 +0900140
141 String testRequest = rpc.toString();
xueliangf83fbc62016-09-16 10:55:28 +0900142 boolean result = request.equals(testRequest);
143 assertTrue("Does not match with generated string", result);
144 return result;
145 }
146
147 /**
148 * Verifies XML request string by comparing with generated string.
149 *
150 * @param request XML string for set operation
151 * @return true if XML string matches with generated
152 */
153 private boolean verifyEditConfigRequest(String request) {
154 String testRequest;
155
156 try {
157 InputStream fileStream = getClass().getResourceAsStream(
158 SET_CONTROLLERS_REQ_FILE);
159 testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
160 testRequest = testRequest.substring(testRequest.indexOf(
161 TEST_END_LICENSE_HEADER) + TEST_END_LICENSE_HEADER.length());
162 } catch (IOException e) {
163 fail("IOException while reading: " + SET_CONTROLLERS_REQ_FILE);
164 return false;
165 }
166
167 testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
168 request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
169 boolean result = request.equals(testRequest);
170 assertTrue("Does not match with string in " + SET_CONTROLLERS_REQ_FILE, result);
171
172 return result;
173 }
174
175 /**
176 * Internal listener for device service events.
177 */
178 private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
179 @Override
180 public boolean verifyEditConfig(String request) {
181 boolean result;
182
183 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
184 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
185 request.contains(TEST_VOLT_NAMESPACE));
186
187 result = verifyEditConfigRequest(request);
188 assertTrue("XML verification failure", result);
189 return result;
190 }
191
192 @Override
193 public boolean verifyEditConfig(String target, String mode, String request) {
194 return false;
195 }
196
197 @Override
198 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
199 boolean result;
200
201 assertTrue("Incorrect withDefaultsMode",
202 withDefaultsMode.equals(TEST_REPORT_ALL));
203 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
204 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
205 filterSchema.contains(TEST_VOLT_NAMESPACE));
206
207 result = verifyGetRequest(filterSchema);
208 assertTrue("XML verification failure", result);
209 return result;
210 }
211
212 @Override
213 public String buildGetReply() {
214 try {
215 InputStream fileStream = getClass().getResourceAsStream(
216 GET_CONTROLLERS_RSP_FILE);
217 String reply = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
218 return (reply);
219 } catch (IOException e) {
220 return null;
221 }
222 }
223
224 @Override
225 public boolean verifyWrappedRpc(String request) {
226 return false;
227 }
228
229 @Override
230 public void verifyStartSubscription(String filterSchema) {
231 }
232 }
233
234}