xueliang
Committed by Gerrit Code Review

[ONOS-5269] JUNIT Test cases for volt-ponlinks and volt-setponlink for FUJITSU NETCONF

Change-Id: Iaff73d2eb1a617a97ebb1dcd8ccad7c0b809ea41
......@@ -25,6 +25,10 @@ import org.onosproject.netconf.NetconfController;
import org.slf4j.Logger;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ImmutableSet;
......@@ -40,14 +44,48 @@ public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
implements VoltPonLinkConfig {
private final Logger log = getLogger(FujitsuVoltPonLinkConfig.class);
private final Set<String> ponLinkParams = ImmutableSet.of(
"admin-state", "onu-discovery-mode", "onu-discovery-interval",
"dba-cycle-time", "mac-age-time", "lof-threshold",
"los-threshold", "pm-enable");
private static final Map<String, List<Integer>> PON_LINK_PARAMS = new HashMap<String, List<Integer>>() {
{
put("onu-discovery-interval", Arrays.asList(ONU_DISCOVERY_INTERVAL_MIN, ONU_DISCOVERY_INTERVAL_MAX));
put("dba-cycle-time", Arrays.asList(DBA_CYCLE_TIME_MIN, DBA_CYCLE_TIME_MAX));
put("mac-age-time", Arrays.asList(MAC_AGE_TIME_MIN, MAC_AGE_TIME_MAX));
put("lof-threshold", Arrays.asList(LOF_THRESHOLD_MIN, LOF_THRESHOLD_MAX));
put("los-threshold", Arrays.asList(LOS_THRESHOLD_MIN, LOS_THRESHOLD_MAX));
put(ONU_DISCOVERY_MODE, null);
put(PM_ENABLE, null);
put(ADMIN_STATE, null);
}
};
private static final String VOLT_PORTS = "volt-ports";
private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports";
private static final String GPON_PONLINK_PORT = "gpon-ponlink-port";
private int pon;
private static final String ADMIN_STATE = "admin-state";
private static final String ONU_DISCOVERY_MODE = "onu-discovery-mode";
private static final String PM_ENABLE = "pm-enable";
private static final Set<String> ADMINSTATES =
ImmutableSet.of("enable", "disable");
private static final Set<String> ONUDISCOVERYMODES =
ImmutableSet.of("auto", "manual", "disabled");
private static final Set<String> PMENABLES =
ImmutableSet.of("true", "false");
private static final int ONU_DISCOVERY_INTERVAL_MIN = 1;
private static final int ONU_DISCOVERY_INTERVAL_MAX = 3600;
private static final int DBA_CYCLE_TIME_MIN = 2;
private static final int DBA_CYCLE_TIME_MAX = 40;
private static final int MAC_AGE_TIME_MIN = 1000;
private static final int MAC_AGE_TIME_MAX = 3600000;
private static final int LOF_THRESHOLD_MIN = 1;
private static final int LOF_THRESHOLD_MAX = 10;
private static final int LOS_THRESHOLD_MIN = 1;
private static final int LOS_THRESHOLD_MAX = 10;
private static final int FIRST_PART = 0;
private static final int SECOND_PART = 1;
private static final int THIRD_PART = 2;
private static final int RANGE_MIN = 0;
private static final int RANGE_MAX = 1;
private static final int ZERO = 0;
private static final int THREE = 3;
@Override
public String getPonLinks(String target) {
......@@ -71,10 +109,15 @@ public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
request.append(ANGLE_RIGHT).append(NEW_LINE);
request.append(buildStartTag(VOLT_PORTS));
if (target != null) {
int pon;
try {
pon = Integer.parseInt(target);
if (pon <= ZERO) {
log.error("Invalid integer for ponlink-id:{}", target);
return reply;
}
} catch (NumberFormatException e) {
log.error("Non-number input");
log.error("Non-number input for ponlink-id:{}", target);
return reply;
}
request.append(buildStartTag(GPON_PONLINK_PORTS));
......@@ -91,17 +134,19 @@ public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
request.append(buildEndTag(VOLT_PORTS));
request.append(VOLT_NE_CLOSE);
reply = controller.
getDevicesMap().get(ncDeviceId).getSession().
get(request.toString(), REPORT_ALL);
reply = controller
.getDevicesMap()
.get(ncDeviceId)
.getSession()
.get(request.toString(), REPORT_ALL);
} catch (IOException e) {
log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
}
return reply;
}
@Override
public void setPonLink(String target) {
public boolean setPonLink(String target) {
DriverHandler handler = handler();
NetconfController controller = handler.get(NetconfController.class);
MastershipService mastershipService = handler.get(MastershipService.class);
......@@ -112,27 +157,16 @@ public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
log.warn("Not master for {} Use {} to execute command",
ncDeviceId,
mastershipService.getMasterFor(ncDeviceId));
return;
}
String[] data = target.split(COLON);
if (data.length != 3) {
log.error("Invalid number of arguments");
return;
}
try {
pon = Integer.parseInt(data[0]);
} catch (NumberFormatException e) {
log.error("Non-number input");
return;
return false;
}
if (!ponLinkParams.contains(data[1])) {
log.error("Unsupported parameter: {} ", data[1]);
return;
String[] data = checkSetInput(target);
if (data == null) {
log.error("Failed to check input: {}", target);
return false;
}
boolean result = false;
try {
StringBuilder request = new StringBuilder();
request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
......@@ -141,23 +175,117 @@ public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
request.append(buildStartTag(GPON_PONLINK_PORTS));
request.append(buildStartTag(GPON_PONLINK_PORT));
request.append(buildStartTag(PONLINK_ID, false));
request.append(data[0]);
request.append(data[FIRST_PART]);
request.append(buildEndTag(PONLINK_ID));
request.append(buildStartTag(data[1], false));
request.append(data[2]);
request.append(buildEndTag(data[1]));
request.append(buildStartTag(data[SECOND_PART], false));
request.append(data[THIRD_PART]);
request.append(buildEndTag(data[SECOND_PART]));
request.append(buildEndTag(GPON_PONLINK_PORT));
request.append(buildEndTag(GPON_PONLINK_PORTS));
request.append(buildEndTag(VOLT_PORTS));
request.append(VOLT_NE_CLOSE);
controller.getDevicesMap().get(ncDeviceId).getSession().
result = controller.getDevicesMap().get(ncDeviceId).getSession().
editConfig(RUNNING, null, request.toString());
} catch (IOException e) {
log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
}
return result;
}
/**
* Verifies input string for valid options.
*
* @param target input data in string
* @return String array
* @return null if an error condition is detected
*/
private String[] checkSetInput(String target) {
String[] data = target.split(COLON);
String paramName = data[SECOND_PART];
String paramValue = data[THIRD_PART];
int pon;
if (data.length != THREE) {
log.error("Invalid number of arguments {}", data.length);
return null;
}
try {
pon = Integer.parseInt(data[FIRST_PART]);
if (pon <= ZERO) {
log.error("Invalid integer for ponlink-id: {}",
data[FIRST_PART]);
return null;
}
} catch (NumberFormatException e) {
log.error("Non-number input for ponlink-id: {}",
data[FIRST_PART]);
return null;
}
if (!PON_LINK_PARAMS.containsKey(paramName)) {
log.error("Unsupported parameter: {}", paramName);
return null;
}
List<Integer> range = PON_LINK_PARAMS.get(paramName);
if (range == null) {
switch (paramName) {
case ADMIN_STATE:
if (!validState(ADMINSTATES, paramName, paramValue)) {
return null;
}
break;
case ONU_DISCOVERY_MODE:
if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) {
return null;
}
break;
default:
if (!validState(PMENABLES, paramName, paramValue)) {
return null;
}
break;
}
} else {
int value;
try {
value = Integer.parseInt(paramValue);
} catch (NumberFormatException e) {
log.error("Non-number input for Name {} : Value {}.",
paramName, paramValue);
return null;
}
if ((value < range.get(RANGE_MIN)) ||
(value > range.get(RANGE_MAX))) {
log.error("Out of value range for Name {} : Value {}.",
paramName, paramValue);
return null;
}
}
return data;
}
/**
* Verifies input string for valid options.
*
* @param states input data in string for parameter state
* @param name input data in string for parameter name
* @param value input data in string for parameter value
* @return true if the param is valid
* @return false if the param is invalid
*/
private boolean validState(Set<String> states, String name, String value) {
if (!states.contains(value)) {
log.error("Invalid value for Name {} : Value {}.", name, value);
return false;
}
return true;
}
}
......
......@@ -36,8 +36,8 @@ public interface VoltPonLinkConfig extends HandlerBehaviour {
* Set a parameter value of PON link in the device.
*
* @param target input data in string
*
* @return true if the operation is successful
*/
void setPonLink(String target);
boolean setPonLink(String target);
}
......
/*
* 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 java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertFalse;
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 FujitsuVoltPonLinkConfig.
*/
public class FujitsuVoltPonLinkConfigTest {
private FujitsuNetconfControllerMock controller;
private FujitsuDriverHandlerAdapter driverHandler;
private FujitsuVoltPonLinkConfig voltConfig;
private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
private static final String TEST_VOLT_PORTS = "volt-ports";
private static final String TEST_GPON_PONLINK_PORTS = "gpon-ponlink-ports";
private static final String TEST_GPON_PONLINK_PORT = "gpon-ponlink-port";
private static final int FIRST_PART = 0;
private static final int SECOND_PART = 1;
private static final int THIRD_PART = 2;
private static final Map<Integer, String> INVALID_GET_TCS = new HashMap<Integer, String>() {
{
put(1, "a-b-c");
put(2, "--1");
put(3, "s-1");
put(4, "1-1");
put(5, "1 A");
put(6, "1*A");
}
};
private static final Map<Integer, String> VALID_GET_TCS = new HashMap<Integer, String>() {
{
put(1, "1");
put(2, null);
}
};
private static final Map<Integer, String> INVALID_SET_TCS = new HashMap<Integer, String>() {
{
put(1, "-11:admin-state:enable");
put(2, "1:admin-state:false");
put(3, "2-1:onu-discovery-mode:manual");
put(4, "2:onu-discovery-mode:abcdef");
put(5, "3:a:onu-discovery-interval:8");
put(6, "3:onu-discovery-interval:-1");
put(7, "3:onu-discovery-interval:s1");
put(8, "4:dba-cycle-time:41");
put(9, "5*8:mac-age-time:30000");
put(10, "8:mac-age-time:3699999");
put(11, "1:lof-threshold:111");
put(12, "2:los-threshold:22");
put(13, "3:pm-enable:xyz");
put(14, "3:abc-enable:xyz");
}
};
private static final Map<Integer, String> VALID_SET_TCS = new HashMap<Integer, String>() {
{
put(1, "1:admin-state:disable");
put(2, "2:onu-discovery-mode:manual");
put(3, "3:onu-discovery-interval:8");
put(4, "4:dba-cycle-time:8");
put(5, "5:mac-age-time:33333");
put(6, "6:lof-threshold:7");
put(7, "7:los-threshold:5");
put(8, "8:pm-enable:true");
}
};
private Integer currentKey;
@Before
public void setUp() throws Exception {
controller = new FujitsuNetconfControllerMock();
driverHandler = controller.setUp(listener);
voltConfig = new FujitsuVoltPonLinkConfig();
voltConfig.setHandler(driverHandler);
}
/**
* Run to verify handling of invalid input for get operation.
*/
@Test
public void testInvalidGetPonLinksInput() throws Exception {
String reply;
String target;
for (Integer key : INVALID_GET_TCS.keySet()) {
target = INVALID_GET_TCS.get(key);
reply = voltConfig.getPonLinks(target);
assertNull("Incorrect response for " + target, reply);
}
}
/**
* Run to verify handling of valid input for get operation.
*/
@Test
public void testValidGetPonLinks() throws Exception {
String reply;
String target;
for (Integer key : VALID_GET_TCS.keySet()) {
target = VALID_GET_TCS.get(key);
currentKey = key;
reply = voltConfig.getPonLinks(target);
assertNotNull("Incorrect response for " + target, reply);
}
}
/**
* Run to verify handling of invalid input for set operation.
*/
@Test
public void testInvalidSetPonLinkInput() throws Exception {
String target;
boolean result;
for (Integer key : INVALID_SET_TCS.keySet()) {
target = INVALID_SET_TCS.get(key);
result = voltConfig.setPonLink(target);
assertFalse("Incorrect response for ", result);
}
}
/**
* Run to verify handling of valid input for set operation.
*/
@Test
public void testValidSetPonLink() throws Exception {
String target;
boolean result;
for (Integer key : VALID_SET_TCS.keySet()) {
target = VALID_SET_TCS.get(key);
currentKey = key;
result = voltConfig.setPonLink(target);
assertTrue("Incorrect response for ", result);
}
}
/**
* Verifies XML request string by comparing with generated string.
*
* @param request XML string for set operation
* @return true if XML string matches with generated
*/
private boolean verifyGetRequest(String request) {
StringBuilder rpc = new StringBuilder();
String target = VALID_GET_TCS.get(currentKey);
rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE);
rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE);
rpc.append(startTag(TEST_VOLT_PORTS));
if (target != null) {
rpc.append(startTag(TEST_GPON_PONLINK_PORTS));
rpc.append(startTag(TEST_GPON_PONLINK_PORT));
rpc.append(startTag(TEST_PONLINK_ID, false));
rpc.append(target);
rpc.append(endTag(TEST_PONLINK_ID));
rpc.append(endTag(TEST_GPON_PONLINK_PORT));
rpc.append(endTag(TEST_GPON_PONLINK_PORTS));
} else {
rpc.append(emptyTag(TEST_GPON_PONLINK_PORTS));
}
rpc.append(endTag(TEST_VOLT_PORTS));
rpc.append(TEST_VOLT_NE_CLOSE);
String testRequest = rpc.toString();
testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
boolean result = request.equals(testRequest);
assertTrue("Does not match with generated string", result);
return result;
}
/**
* Verifies XML request string by comparing with generated string.
*
* @param request XML string for set operation
* @return true or false
*/
private boolean verifyEditConfigRequest(String request) {
StringBuilder rpc = new StringBuilder();
String target = VALID_SET_TCS.get(currentKey);
String[] data = target.split(TEST_COLON);
rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE);
rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE);
rpc.append(startTag(TEST_VOLT_PORTS));
rpc.append(startTag(TEST_GPON_PONLINK_PORTS));
rpc.append(startTag(TEST_GPON_PONLINK_PORT));
rpc.append(startTag(TEST_PONLINK_ID, false));
rpc.append(data[FIRST_PART]);
rpc.append(endTag(TEST_PONLINK_ID));
rpc.append(startTag(data[SECOND_PART], false));
rpc.append(data[THIRD_PART]);
rpc.append(endTag(data[SECOND_PART]));
rpc.append(endTag(TEST_GPON_PONLINK_PORT));
rpc.append(endTag(TEST_GPON_PONLINK_PORTS));
rpc.append(endTag(TEST_VOLT_PORTS));
rpc.append(TEST_VOLT_NE_CLOSE);
String testRequest = rpc.toString();
testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
request = request.replaceAll(TEST_WHITESPACES_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 InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
@Override
public boolean verifyEditConfig(String request) {
return false;
}
@Override
public boolean verifyEditConfig(String target, String mode, String request) {
boolean result;
assertTrue("Incorrect target", target.equals(TEST_RUNNING));
assertNull("Incorrect mode", mode);
request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
request.contains(TEST_VOLT_NAMESPACE));
result = verifyEditConfigRequest(request);
assertTrue("XML verification failure", result);
return result;
}
@Override
public boolean verifyGet(String filterSchema, String withDefaultsMode) {
boolean result;
assertTrue("Incorrect withDefaultsMode",
withDefaultsMode.equals(TEST_REPORT_ALL));
filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
filterSchema.contains(TEST_VOLT_NAMESPACE));
result = verifyGetRequest(filterSchema);
assertTrue("XML verification failure", result);
return result;
}
@Override
public String buildGetReply() {
return null;
}
@Override
public boolean verifyWrappedRpc(String request) {
return false;
}
@Override
public void verifyStartSubscription(String filterSchema) {
}
}
}