Refactoring XenServer56NetworkUsageCommandWrapper in order to cope with the new design

- Unit tests added
CitrixResourceBase, XenServer56Resource and XenServer56FP1Resource are now done.

i
This commit is contained in:
wilderrodrigues 2015-03-31 09:10:40 +02:00
parent f3842c81f8
commit 3ad30a01a8
5 changed files with 181 additions and 77 deletions

View File

@ -3088,7 +3088,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return earliestNetwork != null ? new XsLocalNetwork(this, earliestNetwork, earliestNetworkRecord, null, null) : null;
}
protected long[] getNetworkStats(final Connection conn, final String privateIP) {
public long[] getNetworkStats(final Connection conn, final String privateIP) {
final String result = networkUsage(conn, privateIP, "get", null);
final long[] stats = new long[2];
if (result != null) {

View File

@ -27,12 +27,9 @@ import org.apache.xmlrpc.XmlRpcException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.NetworkUsageAnswer;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.agent.api.StartupCommand;
import com.cloud.hypervisor.xenserver.resource.wrapper.CitrixRequestWrapper;
import com.cloud.resource.ServerResource;
import com.cloud.utils.ExecutionResult;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
import com.cloud.utils.ssh.SSHCmdHelper;
@ -55,13 +52,6 @@ public class XenServer56Resource extends CitrixResourceBase {
try {
return wrapper.execute(cmd, this);
} catch (final Exception e) {
// return Answer.createUnsupportedCommandAnswer(cmd);
// Ignore this for now. Still working on converting the other commands.
}
if (cmd instanceof NetworkUsageCommand) {
return execute((NetworkUsageCommand) cmd);
} else {
return super.executeRequest(cmd);
}
}
@ -143,72 +133,6 @@ public class XenServer56Resource extends CitrixResourceBase {
return executeInVR(privateIpAddress, "netusage.sh", args).getDetails();
}
protected NetworkUsageAnswer VPCNetworkUsage(final NetworkUsageCommand cmd) {
try {
final String option = cmd.getOption();
final String publicIp = cmd.getGatewayIP();
String args = " -l " + publicIp + " ";
if (option.equals("get")) {
args += "-g";
} else if (option.equals("create")) {
args += "-c";
final String vpcCIDR = cmd.getVpcCIDR();
args += " -v " + vpcCIDR;
} else if (option.equals("reset")) {
args += "-r";
} else if (option.equals("vpn")) {
args += "-n";
} else if (option.equals("remove")) {
args += "-d";
} else {
return new NetworkUsageAnswer(cmd, "success", 0L, 0L);
}
final ExecutionResult result = executeInVR(cmd.getPrivateIP(), "vpc_netusage.sh", args);
final String detail = result.getDetails();
if (!result.isSuccess()) {
throw new Exception(" vpc network usage plugin call failed ");
}
if (option.equals("get") || option.equals("vpn")) {
final long[] stats = new long[2];
if (detail != null) {
final String[] splitResult = detail.split(":");
int i = 0;
while (i < splitResult.length - 1) {
stats[0] += new Long(splitResult[i++]).longValue();
stats[1] += new Long(splitResult[i++]).longValue();
}
return new NetworkUsageAnswer(cmd, "success", stats[0], stats[1]);
}
}
return new NetworkUsageAnswer(cmd, "success", 0L, 0L);
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(cmd, ex);
}
}
protected NetworkUsageAnswer execute(final NetworkUsageCommand cmd) {
if (cmd.isForVpc()) {
return VPCNetworkUsage(cmd);
}
try {
final Connection conn = getConnection();
if (cmd.getOption() != null && cmd.getOption().equals("create")) {
final String result = networkUsage(conn, cmd.getPrivateIP(), "create", null);
final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L);
return answer;
}
final long[] stats = getNetworkStats(conn, cmd.getPrivateIP());
final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]);
return answer;
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(cmd, ex);
}
}
public Boolean checkHeartbeat(final String hostuuid) {
final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
try {

View File

@ -47,6 +47,7 @@ import com.cloud.agent.api.ModifySshKeysCommand;
import com.cloud.agent.api.ModifyStoragePoolCommand;
import com.cloud.agent.api.NetworkRulesSystemVmCommand;
import com.cloud.agent.api.NetworkRulesVmSecondaryIpCommand;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.agent.api.OvsCreateGreTunnelCommand;
import com.cloud.agent.api.OvsCreateTunnelCommand;
import com.cloud.agent.api.OvsDeleteFlowCommand;
@ -176,6 +177,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
final Hashtable<Class<? extends Command>, CommandWrapper> xenServer56Commands = new Hashtable<Class<? extends Command>, CommandWrapper>();
xenServer56Commands.put(CheckOnHostCommand.class, new XenServer56CheckOnHostCommandWrapper());
xenServer56Commands.put(FenceCommand.class, new XenServer56FenceCommandWrapper());
xenServer56Commands.put(NetworkUsageCommand.class, new XenServer56NetworkUsageCommandWrapper());
resources.put(XenServer56Resource.class, xenServer56Commands);
// XenServer56FP1Resource commands

View File

@ -0,0 +1,102 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 com.cloud.hypervisor.xenserver.resource.wrapper;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.NetworkUsageAnswer;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.hypervisor.xenserver.resource.XenServer56Resource;
import com.cloud.resource.CommandWrapper;
import com.cloud.utils.ExecutionResult;
import com.xensource.xenapi.Connection;
public final class XenServer56NetworkUsageCommandWrapper extends CommandWrapper<NetworkUsageCommand, Answer, XenServer56Resource> {
private static final Logger s_logger = Logger.getLogger(XenServer56NetworkUsageCommandWrapper.class);
@Override
public Answer execute(final NetworkUsageCommand command, final XenServer56Resource xenServer56) {
if (command.isForVpc()) {
return executeNetworkUsage(command, xenServer56);
}
try {
final Connection conn = xenServer56.getConnection();
if (command.getOption() != null && command.getOption().equals("create")) {
final String result = xenServer56.networkUsage(conn, command.getPrivateIP(), "create", null);
final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
return answer;
}
final long[] stats = xenServer56.getNetworkStats(conn, command.getPrivateIP());
final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
return answer;
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(command, ex);
}
}
protected NetworkUsageAnswer executeNetworkUsage(final NetworkUsageCommand command, final XenServer56Resource xenServer56) {
try {
final String option = command.getOption();
final String publicIp = command.getGatewayIP();
String args = " -l " + publicIp + " ";
if (option.equals("get")) {
args += "-g";
} else if (option.equals("create")) {
args += "-c";
final String vpcCIDR = command.getVpcCIDR();
args += " -v " + vpcCIDR;
} else if (option.equals("reset")) {
args += "-r";
} else if (option.equals("vpn")) {
args += "-n";
} else if (option.equals("remove")) {
args += "-d";
} else {
return new NetworkUsageAnswer(command, "success", 0L, 0L);
}
final ExecutionResult result = xenServer56.executeInVR(command.getPrivateIP(), "vpc_netusage.sh", args);
final String detail = result.getDetails();
if (!result.isSuccess()) {
throw new Exception(" vpc network usage plugin call failed ");
}
if (option.equals("get") || option.equals("vpn")) {
final long[] stats = new long[2];
if (detail != null) {
final String[] splitResult = detail.split(":");
int i = 0;
while (i < splitResult.length - 1) {
stats[0] += new Long(splitResult[i++]).longValue();
stats[1] += new Long(splitResult[i++]).longValue();
}
return new NetworkUsageAnswer(command, "success", stats[0], stats[1]);
}
}
return new NetworkUsageAnswer(command, "success", 0L, 0L);
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(command, ex);
}
}
}

View File

@ -16,8 +16,10 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CheckOnHostCommand;
import com.cloud.agent.api.FenceCommand;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.host.Host;
import com.cloud.hypervisor.xenserver.resource.XenServer56Resource;
import com.cloud.utils.ExecutionResult;
import com.cloud.vm.VMInstanceVO;
import com.xensource.xenapi.Connection;
@ -61,4 +63,78 @@ public class XenServer56WrapperTest {
assertFalse(answer.getResult());
}
@Test
public void testNetworkUsageCommandSuccess() {
final Connection conn = Mockito.mock(Connection.class);
final NetworkUsageCommand networkCommand = new NetworkUsageCommand("192.168.10.10", "domRName", false, "192.168.10.1");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(xenServer56Resource.getConnection()).thenReturn(conn);
when(xenServer56Resource.getNetworkStats(conn, networkCommand.getPrivateIP())).thenReturn(new long[]{1, 1});
final Answer answer = wrapper.execute(networkCommand, xenServer56Resource);
verify(xenServer56Resource, times(1)).getConnection();
assertTrue(answer.getResult());
}
@Test
public void testNetworkUsageCommandFailure() {
final Connection conn = Mockito.mock(Connection.class);
final NetworkUsageCommand networkCommand = new NetworkUsageCommand("192.168.10.10", "domRName", false, "192.168.10.1");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(xenServer56Resource.getConnection()).thenReturn(conn);
when(xenServer56Resource.getNetworkStats(conn, networkCommand.getPrivateIP())).thenReturn(new long[0]);
final Answer answer = wrapper.execute(networkCommand, xenServer56Resource);
verify(xenServer56Resource, times(1)).getConnection();
assertFalse(answer.getResult());
}
@Test
public void testNetworkUsageCommandCreateVpc() {
final ExecutionResult executionResult = Mockito.mock(ExecutionResult.class);
final NetworkUsageCommand networkCommand = new NetworkUsageCommand("192.168.10.10", "domRName", true, "192.168.10.1", "10.1.1.1/24");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final String args = " -l 192.168.10.1 -c -v 10.1.1.1/24";
when(xenServer56Resource.executeInVR(networkCommand.getPrivateIP(), "vpc_netusage.sh", args)).thenReturn(executionResult);
when(executionResult.isSuccess()).thenReturn(true);
final Answer answer = wrapper.execute(networkCommand, xenServer56Resource);
assertTrue(answer.getResult());
}
@Test
public void testNetworkUsageCommandCreateVpcFailure() {
final ExecutionResult executionResult = Mockito.mock(ExecutionResult.class);
final NetworkUsageCommand networkCommand = new NetworkUsageCommand("192.168.10.10", "domRName", true, "192.168.10.1", "10.1.1.1/24");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final String args = " -l 192.168.10.1 -c -v 10.1.1.1/24";
when(xenServer56Resource.executeInVR(networkCommand.getPrivateIP(), "vpc_netusage.sh", args)).thenReturn(executionResult);
when(executionResult.isSuccess()).thenReturn(false);
final Answer answer = wrapper.execute(networkCommand, xenServer56Resource);
assertFalse(answer.getResult());
}
}