use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
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<>();
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<VifDriver> drivers = new ArrayList<>();
drivers.add(vifDriver);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(this.libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(this.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, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPlugNicCommandMatchMack.
@Test
public void testPlugNicCommandMatchMack() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final VirtualMachineType vmtype = VirtualMachineType.DomainRouter;
final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
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<>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(this.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(this.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, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.
@Test
public void testPlugNicCommandInternalError() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final VirtualMachineType vmtype = VirtualMachineType.DomainRouter;
final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<InterfaceDef> nics = new ArrayList<>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(this.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(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(this.libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
when(this.libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
when(vifDriver.plug(nic, "Default - VirtIO capable OS (64-bit)", "")).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, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
verify(this.libvirtComputingResource, times(1)).getVifDriver(nic.getType());
verify(vifDriver, times(1)).plug(nic, "Default - VirtIO capable OS (64-bit)", "");
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class OvsVifDriver method plug.
@Override
public InterfaceDef plug(final NicTO nic, final String guestOsType, final String nicAdapter) throws InternalErrorException, LibvirtException {
this.logger.debug("plugging nic=" + nic);
final LibvirtVmDef.InterfaceDef intf = new LibvirtVmDef.InterfaceDef();
intf.setVirtualPortType("openvswitch");
String vlanId = null;
String logicalSwitchUuid = null;
if (nic.getBroadcastType() == BroadcastDomainType.Vlan) {
vlanId = BroadcastDomainType.getValue(nic.getBroadcastUri());
} else if (nic.getBroadcastType() == BroadcastDomainType.Lswitch) {
logicalSwitchUuid = BroadcastDomainType.getValue(nic.getBroadcastUri());
} else if (nic.getBroadcastType() == BroadcastDomainType.Pvlan) {
// TODO consider moving some of this functionality from NetUtils to Networks....
vlanId = NetUtils.getPrimaryPvlanFromUri(nic.getBroadcastUri());
}
final String trafficLabel = nic.getName();
if (nic.getType() == TrafficType.Guest) {
final Integer networkRateKBps = nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1 ? nic.getNetworkRateMbps().intValue() * 128 : 0;
if ((nic.getBroadcastType() == BroadcastDomainType.Vlan || nic.getBroadcastType() == BroadcastDomainType.Pvlan) && !vlanId.equalsIgnoreCase("untagged")) {
if (trafficLabel != null && !trafficLabel.isEmpty()) {
this.logger.debug("creating a vlan dev and bridge for guest traffic per traffic label " + trafficLabel);
intf.defBridgeNet(this.pifs.get(trafficLabel), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
intf.setVlanTag(Integer.parseInt(vlanId));
} else {
intf.defBridgeNet(this.pifs.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
intf.setVlanTag(Integer.parseInt(vlanId));
}
} else if (nic.getBroadcastType() == BroadcastDomainType.Lswitch) {
this.logger.debug("nic " + nic + " needs to be connected to LogicalSwitch " + logicalSwitchUuid);
intf.setVirtualPortInterfaceId(nic.getUuid());
final String brName = trafficLabel != null && !trafficLabel.isEmpty() ? this.pifs.get(trafficLabel) : this.pifs.get("private");
intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
} else if (nic.getBroadcastType() == BroadcastDomainType.Vswitch) {
final String vnetId = BroadcastDomainType.getValue(nic.getBroadcastUri());
final String brName = "OVSTunnel" + vnetId;
this.logger.debug("nic " + nic + " needs to be connected to LogicalSwitch " + brName);
intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
} else if (nic.getBroadcastType() == BroadcastDomainType.Vsp) {
intf.setVirtualPortInterfaceId(nic.getUuid());
final String brName = trafficLabel != null && !trafficLabel.isEmpty() ? this.pifs.get(trafficLabel) : this.pifs.get("private");
intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
} else {
intf.defBridgeNet(this.bridges.get("guest"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
}
} else if (nic.getType() == TrafficType.Control) {
/* Make sure the network is still there */
createControlNetwork(this.bridges.get("linklocal"));
intf.defBridgeNet(this.bridges.get("linklocal"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
} else if (nic.getType() == TrafficType.Public) {
final Integer networkRateKBps = nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1 ? nic.getNetworkRateMbps().intValue() * 128 : 0;
if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
if (trafficLabel != null && !trafficLabel.isEmpty()) {
this.logger.debug("creating a vlan dev and bridge for public traffic per traffic label " + trafficLabel);
intf.defBridgeNet(this.pifs.get(trafficLabel), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
intf.setVlanTag(Integer.parseInt(vlanId));
} else {
intf.defBridgeNet(this.pifs.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
intf.setVlanTag(Integer.parseInt(vlanId));
}
} else {
intf.defBridgeNet(this.bridges.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
}
} else if (nic.getType() == TrafficType.Management) {
intf.defBridgeNet(this.bridges.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
} else if (nic.getType() == TrafficType.Storage) {
final String storageBrName = nic.getName() == null ? this.bridges.get("private") : nic.getName();
intf.defBridgeNet(storageBrName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
}
return intf;
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPvlanSetupCommandDhcpAdd.
@Test
public void testPvlanSetupCommandDhcpAdd() {
final String op = "add";
final URI uri = URI.create("http://localhost");
final String networkTag = "/105";
final String dhcpName = "dhcp";
final String dhcpMac = "00:00:00:00";
final String dhcpIp = "127.0.0.1";
final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final String guestBridgeName = "br0";
when(this.libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
final int timeout = 0;
when(this.libvirtComputingResource.getScriptsTimeout()).thenReturn(timeout);
final String ovsPvlanDhcpHostPath = "/pvlan";
when(this.libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
final List<InterfaceDef> ifaces = new ArrayList<>();
final InterfaceDef nic = Mockito.mock(InterfaceDef.class);
ifaces.add(nic);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenReturn(conn);
when(this.libvirtComputingResource.getInterfaces(conn, dhcpName)).thenReturn(ifaces);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(dhcpName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
Aggregations