[ONOS-5326] JUNIT Test cases for volt-rebootonu and volt-ethloopback as FUJITSU NETCONF
Change-Id: Ied14896955595ce4f694885991231b75be2e63c5
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfig.java
index 67f1db2..d2460ac 100644
--- a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfig.java
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfig.java
@@ -16,6 +16,7 @@
package org.onosproject.drivers.fujitsu;
+import com.google.common.collect.ImmutableSet;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.DeviceId;
import org.onosproject.drivers.fujitsu.behaviour.VoltOnuOperConfig;
@@ -25,6 +26,7 @@
import org.slf4j.Logger;
import java.io.IOException;
+import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
@@ -41,10 +43,9 @@
private static final String ONU_REBOOT = "onu-reboot";
private static final String ONU_ETHPORT_LOOPBACK = "onu-ethport-loopback";
private static final String ETHPORT_ID = "ethport-id";
- private int pon;
- private int onu;
- private int eth;
-
+ private static final String LOOPBACK_MODE = "mode";
+ private static final Set<String> LOOPBACKMODES =
+ ImmutableSet.of("operate", "release");
@Override
public String rebootOnu(String target) {
@@ -60,39 +61,35 @@
log.warn("Not master for {} Use {} to execute command",
ncDeviceId,
mastershipService.getMasterFor(ncDeviceId));
- return reply;
+ return null;
}
- onuId = target.split(HYPHEN);
- if (onuId.length != 2) {
- log.error("Invalid number of arguments");
- return reply;
- }
- try {
- pon = Integer.parseInt(onuId[0]);
- onu = Integer.parseInt(onuId[1]);
- } catch (NumberFormatException e) {
- log.error("Non-number input");
- return reply;
+ onuId = checkIdString(target, TWO);
+ if (onuId == null) {
+ log.error("Invalid ONU identifier {}", target);
+ return null;
}
try {
StringBuilder request = new StringBuilder();
- request.append(ANGLE_LEFT).append(ONU_REBOOT).append(SPACE);
- request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE);
- request.append(buildStartTag(PONLINK_ID, false));
- request.append(onuId[0]);
- request.append(buildEndTag(PONLINK_ID));
- request.append(buildStartTag(ONU_ID, false));
- request.append(onuId[1]);
- request.append(buildEndTag(ONU_ID));
- request.append(buildEndTag(ONU_REBOOT));
+ request.append(ANGLE_LEFT + ONU_REBOOT + SPACE);
+ request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
- reply = controller.
- getDevicesMap().get(ncDeviceId).getSession().
- doWrappedRpc(request.toString());
+ request.append(buildStartTag(PONLINK_ID, false))
+ .append(onuId[FIRST_PART])
+ .append(buildEndTag(PONLINK_ID))
+ .append(buildStartTag(ONU_ID, false))
+ .append(onuId[SECOND_PART])
+ .append(buildEndTag(ONU_ID))
+ .append(buildEndTag(ONU_REBOOT));
+
+ 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;
}
@@ -105,51 +102,110 @@
DeviceId ncDeviceId = handler.data().deviceId();
checkNotNull(controller, "Netconf controller is null");
String reply = null;
+ String[] data = null;
String[] ethId = null;
if (!mastershipService.isLocalMaster(ncDeviceId)) {
log.warn("Not master for {} Use {} to execute command",
ncDeviceId,
mastershipService.getMasterFor(ncDeviceId));
- return reply;
+ return null;
}
- ethId = target.split(HYPHEN);
- if (ethId.length != 3) {
- log.error("Invalid number of arguments");
- return reply;
+ data = target.split(COLON);
+ if (data.length > TWO) {
+ log.error("Invalid number of parameters {}", target);
+ return null;
}
- try {
- pon = Integer.parseInt(ethId[0]);
- onu = Integer.parseInt(ethId[1]);
- eth = Integer.parseInt(ethId[2]);
- } catch (NumberFormatException e) {
- log.error("Non-number input");
- return reply;
+
+ ethId = checkIdString(data[FIRST_PART], THREE);
+ if (ethId == null) {
+ log.error("Invalid ETH port identifier {}", data[FIRST_PART]);
+ return null;
+ }
+
+ if (data.length > ONE) {
+ if (!LOOPBACKMODES.contains(data[SECOND_PART])) {
+ log.error("Unsupported parameter: {}", data[SECOND_PART]);
+ return null;
+ }
}
try {
StringBuilder request = new StringBuilder();
- request.append(ANGLE_LEFT).append(ONU_ETHPORT_LOOPBACK).append(SPACE);
- request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE);
- request.append(buildStartTag(PONLINK_ID, false));
- request.append(ethId[0]);
- request.append(buildEndTag(PONLINK_ID));
- request.append(buildStartTag(ONU_ID, false));
- request.append(ethId[1]);
- request.append(buildEndTag(ONU_ID));
- request.append(buildStartTag(ETHPORT_ID, false));
- request.append(ethId[2]);
- request.append(buildEndTag(ETHPORT_ID));
+ request.append(ANGLE_LEFT + ONU_ETHPORT_LOOPBACK + SPACE);
+ request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
+
+ request.append(buildStartTag(PONLINK_ID, false))
+ .append(ethId[FIRST_PART])
+ .append(buildEndTag(PONLINK_ID))
+ .append(buildStartTag(ONU_ID, false))
+ .append(ethId[SECOND_PART])
+ .append(buildEndTag(ONU_ID))
+ .append(buildStartTag(ETHPORT_ID, false))
+ .append(ethId[THIRD_PART])
+ .append(buildEndTag(ETHPORT_ID));
+ if (data.length > ONE) {
+ request.append(buildStartTag(LOOPBACK_MODE, false))
+ .append(data[SECOND_PART])
+ .append(buildEndTag(LOOPBACK_MODE));
+ }
request.append(buildEndTag(ONU_ETHPORT_LOOPBACK));
- 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;
}
+ /**
+ * Verifies input string for ponlink-id{-onu-id}{-ethport-id}.
+ *
+ * @param target input data in string
+ * @param expected number of IDs expected
+ * @return String array containing IDs; may be null if an error is detected
+ */
+ private String[] checkIdString(String target, int expected) {
+ String[] id = target.split(HYPHEN);
+ int pon;
+ int onu;
+
+ if (id.length < TWO) {
+ log.error("Invalid number of arguments for id: {}", id.length);
+ return null;
+ }
+ if (id.length != expected) {
+ log.error("Invalid number of arguments for id: {}", id.length);
+ return null;
+ }
+ try {
+ pon = Integer.parseInt(id[FIRST_PART]);
+ if (pon <= ZERO) {
+ log.error("Invalid integer for ponlink-id: {}", id[FIRST_PART]);
+ return null;
+ }
+ onu = Integer.parseInt(id[SECOND_PART]);
+ if (onu <= ZERO) {
+ log.error("Invalid integer for onu-id: {}", id[SECOND_PART]);
+ return null;
+ }
+ if (expected > TWO) {
+ int port = Integer.parseInt(id[THIRD_PART]);
+ if (port <= ZERO) {
+ log.error("Invalid integer for port-id: {}", id[THIRD_PART]);
+ return null;
+ }
+ }
+ } catch (NumberFormatException e) {
+ log.error("Non-number input for id: {}", target);
+ return null;
+ }
+ return id;
+ }
+
}
diff --git a/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfigTest.java b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfigTest.java
new file mode 100644
index 0000000..b6bf0de
--- /dev/null
+++ b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltOnuOperConfigTest.java
@@ -0,0 +1,279 @@
+/*
+ * 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 FujitsuVoltOnuOperConfig.
+ */
+public class FujitsuVoltOnuOperConfigTest {
+
+ private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener();
+
+ private static final String TEST_ONU_REBOOT = "onu-reboot";
+ private static final String TEST_ONU_ETHPORT_LOOPBACK = "onu-ethport-loopback";
+ private static final String TEST_ETHPORT_ID = "ethport-id";
+ private static final String TEST_LOOPBACK_MODE = "mode";
+
+ private static final String TEST_ONU_REBOOT_WITH_NAMESPACE = TEST_ANGLE_LEFT +
+ TEST_ONU_REBOOT + TEST_SPACE + TEST_VOLT_NE_NAMESPACE;
+ private static final String TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE =
+ TEST_ANGLE_LEFT + TEST_ONU_ETHPORT_LOOPBACK + TEST_SPACE +
+ TEST_VOLT_NE_NAMESPACE;
+
+ private static final String[] INVALID_REBOOT_TCS = {
+ "xy1-b",
+ "--1",
+ "s-1",
+ "16-1-1",
+ "&AA-1",
+ "-1-1",
+ };
+ private static final String[] VALID_REBOOT_TCS = {
+ "1-2",
+ "16-11",
+ };
+ private static final String[] INVALID_ETHPORT_LOOPBACK_TCS = {
+ "-11-3--11",
+ "1-CCa",
+ "abc-1",
+ "^1-1-3",
+ "1-2:23-1",
+ "1:33:2",
+ "2-2-2:false",
+ };
+ private static final String[] VALID_ETHPORT_LOOPBACK_TCS = {
+ "8-1-1",
+ "1-11-3:release",
+ "2-2-2:operate",
+ };
+ private Integer currentKey;
+ private FujitsuNetconfControllerMock controller;
+ private FujitsuDriverHandlerAdapter driverHandler;
+ private FujitsuVoltOnuOperConfig voltConfig;
+
+ @Before
+ public void setUp() throws Exception {
+ controller = new FujitsuNetconfControllerMock();
+ driverHandler = controller.setUp(listener);
+ voltConfig = new FujitsuVoltOnuOperConfig();
+ voltConfig.setHandler(driverHandler);
+ }
+
+ /**
+ * Run to verify handling of invalid input for rpc operation.
+ */
+ @Test
+ public void testInvalidRebootOnuInput() throws Exception {
+ String reply;
+ String target;
+
+ for (int i = ZERO; i < INVALID_REBOOT_TCS.length; i++) {
+ target = INVALID_REBOOT_TCS[i];
+ reply = voltConfig.rebootOnu(target);
+ assertNull("Incorrect response for INVALID_REBOOT_TCS", reply);
+ }
+ }
+
+ /**
+ * Run to verify handling of valid input for rpc operation.
+ */
+ @Test
+ public void testValidRebootOnu() throws Exception {
+ String reply;
+ String target;
+
+ for (int i = ZERO; i < VALID_REBOOT_TCS.length; i++) {
+ target = VALID_REBOOT_TCS[i];
+ currentKey = i;
+ reply = voltConfig.rebootOnu(target);
+ assertNotNull("Incorrect response for VALID_REBOOT_TCS", reply);
+ }
+ }
+
+ /**
+ * Run to verify handling of invalid input for rpc operation.
+ */
+ @Test
+ public void testInvalidEthLoopbackOnuInput() throws Exception {
+ String target;
+ String reply;
+
+ for (int i = ZERO; i < INVALID_ETHPORT_LOOPBACK_TCS.length; i++) {
+ target = INVALID_ETHPORT_LOOPBACK_TCS[i];
+ reply = voltConfig.loopbackEthOnu(target);
+ assertNull("Incorrect response for INVALID_ETHPORT_LOOPBACK_TCS", reply);
+ }
+ }
+
+ /**
+ * Run to verify handling of valid input for rpc operation.
+ */
+ @Test
+ public void testValidLoopbackEthOnu() throws Exception {
+ String target;
+ String reply;
+
+ for (int i = ZERO; i < VALID_ETHPORT_LOOPBACK_TCS.length; i++) {
+ target = VALID_ETHPORT_LOOPBACK_TCS[i];
+ currentKey = i;
+ reply = voltConfig.loopbackEthOnu(target);
+ assertNotNull("Incorrect response for VALID_ETHPORT_LOOPBACK_TCS", reply);
+ }
+ }
+
+ /**
+ * Verifies XML request string by comparing with generated string.
+ *
+ * @param request XML string for rpc operation
+ * @return true or false
+ */
+ private boolean verifyWrappedRpcRequestForReboot(String request) {
+ StringBuilder rpc = new StringBuilder();
+ String target = VALID_REBOOT_TCS[currentKey];
+ String[] data = target.split(TEST_COLON);
+ String[] onuId = data[FIRST_PART].split(TEST_HYPHEN);
+
+ rpc.append(TEST_ANGLE_LEFT + TEST_ONU_REBOOT + TEST_SPACE);
+ rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
+
+ rpc.append(startTag(TEST_PONLINK_ID, false))
+ .append(onuId[FIRST_PART])
+ .append(endTag(TEST_PONLINK_ID))
+ .append(startTag(TEST_ONU_ID, false))
+ .append(onuId[SECOND_PART])
+ .append(endTag(TEST_ONU_ID))
+ .append(endTag(TEST_ONU_REBOOT));
+
+ 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;
+ }
+
+ /**
+ * Verifies XML request string by comparing with generated string.
+ *
+ * @param request XML string for rpc operation
+ * @return true or false
+ */
+ private boolean verifyWrappedRpcRequestForEthLoopback(String request) {
+ StringBuilder rpc = new StringBuilder();
+ String target = VALID_ETHPORT_LOOPBACK_TCS[currentKey];
+ String[] data = target.split(TEST_COLON);
+ String[] ethId = data[FIRST_PART].split(TEST_HYPHEN);
+
+ rpc.append(TEST_ANGLE_LEFT + TEST_ONU_ETHPORT_LOOPBACK + TEST_SPACE);
+ rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
+
+ rpc.append(startTag(TEST_PONLINK_ID, false))
+ .append(ethId[FIRST_PART])
+ .append(endTag(TEST_PONLINK_ID))
+ .append(startTag(TEST_ONU_ID, false))
+ .append(ethId[SECOND_PART])
+ .append(endTag(TEST_ONU_ID))
+ .append(startTag(TEST_ETHPORT_ID, false))
+ .append(ethId[THIRD_PART])
+ .append(endTag(TEST_ETHPORT_ID));
+ if (data.length > SECOND_PART) {
+ rpc.append(startTag(TEST_LOOPBACK_MODE, false))
+ .append(data[SECOND_PART])
+ .append(endTag(TEST_LOOPBACK_MODE));
+ }
+ rpc.append(endTag(TEST_ONU_ETHPORT_LOOPBACK));
+
+ 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;
+ boolean reboot = false;
+
+ if (request.contains(TEST_ONU_REBOOT)) {
+ request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
+ assertTrue("Does not contain:" + TEST_ONU_REBOOT_WITH_NAMESPACE,
+ request.contains(TEST_ONU_REBOOT_WITH_NAMESPACE));
+ reboot = true;
+ } else {
+ request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
+ assertTrue("Does not contain:" + TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE,
+ request.contains(TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE));
+ }
+
+ if (reboot) {
+ result = verifyWrappedRpcRequestForReboot(request);
+ } else {
+ result = verifyWrappedRpcRequestForEthLoopback(request);
+ }
+
+ assertTrue("XML verification failure", result);
+ return result;
+ }
+
+ @Override
+ public void verifyStartSubscription(String filterSchema) {
+ }
+ }
+
+}
diff --git a/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltXmlUtilityMock.java b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltXmlUtilityMock.java
index e9200a3..19a6981 100644
--- a/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltXmlUtilityMock.java
+++ b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltXmlUtilityMock.java
@@ -58,6 +58,7 @@
public static final int FIRST_PART = 0;
public static final int SECOND_PART = 1;
public static final int THIRD_PART = 2;
+ public static final int ZERO = 0;
public static final int ONE = 1;
private FujitsuVoltXmlUtilityMock() {