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; |
| 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 = "-->"; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 55 | |
| 56 | private static final Map<Integer, String> GET_CONTROLLERS = new HashMap<Integer, String>() { |
| 57 | { |
| 58 | put(1, "tcp:172.10.10.45:6633"); |
| 59 | put(2, "tcp:100.0.0.22:5555"); |
| 60 | } |
| 61 | }; |
| 62 | private static final Map<Integer, String> SET_CONTROLLERS = new HashMap<Integer, String>() { |
| 63 | { |
| 64 | put(1, "tcp:172.10.10.55:2222"); |
| 65 | put(3, "tcp:172.20.33.11:6633"); |
| 66 | } |
| 67 | }; |
| 68 | private static final String GET_CONTROLLERS_RSP_FILE = "/getcontrollers.xml"; |
| 69 | private static final String SET_CONTROLLERS_REQ_FILE = "/setcontrollers.xml"; |
| 70 | |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | controller = new FujitsuNetconfControllerMock(); |
| 75 | driverHandler = controller.setUp(listener); |
| 76 | voltConfig = new FujitsuVoltControllerConfig(); |
| 77 | voltConfig.setHandler(driverHandler); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Run to verify handling of valid get operation. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testGetControllers() throws Exception { |
| 85 | List<ControllerInfo> controllers; |
| 86 | List<ControllerInfo> expectedControllers = new ArrayList<>(); |
| 87 | |
| 88 | for (Integer key : GET_CONTROLLERS.keySet()) { |
| 89 | String target = GET_CONTROLLERS.get(key); |
| 90 | String[] data = target.split(TEST_COLON); |
| 91 | |
| 92 | Annotations annotations = DefaultAnnotations |
| 93 | .builder() |
| 94 | .set(TEST_OFCONFIG_ID, key.toString()) |
| 95 | .build(); |
| 96 | ControllerInfo controller = new ControllerInfo( |
| 97 | IpAddress.valueOf(data[SECOND_PART]), |
| 98 | Integer.parseInt(data[THIRD_PART]), |
| 99 | data[FIRST_PART], annotations); |
| 100 | expectedControllers.add(controller); |
| 101 | } |
| 102 | |
| 103 | controllers = voltConfig.getControllers(); |
| 104 | assertTrue("Incorrect response", controllers.equals(expectedControllers)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Run to verify handling of valid set operation. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testSetControllers() throws Exception { |
| 112 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 113 | |
| 114 | for (Integer key : SET_CONTROLLERS.keySet()) { |
| 115 | String target = SET_CONTROLLERS.get(key); |
| 116 | String[] data = target.split(TEST_COLON); |
| 117 | |
| 118 | Annotations annotations = DefaultAnnotations |
| 119 | .builder() |
| 120 | .set(TEST_OFCONFIG_ID, key.toString()) |
| 121 | .build(); |
| 122 | ControllerInfo controller = new ControllerInfo( |
| 123 | IpAddress.valueOf(data[SECOND_PART]), |
| 124 | Integer.parseInt(data[THIRD_PART]), |
| 125 | data[FIRST_PART], annotations); |
| 126 | controllers.add(controller); |
| 127 | } |
| 128 | |
| 129 | voltConfig.setControllers(controllers); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Verifies XML request string by comparing with generated string. |
| 134 | * |
| 135 | * @param request XML string for set operation |
| 136 | * @return true if XML string matches with generated |
| 137 | */ |
| 138 | private boolean verifyGetRequest(String request) { |
| 139 | StringBuilder rpc = new StringBuilder(); |
| 140 | rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE); |
| 141 | rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE); |
| 142 | rpc.append(emptyTag(TEST_VOLT_OFCONFIG)); |
| 143 | rpc.append(endTag(TEST_VOLT_NE)); |
| 144 | |
| 145 | String testRequest = rpc.toString(); |
| 146 | testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 147 | request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 148 | boolean result = request.equals(testRequest); |
| 149 | assertTrue("Does not match with generated string", result); |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Verifies XML request string by comparing with generated string. |
| 155 | * |
| 156 | * @param request XML string for set operation |
| 157 | * @return true if XML string matches with generated |
| 158 | */ |
| 159 | private boolean verifyEditConfigRequest(String request) { |
| 160 | String testRequest; |
| 161 | |
| 162 | try { |
| 163 | InputStream fileStream = getClass().getResourceAsStream( |
| 164 | SET_CONTROLLERS_REQ_FILE); |
| 165 | testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8); |
| 166 | testRequest = testRequest.substring(testRequest.indexOf( |
| 167 | TEST_END_LICENSE_HEADER) + TEST_END_LICENSE_HEADER.length()); |
| 168 | } catch (IOException e) { |
| 169 | fail("IOException while reading: " + SET_CONTROLLERS_REQ_FILE); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 174 | request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 175 | boolean result = request.equals(testRequest); |
| 176 | assertTrue("Does not match with string in " + SET_CONTROLLERS_REQ_FILE, result); |
| 177 | |
| 178 | return result; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Internal listener for device service events. |
| 183 | */ |
| 184 | private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest { |
| 185 | @Override |
| 186 | public boolean verifyEditConfig(String request) { |
| 187 | boolean result; |
| 188 | |
| 189 | request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 190 | assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE, |
| 191 | request.contains(TEST_VOLT_NAMESPACE)); |
| 192 | |
| 193 | result = verifyEditConfigRequest(request); |
| 194 | assertTrue("XML verification failure", result); |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean verifyEditConfig(String target, String mode, String request) { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean verifyGet(String filterSchema, String withDefaultsMode) { |
| 205 | boolean result; |
| 206 | |
| 207 | assertTrue("Incorrect withDefaultsMode", |
| 208 | withDefaultsMode.equals(TEST_REPORT_ALL)); |
| 209 | filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 210 | assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE, |
| 211 | filterSchema.contains(TEST_VOLT_NAMESPACE)); |
| 212 | |
| 213 | result = verifyGetRequest(filterSchema); |
| 214 | assertTrue("XML verification failure", result); |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String buildGetReply() { |
| 220 | try { |
| 221 | InputStream fileStream = getClass().getResourceAsStream( |
| 222 | GET_CONTROLLERS_RSP_FILE); |
| 223 | String reply = IOUtils.toString(fileStream, StandardCharsets.UTF_8); |
| 224 | return (reply); |
| 225 | } catch (IOException e) { |
| 226 | return null; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean verifyWrappedRpc(String request) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void verifyStartSubscription(String filterSchema) { |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | } |