xueliang
Committed by Gerrit Code Review

[ONOS-5418] JUNIT Test cases for volt-ondemandfwdl as FUJITSU NETCONF

Change-Id: I515d48a664ad2edc18be801977e4bff7713f1052
......@@ -43,9 +43,8 @@ public class FujitsuVoltFwdlConfig extends AbstractHandlerBehaviour
private static final String MEMBER = "member";
private static final String IMAGE_NAME = "image-name";
private static final String REBOOT_MODE = "reboot-mode";
private int pon;
private int onu;
private static final String AUTO = "auto";
private static final String COMMA = ",";
@Override
public String upgradeFirmwareOndemand(String target) {
......@@ -61,67 +60,84 @@ public class FujitsuVoltFwdlConfig extends AbstractHandlerBehaviour
log.warn("Not master for {} Use {} to execute command",
ncDeviceId,
mastershipService.getMasterFor(ncDeviceId));
return reply;
return null;
}
String[] data = target.split(":");
if ((data.length < 2) || (data.length > 3)) {
String[] data = target.split(COLON);
if ((data.length < TWO) || (data.length > THREE)) {
log.error("Invalid number of arguments");
return reply;
return null;
}
String[] onuList = data[1].split(",");
if (onuList.length == 0) {
String[] onuList = data[SECOND_PART].split(COMMA);
if (onuList.length == ZERO) {
log.error("No ONU listed");
return reply;
return null;
}
if ((data.length > TWO) && (!AUTO.equals(data[THIRD_PART]))) {
log.error("Invalid reboot-mode {}", data[THIRD_PART]);
return null;
}
try {
StringBuilder request = new StringBuilder();
request.append(ANGLE_LEFT).append(ONDEMAND_FIRMWARE_UPGRADE).append(SPACE);
request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE);
request.append(ANGLE_LEFT + ONDEMAND_FIRMWARE_UPGRADE + SPACE);
request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
request.append(buildStartTag(PARTICIPANT_LIST));
for (count = 0; count < onuList.length; count++) {
String[] onuId = onuList[count].split("-");
if (onuId.length != 2) {
for (count = ZERO; count < onuList.length; count++) {
String[] onuId = onuList[count].split(HYPHEN);
if (onuId.length != TWO) {
log.error("Invalid ONU identifier");
return reply;
return null;
}
try {
pon = Integer.parseInt(onuId[0]);
onu = Integer.parseInt(onuId[1]);
int pon;
pon = Integer.parseInt(onuId[FIRST_PART]);
if (pon <= ZERO) {
log.error("Invalid integer for ponlink-id:{}", onuId[FIRST_PART]);
return null;
}
int onu;
onu = Integer.parseInt(onuId[SECOND_PART]);
if (onu <= ZERO) {
log.error("Invalid integer for onu-id:{}", onuId[SECOND_PART]);
return null;
}
} catch (NumberFormatException e) {
log.error("Non-number input");
return reply;
return null;
}
request.append(buildStartTag(MEMBER));
request.append(buildStartTag(PONLINK_ID));
request.append(onuId[0]);
request.append(buildEndTag(PONLINK_ID));
request.append(buildStartTag(ONU_ID));
request.append(onuId[1]);
request.append(buildEndTag(ONU_ID));
request.append(buildEndTag(MEMBER));
request.append(buildStartTag(MEMBER))
.append(buildStartTag(PONLINK_ID))
.append(onuId[FIRST_PART])
.append(buildEndTag(PONLINK_ID))
.append(buildStartTag(ONU_ID))
.append(onuId[SECOND_PART])
.append(buildEndTag(ONU_ID))
.append(buildEndTag(MEMBER));
}
request.append(buildEndTag(PARTICIPANT_LIST));
request.append(buildStartTag(IMAGE_NAME));
request.append(data[0]);
request.append(buildEndTag(IMAGE_NAME));
if (data.length == 3) {
request.append(buildStartTag(REBOOT_MODE));
request.append(data[2]);
request.append(buildEndTag(REBOOT_MODE));
request.append(buildEndTag(PARTICIPANT_LIST))
.append(buildStartTag(IMAGE_NAME))
.append(data[FIRST_PART])
.append(buildEndTag(IMAGE_NAME));
if (data.length == THREE) {
request.append(buildStartTag(REBOOT_MODE))
.append(data[THIRD_PART])
.append(buildEndTag(REBOOT_MODE));
}
request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE));
reply = controller.
getDevicesMap().get(ncDeviceId).getSession().
doWrappedRpc(request.toString());
reply = controller
.getDevicesMap()
.get(ncDeviceId)
.getSession()
.doWrappedRpc(request.toString());
} catch (IOException e) {
log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
}
return reply;
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.drivers.fujitsu;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
/**
* Unit tests for methods of FujitsuVoltFwdlConfig.
*/
public class FujitsuVoltFwdlConfigTest {
private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener();
private static final String TEST_ONDEMAND_FIRMWARE_UPGRADE = "ondemand-firmware-upgrade";
private static final String TEST_PARTICIPANT_LIST = "participant-list";
private static final String TEST_MEMBER = "member";
private static final String TEST_IMAGE_NAME = "image-name";
private static final String TEST_REBOOT_MODE = "reboot-mode";
private static final String TEST_COMMA = ",";
private static final String TEST_ONDEMAND_FWDL_WITH_NAMESPACE =
TEST_ANGLE_LEFT + TEST_ONDEMAND_FIRMWARE_UPGRADE +
TEST_SPACE + TEST_VOLT_NE_NAMESPACE;
private static final String[] INVALID_ONDEMAND_FWDL_TCS = {
"xy1-b:a1-1",
"AAA:1-2,--1",
"CcC:s-1,2-2,3-2:auto",
"xYZam:1-1,2-2,3-3:false",
"JKml901:16-1-1,2-16:a-2",
"abc:&AA-1,11-2:auto",
"abc:xyz:-1-1",
"@bcf11:xyz:auto",
"FJ123:1-1&5-2:auto",
};
private static final String[] VALID_ONDEMAND_FWDL_TCS = {
"Fujitsu123:1-2",
"abcDE90f:16-11,1-1,17-3:auto",
"fujitsuONU12:1-1,2-2,3-3,4-4,5-5,6-6,7-7",
};
private Integer currentKey;
private FujitsuNetconfControllerMock controller;
private FujitsuDriverHandlerAdapter driverHandler;
private FujitsuVoltFwdlConfig voltConfig;
@Before
public void setUp() throws Exception {
controller = new FujitsuNetconfControllerMock();
driverHandler = controller.setUp(listener);
voltConfig = new FujitsuVoltFwdlConfig();
voltConfig.setHandler(driverHandler);
}
/**
* Run to verify handling of invalid input for rpc operation.
*/
@Test
public void testInvalidOndemandFirmwareUpgradeInput() throws Exception {
String reply;
String target;
for (int i = ZERO; i < INVALID_ONDEMAND_FWDL_TCS.length; i++) {
target = INVALID_ONDEMAND_FWDL_TCS[i];
reply = voltConfig.upgradeFirmwareOndemand(target);
assertNull("Incorrect response for INVALID_ONDEMAND_FWDL_TCS", reply);
}
}
/**
* Run to verify handling of valid input for rpc operation.
*/
@Test
public void testValidOndemandFirmwareUpgrade() throws Exception {
String reply;
String target;
for (int i = ZERO; i < VALID_ONDEMAND_FWDL_TCS.length; i++) {
target = VALID_ONDEMAND_FWDL_TCS[i];
currentKey = i;
reply = voltConfig.upgradeFirmwareOndemand(target);
assertNotNull("Incorrect response for VALID_ONDEMAND_FWDL_TCS", reply);
}
}
/**
* Verifies XML request string by comparing with generated string.
*
* @param request XML string for rpc operation
* @return true or false
*/
private boolean verifyWrappedRpcRequest(String request) {
StringBuilder rpc = new StringBuilder();
String target = VALID_ONDEMAND_FWDL_TCS[currentKey];
String[] data = target.split(TEST_COLON);
String[] onuList = data[SECOND_PART].split(TEST_COMMA);
int count;
rpc.append(TEST_ANGLE_LEFT + TEST_ONDEMAND_FIRMWARE_UPGRADE + TEST_SPACE);
rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
rpc.append(startTag(TEST_PARTICIPANT_LIST));
for (count = ZERO; count < onuList.length; count++) {
String[] onuId = onuList[count].split(TEST_HYPHEN);
rpc.append(startTag(TEST_MEMBER))
.append(startTag(TEST_PONLINK_ID))
.append(onuId[FIRST_PART])
.append(endTag(TEST_PONLINK_ID))
.append(startTag(TEST_ONU_ID))
.append(onuId[SECOND_PART])
.append(endTag(TEST_ONU_ID))
.append(endTag(TEST_MEMBER));
}
rpc.append(endTag(TEST_PARTICIPANT_LIST))
.append(startTag(TEST_IMAGE_NAME))
.append(data[FIRST_PART])
.append(endTag(TEST_IMAGE_NAME));
if (data.length == THREE) {
rpc.append(startTag(TEST_REBOOT_MODE))
.append(data[THIRD_PART])
.append(endTag(TEST_REBOOT_MODE));
}
rpc.append(endTag(TEST_ONDEMAND_FIRMWARE_UPGRADE));
String testRequest = rpc.toString();
String regex = TEST_WHITESPACES_REGEX;
int index = rpc.indexOf(regex);
while (index >= ZERO) {
testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
request = request.replaceAll(regex, TEST_EMPTY_STRING);
}
boolean result = request.equals(testRequest);
assertTrue("Does not match with generated string", result);
return result;
}
/**
* Internal listener for device service events.
*/
private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
@Override
public boolean verifyEditConfig(String request) {
return false;
}
@Override
public boolean verifyEditConfig(String target, String mode, String request) {
return false;
}
@Override
public boolean verifyGet(String filterSchema, String withDefaultsMode) {
return false;
}
@Override
public String buildGetReply() {
return null;
}
@Override
public boolean verifyWrappedRpc(String request) {
boolean result;
request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
assertTrue("Does not contain:" + TEST_ONDEMAND_FWDL_WITH_NAMESPACE,
request.contains(TEST_ONDEMAND_FWDL_WITH_NAMESPACE));
result = verifyWrappedRpcRequest(request);
assertTrue("XML verification failure", result);
return result;
}
@Override
public void verifyStartSubscription(String filterSchema) {
}
}
}
......@@ -60,6 +60,7 @@ final class FujitsuVoltXmlUtilityMock {
public static final int THIRD_PART = 2;
public static final int ZERO = 0;
public static final int ONE = 1;
public static final int THREE = 3;
private FujitsuVoltXmlUtilityMock() {
}
......