use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.
the class LibvirtReplugNicCommandWrapper method execute.
@Override
public Answer execute(final ReplugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
final NicTO nic = command.getNic();
final String vmName = command.getVmName();
Domain vm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
vm = libvirtComputingResource.getDomain(conn, vmName);
InterfaceDef oldPluggedNic = findPluggedNic(libvirtComputingResource, nic, vmName, conn);
final VifDriver newVifDriver = libvirtComputingResource.getVifDriver(nic.getType(), nic.getName());
final InterfaceDef interfaceDef = newVifDriver.plug(nic, "Other PV", oldPluggedNic.getModel().toString(), null);
interfaceDef.setSlot(oldPluggedNic.getSlot());
interfaceDef.setDevName(oldPluggedNic.getDevName());
interfaceDef.setLinkStateUp(false);
oldPluggedNic.setSlot(null);
int i = 0;
do {
i++;
s_logger.debug("ReplugNic: Detaching interface" + oldPluggedNic + " (Attempt: " + i + ")");
vm.detachDevice(oldPluggedNic.toString());
} while (findPluggedNic(libvirtComputingResource, nic, vmName, conn) != null && i <= 10);
s_logger.debug("ReplugNic: Attaching interface" + interfaceDef);
vm.attachDevice(interfaceDef.toString());
interfaceDef.setLinkStateUp(true);
s_logger.debug("ReplugNic: Updating interface" + interfaceDef);
vm.updateDeviceFlags(interfaceDef.toString(), DomainAffect.LIVE.getValue());
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(oldPluggedNic, true);
}
return new ReplugNicAnswer(command, true, "success");
} catch (final LibvirtException | InternalErrorException e) {
final String msg = " Plug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new ReplugNicAnswer(command, false, msg);
} finally {
if (vm != null) {
try {
vm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.
the class LibvirtReplugNicCommandWrapper method findPluggedNic.
private InterfaceDef findPluggedNic(LibvirtComputingResource libvirtComputingResource, NicTO nic, String vmName, Connect conn) {
InterfaceDef oldPluggedNic = null;
final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
for (final InterfaceDef pluggedNic : pluggedNics) {
if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
oldPluggedNic = pluggedNic;
}
}
return oldPluggedNic;
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.
the class LibvirtSecurityGroupRulesCommandWrapper method execute.
@Override
public Answer execute(final SecurityGroupRulesCmd command, final LibvirtComputingResource libvirtComputingResource) {
String vif = null;
String brname = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName());
vif = nics.get(0).getDevName();
brname = nics.get(0).getBrName();
final VirtualMachineTO vm = command.getVmTO();
if (!libvirtComputingResource.applyDefaultNetworkRules(conn, vm, true)) {
s_logger.warn("Failed to program default network rules for vm " + command.getVmName());
return new SecurityGroupRuleAnswer(command, false, "programming default network rules failed");
}
} catch (final LibvirtException e) {
return new SecurityGroupRuleAnswer(command, false, e.toString());
}
final boolean result = libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getGuestIp6(), command.getSignature(), Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString());
if (!result) {
s_logger.warn("Failed to program network rules for vm " + command.getVmName());
return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
} else {
s_logger.debug("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ",ingress numrules=" + command.getIngressRuleSet().size() + ",egress numrules=" + command.getEgressRuleSet().size());
return new SecurityGroupRuleAnswer(command);
}
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.
the class LibvirtComputingResourceTest method testUnPlugNicCommandMatchMack.
@Test
public void testUnPlugNicCommandMatchMack() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<VifDriver> drivers = new ArrayList<VifDriver>();
drivers.add(vifDriver);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
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(libvirtUtilitiesHelper.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, true);
} 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)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
verify(libvirtComputingResource, times(1)).getAllVifDrivers();
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.
the class LibvirtComputingResourceTest method testUnPlugNicCommandNoNics.
@Test
public void testUnPlugNicCommandNoNics() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<VifDriver> drivers = new ArrayList<VifDriver>();
drivers.add(vifDriver);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
try {
when(libvirtUtilitiesHelper.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)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
Aggregations