xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 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; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 30 | |
| 31 | import java.nio.charset.StandardCharsets; |
| 32 | import java.io.InputStream; |
| 33 | |
| 34 | import static org.junit.Assert.assertTrue; |
| 35 | import static org.junit.Assert.fail; |
| 36 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for methods of FujitsuVoltControllerConfig. |
| 41 | */ |
| 42 | public class FujitsuVoltControllerConfigTest { |
| 43 | |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 44 | 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 = "-->"; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 49 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 50 | private static final String[] GET_CONTROLLERS = { |
| 51 | "tcp:172.10.10.45:6633", |
| 52 | "tcp:100.0.0.22:5555", |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 53 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 54 | private static final String[] SET_CONTROLLERS = { |
| 55 | "tcp:172.10.10.55:2222", |
| 56 | "tcp:172.20.33.11:6633", |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 57 | }; |
| 58 | private static final String GET_CONTROLLERS_RSP_FILE = "/getcontrollers.xml"; |
| 59 | private static final String SET_CONTROLLERS_REQ_FILE = "/setcontrollers.xml"; |
| 60 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 61 | private Integer currentKey; |
| 62 | private FujitsuNetconfControllerMock controller; |
| 63 | private FujitsuDriverHandlerAdapter driverHandler; |
| 64 | private FujitsuVoltControllerConfig voltConfig; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 65 | |
| 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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 82 | for (int i = ZERO; i < GET_CONTROLLERS.length; i++) { |
| 83 | String target = GET_CONTROLLERS[i]; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 84 | String[] data = target.split(TEST_COLON); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 85 | currentKey = i + ONE; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 86 | |
| 87 | Annotations annotations = DefaultAnnotations |
| 88 | .builder() |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 89 | .set(TEST_OFCONFIG_ID, currentKey.toString()) |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 90 | .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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 109 | for (int i = ZERO; i < SET_CONTROLLERS.length; i++) { |
| 110 | String target = SET_CONTROLLERS[i]; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 111 | String[] data = target.split(TEST_COLON); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 112 | currentKey = i + ONE; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 113 | |
| 114 | Annotations annotations = DefaultAnnotations |
| 115 | .builder() |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 116 | .set(TEST_OFCONFIG_ID, currentKey.toString()) |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 117 | .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(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame^] | 136 | 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); |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 140 | |
| 141 | String testRequest = rpc.toString(); |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 142 | 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 | } |