diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 6f24b31b716..3c40c908071 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -87,13 +87,9 @@ import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand; import com.cloud.agent.api.HostVmStateReportEntry; import com.cloud.agent.api.ManageSnapshotAnswer; import com.cloud.agent.api.ManageSnapshotCommand; -import com.cloud.agent.api.NetworkUsageAnswer; -import com.cloud.agent.api.NetworkUsageCommand; import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.PingRoutingCommand; import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; -import com.cloud.agent.api.PlugNicAnswer; -import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PvlanSetupCommand; import com.cloud.agent.api.SetupGuestNetworkCommand; import com.cloud.agent.api.StartAnswer; @@ -101,8 +97,6 @@ import com.cloud.agent.api.StartCommand; import com.cloud.agent.api.StartupCommand; import com.cloud.agent.api.StartupRoutingCommand; import com.cloud.agent.api.StartupStorageCommand; -import com.cloud.agent.api.UnPlugNicAnswer; -import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.VmDiskStatsEntry; import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.routing.IpAssocCommand; @@ -1277,14 +1271,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return execute((CreatePrivateTemplateFromSnapshotCommand)cmd); } else if (cmd instanceof StartCommand) { return execute((StartCommand)cmd); - } else if (cmd instanceof PlugNicCommand) { - return execute((PlugNicCommand)cmd); - } else if (cmd instanceof UnPlugNicCommand) { - return execute((UnPlugNicCommand)cmd); } else if (cmd instanceof NetworkElementCommand) { return _virtRouterResource.executeRequest((NetworkElementCommand)cmd); - } else if (cmd instanceof NetworkUsageCommand) { - return execute((NetworkUsageCommand)cmd); } else if (cmd instanceof CopyVolumeCommand) { return execute((CopyVolumeCommand)cmd); } else if (cmd instanceof ResizeVolumeCommand) { @@ -1702,79 +1690,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } - private PlugNicAnswer execute(final PlugNicCommand cmd) { - final NicTO nic = cmd.getNic(); - final String vmName = cmd.getVmName(); - Domain vm = null; - try { - final Connect conn = LibvirtConnection.getConnectionByVmName(vmName); - vm = getDomain(conn, vmName); - final List pluggedNics = getInterfaces(conn, vmName); - Integer nicnum = 0; - for (final InterfaceDef pluggedNic : pluggedNics) { - if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) { - s_logger.debug("found existing nic for mac " + pluggedNic.getMacAddress() + " at index " + nicnum); - return new PlugNicAnswer(cmd, true, "success"); - } - nicnum++; - } - vm.attachDevice(getVifDriver(nic.getType()).plug(nic, "Other PV", "").toString()); - return new PlugNicAnswer(cmd, true, "success"); - } catch (final LibvirtException e) { - final String msg = " Plug Nic failed due to " + e.toString(); - s_logger.warn(msg, e); - return new PlugNicAnswer(cmd, false, msg); - } catch (final InternalErrorException e) { - final String msg = " Plug Nic failed due to " + e.toString(); - s_logger.warn(msg, e); - return new PlugNicAnswer(cmd, false, msg); - } finally { - if (vm != null) { - try { - vm.free(); - } catch (final LibvirtException l) { - s_logger.trace("Ignoring libvirt error.", l); - } - } - } - } - - private UnPlugNicAnswer execute(final UnPlugNicCommand cmd) { - Connect conn; - final NicTO nic = cmd.getNic(); - final String vmName = cmd.getVmName(); - Domain vm = null; - try { - conn = LibvirtConnection.getConnectionByVmName(vmName); - vm = getDomain(conn, vmName); - final List pluggedNics = getInterfaces(conn, vmName); - for (final InterfaceDef pluggedNic : pluggedNics) { - if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) { - vm.detachDevice(pluggedNic.toString()); - // We don't know which "traffic type" is associated with - // each interface at this point, so inform all vif drivers - for (final VifDriver vifDriver : getAllVifDrivers()) { - vifDriver.unplug(pluggedNic); - } - return new UnPlugNicAnswer(cmd, true, "success"); - } - } - return new UnPlugNicAnswer(cmd, true, "success"); - } catch (final LibvirtException e) { - final String msg = " Unplug Nic failed due to " + e.toString(); - s_logger.warn(msg, e); - return new UnPlugNicAnswer(cmd, false, msg); - } finally { - if (vm != null) { - try { - vm.free(); - } catch (final LibvirtException l) { - s_logger.trace("Ignoring libvirt error.", l); - } - } - } - } - private ExecutionResult prepareNetworkElementCommand(final SetupGuestNetworkCommand cmd) { Connect conn; final NicTO nic = cmd.getNic(); @@ -2445,7 +2360,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return usageParser.getLine(); } - protected long[] getNetworkStats(final String privateIP) { + public long[] getNetworkStats(final String privateIP) { final String result = networkUsage(privateIP, "get", null); final long[] stats = new long[2]; if (result != null) { @@ -2459,7 +2374,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return stats; } - protected String VPCNetworkUsage(final String privateIpAddress, final String publicIp, final String option, final String vpcCIDR) { + public String configureVPCNetworkUsage(final String privateIpAddress, final String publicIp, final String option, final String vpcCIDR) { final Script getUsage = new Script(_routerProxyPath, s_logger); getUsage.add("vpc_netusage.sh"); getUsage.add(privateIpAddress); @@ -2487,8 +2402,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return usageParser.getLine(); } - protected long[] getVPCNetworkStats(final String privateIP, final String publicIp, final String option) { - final String result = VPCNetworkUsage(privateIP, publicIp, option, null); + public long[] getVPCNetworkStats(final String privateIP, final String publicIp, final String option) { + final String result = configureVPCNetworkUsage(privateIP, publicIp, option, null); final long[] stats = new long[2]; if (result != null) { final String[] splitResult = result.split(":"); @@ -2501,33 +2416,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return stats; } - private Answer execute(final NetworkUsageCommand cmd) { - if (cmd.isForVpc()) { - if (cmd.getOption() != null && cmd.getOption().equals("create")) { - final String result = VPCNetworkUsage(cmd.getPrivateIP(), cmd.getGatewayIP(), "create", cmd.getVpcCIDR()); - final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L); - return answer; - } else if (cmd.getOption() != null && (cmd.getOption().equals("get") || cmd.getOption().equals("vpn"))) { - final long[] stats = getVPCNetworkStats(cmd.getPrivateIP(), cmd.getGatewayIP(), cmd.getOption()); - final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); - return answer; - } else { - final String result = VPCNetworkUsage(cmd.getPrivateIP(), cmd.getGatewayIP(), cmd.getOption(), cmd.getVpcCIDR()); - final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L); - return answer; - } - } else { - if (cmd.getOption() != null && cmd.getOption().equals("create")) { - final String result = networkUsage(cmd.getPrivateIP(), "create", null); - final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L); - return answer; - } - final long[] stats = getNetworkStats(cmd.getPrivateIP()); - final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); - return answer; - } - } - protected void handleVmStartFailure(final Connect conn, final String vmName, final LibvirtVMDef vm) { if (vm != null && vm.getDevices() != null) { cleanupVMNetworks(conn, vm.getDevices().getInterfaces()); diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkUsageCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkUsageCommandWrapper.java new file mode 100644 index 00000000000..3ac21825bee --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkUsageCommandWrapper.java @@ -0,0 +1,57 @@ +// +// 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.kvm.resource.wrapper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.NetworkUsageAnswer; +import com.cloud.agent.api.NetworkUsageCommand; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtNetworkUsageCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final NetworkUsageCommand command, final LibvirtComputingResource libvirtComputingResource) { + if (command.isForVpc()) { + if (command.getOption() != null && command.getOption().equals("create")) { + final String result = libvirtComputingResource.configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), "create", command.getVpcCIDR()); + final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L); + return answer; + } else if (command.getOption() != null && (command.getOption().equals("get") || command.getOption().equals("vpn"))) { + final long[] stats = libvirtComputingResource.getVPCNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption()); + final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]); + return answer; + } else { + final String result = libvirtComputingResource.configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR()); + final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L); + return answer; + } + } else { + if (command.getOption() != null && command.getOption().equals("create")) { + final String result = libvirtComputingResource.networkUsage(command.getPrivateIP(), "create", null); + final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L); + return answer; + } + final long [] stats = libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]); + return answer; + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java new file mode 100644 index 00000000000..7e6f642964a --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java @@ -0,0 +1,85 @@ +// +// 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.kvm.resource.wrapper; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.libvirt.Connect; +import org.libvirt.Domain; +import org.libvirt.LibvirtException; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.PlugNicAnswer; +import com.cloud.agent.api.PlugNicCommand; +import com.cloud.agent.api.to.NicTO; +import com.cloud.exception.InternalErrorException; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef; +import com.cloud.hypervisor.kvm.resource.VifDriver; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtPlugNicCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(LibvirtPlugNicCommandWrapper.class); + + @Override + public Answer execute(final PlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) { + final NicTO nic = command.getNic(); + final String vmName = command.getVmName(); + Domain vm = null; + try { + final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + vm = libvirtComputingResource.getDomain(conn, vmName); + + final List pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName); + Integer nicnum = 0; + for (final InterfaceDef pluggedNic : pluggedNics) { + if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) { + s_logger.debug("found existing nic for mac " + pluggedNic.getMacAddress() + " at index " + nicnum); + return new PlugNicAnswer(command, true, "success"); + } + nicnum++; + } + VifDriver vifDriver = libvirtComputingResource.getVifDriver(nic.getType()); + InterfaceDef interfaceDef = vifDriver.plug(nic, "Other PV", ""); + vm.attachDevice(interfaceDef.toString()); + + return new PlugNicAnswer(command, true, "success"); + } catch (final LibvirtException e) { + final String msg = " Plug Nic failed due to " + e.toString(); + s_logger.warn(msg, e); + return new PlugNicAnswer(command, false, msg); + } catch (final InternalErrorException e) { + final String msg = " Plug Nic failed due to " + e.toString(); + s_logger.warn(msg, e); + return new PlugNicAnswer(command, false, msg); + } finally { + if (vm != null) { + try { + vm.free(); + } catch (final LibvirtException l) { + s_logger.trace("Ignoring libvirt error.", l); + } + } + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java index 23248c4a6d2..148075b8575 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java @@ -44,6 +44,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.OvsCreateTunnelCommand; import com.cloud.agent.api.OvsDestroyBridgeCommand; import com.cloud.agent.api.OvsDestroyTunnelCommand; @@ -52,12 +53,14 @@ import com.cloud.agent.api.OvsSetupBridgeCommand; import com.cloud.agent.api.OvsVpcPhysicalTopologyConfigCommand; import com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand; import com.cloud.agent.api.PingTestCommand; +import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.SecurityGroupRulesCmd; import com.cloud.agent.api.StopCommand; +import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.UpgradeSnapshotCommand; import com.cloud.agent.api.check.CheckSshCommand; import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; @@ -130,6 +133,9 @@ public class LibvirtRequestWrapper extends RequestWrapper { linbvirtCommands.put(CreateVolumeFromSnapshotCommand.class, new LibvirtCreateVolumeFromSnapshotCommandWrapper()); linbvirtCommands.put(FenceCommand.class, new LibvirtFenceCommandWrapper()); linbvirtCommands.put(SecurityGroupRulesCmd.class, new LibvirtSecurityGroupRulesCommandWrapper()); + linbvirtCommands.put(PlugNicCommand.class, new LibvirtPlugNicCommandWrapper()); + linbvirtCommands.put(UnPlugNicCommand.class, new LibvirtUnPlugNicCommandWrapper()); + linbvirtCommands.put(NetworkUsageCommand.class, new LibvirtNetworkUsageCommandWrapper()); resources.put(LibvirtComputingResource.class, linbvirtCommands); } diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java new file mode 100644 index 00000000000..4ce14f2c824 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java @@ -0,0 +1,80 @@ +// +// 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.kvm.resource.wrapper; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.libvirt.Connect; +import org.libvirt.Domain; +import org.libvirt.LibvirtException; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.UnPlugNicAnswer; +import com.cloud.agent.api.UnPlugNicCommand; +import com.cloud.agent.api.to.NicTO; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef; +import com.cloud.hypervisor.kvm.resource.VifDriver; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtUnPlugNicCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(LibvirtUnPlugNicCommandWrapper.class); + + @Override + public Answer execute(final UnPlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) { + final NicTO nic = command.getNic(); + final String vmName = command.getVmName(); + Domain vm = null; + try { + final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + + final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + vm = libvirtComputingResource.getDomain(conn, vmName); + final List pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName); + + for (final InterfaceDef pluggedNic : pluggedNics) { + if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) { + vm.detachDevice(pluggedNic.toString()); + // We don't know which "traffic type" is associated with + // each interface at this point, so inform all vif drivers + for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) { + vifDriver.unplug(pluggedNic); + } + return new UnPlugNicAnswer(command, true, "success"); + } + } + return new UnPlugNicAnswer(command, true, "success"); + } catch (final LibvirtException e) { + final String msg = " Unplug Nic failed due to " + e.toString(); + s_logger.warn(msg, e); + return new UnPlugNicAnswer(command, false, msg); + } finally { + if (vm != null) { + try { + vm.free(); + } catch (final LibvirtException l) { + s_logger.trace("Ignoring libvirt error.", l); + } + } + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 15ba52b251d..9b0be08cb06 100644 --- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -86,6 +86,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.OvsCreateTunnelCommand; import com.cloud.agent.api.OvsDestroyBridgeCommand; import com.cloud.agent.api.OvsDestroyTunnelCommand; @@ -98,6 +99,7 @@ import com.cloud.agent.api.OvsVpcPhysicalTopologyConfigCommand.Vm; import com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand; import com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand.Acl; import com.cloud.agent.api.PingTestCommand; +import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; @@ -105,6 +107,7 @@ import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.SecurityGroupRulesCmd; import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto; import com.cloud.agent.api.StopCommand; +import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.UpgradeSnapshotCommand; import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.check.CheckSshCommand; @@ -2928,4 +2931,482 @@ public class LibvirtComputingResourceTest { fail(e.getMessage()); } } + + @Test + public void testPlugNicCommandMatchMack() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + final Type vmtype = Type.DomainRouter; + + final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + final Connect conn = Mockito.mock(Connect.class); + final Domain vm = Mockito.mock(Domain.class); + + final List nics = new ArrayList(); + final InterfaceDef intDef = Mockito.mock(InterfaceDef.class); + nics.add(intDef); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics); + + when(intDef.getDevName()).thenReturn("eth0"); + when(intDef.getBrName()).thenReturn("br0"); + when(intDef.getMacAddress()).thenReturn("00:00:00:00"); + + when(nic.getMac()).thenReturn("00:00:00:00"); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn); + when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @Test + public void testPlugNicCommandNoMatchMack() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + final Type vmtype = Type.DomainRouter; + + final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + final Connect conn = Mockito.mock(Connect.class); + final Domain vm = Mockito.mock(Domain.class); + final VifDriver vifDriver = Mockito.mock(VifDriver.class); + final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class); + + final List nics = new ArrayList(); + final InterfaceDef intDef = Mockito.mock(InterfaceDef.class); + nics.add(intDef); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics); + + when(intDef.getDevName()).thenReturn("eth0"); + when(intDef.getBrName()).thenReturn("br0"); + when(intDef.getMacAddress()).thenReturn("00:00:00:00"); + + when(nic.getMac()).thenReturn("00:00:00:01"); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn); + when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm); + + when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver); + + when(vifDriver.plug(nic, "Other PV", "")).thenReturn(interfaceDef); + when(interfaceDef.toString()).thenReturn("Interface"); + + final String interfaceDefStr = interfaceDef.toString(); + doNothing().when(vm).attachDevice(interfaceDefStr); + + } catch (final LibvirtException e) { + fail(e.getMessage()); + } catch (final InternalErrorException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName); + verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType()); + verify(vifDriver, times(1)).plug(nic, "Other PV", ""); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } catch (final InternalErrorException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testPlugNicCommandLibvirtException() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + final Type vmtype = Type.DomainRouter; + + final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testPlugNicCommandInternalError() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + final Type vmtype = Type.DomainRouter; + + final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + final Connect conn = Mockito.mock(Connect.class); + final Domain vm = Mockito.mock(Domain.class); + final VifDriver vifDriver = Mockito.mock(VifDriver.class); + + final List nics = new ArrayList(); + final InterfaceDef intDef = Mockito.mock(InterfaceDef.class); + nics.add(intDef); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics); + + when(intDef.getDevName()).thenReturn("eth0"); + when(intDef.getBrName()).thenReturn("br0"); + when(intDef.getMacAddress()).thenReturn("00:00:00:00"); + + when(nic.getMac()).thenReturn("00:00:00:01"); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn); + when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm); + + when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver); + + when(vifDriver.plug(nic, "Other PV", "")).thenThrow(InternalErrorException.class); + + } catch (final LibvirtException e) { + fail(e.getMessage()); + } catch (final InternalErrorException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName); + verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType()); + verify(vifDriver, times(1)).plug(nic, "Other PV", ""); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } catch (final InternalErrorException e) { + fail(e.getMessage()); + } + } + + @Test + public void testUnPlugNicCommandMatchMack() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + + final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + final Connect conn = Mockito.mock(Connect.class); + final Domain vm = Mockito.mock(Domain.class); + final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class); + + final List nics = new ArrayList(); + final InterfaceDef intDef = Mockito.mock(InterfaceDef.class); + nics.add(intDef); + + final VifDriver vifDriver = Mockito.mock(VifDriver.class); + final List drivers = new ArrayList(); + drivers.add(vifDriver); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics); + + when(intDef.getDevName()).thenReturn("eth0"); + when(intDef.getBrName()).thenReturn("br0"); + when(intDef.getMacAddress()).thenReturn("00:00:00:00"); + + when(nic.getMac()).thenReturn("00:00:00:00"); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn); + when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm); + + when(interfaceDef.toString()).thenReturn("Interface"); + + final String interfaceDefStr = interfaceDef.toString(); + doNothing().when(vm).detachDevice(interfaceDefStr); + + when(libvirtComputingResource.getAllVifDrivers()).thenReturn(drivers); + + doNothing().when(vifDriver).unplug(intDef); + + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName); + verify(libvirtComputingResource, times(1)).getAllVifDrivers(); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @Test + public void testUnPlugNicCommandNoNics() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + + final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + final Connect conn = Mockito.mock(Connect.class); + final Domain vm = Mockito.mock(Domain.class); + + final List nics = new ArrayList(); + + final VifDriver vifDriver = Mockito.mock(VifDriver.class); + final List drivers = new ArrayList(); + drivers.add(vifDriver); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn); + when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testUnPlugNicCommandLibvirtException() { + final NicTO nic = Mockito.mock(NicTO.class); + final String instanceName = "Test"; + + final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName); + + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getVmName()); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @Test + public void testNetworkUsageCommandNonVpc() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = false; + final String gatewayIP = "10.1.1.1"; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, forVpc, gatewayIP); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.getNetworkStats(command.getPrivateIP())).thenReturn(new long[]{10l, 10l}); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(2)).getNetworkStats(command.getPrivateIP()); + } + + @Test + public void testNetworkUsageCommandNonVpcCreate() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = false; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, "create", forVpc); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.networkUsage(command.getPrivateIP(), "create", null)).thenReturn("SUCCESS"); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(1)).networkUsage(command.getPrivateIP(), "create", null); + } + + @Test + public void testNetworkUsageCommandVpcCreate() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = true; + final String gatewayIP = "10.1.1.1"; + final String vpcCidr = "10.1.1.0/24"; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, forVpc, gatewayIP, vpcCidr); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), "create", command.getVpcCIDR())).thenReturn("SUCCESS"); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(1)).configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), "create", command.getVpcCIDR()); + } + + @Test + public void testNetworkUsageCommandVpcGet() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = true; + final String gatewayIP = "10.1.1.1"; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, forVpc, gatewayIP); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.getVPCNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption())).thenReturn(new long[]{10l, 10l}); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(1)).getVPCNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption()); + } + + @Test + public void testNetworkUsageCommandVpcVpn() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = true; + final String gatewayIP = "10.1.1.1"; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, "vpn", forVpc, gatewayIP); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.getVPCNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption())).thenReturn(new long[]{10l, 10l}); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(1)).getVPCNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption()); + } + + @Test + public void testNetworkUsageCommandVpcNoOption() { + final String privateIP = "169.16.16.16"; + final String domRName = "domR"; + final boolean forVpc = true; + final String gatewayIP = "10.1.1.1"; + + final NetworkUsageCommand command = new NetworkUsageCommand(privateIP, domRName, null, forVpc, gatewayIP); + + libvirtComputingResource.getNetworkStats(command.getPrivateIP()); + + when(libvirtComputingResource.configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR())).thenReturn("FAILURE"); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + //Being called twice, although I did not find the second place yet. + verify(libvirtComputingResource, times(1)).configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR()); + } } \ No newline at end of file