xueliang
Committed by Gerrit Code Review

[ONOS-5281] JUNIT Test cases for device-controllers and device-setcontrollers as FUJITSU NETCONF

Change-Id: I2a6b30d357061ca265175a0a6057728bf83fa5a7
...@@ -44,6 +44,7 @@ import java.util.ArrayList; ...@@ -44,6 +44,7 @@ import java.util.ArrayList;
44 import java.util.List; 44 import java.util.List;
45 45
46 import static com.google.common.base.Preconditions.checkNotNull; 46 import static com.google.common.base.Preconditions.checkNotNull;
47 +import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
47 import static org.slf4j.LoggerFactory.getLogger; 48 import static org.slf4j.LoggerFactory.getLogger;
48 49
49 /** 50 /**
...@@ -57,33 +58,20 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -57,33 +58,20 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
57 private static final String RESOURCE_XML = "voltcontrollers.xml"; 58 private static final String RESOURCE_XML = "voltcontrollers.xml";
58 59
59 private static final String DOT = "."; 60 private static final String DOT = ".";
60 - private static final String L_ANGLE_BR = "<";
61 - private static final String R_ANGLE_BR = "/>";
62 - private static final String VOLT_NE_NAMESPACE =
63 - "xmlns=\"http://fujitsu.com/ns/volt/1.1\"";
64 private static final String DATA = "data"; 61 private static final String DATA = "data";
65 - private static final String VOLT_NE = "volt-ne";
66 private static final String VOLT_OFCONFIG = "volt-ofconfig"; 62 private static final String VOLT_OFCONFIG = "volt-ofconfig";
67 private static final String OF_CONTROLLERS = "of-controllers"; 63 private static final String OF_CONTROLLERS = "of-controllers";
68 private static final String OF_CONTROLLER = "of-controller"; 64 private static final String OF_CONTROLLER = "of-controller";
69 private static final String CONTROLLER_INFO = "controller-info"; 65 private static final String CONTROLLER_INFO = "controller-info";
70 - private static final String REPORT_ALL = "report-all";
71 private static final String IP_ADDRESS = "ip-address"; 66 private static final String IP_ADDRESS = "ip-address";
72 private static final String PORT = "port"; 67 private static final String PORT = "port";
73 private static final String PROTOCOL = "protocol"; 68 private static final String PROTOCOL = "protocol";
74 private static final String CONFIG = "config"; 69 private static final String CONFIG = "config";
75 private static final String OFCONFIG_ID = "ofconfig-id"; 70 private static final String OFCONFIG_ID = "ofconfig-id";
76 - private static final String EDIT_CONFIG = "edit-config";
77 private static final String TARGET = "target"; 71 private static final String TARGET = "target";
78 - private static final String RUNNING = "running";
79 private static final String MERGE = "merge"; 72 private static final String MERGE = "merge";
80 private static final String DEFAULT_OPERATION = "default-operation"; 73 private static final String DEFAULT_OPERATION = "default-operation";
81 74
82 - private static final String VOLT_NE_OPEN = "<" + VOLT_NE + " ";
83 - private static final String VOLT_NE_CLOSE = "</" + VOLT_NE + ">";
84 - private static final String VOLT_OFCONFIG_EL = "<" + VOLT_OFCONFIG + "/>\n";
85 - private static final String TARGET_OPEN = "<" + TARGET + ">";
86 - private static final String TARGET_CLOSE = "</" + TARGET + ">";
87 private static final String END_LICENSE_HEADER = "-->"; 75 private static final String END_LICENSE_HEADER = "-->";
88 76
89 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT + 77 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
...@@ -95,11 +83,9 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -95,11 +83,9 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
95 private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS; 83 private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS;
96 private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT; 84 private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT;
97 private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL; 85 private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL;
98 -
99 private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT + 86 private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT +
100 CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS; 87 CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS;
101 88
102 -
103 @Override 89 @Override
104 public List<ControllerInfo> getControllers() { 90 public List<ControllerInfo> getControllers() {
105 DriverHandler handler = handler(); 91 DriverHandler handler = handler();
...@@ -112,13 +98,15 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -112,13 +98,15 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
112 try { 98 try {
113 StringBuilder request = new StringBuilder(); 99 StringBuilder request = new StringBuilder();
114 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n"); 100 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n");
115 - request.append(VOLT_OFCONFIG_EL); 101 + request.append(buildEmptyTag(VOLT_OFCONFIG));
116 request.append(VOLT_NE_CLOSE); 102 request.append(VOLT_NE_CLOSE);
117 103
118 String reply; 104 String reply;
119 - reply = controller. 105 + reply = controller
120 - getDevicesMap().get(ncDeviceId).getSession(). 106 + .getDevicesMap()
121 - get(request.toString(), REPORT_ALL); 107 + .get(ncDeviceId)
108 + .getSession()
109 + .get(request.toString(), REPORT_ALL);
122 log.debug("Reply XML {}", reply); 110 log.debug("Reply XML {}", reply);
123 controllers.addAll(parseStreamVoltControllers(XmlConfigParser. 111 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
124 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); 112 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
...@@ -150,7 +138,7 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -150,7 +138,7 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
150 device.getSession().editConfig(config.substring( 138 device.getSession().editConfig(config.substring(
151 config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length())); 139 config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
152 } catch (NetconfException e) { 140 } catch (NetconfException e) {
153 - log.error("Cannot communicate to device {} , exception ", ncdeviceId, e); 141 + log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e);
154 } 142 }
155 } else { 143 } else {
156 log.warn("I'm not master for {} please use master, {} to execute command", 144 log.warn("I'm not master for {} please use master, {} to execute command",
...@@ -201,7 +189,7 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -201,7 +189,7 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
201 * @param controllers list of controllers 189 * @param controllers list of controllers
202 * @return XML string 190 * @return XML string
203 */ 191 */
204 - public static String createVoltControllersConfig(HierarchicalConfiguration cfg, 192 + private String createVoltControllersConfig(HierarchicalConfiguration cfg,
205 String target, String netconfOperation, 193 String target, String netconfOperation,
206 List<ControllerInfo> controllers) { 194 List<ControllerInfo> controllers) {
207 XMLConfiguration editcfg = null; 195 XMLConfiguration editcfg = null;
...@@ -234,8 +222,11 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour ...@@ -234,8 +222,11 @@ public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
234 e.printStackTrace(); 222 e.printStackTrace();
235 } 223 }
236 String s = stringWriter.toString(); 224 String s = stringWriter.toString();
237 - s = s.replace(TARGET_OPEN + target + TARGET_CLOSE, 225 + String fromStr = buildStartTag(TARGET, false) + target +
238 - TARGET_OPEN + L_ANGLE_BR + target + R_ANGLE_BR + TARGET_CLOSE); 226 + buildEndTag(TARGET, false);
227 + String toStr = buildStartTag(TARGET, false) +
228 + buildEmptyTag(target, false) + buildEndTag(TARGET, false);
229 + s = s.replace(fromStr, toStr);
239 return s; 230 return s;
240 } 231 }
241 232
......
1 +/*
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 +
17 +package org.onosproject.drivers.fujitsu;
18 +
19 +import org.apache.commons.io.IOUtils;
20 +import org.onlab.packet.IpAddress;
21 +import org.onosproject.net.Annotations;
22 +import org.onosproject.net.DefaultAnnotations;
23 +import org.onosproject.net.behaviour.ControllerInfo;
24 +import org.junit.Before;
25 +import org.junit.Test;
26 +
27 +import java.io.IOException;
28 +import java.util.ArrayList;
29 +import java.util.List;
30 +import java.util.HashMap;
31 +import java.util.Map;
32 +
33 +import java.nio.charset.StandardCharsets;
34 +import java.io.InputStream;
35 +
36 +import static org.junit.Assert.assertTrue;
37 +import static org.junit.Assert.fail;
38 +import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
39 +
40 +
41 +/**
42 + * Unit tests for methods of FujitsuVoltControllerConfig.
43 + */
44 +public class FujitsuVoltControllerConfigTest {
45 +
46 + private FujitsuNetconfControllerMock controller;
47 + private FujitsuDriverHandlerAdapter driverHandler;
48 + private FujitsuVoltControllerConfig voltConfig;
49 +
50 + private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
51 +
52 + private static final String TEST_VOLT_OFCONFIG = "volt-ofconfig";
53 + private static final String TEST_OFCONFIG_ID = "ofconfig-id";
54 + private static final String TEST_END_LICENSE_HEADER = "-->";
55 + private static final int FIRST_PART = 0;
56 + private static final int SECOND_PART = 1;
57 + private static final int THIRD_PART = 2;
58 +
59 + private static final Map<Integer, String> GET_CONTROLLERS = new HashMap<Integer, String>() {
60 + {
61 + put(1, "tcp:172.10.10.45:6633");
62 + put(2, "tcp:100.0.0.22:5555");
63 + }
64 + };
65 + private static final Map<Integer, String> SET_CONTROLLERS = new HashMap<Integer, String>() {
66 + {
67 + put(1, "tcp:172.10.10.55:2222");
68 + put(3, "tcp:172.20.33.11:6633");
69 + }
70 + };
71 + private static final String GET_CONTROLLERS_RSP_FILE = "/getcontrollers.xml";
72 + private static final String SET_CONTROLLERS_REQ_FILE = "/setcontrollers.xml";
73 +
74 +
75 + @Before
76 + public void setUp() throws Exception {
77 + controller = new FujitsuNetconfControllerMock();
78 + driverHandler = controller.setUp(listener);
79 + voltConfig = new FujitsuVoltControllerConfig();
80 + voltConfig.setHandler(driverHandler);
81 + }
82 +
83 + /**
84 + * Run to verify handling of valid get operation.
85 + */
86 + @Test
87 + public void testGetControllers() throws Exception {
88 + List<ControllerInfo> controllers;
89 + List<ControllerInfo> expectedControllers = new ArrayList<>();
90 +
91 + for (Integer key : GET_CONTROLLERS.keySet()) {
92 + String target = GET_CONTROLLERS.get(key);
93 + String[] data = target.split(TEST_COLON);
94 +
95 + Annotations annotations = DefaultAnnotations
96 + .builder()
97 + .set(TEST_OFCONFIG_ID, key.toString())
98 + .build();
99 + ControllerInfo controller = new ControllerInfo(
100 + IpAddress.valueOf(data[SECOND_PART]),
101 + Integer.parseInt(data[THIRD_PART]),
102 + data[FIRST_PART], annotations);
103 + expectedControllers.add(controller);
104 + }
105 +
106 + controllers = voltConfig.getControllers();
107 + assertTrue("Incorrect response", controllers.equals(expectedControllers));
108 + }
109 +
110 + /**
111 + * Run to verify handling of valid set operation.
112 + */
113 + @Test
114 + public void testSetControllers() throws Exception {
115 + List<ControllerInfo> controllers = new ArrayList<>();
116 +
117 + for (Integer key : SET_CONTROLLERS.keySet()) {
118 + String target = SET_CONTROLLERS.get(key);
119 + String[] data = target.split(TEST_COLON);
120 +
121 + Annotations annotations = DefaultAnnotations
122 + .builder()
123 + .set(TEST_OFCONFIG_ID, key.toString())
124 + .build();
125 + ControllerInfo controller = new ControllerInfo(
126 + IpAddress.valueOf(data[SECOND_PART]),
127 + Integer.parseInt(data[THIRD_PART]),
128 + data[FIRST_PART], annotations);
129 + controllers.add(controller);
130 + }
131 +
132 + voltConfig.setControllers(controllers);
133 + }
134 +
135 + /**
136 + * Verifies XML request string by comparing with generated string.
137 + *
138 + * @param request XML string for set operation
139 + * @return true if XML string matches with generated
140 + */
141 + private boolean verifyGetRequest(String request) {
142 + StringBuilder rpc = new StringBuilder();
143 + rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE);
144 + rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE);
145 + rpc.append(emptyTag(TEST_VOLT_OFCONFIG));
146 + rpc.append(endTag(TEST_VOLT_NE));
147 +
148 + String testRequest = rpc.toString();
149 + testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
150 + request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
151 + boolean result = request.equals(testRequest);
152 + assertTrue("Does not match with generated string", result);
153 + return result;
154 + }
155 +
156 + /**
157 + * Verifies XML request string by comparing with generated string.
158 + *
159 + * @param request XML string for set operation
160 + * @return true if XML string matches with generated
161 + */
162 + private boolean verifyEditConfigRequest(String request) {
163 + String testRequest;
164 +
165 + try {
166 + InputStream fileStream = getClass().getResourceAsStream(
167 + SET_CONTROLLERS_REQ_FILE);
168 + testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
169 + testRequest = testRequest.substring(testRequest.indexOf(
170 + TEST_END_LICENSE_HEADER) + TEST_END_LICENSE_HEADER.length());
171 + } catch (IOException e) {
172 + fail("IOException while reading: " + SET_CONTROLLERS_REQ_FILE);
173 + return false;
174 + }
175 +
176 + testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
177 + request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
178 + boolean result = request.equals(testRequest);
179 + assertTrue("Does not match with string in " + SET_CONTROLLERS_REQ_FILE, result);
180 +
181 + return result;
182 + }
183 +
184 + /**
185 + * Internal listener for device service events.
186 + */
187 + private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
188 + @Override
189 + public boolean verifyEditConfig(String request) {
190 + boolean result;
191 +
192 + request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
193 + assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
194 + request.contains(TEST_VOLT_NAMESPACE));
195 +
196 + result = verifyEditConfigRequest(request);
197 + assertTrue("XML verification failure", result);
198 + return result;
199 + }
200 +
201 + @Override
202 + public boolean verifyEditConfig(String target, String mode, String request) {
203 + return false;
204 + }
205 +
206 + @Override
207 + public boolean verifyGet(String filterSchema, String withDefaultsMode) {
208 + boolean result;
209 +
210 + assertTrue("Incorrect withDefaultsMode",
211 + withDefaultsMode.equals(TEST_REPORT_ALL));
212 + filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
213 + assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
214 + filterSchema.contains(TEST_VOLT_NAMESPACE));
215 +
216 + result = verifyGetRequest(filterSchema);
217 + assertTrue("XML verification failure", result);
218 + return result;
219 + }
220 +
221 + @Override
222 + public String buildGetReply() {
223 + try {
224 + InputStream fileStream = getClass().getResourceAsStream(
225 + GET_CONTROLLERS_RSP_FILE);
226 + String reply = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
227 + return (reply);
228 + } catch (IOException e) {
229 + return null;
230 + }
231 + }
232 +
233 + @Override
234 + public boolean verifyWrappedRpc(String request) {
235 + return false;
236 + }
237 +
238 + @Override
239 + public void verifyStartSubscription(String filterSchema) {
240 + }
241 + }
242 +
243 +}
1 +<!--
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 +
17 +<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
18 + <data>
19 + <volt-ne xmlns="http://fujitsu.com/ns/volt/1.1">
20 + <volt-ofconfig>
21 + <of-controllers>
22 + <of-controller>
23 + <ofconfig-id>1</ofconfig-id>
24 + <controller-info>
25 + <id>1</id>
26 + <ip-address>172.10.10.45</ip-address>
27 + <port>6633</port>
28 + <protocol>tcp</protocol>
29 + </controller-info>
30 + <state>
31 + <connection-state>up</connection-state>
32 + <local-port-in-use>12345</local-port-in-use>
33 + </state>
34 + </of-controller>
35 + <of-controller>
36 + <ofconfig-id>2</ofconfig-id>
37 + <controller-info>
38 + <id>2</id>
39 + <ip-address>100.0.0.22</ip-address>
40 + <port>5555</port>
41 + <protocol>tcp</protocol>
42 + </controller-info>
43 + <state>
44 + <connection-state>up</connection-state>
45 + <local-port-in-use>12345</local-port-in-use>
46 + </state>
47 + </of-controller>
48 + </of-controllers>
49 + </volt-ofconfig>
50 + </volt-ne>
51 + </data>
52 +</rpc-reply>
1 +<!--
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 +
17 +<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
18 + <edit-config>
19 + <target><running/></target>
20 + <default-operation>merge</default-operation>
21 + <config>
22 + <volt-ne xmlns="http://fujitsu.com/ns/volt/1.1">
23 + <volt-ofconfig>
24 + <of-controllers>
25 + <of-controller>
26 + <ofconfig-id>1</ofconfig-id>
27 + <controller-info>
28 + <id>1</id>
29 + <ip-address>172.10.10.55</ip-address>
30 + <port>2222</port>
31 + <protocol>tcp</protocol>
32 + </controller-info>
33 + </of-controller>
34 + <of-controller>
35 + <ofconfig-id>3</ofconfig-id>
36 + <controller-info>
37 + <id>3</id>
38 + <ip-address>172.20.33.11</ip-address>
39 + <port>6633</port>
40 + <protocol>tcp</protocol>
41 + </controller-info>
42 + </of-controller>
43 + </of-controllers>
44 + </volt-ofconfig>
45 + </volt-ne>
46 + </config>
47 + </edit-config>
48 +</rpc>