xueliang
Committed by Gerrit Code Review

[ONOS-5254] JUNIT Mocks for FUJITSU NETCONF

Change-Id: Iac2b6473a71c1d981b6d2dadc316a31d9b4a02df
......@@ -2,6 +2,7 @@ COMPILE_DEPS = [
'//lib:CORE_DEPS',
'//drivers/utilities:onos-drivers-utilities',
'//protocols/netconf/api:onos-protocols-netconf-api',
'//protocols/netconf/ctl:onos-protocols-netconf-ctl',
'//lib:org.apache.karaf.shell.console',
'//cli:onos-cli',
'//apps/optical-model:onos-apps-optical-model',
......
......@@ -56,6 +56,11 @@
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-netconf-ctl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-drivers-utilities</artifactId>
<version>${project.version}</version>
</dependency>
......@@ -69,6 +74,11 @@
<artifactId>org.apache.karaf.shell.console</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
......
/*
* 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.onosproject.net.DeviceId;
import org.onosproject.net.driver.Behaviour;
import org.onosproject.net.driver.DefaultDriverHandler;
import org.onosproject.net.driver.DriverData;
import org.onosproject.mastership.MastershipService;
import org.onosproject.mastership.MastershipServiceAdapter;
import org.onosproject.netconf.NetconfController;
/**
* Mock DefaultDriverHandler.
*/
public class FujitsuDriverHandlerAdapter extends DefaultDriverHandler {
private NetconfController controller;
private final MastershipService mastershipService = new InternalMastershipServiceMock();;
/**
* Creates new driver handler with the attached driver data.
*
* @param data driver data to attach
*/
public FujitsuDriverHandlerAdapter(DriverData data) {
super(data);
}
@Override
public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
return true;
}
@Override
public <T> T get(Class<T> serviceClass) {
if (serviceClass == NetconfController.class) {
return (T) this.controller;
} else if (serviceClass == MastershipService.class) {
return (T) this.mastershipService;
}
return null;
}
/**
* Set up initial environment.
*
* @param controller NETCONF controller instance
*/
public void setUp(NetconfController controller) {
this.controller = controller;
}
/**
* Mock MastershipServiceAdapter.
*/
private class InternalMastershipServiceMock extends MastershipServiceAdapter {
@Override
public boolean isLocalMaster(DeviceId deviceId) {
return true;
}
}
}
/*
* 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 com.google.common.collect.ImmutableMap;
import org.onlab.packet.IpAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.driver.DefaultDriver;
import org.onosproject.net.driver.DefaultDriverData;
import org.onosproject.netconf.NetconfDevice;
import org.onosproject.netconf.NetconfDeviceInfo;
import org.onosproject.netconf.NetconfException;
import org.onosproject.netconf.ctl.NetconfControllerImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Mock NetconfControllerImpl.
*/
class FujitsuNetconfControllerMock extends NetconfControllerImpl {
private static final String VOLT_DRIVER_NAME = "fujitsu-volt-netconf";
private static final String VOLT_DEVICE_USERNAME = "abc";
private static final String VOLT_DEVICE_PASSWORD = "123";
private static final String VOLT_DEVICE_IP = "10.10.10.11";
private static final int VOLT_DEVICE_PORT = 830;
private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
@Override
public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
return netconfDeviceMap.get(deviceInfo);
}
@Override
public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
for (DeviceId info : netconfDeviceMap.keySet()) {
if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
return netconfDeviceMap.get(info);
}
}
return null;
}
@Override
public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
if (netconfDeviceMap.containsKey(deviceId)) {
log.debug("Device {} is already present", deviceId);
return netconfDeviceMap.get(deviceId);
} else {
log.debug("Creating NETCONF device {}", deviceId);
String ip;
int port;
String[] info = deviceId.toString().split(":");
if (info.length == 3) {
ip = info[1];
port = Integer.parseInt(info[2]);
} else {
ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
&& !el.equals(info[info.length - 1]))
.reduce((t, u) -> t + ":" + u)
.get();
log.debug("ip v6 {}", ip);
port = Integer.parseInt(info[info.length - 1]);
}
try {
NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(
VOLT_DEVICE_USERNAME,
VOLT_DEVICE_PASSWORD,
IpAddress.valueOf(ip),
port);
NetconfDevice netconfDevice = new FujitsuNetconfDeviceMock(deviceInfo);
netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
return netconfDevice;
} catch (NullPointerException e) {
throw new NetconfException("Cannot connect a device " + deviceId, e);
}
}
}
@Override
public void disconnectDevice(DeviceId deviceId, boolean remove) {
if (!netconfDeviceMap.containsKey(deviceId)) {
log.warn("Device {} is not present", deviceId);
} else {
netconfDeviceMap.remove(deviceId);
}
}
@Override
public Map<DeviceId, NetconfDevice> getDevicesMap() {
return netconfDeviceMap;
}
@Override
public Set<DeviceId> getNetconfDevices() {
return netconfDeviceMap.keySet();
}
@Override
public void removeDevice(DeviceId deviceId) {
if (!netconfDeviceMap.containsKey(deviceId)) {
log.warn("Device {} is not present", deviceId);
} else {
netconfDeviceMap.remove(deviceId);
}
}
/**
* Sets up initial test environment.
*
* @param listener listener to be added
* @return driver handler
* @throws NetconfException when there is a problem
*/
public FujitsuDriverHandlerAdapter setUp(FujitsuNetconfSessionListenerTest listener) throws NetconfException {
try {
NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(
VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD,
IpAddress.valueOf(VOLT_DEVICE_IP), VOLT_DEVICE_PORT);
NetconfDevice netconfDevice = connectDevice(deviceInfo.getDeviceId());
FujitsuNetconfSessionMock session = (FujitsuNetconfSessionMock) netconfDevice.getSession();
session.setListener(listener);
DeviceId deviceId = deviceInfo.getDeviceId();
DefaultDriver driver = new DefaultDriver(
VOLT_DRIVER_NAME, new ArrayList<>(),
"Fujitsu", "1.0", "1.0",
ImmutableMap.of(), ImmutableMap.of());
DefaultDriverData driverData = new DefaultDriverData(driver, deviceId);
FujitsuDriverHandlerAdapter driverHandler;
driverHandler = new FujitsuDriverHandlerAdapter(driverData);
driverHandler.setUp(this);
return driverHandler;
} catch (NetconfException e) {
throw new NetconfException("Cannot create a device ", e);
}
}
}
/*
* 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.onosproject.netconf.NetconfDevice;
import org.onosproject.netconf.NetconfDeviceInfo;
import org.onosproject.netconf.NetconfException;
import org.onosproject.netconf.NetconfSession;
/**
* Mock DefaultNetconfDevice.
*/
class FujitsuNetconfDeviceMock implements NetconfDevice {
private NetconfDeviceInfo netconfDeviceInfo;
private boolean deviceState = false;
private NetconfSession netconfSession;
/**
* Creates a new NETCONF device with the information provided.
*
* @param deviceInfo information about the device to be created.
* @throws NetconfException if there are problems in creating or establishing
* the underlying NETCONF connection and session.
*/
public FujitsuNetconfDeviceMock(NetconfDeviceInfo deviceInfo) throws NetconfException {
netconfDeviceInfo = deviceInfo;
try {
netconfSession = new FujitsuNetconfSessionMock();
deviceState = true;
} catch (Exception e) {
throw new NetconfException("Cannot create Connection and Session");
}
}
@Override
public boolean isActive() {
return deviceState;
}
@Override
public NetconfSession getSession() {
return netconfSession;
}
@Override
public void disconnect() {
deviceState = false;
netconfSession = null;
}
@Override
public NetconfDeviceInfo getDeviceInfo() {
return netconfDeviceInfo;
}
}
/*
* 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;
public interface FujitsuNetconfSessionListenerTest {
/**
* Verify editConfig request arguments.
*
* @param newConfiguration configuration to set
* @return true if everuthing as expected
*/
boolean verifyEditConfig(String newConfiguration);
/**
* Verify editConfig request arguments.
*
* @param targetConfiguration the targetConfiguration to change
* @param mode selected mode to change the configuration
* @param newConfiguration configuration to set
* @return true if everuthing as expected
*/
boolean verifyEditConfig(String targetConfiguration, String mode, String newConfiguration);
/**
* Verify get request arguments.
*
* @param filterSchema XML subtrees to include in the reply
* @param withDefaultsMode with-defaults mode
* @return true if everuthing as expected
*/
boolean verifyGet(String filterSchema, String withDefaultsMode);
/**
* Build get RPC response if necessary.
*
* @return String or null if not support
*/
String buildGetReply();
/**
* Verify rpc request arguments.
*
* @param request the XML containing the request to the server.
* @return true if everuthing as expected
*/
boolean verifyWrappedRpc(String request);
/**
* Verify rpc request arguments.
*
* @param filterSchema XML subtrees to indicate specific notification
*/
void verifyStartSubscription(String filterSchema);
}
/*
* Copyright 2015-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 com.google.common.annotations.Beta;
import org.onosproject.netconf.NetconfDeviceOutputEventListener;
import org.onosproject.netconf.NetconfException;
import org.onosproject.netconf.NetconfSession;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Mock NetconfSessionImpl.
*/
public class FujitsuNetconfSessionMock implements NetconfSession {
private FujitsuNetconfSessionListenerTest listener;
/**
* Registers a listener to be invoked for verification.
*
* @param listener listener to be added
*/
public void setListener(FujitsuNetconfSessionListenerTest listener) {
this.listener = listener;
}
@Override
public CompletableFuture<String> request(String request) throws NetconfException {
return null;
}
@Override
public String get(String request) throws NetconfException {
return null;
}
@Override
public String get(String filterSchema, String withDefaultsMode)
throws NetconfException {
boolean result = true;
String reply = null;
if (listener != null) {
result = listener.verifyGet(filterSchema, withDefaultsMode);
if (result) {
reply = listener.buildGetReply();
}
}
return result ? ((reply == null) ? filterSchema : reply) : null;
}
@Override
public String doWrappedRpc(String request) throws NetconfException {
boolean result = true;
if (listener != null) {
result = listener.verifyWrappedRpc(request);
}
return result ? request : null;
}
@Override
public String requestSync(String request) throws NetconfException {
return null;
}
@Override
public String getConfig(String targetConfiguration) throws NetconfException {
return null;
}
@Override
public String getConfig(String targetConfiguration, String configurationFilterSchema)
throws NetconfException {
return null;
}
@Override
public boolean editConfig(String newConfiguration) throws NetconfException {
boolean result = true;
if (listener != null) {
result = listener.verifyEditConfig(newConfiguration);
}
return result;
}
@Override
public boolean editConfig(String targetConfiguration, String mode, String newConfiguration)
throws NetconfException {
boolean result = true;
if (listener != null) {
result = listener.verifyEditConfig(targetConfiguration, mode, newConfiguration);
}
return result;
}
@Override
public boolean copyConfig(String targetConfiguration, String newConfiguration)
throws NetconfException {
return false;
}
@Override
public boolean deleteConfig(String targetConfiguration) throws NetconfException {
return false;
}
@Override
public void startSubscription() throws NetconfException {
}
@Beta
@Override
public void startSubscription(String filterSchema) throws NetconfException {
if (listener != null) {
listener.verifyStartSubscription(filterSchema);
}
return;
}
@Override
public void endSubscription() throws NetconfException {
}
@Override
public boolean lock(String configType) throws NetconfException {
return false;
}
@Override
public boolean unlock(String configType) throws NetconfException {
return false;
}
@Override
public boolean lock() throws NetconfException {
return false;
}
@Override
public boolean unlock() throws NetconfException {
return false;
}
@Override
public boolean close() throws NetconfException {
return false;
}
@Override
public String getSessionId() {
return null;
}
@Override
public String getServerCapabilities() {
return null;
}
@Override
public void setDeviceCapabilities(List<String> capabilities) {
}
@Override
public void addDeviceOutputListener(NetconfDeviceOutputEventListener listener) {
}
@Override
public void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener) {
}
}
/*
* 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;
/**
* Mock FujitsuVoltXmlUtility.
* This is to avoid using the same names/definitions
* in FujitsuVoltXmlUtility in test codes - not tied to actual codes.
*/
final class FujitsuVoltXmlUtilityMock {
public static final String TEST_COLON = ":";
public static final String TEST_DOT = ".";
public static final String TEST_HYPHEN = "-";
public static final String TEST_SLASH = "/";
public static final String TEST_SPACE = " ";
public static final String TEST_NEW_LINE = "\n";
public static final String TEST_ANGLE_LEFT = "<";
public static final String TEST_ANGLE_RIGHT = ">";
public static final String TEST_REPORT_ALL = "report-all";
public static final String TEST_RUNNING = "running";
public static final String TEST_VOLT_NE_NAMESPACE =
"xmlns=\"http://fujitsu.com/ns/volt/1.1\"";
public static final String TEST_VOLT_NE = "volt-ne";
public static final String TEST_PONLINK_ID = "ponlink-id";
public static final String TEST_ONU_ID = "onu-id";
public static final String TEST_ROOT = "fakeroot";
public static final String TEST_VOLT_NE_OPEN = TEST_ANGLE_LEFT + TEST_VOLT_NE + TEST_SPACE;
public static final String TEST_VOLT_NE_CLOSE = TEST_ANGLE_LEFT + TEST_SLASH + TEST_VOLT_NE + TEST_ANGLE_RIGHT;
public static final String TEST_BASE_NAMESPACE =
" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n";
public static final String TEST_DUPLICATE_SPACES_REGEX = " +";
public static final String TEST_WHITESPACES_REGEX = "\\s+";
public static final String TEST_EMPTY_STRING = "";
public static final String TEST_VOLT_NAMESPACE = TEST_VOLT_NE_OPEN +
TEST_VOLT_NE_NAMESPACE;
private FujitsuVoltXmlUtilityMock() {
}
/**
* Builds XML start tag with name provided.
*
* @param name tag name
* @return string
*/
public static String startTag(String name) {
return startTag(name, true);
}
/**
* Builds XML end tag with name provided.
*
* @param name tag name
* @return string
*/
public static String endTag(String name) {
return endTag(name, true);
}
/**
* Builds XML empty tag with name provided.
*
* @param name tag name
* @return string
*/
public static String emptyTag(String name) {
return emptyTag(name, true);
}
/**
* Builds XML start tag with name provided.
*
* @param name tag name
* @param addNewLine option to add new line character after tag
* @return string
*/
public static String startTag(String name, boolean addNewLine) {
if (addNewLine) {
return (TEST_ANGLE_LEFT + name + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
} else {
return (TEST_ANGLE_LEFT + name + TEST_ANGLE_RIGHT);
}
}
/**
* Builds XML end tag with name provided.
*
* @param name tag name
* @param addNewLine option to add new line character after tag
* @return string
*/
public static String endTag(String name, boolean addNewLine) {
if (addNewLine) {
return (TEST_ANGLE_LEFT + TEST_SLASH + name + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
} else {
return (TEST_ANGLE_LEFT + TEST_SLASH + name + TEST_ANGLE_RIGHT);
}
}
/**
* Builds XML empty element tag with name provided.
*
* @param name tag name
* @param addNewLine option to add new line character after tag
* @return string
*/
public static String emptyTag(String name, boolean addNewLine) {
if (addNewLine) {
return (TEST_ANGLE_LEFT + name + TEST_SLASH + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
} else {
return (TEST_ANGLE_LEFT + name + TEST_SLASH + TEST_ANGLE_RIGHT);
}
}
}