Search in sources :

Example 1 with ImageFormat

use of com.cloud.model.enumeration.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNODisk.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNODisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StoragePool(com.cloud.legacymodel.storage.StoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.legacymodel.communication.command.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.model.enumeration.ImageFormat) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 2 with ImageFormat

use of com.cloud.model.enumeration.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNOQcow2.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    final List<KvmPhysicalDisk> spiedDisks = Mockito.spy(disks);
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks);
    when(spiedDisks.isEmpty()).thenReturn(false);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StoragePool(com.cloud.legacymodel.storage.StoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.legacymodel.communication.command.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.model.enumeration.ImageFormat) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 3 with ImageFormat

use of com.cloud.model.enumeration.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandTemplateNoDisk.

@Test(expected = NullPointerException.class)
public void testPrimaryStorageDownloadCommandTemplateNoDisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final String name = "Test";
    final String url = "http://template/template.qcow2";
    final ImageFormat format = ImageFormat.VHD;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.getPhysicalDisk("template.qcow2")).thenReturn(tmplVol);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), command.getPoolUuid());
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) NfsStoragePool(com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StoragePool(com.cloud.legacymodel.storage.StoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.legacymodel.communication.command.PrimaryStorageDownloadCommand) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) ImageFormat(com.cloud.model.enumeration.ImageFormat) Test(org.junit.Test)

Example 4 with ImageFormat

use of com.cloud.model.enumeration.ImageFormat 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;
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Document(org.w3c.dom.Document) ImageFormat(com.cloud.model.enumeration.ImageFormat) SAXException(org.xml.sax.SAXException) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef) RngDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.RngDef) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) WatchDogDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.WatchDogDef) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with ImageFormat

use of com.cloud.model.enumeration.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtVMDefTest method testDiskDef.

public void testDiskDef() {
    final String filePath = "/var/lib/libvirt/images/disk.qcow2";
    final String diskLabel = "sda";
    final int deviceId = 1;
    final LibvirtDiskDef disk = new LibvirtDiskDef();
    final DiskControllerType bus = DiskControllerType.SCSI;
    final ImageFormat imageFormat = ImageFormat.QCOW2;
    final LibvirtDiskDef.DiskCacheMode cacheMode = LibvirtDiskDef.DiskCacheMode.WRITEBACK;
    disk.defFileBasedDisk(filePath, diskLabel, bus, imageFormat);
    disk.setCacheMode(cacheMode);
    disk.setDeviceId(deviceId);
    assertEquals(filePath, disk.getDiskPath());
    assertEquals(diskLabel, disk.getDiskLabel());
    assertEquals(bus.toString().toLowerCase(), disk.getBusType().toString().toLowerCase());
    assertEquals(LibvirtDiskDef.DeviceType.DISK, disk.getDeviceType());
    final String xmlDef = disk.toString();
    final String expectedXml = "<disk  device='disk' type='file'>\n<driver name='qemu' type='" + imageFormat.toString().toLowerCase() + "' cache='" + cacheMode.toString() + "' />\n" + "<source file='" + filePath + "'/>\n<target dev='" + diskLabel + "' bus='" + bus.toString().toLowerCase() + "'/>\n" + "<address type='drive' controller='0' bus='0' target='0' unit='1'/></disk>\n";
    assertEquals(xmlDef, expectedXml);
}
Also used : DiskControllerType(com.cloud.model.enumeration.DiskControllerType) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) ImageFormat(com.cloud.model.enumeration.ImageFormat)

Aggregations

ImageFormat (com.cloud.model.enumeration.ImageFormat)16 Answer (com.cloud.legacymodel.communication.answer.Answer)5 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)5 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)5 NfsStoragePool (com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool)4 KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)4 KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)4 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)4 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)4 DiskControllerType (com.cloud.model.enumeration.DiskControllerType)4 ArrayList (java.util.ArrayList)4 LibvirtDiskDef (com.cloud.agent.resource.kvm.xml.LibvirtDiskDef)3 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)3 PrimaryStorageDownloadCommand (com.cloud.legacymodel.communication.command.PrimaryStorageDownloadCommand)3 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)3 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)3 StoragePool (com.cloud.legacymodel.storage.StoragePool)3 Account (com.cloud.legacymodel.user.Account)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 IOException (java.io.IOException)3