use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method prepareNetworkElementCommand.
protected ExecutionResult prepareNetworkElementCommand(final UpdateNetworkOverviewCommand cmd) {
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(routerName);
final Map<String, Integer> bridgeToNicNum = new HashMap<>();
final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
buildBridgeToNicNumHashMap(bridgeToNicNum, pluggedNics);
return new ExecutionResult(true, null);
} catch (final LibvirtException e) {
logger.error("Ip Assoc failure on applying one ip due to exception: ", e);
return new ExecutionResult(false, e.getMessage());
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method buildBridgeToNicNumHashMap.
private Integer buildBridgeToNicNumHashMap(final Map<String, Integer> bridgeToNicNum, final List<InterfaceDef> pluggedNics) {
Integer devNum = 0;
for (final InterfaceDef pluggedNic : pluggedNics) {
final String pluggedVlan = pluggedNic.getBrName();
if (pluggedVlan.equalsIgnoreCase(getLinkLocalBridgeName()) || pluggedVlan.equalsIgnoreCase(getPublicBridgeName()) || pluggedVlan.equalsIgnoreCase(getPrivBridgeName()) || pluggedVlan.equalsIgnoreCase(getGuestBridgeName())) {
bridgeToNicNum.put(pluggedVlan, devNum);
}
devNum++;
}
return devNum;
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
the class LibvirtDomainXmlParser method parseDomainXml.
public boolean parseDomainXml(final String domXml) {
final DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final InputSource is = new InputSource();
is.setCharacterStream(new StringReader(domXml));
final Document doc = builder.parse(is);
final Element rootElement = doc.getDocumentElement();
this.desc = getTagValue("description", rootElement);
final Element devices = (Element) rootElement.getElementsByTagName("devices").item(0);
final NodeList disks = devices.getElementsByTagName("disk");
for (int i = 0; i < disks.getLength(); i++) {
final Element disk = (Element) disks.item(i);
final String type = disk.getAttribute("type");
final LibvirtDiskDef def = new LibvirtDiskDef();
if (type.equalsIgnoreCase("network")) {
final String diskFormatType = getAttrValue("driver", "type", disk);
final String diskCacheMode = getAttrValue("driver", "cache", disk);
final String diskPath = getAttrValue("source", "name", disk);
final String protocol = getAttrValue("source", "protocol", disk);
final String authUserName = getAttrValue("auth", "username", disk);
final String poolUuid = getAttrValue("secret", "uuid", disk);
final String host = getAttrValue("host", "name", disk);
final int port = Integer.parseInt(getAttrValue("host", "port", disk));
final String diskLabel = getAttrValue("target", "dev", disk);
final String bus = getAttrValue("target", "bus", disk);
ImageFormat imageFormat = null;
if (diskFormatType != null) {
imageFormat = ImageFormat.valueOf(diskFormatType.toUpperCase());
}
def.defNetworkBasedDisk(diskPath, host, port, authUserName, poolUuid, diskLabel, DiskControllerType.valueOf(bus.toUpperCase()), LibvirtDiskDef.DiskProtocol.valueOf(protocol.toUpperCase()), imageFormat);
def.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(diskCacheMode.toUpperCase()));
} else {
final String diskFormatType = getAttrValue("driver", "type", disk);
final String diskCacheMode = getAttrValue("driver", "cache", disk);
final String diskFile = getAttrValue("source", "file", disk);
final String diskDev = getAttrValue("source", "dev", disk);
final String diskLabel = getAttrValue("target", "dev", disk);
final String bus = getAttrValue("target", "bus", disk);
final String device = disk.getAttribute("device");
if (type.equalsIgnoreCase("file")) {
if (device.equalsIgnoreCase("disk")) {
ImageFormat imageFormat = null;
if (diskFormatType != null) {
imageFormat = ImageFormat.valueOf(diskFormatType.toUpperCase());
}
def.defFileBasedDisk(diskFile, diskLabel, DiskControllerType.valueOf(bus.toUpperCase()), imageFormat);
} else if (device.equalsIgnoreCase("cdrom")) {
def.defIsoDisk(diskFile);
}
} else if (type.equalsIgnoreCase("block")) {
def.defBlockBasedDisk(diskDev, diskLabel, DiskControllerType.valueOf(bus.toUpperCase()));
}
if (diskCacheMode != null) {
def.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(diskCacheMode.toUpperCase()));
}
}
final NodeList iotune = disk.getElementsByTagName("iotune");
if (iotune != null && iotune.getLength() != 0) {
final String bytesReadRateStr = getTagValue("read_bytes_sec", (Element) iotune.item(0));
if (bytesReadRateStr != null) {
final Long bytesReadRate = Long.parseLong(bytesReadRateStr);
def.setBytesReadRate(bytesReadRate);
}
final String bytesWriteRateStr = getTagValue("write_bytes_sec", (Element) iotune.item(0));
if (bytesWriteRateStr != null) {
final Long bytesWriteRate = Long.parseLong(bytesWriteRateStr);
def.setBytesWriteRate(bytesWriteRate);
}
final String iopsReadRateStr = getTagValue("read_iops_sec", (Element) iotune.item(0));
if (iopsReadRateStr != null) {
final Long iopsReadRate = Long.parseLong(iopsReadRateStr);
def.setIopsReadRate(iopsReadRate);
}
final String iopsWriteRateStr = getTagValue("write_iops_sec", (Element) iotune.item(0));
if (iopsWriteRateStr != null) {
final Long iopsWriteRate = Long.parseLong(iopsWriteRateStr);
def.setIopsWriteRate(iopsWriteRate);
}
final String iopsTotalRateStr = getTagValue("total_iops_sec", (Element) iotune.item(0));
if (iopsTotalRateStr != null) {
final Long iopsTotalRate = Long.parseLong(iopsTotalRateStr);
def.setIopsTotalRate(iopsTotalRate);
}
}
this.diskDefs.add(def);
}
final NodeList nics = devices.getElementsByTagName("interface");
for (int i = 0; i < nics.getLength(); i++) {
final Element nic = (Element) nics.item(i);
final String type = nic.getAttribute("type");
final String mac = getAttrValue("mac", "address", nic);
final String dev = getAttrValue("target", "dev", nic);
final String model = getAttrValue("model", "type", nic);
final InterfaceDef def = new InterfaceDef();
final NodeList bandwidth = nic.getElementsByTagName("bandwidth");
Integer networkRateKBps = 0;
if (bandwidth != null && bandwidth.getLength() != 0) {
final Integer inbound = Integer.valueOf(getAttrValue("inbound", "average", (Element) bandwidth.item(0)));
final Integer outbound = Integer.valueOf(getAttrValue("outbound", "average", (Element) bandwidth.item(0)));
if (inbound.equals(outbound)) {
networkRateKBps = inbound;
}
}
if (type.equalsIgnoreCase("network")) {
final String network = getAttrValue("source", "network", nic);
def.defPrivateNet(network, dev, mac, NicModel.valueOf(model.toUpperCase()), networkRateKBps);
} else if (type.equalsIgnoreCase("bridge")) {
final String bridge = getAttrValue("source", "bridge", nic);
def.defBridgeNet(bridge, dev, mac, NicModel.valueOf(model.toUpperCase()), networkRateKBps);
} else if (type.equalsIgnoreCase("ethernet")) {
final String scriptPath = getAttrValue("script", "path", nic);
def.defEthernet(dev, mac, NicModel.valueOf(model.toUpperCase()), scriptPath, networkRateKBps);
}
this.interfaces.add(def);
}
final Element graphic = (Element) devices.getElementsByTagName("graphics").item(0);
if (graphic != null) {
final String port = graphic.getAttribute("port");
if (port != null) {
try {
this.vncPort = Integer.parseInt(port);
if (this.vncPort != -1) {
this.vncPort = this.vncPort - 5900;
} else {
this.vncPort = null;
}
} catch (final NumberFormatException nfe) {
this.vncPort = null;
}
}
}
final NodeList rngs = devices.getElementsByTagName("rng");
for (int i = 0; i < rngs.getLength(); i++) {
RngDef def = null;
final Element rng = (Element) rngs.item(i);
final String backendModel = getAttrValue("backend", "model", rng);
final String path = getTagValue("backend", rng);
if (Strings.isNullOrEmpty(backendModel)) {
def = new RngDef(path);
} else {
def = new RngDef(path, RngBackendModel.valueOf(backendModel.toUpperCase()));
}
this.rngDefs.add(def);
}
final NodeList watchDogs = devices.getElementsByTagName("watchdog");
for (int i = 0; i < watchDogs.getLength(); i++) {
WatchDogDef def = null;
final Element watchDog = (Element) watchDogs.item(i);
final String action = watchDog.getAttribute("action");
final String model = watchDog.getAttribute("model");
if (Strings.isNullOrEmpty(action)) {
def = new WatchDogDef(WatchDogModel.valueOf(model.toUpperCase()));
} else {
def = new WatchDogDef(WatchDogAction.valueOf(action.toUpperCase()), WatchDogModel.valueOf(model.toUpperCase()));
}
this.watchDogDefs.add(def);
}
return true;
} catch (final ParserConfigurationException e) {
s_logger.debug(e.toString());
} catch (final SAXException e) {
s_logger.debug(e.toString());
} catch (final IOException e) {
s_logger.debug(e.toString());
}
return false;
}
use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.
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<>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
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);
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);
when(interfaceDef.toString()).thenReturn("Interface");
final String interfaceDefStr = interfaceDef.toString();
doNothing().when(vm).detachDevice(interfaceDefStr);
when(this.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, 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);
verify(this.libvirtComputingResource, times(1)).getAllVifDrivers();
} 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 testGetVmStat.
@Test
public void testGetVmStat() throws LibvirtException {
final Connect connect = Mockito.mock(Connect.class);
final Domain domain = Mockito.mock(Domain.class);
final DomainInfo domainInfo = new DomainInfo();
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
final NodeInfo nodeInfo = new NodeInfo();
nodeInfo.cpus = 8;
nodeInfo.memory = 8 * 1024 * 1024;
nodeInfo.sockets = 2;
nodeInfo.threads = 2;
nodeInfo.model = "Foo processor";
Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
// this is testing the interface stats, returns an increasing number of sent and received bytes
Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {
// increment with less than a KB, so this should be less than 1 KB
static final int increment = 1000;
int rxBytes = 1000;
int txBytes = 1000;
@Override
public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
domainInterfaceStats.rx_bytes = this.rxBytes += increment;
domainInterfaceStats.tx_bytes = this.txBytes += increment;
return domainInterfaceStats;
}
});
Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {
// a little less than a KB
static final int increment = 1000;
int rdBytes = 0;
int wrBytes = 1024;
@Override
public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainBlockStats domainBlockStats = new DomainBlockStats();
domainBlockStats.rd_bytes = this.rdBytes += increment;
domainBlockStats.wr_bytes = this.wrBytes += increment;
return domainBlockStats;
}
});
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {
@Override
public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
final InterfaceDef interfaceDef = new InterfaceDef();
return Arrays.asList(interfaceDef);
}
@Override
public List<LibvirtDiskDef> getDisks(final Connect conn, final String vmName) {
final LibvirtDiskDef diskDef = new LibvirtDiskDef();
return Arrays.asList(diskDef);
}
};
libvirtComputingResource.getVmStat(connect, VMNAME);
final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
// network traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
// IO traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
}
Aggregations