summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bentele2021-01-29 12:23:33 +0100
committerManuel Bentele2021-01-29 12:23:33 +0100
commitf013f11be3783b5818c49b0b0265f912dd379973 (patch)
treed7d9290012b897d1f922f31243197fc38f754210
parentAdd base classes and utilites to represent Libvirt XML documents (diff)
downloadmaster-sync-shared-f013f11be3783b5818c49b0b0265f912dd379973.tar.gz
master-sync-shared-f013f11be3783b5818c49b0b0265f912dd379973.tar.xz
master-sync-shared-f013f11be3783b5818c49b0b0265f912dd379973.zip
Add implementation of Libvirt domain XML documents
-rw-r--r--src/main/java/org/openslx/libvirt/domain/Domain.java1017
-rw-r--r--src/main/java/org/openslx/libvirt/domain/DomainUtils.java118
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Controller.java181
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerFloppy.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerIde.java128
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerPci.java145
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerSata.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerScsi.java138
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/ControllerUsb.java139
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Device.java205
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Disk.java427
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/DiskCdrom.java55
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/DiskFloppy.java52
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/DiskStorage.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Graphics.java138
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/GraphicsSdl.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java74
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/GraphicsVnc.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Hostdev.java139
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java79
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java52
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Interface.java316
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/InterfaceBridge.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/InterfaceNetwork.java54
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Sound.java128
-rw-r--r--src/main/java/org/openslx/libvirt/domain/device/Video.java188
-rw-r--r--src/test/java/org/openslx/libvirt/domain/DomainTest.java294
27 files changed, 4391 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java
new file mode 100644
index 0000000..35cd012
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/Domain.java
@@ -0,0 +1,1017 @@
+package org.openslx.libvirt.domain;
+
+import java.io.File;
+import java.io.InputStream;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.openslx.libvirt.domain.device.Device;
+import org.openslx.libvirt.domain.device.Controller;
+import org.openslx.libvirt.domain.device.ControllerFloppy;
+import org.openslx.libvirt.domain.device.ControllerIde;
+import org.openslx.libvirt.domain.device.ControllerPci;
+import org.openslx.libvirt.domain.device.ControllerSata;
+import org.openslx.libvirt.domain.device.ControllerScsi;
+import org.openslx.libvirt.domain.device.ControllerUsb;
+import org.openslx.libvirt.domain.device.Disk;
+import org.openslx.libvirt.domain.device.DiskCdrom;
+import org.openslx.libvirt.domain.device.DiskFloppy;
+import org.openslx.libvirt.domain.device.DiskStorage;
+import org.openslx.libvirt.domain.device.Graphics;
+import org.openslx.libvirt.domain.device.GraphicsSdl;
+import org.openslx.libvirt.domain.device.GraphicsSpice;
+import org.openslx.libvirt.domain.device.GraphicsVnc;
+import org.openslx.libvirt.domain.device.Hostdev;
+import org.openslx.libvirt.domain.device.Interface;
+import org.openslx.libvirt.domain.device.InterfaceBridge;
+import org.openslx.libvirt.domain.device.InterfaceNetwork;
+import org.openslx.libvirt.domain.device.Sound;
+import org.openslx.libvirt.domain.device.Video;
+import org.openslx.libvirt.xml.LibvirtXmlDocument;
+import org.openslx.libvirt.xml.LibvirtXmlDocumentException;
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+import org.openslx.libvirt.xml.LibvirtXmlSerializationException;
+import org.openslx.libvirt.xml.LibvirtXmlValidationException;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+/**
+ * Implementation of the Libvirt domain XML document.
+ *
+ * The Libvirt domain XML document is used to describe virtual machines and their configurations.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Domain extends LibvirtXmlDocument
+{
+ /**
+ * Creates Libvirt domain XML document from {@link String} providing Libvirt domain XML content.
+ *
+ * @param xml {@link String} with Libvirt domain XML content.
+ *
+ * @throws LibvirtXmlDocumentException creation of XML context failed.
+ * @throws LibvirtXmlSerializationException serialization of the domain XML content failed.
+ * @throws LibvirtXmlValidationException XML content is not a valid domain XML document.
+ */
+ public Domain( String xml )
+ throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
+ {
+ super( xml );
+ }
+
+ /**
+ * Creates Libvirt domain XML document from {@link File} containing Libvirt domain XML content.
+ *
+ * @param xml existing {@link File} containing Libvirt domain XML content.
+ *
+ * @throws LibvirtXmlDocumentException creation of XML context failed.
+ * @throws LibvirtXmlSerializationException serialization of the domain XML content failed.
+ * @throws LibvirtXmlValidationException XML content is not a valid domain XML document.
+ */
+ public Domain( File xml )
+ throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
+ {
+ super( xml );
+ }
+
+ /**
+ * Creates Libvirt domain XML document from {@link InputStream} providing Libvirt domain XML
+ * content.
+ *
+ * @param xml {@link InputStream} providing Libvirt domain XML content.
+ *
+ * @throws LibvirtXmlDocumentException creation of XML context failed.
+ * @throws LibvirtXmlSerializationException serialization of the domain XML content failed.
+ * @throws LibvirtXmlValidationException XML content is not a valid domain XML document.
+ */
+ public Domain( InputStream xml )
+ throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
+ {
+ super( xml );
+ }
+
+ /**
+ * Creates Libvirt domain XML document from {@link InputSource} providing Libvirt domain XML
+ * content.
+ *
+ * @param xml {@link InputSource} providing Libvirt domain XML content.
+ *
+ * @throws LibvirtXmlDocumentException creation of XML context failed.
+ * @throws LibvirtXmlSerializationException serialization of the domain XML content failed.
+ * @throws LibvirtXmlValidationException XML content is not a valid domain XML document.
+ */
+ public Domain( InputSource xml )
+ throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
+ {
+ super( xml );
+ }
+
+ /**
+ * Types of hypervisors specifiable for a virtual machine in the Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Type
+ {
+ // @formatter:off
+ QEMU ( "qemu" ),
+ KQEMU ( "kqemu" ),
+ KVM ( "kvm" ),
+ XEN ( "xen" ),
+ LXC ( "lxc" ),
+ UML ( "uml" ),
+ OPENVZ( "openvz" ),
+ TEST ( "test" ),
+ VMWARE( "vmware" ),
+ HYPERV( "hyperv" ),
+ VBOX ( "vbox" ),
+ PHYP ( "phyp" ),
+ VZ ( "vz" ),
+ BHYVE ( "bhyve" );
+ // @formatter:on
+
+ /**
+ * Name of the hypervisor in a Libvirt domain XML document.
+ */
+ private String type;
+
+ /**
+ * Creates a hypervisor type.
+ *
+ * @param type valid name of the hypervisor in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates a hypervisor type from its name with error check.
+ *
+ * @param type name of the hypervisor in the Libvirt domain XML document.
+ * @return valid hypervisor type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Returns hypervisor type defined in the Libvirt domain XML document.
+ *
+ * @return hypervisor type.
+ */
+ public Type getType()
+ {
+ String typeValue = this.getRootXmlNode().getXmlElementAttributeValue( null, "type" );
+ return Type.fromString( typeValue );
+ }
+
+ /**
+ * Sets hypervisor type in Libvirt domain XML document.
+ *
+ * @param type hypervisor type for Libvirt domain XML document.
+ */
+ public void setType( Type type )
+ {
+ this.getRootXmlNode().setXmlElementAttributeValue( null, "type", type.toString() );
+ }
+
+ /**
+ * Returns virtual machine name defined in the Libvirt domain XML document.
+ *
+ * @return name of the virtual machine.
+ */
+ public String getName()
+ {
+ return this.getRootXmlNode().getXmlElementValue( "name" );
+ }
+
+ /**
+ * Sets virtual machine name in the Libvirt domain XML document.
+ *
+ * @param name virtual machine name for Libvirt domain XML document.
+ */
+ public void setName( String name )
+ {
+ this.getRootXmlNode().setXmlElementValue( "name", name );
+ }
+
+ /**
+ * Returns virtual machine title defined in the Libvirt domain XML document.
+ *
+ * @return title of the virtual machine.
+ */
+ public String getTitle()
+ {
+ return this.getRootXmlNode().getXmlElementValue( "title" );
+ }
+
+ /**
+ * Sets virtual machine title in the Libvirt domain XML document.
+ *
+ * @param title virtual machine title for Libvirt domain XML document.
+ */
+ public void setTitle( String title )
+ {
+ this.getRootXmlNode().setXmlElementValue( "title", title );
+ }
+
+ /**
+ * Returns virtual machine description defined in the Libvirt domain XML document.
+ *
+ * @return description of virtual machine.
+ */
+ public String getDescription()
+ {
+ return this.getRootXmlNode().getXmlElementValue( "description" );
+ }
+
+ /**
+ * Sets virtual machine description in the Libvirt domain XML document.
+ *
+ * @param description virtual machine description for Libvirt domain XML document.
+ */
+ public void setDescription( String description )
+ {
+ this.getRootXmlNode().setXmlElementValue( "description", description );
+ }
+
+ /**
+ * Returns virtual machine UUID defined in the Libvirt domain XML document.
+ *
+ * @return UUID of virtual machine.
+ */
+ public String getUuid()
+ {
+ return this.getRootXmlNode().getXmlElementValue( "uuid" );
+ }
+
+ /**
+ * Sets virtual machine UUID in the Libvirt domain XML document.
+ *
+ * @param uuid virtual machine UUID for Libvirt domain XML document.
+ */
+ public void setUuid( String uuid )
+ {
+ this.getRootXmlNode().setXmlElementValue( "uuid", uuid );
+ }
+
+ /**
+ * Removes virtual machine UUID in the Libvirt domain XML document.
+ */
+ public void removeUuid()
+ {
+ this.getRootXmlNode().removeXmlElement( "uuid" );
+ }
+
+ /**
+ * Returns virtual machine memory defined in the Libvirt domain XML document.
+ *
+ * @return memory of virtual machine.
+ */
+ public BigInteger getMemory()
+ {
+ String memValue = this.getRootXmlNode().getXmlElementValue( "memory" );
+ String memUnit = this.getRootXmlNode().getXmlElementAttributeValue( "memory", "unit" );
+ return DomainUtils.decodeMemory( memValue, memUnit );
+ }
+
+ /**
+ * Sets virtual machine memory in the Libvirt domain XML document.
+ *
+ * @param memory virtual machine memory in the Libvirt domain XML document.
+ */
+ public void setMemory( BigInteger memory )
+ {
+ this.getRootXmlNode().setXmlElementAttributeValue( "memory", "unit", "KiB" );
+ this.getRootXmlNode().setXmlElementValue( "memory", DomainUtils.encodeMemory( memory, "KiB" ) );
+ }
+
+ /**
+ * Returns current virtual machine memory defined in the Libvirt domain XML document.
+ *
+ * @return current memory of virtual machine.
+ */
+ public BigInteger getCurrentMemory()
+ {
+ String memValue = this.getRootXmlNode().getXmlElementValue( "currentMemory" );
+ String memUnit = this.getRootXmlNode().getXmlElementAttributeValue( "currentMemory", "unit" );
+ return DomainUtils.decodeMemory( memValue, memUnit );
+ }
+
+ /**
+ * Set current virtual machine memory in the Libvirt domain XML document.
+ *
+ * @param currentMemory current virtual machine memory in the Libvirt domain XML document.
+ */
+ public void setCurrentMemory( BigInteger currentMemory )
+ {
+ this.getRootXmlNode().setXmlElementAttributeValue( "currentMemory", "unit", "KiB" );
+ this.getRootXmlNode().setXmlElementValue( "currentMemory", DomainUtils.encodeMemory( currentMemory, "KiB" ) );
+ }
+
+ /**
+ * Returns number of virtual machine CPUs defined in the Libvirt domain XML document.
+ *
+ * @return number of CPUs of the virtual machine.
+ */
+ public int getVCpu()
+ {
+ String number = this.getRootXmlNode().getXmlElementValue( "vcpu" );
+ return Integer.parseUnsignedInt( number );
+ }
+
+ /**
+ * Set number of virtual machine CPUs in the Libvirt domain XML document.
+ *
+ * @param number virtual machine CPUs.
+ */
+ public void setVCpu( int number )
+ {
+ this.getRootXmlNode().setXmlElementValue( "vcpu", Integer.toString( number ) );
+ }
+
+ /**
+ * Returns virtual machine CPU model defined in the Libvirt domain XML document.
+ *
+ * @return CPU model of virtual machine.
+ */
+ public String getCpuModel()
+ {
+ return this.getRootXmlNode().getXmlElementValue( "cpu/model" );
+ }
+
+ /**
+ * Sets virtual machine CPU model in the Libvirt domain XML document.
+ *
+ * @param model virtual machine CPU model.
+ */
+ public void setCpuModel( String model )
+ {
+ this.getRootXmlNode().setXmlElementValue( "cpu/model", model );
+ }
+
+ /**
+ * CPU modes specifiable for a virtual machine in the Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum CpuMode
+ {
+ // @formatter:off
+ CUSTOM ( "custom" ),
+ HOST_MODEL ( "host-model" ),
+ HOST_PASSTHROUGH( "host-passthrough" );
+ // @formatter:on
+
+ /**
+ * Name of the CPU mode in a Libvirt domain XML document.
+ */
+ private String cpuMode;
+
+ /**
+ * Creates a CPU mode.
+ *
+ * @param mode valid name of the CPU mode in the Libvirt domain XML document.
+ */
+ CpuMode( String mode )
+ {
+ this.cpuMode = mode;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.cpuMode;
+ }
+
+ /**
+ * Creates a CPU mode from its name with error check.
+ *
+ * @param mode name of the CPU mode in the Libvirt domain XML document.
+ * @return valid CPU mode.
+ */
+ public static CpuMode fromString( String mode )
+ {
+ for ( CpuMode t : CpuMode.values() ) {
+ if ( t.cpuMode.equalsIgnoreCase( mode ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Returns virtual machine CPU mode defined in the Libvirt domain XML document.
+ *
+ * @return CPU mode of the virtual machine.
+ */
+ public CpuMode getCpuMode()
+ {
+ String cpuMode = this.getRootXmlNode().getXmlElementAttributeValue( "cpu", "mode" );
+ return CpuMode.fromString( cpuMode );
+ }
+
+ /**
+ * Sets virtual machine CPU mode in the Libvirt domain XML document.
+ *
+ * @param mode virtual machine CPU mode.
+ */
+ public void setCpuMode( CpuMode mode )
+ {
+ this.getRootXmlNode().setXmlElementAttributeValue( "cpu", "mode", mode.toString() );
+ }
+
+ /**
+ * CPU checks specifiable for a virtual machine in the Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum CpuCheck
+ {
+ // @formatter:off
+ NONE ( "none" ),
+ PARTIAL( "partial" ),
+ FULL ( "full" );
+ // @formatter:on
+
+ /**
+ * Name of the CPU check in the Libvirt domain XML document.
+ */
+ private String cpuCheck;
+
+ /**
+ * Creates a CPU check.
+ *
+ * @param check valid name of the CPU check in the Libvirt domain XML document.
+ */
+ CpuCheck( String check )
+ {
+ this.cpuCheck = check;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.cpuCheck;
+ }
+
+ /**
+ * Creates a CPU check from its name with error check.
+ *
+ * @param mode name of the CPU check in the Libvirt domain XML document.
+ * @return valid CPU check.
+ */
+ public static CpuCheck fromString( String check )
+ {
+ for ( CpuCheck t : CpuCheck.values() ) {
+ if ( t.cpuCheck.equalsIgnoreCase( check ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Returns virtual machine CPU check defined in the Libvirt domain XML document.
+ *
+ * @return CPU check of the virtual machine.
+ */
+ public CpuCheck getCpuCheck()
+ {
+ String cpuCheck = this.getRootXmlNode().getXmlElementAttributeValue( "cpu", "check" );
+ return CpuCheck.fromString( cpuCheck );
+ }
+
+ /**
+ * Sets virtual machine CPU check in the Libvirt domain XML document.
+ *
+ * @param check virtual machine CPU check.
+ */
+ public void setCpuCheck( CpuCheck check )
+ {
+ this.getRootXmlNode().setXmlElementAttributeValue( "cpu", "check", check.toString() );
+ }
+
+ /**
+ * Returns virtual machine devices defined in the Libvirt domain XML document.
+ *
+ * @return devices of the virtual machine.
+ */
+ public ArrayList<Device> getDevices()
+ {
+ ArrayList<Device> devices = new ArrayList<Device>();
+ Node devicesNode = this.getRootXmlNode().getXmlElement( "devices" );
+
+ if ( devicesNode != null ) {
+
+ NodeList devicesElements = devicesNode.getChildNodes();
+
+ for ( int i = 0; i < devicesElements.getLength(); i++ ) {
+ LibvirtXmlNode deviceNode = null;
+ deviceNode = new LibvirtXmlNode( this.getRootXmlNode().getXmlDocument(), devicesElements.item( i ) );
+ Device device = Device.newInstance( deviceNode );
+
+ if ( device != null ) {
+ devices.add( device );
+ }
+ }
+ }
+
+ return devices;
+ }
+
+ /**
+ * Filter list of virtual machine devices of type {@link Device} and cast filtered instances to
+ * more specific device type <code>R</code>.
+ *
+ * @param <R> specific device type for filtering and casting.
+ * @param cls specific device type's class.
+ * @param devices list of virtual machines devices.
+ * @return filtered list of virtual machines devices of type <code>R</code>.
+ */
+ private static <R> ArrayList<R> filterDevices( Class<R> cls, ArrayList<Device> devices )
+ {
+ Predicate<Device> byFilter = device -> cls.isInstance( device );
+ Function<Device, R> castFunction = device -> cls.cast( device );
+
+ return devices.stream().filter( byFilter ).map( castFunction )
+ .collect( Collectors.toCollection( ArrayList::new ) );
+ }
+
+ /**
+ * Returns list of virtual machine controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine controller devices.
+ */
+ public ArrayList<Controller> getControllerDevices()
+ {
+ return Domain.filterDevices( Controller.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine floppy controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine floppy controller devices.
+ */
+ public ArrayList<ControllerFloppy> getFloppyControllerDevices()
+ {
+ return Domain.filterDevices( ControllerFloppy.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine IDE controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine IDE controller devices.
+ */
+ public ArrayList<ControllerIde> getIdeControllerDevices()
+ {
+ return Domain.filterDevices( ControllerIde.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine floppy controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine floppy controller devices.
+ */
+ public ArrayList<ControllerPci> getPciControllerDevices()
+ {
+ return Domain.filterDevices( ControllerPci.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine SATA controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine SATA controller devices.
+ */
+ public ArrayList<ControllerSata> getSataControllerDevices()
+ {
+ return Domain.filterDevices( ControllerSata.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine SCSI controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine SCSI controller devices.
+ */
+ public ArrayList<ControllerScsi> getScsiControllerDevices()
+ {
+ return Domain.filterDevices( ControllerScsi.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine USB controller devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine USB controller devices.
+ */
+ public ArrayList<ControllerUsb> getUsbControllerDevices()
+ {
+ return Domain.filterDevices( ControllerUsb.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine disk devices specified in the Libvirt domain XML document.
+ *
+ * @return list of virtual machine disk devices.
+ */
+ public ArrayList<Disk> getDiskDevices()
+ {
+ return Domain.filterDevices( Disk.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine disk CDROM devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine disk CDROM devices.
+ */
+ public ArrayList<DiskCdrom> getDiskCdromDevices()
+ {
+ return Domain.filterDevices( DiskCdrom.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine disk floppy devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine disk floppy devices.
+ */
+ public ArrayList<DiskFloppy> getDiskFloppyDevices()
+ {
+ return Domain.filterDevices( DiskFloppy.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine disk storage devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine disk storage devices.
+ */
+ public ArrayList<DiskStorage> getDiskStorageDevices()
+ {
+ return Domain.filterDevices( DiskStorage.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine hostdev devices specified in the Libvirt domain XML document.
+ *
+ * @return list of virtual machine hostdev devices.
+ */
+ public ArrayList<Hostdev> getHostdevDevices()
+ {
+ return Domain.filterDevices( Hostdev.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine network interface devices specified in the Libvirt domain XML
+ * document.
+ *
+ * @return list of virtual machine network interface devices.
+ */
+ public ArrayList<Interface> getInterfaceDevices()
+ {
+ return Domain.filterDevices( Interface.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine graphic devices specified in the Libvirt domain XML document.
+ *
+ * @return list of virtual machine graphic devices.
+ */
+ public ArrayList<Graphics> getGraphicDevices()
+ {
+ return Domain.filterDevices( Graphics.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine sound devices specified in the Libvirt domain XML document.
+ *
+ * @return list of virtual machine sound devices.
+ */
+ public ArrayList<Sound> getSoundDevices()
+ {
+ return Domain.filterDevices( Sound.class, this.getDevices() );
+ }
+
+ /**
+ * Returns list of virtual machine video devices specified in the Libvirt domain XML document.
+ *
+ * @return list of virtual machine video devices.
+ */
+ public ArrayList<Video> getVideoDevices()
+ {
+ return Domain.filterDevices( Video.class, this.getDevices() );
+ }
+
+ /**
+ * Adds a virtual machine device to the Libvirt domain XML document.
+ *
+ * @param device virtual machine device that is added to the Libvirt domain XML document.
+ * @return reference to the added device for configuration purposes if creation was successful.
+ */
+ public Device addDevice( Device device )
+ {
+ Device addedDevice = null;
+
+ if ( device != null ) {
+ Node devicesNode = this.getRootXmlNode().getXmlElement( "devices" );
+
+ if ( devicesNode != null ) {
+ LibvirtXmlNode parentDevicesNode = null;
+ parentDevicesNode = new LibvirtXmlNode( this.getRootXmlNode().getXmlDocument(), devicesNode );
+ addedDevice = Device.createInstance( device, parentDevicesNode );
+ }
+ }
+
+ return addedDevice;
+ }
+
+ /**
+ * Adds a virtual machine controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added controller device if creation was successful.
+ */
+ public Controller addControllerDevice()
+ {
+ return Controller.class.cast( this.addDevice( new Controller() ) );
+ }
+
+ /**
+ * Adds a virtual machine floppy controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added floppy controller device if creation was successful.
+ */
+ public ControllerFloppy addControllerFloppyDevice()
+ {
+ return ControllerFloppy.class.cast( this.addDevice( new ControllerFloppy() ) );
+ }
+
+ /**
+ * Adds a virtual machine IDE controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added IDE controller device if creation was successful.
+ */
+ public ControllerIde addControllerIdeDevice()
+ {
+ return ControllerIde.class.cast( this.addDevice( new ControllerIde() ) );
+ }
+
+ /**
+ * Adds a virtual machine PCI controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added PCI controller device if creation was successful.
+ */
+ public ControllerPci addControllerPciDevice()
+ {
+ return ControllerPci.class.cast( this.addDevice( new ControllerPci() ) );
+ }
+
+ /**
+ * Adds a virtual machine SATA controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added SATA controller device if creation was successful.
+ */
+ public ControllerSata addControllerSataDevice()
+ {
+ return ControllerSata.class.cast( this.addDevice( new ControllerSata() ) );
+ }
+
+ /**
+ * Adds a virtual machine SCSI controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added SCSI controller device if creation was successful.
+ */
+ public ControllerScsi addControllerScsiDevice()
+ {
+ return ControllerScsi.class.cast( this.addDevice( new ControllerScsi() ) );
+ }
+
+ /**
+ * Adds a virtual machine USB controller device to the Libvirt domain XML document.
+ *
+ * @return reference to the added USB controller device if creation was successful.
+ */
+ public ControllerUsb addControllerUsbDevice()
+ {
+ return ControllerUsb.class.cast( this.addDevice( new ControllerUsb() ) );
+ }
+
+ /**
+ * Adds a virtual machine disk device to the Libvirt domain XML document.
+ *
+ * @return reference to the added disk device if creation was successful.
+ */
+ public Disk addDiskDevice()
+ {
+ return Disk.class.cast( this.addDevice( new Disk() ) );
+ }
+
+ /**
+ * Adds a virtual machine CDROM disk device to the Libvirt domain XML document.
+ *
+ * @return reference to the added CDROM disk device if creation was successful.
+ */
+ public DiskCdrom addDiskCdromDevice()
+ {
+ return DiskCdrom.class.cast( this.addDevice( new DiskCdrom() ) );
+ }
+
+ /**
+ * Adds a virtual machine floppy disk device to the Libvirt domain XML document.
+ *
+ * @return reference to the added floppy disk device if creation was successful.
+ */
+ public DiskFloppy addDiskFloppyDevice()
+ {
+ return DiskFloppy.class.cast( this.addDevice( new DiskFloppy() ) );
+ }
+
+ /**
+ * Adds a virtual machine storage disk device to the Libvirt domain XML document.
+ *
+ * @return reference to the added storage disk device if creation was successful.
+ */
+ public DiskStorage addDiskStorageDevice()
+ {
+ return DiskStorage.class.cast( this.addDevice( new DiskStorage() ) );
+ }
+
+ /**
+ * Adds a virtual machine disk device to the Libvirt domain XML document.
+ *
+ * @return reference to the added disk device if creation was successful.
+ */
+ public Hostdev addHostdevDevice()
+ {
+ return Hostdev.class.cast( this.addDevice( new Hostdev() ) );
+ }
+
+ /**
+ * Adds a virtual machine network device to the Libvirt domain XML document.
+ *
+ * @return reference to the added network device if creation was successful.
+ */
+ public Interface addInterfaceDevice()
+ {
+ return Interface.class.cast( this.addDevice( new Interface() ) );
+ }
+
+ /**
+ * Adds a virtual machine network bridge interface device to the Libvirt domain XML document.
+ *
+ * @return reference to the added network interface device if creation was successful.
+ */
+ public InterfaceBridge addInterfaceBridgeDevice()
+ {
+ return InterfaceBridge.class.cast( this.addDevice( new InterfaceBridge() ) );
+ }
+
+ /**
+ * Adds a virtual machine network interface device to the Libvirt domain XML document.
+ *
+ * @return reference to the added network interface device if creation was successful.
+ */
+ public InterfaceNetwork addInterfaceNetworkDevice()
+ {
+ return InterfaceNetwork.class.cast( this.addDevice( new InterfaceNetwork() ) );
+ }
+
+ /**
+ * Adds a virtual machine graphics device to the Libvirt domain XML document.
+ *
+ * @return reference to the added graphics device if creation was successful.
+ */
+ public Graphics addGraphicsDevice()
+ {
+ return Graphics.class.cast( this.addDevice( new Graphics() ) );
+ }
+
+ /**
+ * Adds a virtual machine SDL graphics device to the Libvirt domain XML document.
+ *
+ * @return reference to the added SDL graphics device if creation was successful.
+ */
+ public GraphicsSdl addGraphicsSdlDevice()
+ {
+ return GraphicsSdl.class.cast( this.addDevice( new GraphicsSdl() ) );
+ }
+
+ /**
+ * Adds a virtual machine SPICE graphics device to the Libvirt domain XML document.
+ *
+ * @return reference to the added SPICE graphics device if creation was successful.
+ */
+ public GraphicsSpice addGraphicsSpiceDevice()
+ {
+ return GraphicsSpice.class.cast( this.addDevice( new GraphicsSpice() ) );
+ }
+
+ /**
+ * Adds a virtual machine VNC graphics device to the Libvirt domain XML document.
+ *
+ * @return reference to the added VNC graphics device if creation was successful.
+ */
+ public GraphicsVnc addGraphicsVncDevice()
+ {
+ return GraphicsVnc.class.cast( this.addDevice( new GraphicsVnc() ) );
+ }
+
+ /**
+ * Adds a virtual machine sound device to the Libvirt domain XML document.
+ *
+ * @return reference to the added sound device if creation was successful.
+ */
+ public Sound addSoundDevice()
+ {
+ return Sound.class.cast( this.addDevice( new Sound() ) );
+ }
+
+ /**
+ * Adds a virtual machine video device to the Libvirt domain XML document.
+ *
+ * @return reference to the added video device if creation was successful.
+ */
+ public Video addVideoDevice()
+ {
+ return Video.class.cast( this.addDevice( new Video() ) );
+ }
+
+ /**
+ * Removes boot oder entries in the Libvirt domain XML document.
+ */
+ public void removeBootOrder()
+ {
+ // remove boot order entries of all disk devices
+ for ( Disk diskDevice : this.getDiskDevices() ) {
+ diskDevice.removeBootOrder();
+ }
+
+ // remove boot order entries of all network interface devices
+ for ( Interface interfaceDevice : this.getInterfaceDevices() ) {
+ interfaceDevice.removeBootOrder();
+ }
+
+ // remove boot order entries of all hostdev devices
+ for ( Hostdev hostdevDevice : this.getHostdevDevices() ) {
+ hostdevDevice.removeBootOrder();
+ }
+
+ // remove boot oder entries under the 'os' element
+ this.getRootXmlNode().removeXmlElement( "os/boot" );
+ }
+
+ /**
+ * Removes underlying source for all disk devices in the Libvirt domain XML document.
+ *
+ * @implNote Calling this method will result in an invalid Libvirt domain XML document.
+ */
+ public void removeDiskDevicesStorage()
+ {
+ for ( Disk diskDevice : this.getDiskDevices() ) {
+ diskDevice.removeStorage();
+ }
+ }
+
+ /**
+ * Removes network source for all interface devices in the Libvirt domain XML document.
+ */
+ public void removeInterfaceDevicesSource()
+ {
+ for ( Interface interfaceDevice : this.getInterfaceDevices() ) {
+ interfaceDevice.removeSource();
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/DomainUtils.java b/src/main/java/org/openslx/libvirt/domain/DomainUtils.java
new file mode 100644
index 0000000..2462371
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/DomainUtils.java
@@ -0,0 +1,118 @@
+package org.openslx.libvirt.domain;
+
+import java.math.BigInteger;
+
+/**
+ * Collection of helper functions to maintain a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public final class DomainUtils
+{
+
+ /**
+ * Converts memory value with specified SI unit to absolute value in bytes.
+ *
+ * @param value amount of memory in specified SI unit.
+ * @param unit SI unit name, one of: bytes, KB, k, KiB, MB, M, MiB, GB, G, GiB, TB, T, TiB, for
+ * <code>value</code>.
+ * @return absolute amount of memory in bytes.
+ */
+ public static BigInteger decodeMemory( String value, String unit )
+ {
+ BigInteger factor = null;
+ BigInteger result = new BigInteger( value );
+
+ switch ( unit ) {
+ case "b":
+ case "bytes":
+ factor = new BigInteger( "1" );
+ break;
+ case "KB":
+ factor = new BigInteger( "1000" );
+ break;
+ case "k":
+ case "KiB":
+ factor = new BigInteger( "1024" );
+ break;
+ case "MB":
+ factor = new BigInteger( "1000000" );
+ break;
+ case "M":
+ case "MiB":
+ factor = new BigInteger( "1048576" );
+ break;
+ case "GB":
+ factor = new BigInteger( "1000000000" );
+ break;
+ case "G":
+ case "GiB":
+ factor = new BigInteger( "1073741824" );
+ break;
+ case "TB":
+ factor = new BigInteger( "1000000000000" );
+ break;
+ case "T":
+ case "TiB":
+ factor = new BigInteger( "1099511627776" );
+ break;
+ default:
+ return null;
+ }
+
+ return result.multiply( factor );
+ }
+
+ /**
+ * Convert memory from absolute value in bytes to value in specified SI unit.
+ *
+ * @param value absolute amount of memory in bytes.
+ * @param unit SI unit name, one of: bytes, KB, k, KiB, MB, M, MiB, GB, G, GiB, TB, T, TiB for
+ * returned memory value.
+ * @return amount of memory in specified SI unit.
+ */
+ public static String encodeMemory( BigInteger value, String unit )
+ {
+ BigInteger dividend = null;
+
+ switch ( unit ) {
+ case "b":
+ case "bytes":
+ dividend = new BigInteger( "1" );
+ break;
+ case "KB":
+ dividend = new BigInteger( "1000" );
+ break;
+ case "k":
+ case "KiB":
+ dividend = new BigInteger( "1024" );
+ break;
+ case "MB":
+ dividend = new BigInteger( "1000000" );
+ break;
+ case "M":
+ case "MiB":
+ dividend = new BigInteger( "1048576" );
+ break;
+ case "GB":
+ dividend = new BigInteger( "1000000000" );
+ break;
+ case "G":
+ case "GiB":
+ dividend = new BigInteger( "1073741824" );
+ break;
+ case "TB":
+ dividend = new BigInteger( "1000000000000" );
+ break;
+ case "T":
+ case "TiB":
+ dividend = new BigInteger( "1099511627776" );
+ break;
+ default:
+ return null;
+ }
+
+ return value.divide( dividend ).toString();
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Controller.java b/src/main/java/org/openslx/libvirt/domain/device/Controller.java
new file mode 100644
index 0000000..626462b
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Controller.java
@@ -0,0 +1,181 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A controller (PCI, USB, ...) device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Controller extends Device
+{
+ /**
+ * Creates an empty controller device.
+ */
+ public Controller()
+ {
+ super();
+ }
+
+ /**
+ * Creates a controller device representing an existing Libvirt XML controller device element.
+ *
+ * @param xmlNode existing Libvirt XML controller device element.
+ */
+ public Controller( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns index of the controller.
+ *
+ * @return index of the controller.
+ */
+ public int getIndex()
+ {
+ String index = this.getXmlElementAttributeValue( "index" );
+ return Integer.parseInt( index );
+ }
+
+ /**
+ * Sets index for the controller.
+ *
+ * @param index index for the controller.
+ */
+ public void setIndex( int index )
+ {
+ this.setXmlElementAttributeValue( "index", Integer.toString( index ) );
+ }
+
+ /**
+ * Creates a non-existent controller device as Libvirt XML device element.
+ *
+ * @param controller controller device that is created.
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created controller device instance.
+ */
+ public static Controller createInstance( Controller controller, LibvirtXmlNode xmlNode )
+ {
+ Controller addedController = null;
+
+ if ( controller instanceof ControllerFloppy ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.FLOPPY.toString() );
+ addedController = ControllerFloppy.createInstance( xmlNode );
+ } else if ( controller instanceof ControllerIde ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.IDE.toString() );
+ addedController = ControllerIde.createInstance( xmlNode );
+ } else if ( controller instanceof ControllerPci ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.PCI.toString() );
+ addedController = ControllerPci.createInstance( xmlNode );
+ } else if ( controller instanceof ControllerSata ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.SATA.toString() );
+ addedController = ControllerSata.createInstance( xmlNode );
+ } else if ( controller instanceof ControllerScsi ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.SCSI.toString() );
+ addedController = ControllerScsi.createInstance( xmlNode );
+ } else if ( controller instanceof ControllerUsb ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.USB.toString() );
+ addedController = ControllerUsb.createInstance( xmlNode );
+ }
+
+ return addedController;
+ }
+
+ /**
+ * Creates a controller device representing an existing Libvirt XML controller device element.
+ *
+ * @param xmlNode existing Libvirt XML controller device element.
+ * @return controller device instance.
+ */
+ public static Controller newInstance( LibvirtXmlNode xmlNode )
+ {
+
+ Controller deviceController = null;
+ Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case FLOPPY:
+ deviceController = ControllerFloppy.newInstance( xmlNode );
+ break;
+ case IDE:
+ deviceController = ControllerIde.newInstance( xmlNode );
+ break;
+ case PCI:
+ deviceController = ControllerPci.newInstance( xmlNode );
+ break;
+ case SATA:
+ deviceController = ControllerSata.newInstance( xmlNode );
+ break;
+ case SCSI:
+ deviceController = ControllerScsi.newInstance( xmlNode );
+ break;
+ case USB:
+ deviceController = ControllerUsb.newInstance( xmlNode );
+ break;
+ }
+
+ return deviceController;
+ }
+
+ /**
+ * Type of controller device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Type
+ {
+ // @formatter:off
+ FLOPPY( "fdc" ),
+ IDE ( "ide" ),
+ PCI ( "pci" ),
+ SATA ( "sata" ),
+ SCSI ( "scsi" ),
+ USB ( "usb" );
+ // @formatter:on
+
+ /**
+ * Name of the controller device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates controller device type.
+ *
+ * @param type valid name of the controller device type in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates controller device type from its name with error check.
+ *
+ * @param type name of the controller device type in a Libvirt domain XML document.
+ * @return valid controller device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerFloppy.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerFloppy.java
new file mode 100644
index 0000000..5e580bd
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerFloppy.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A floppy controller device in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerFloppy extends Controller
+{
+ /**
+ * Creates an empty floppy controller device.
+ */
+ public ControllerFloppy()
+ {
+ super();
+ }
+
+ /**
+ * Creates a floppy controller device representing an existing Libvirt XML floppy controller
+ * device element.
+ *
+ * @param xmlNode existing Libvirt XML controller device element.
+ */
+ public ControllerFloppy( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent floppy controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created floppy controller device instance.
+ */
+ public static ControllerFloppy createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerFloppy.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a floppy controller device representing an existing Libvirt XML floppy controller
+ * device element.
+ *
+ * @param xmlNode existing Libvirt XML controller device element.
+ * @return floppy controller device instance.
+ */
+ public static ControllerFloppy newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerFloppy( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerIde.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerIde.java
new file mode 100644
index 0000000..062b67d
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerIde.java
@@ -0,0 +1,128 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * An IDE controller device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerIde extends Controller
+{
+ /**
+ * Creates an empty IDE controller device.
+ */
+ public ControllerIde()
+ {
+ super();
+ }
+
+ /**
+ * Creates an IDE controller device representing an existing Libvirt XML IDE controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML IDE controller device element.
+ */
+ public ControllerIde( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns emulated hardware model of the IDE controller.
+ *
+ * @return hardware model of the IDE controller.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the IDE controller.
+ *
+ * @param model hardware model for the IDE controller.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", model.toString() );
+ }
+
+ /**
+ * Creates a non-existent IDE controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created IDE controller device instance.
+ */
+ public static ControllerIde createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerIde.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates an IDE controller device representing an existing Libvirt XML IDE controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML IDE controller device element.
+ * @return IDE controller device instance.
+ */
+ public static ControllerIde newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerIde( xmlNode );
+ }
+
+ /**
+ * Model of IDE controller device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Model
+ {
+ // @formatter:off
+ PIIX3( "piix3" ),
+ PIIX4( "pixx4" ),
+ ICH6 ( "ich6" );
+ // @formatter:on
+
+ /**
+ * Name of the IDE controller device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates IDE controller device model.
+ *
+ * @param type valid name of the IDE controller device model in a Libvirt domain XML document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates IDE controller device model from its name with error check.
+ *
+ * @param type name of the IDE controller device model in a Libvirt domain XML document.
+ * @return valid IDE controller device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model t : Model.values() ) {
+ if ( t.model.equalsIgnoreCase( model ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerPci.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerPci.java
new file mode 100644
index 0000000..feb8b1a
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerPci.java
@@ -0,0 +1,145 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A PCI controller device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerPci extends Controller
+{
+ /**
+ * Creates an empty PCI controller device.
+ */
+ public ControllerPci()
+ {
+ super();
+ }
+
+ /**
+ * Creates a PCI controller device representing an existing Libvirt XML PCI controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML PCI controller device element.
+ */
+ public ControllerPci( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns model of the PCI controller.
+ *
+ * @return model of the PCI controller.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets model for the PCI controller.
+ *
+ * @param model model for the PCI controller.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", model.toString() );
+ }
+
+ /**
+ * Returns emulated hardware model of the PCI controller.
+ *
+ * @return emulated hardware model of the PCI controller.
+ */
+ public String getModelEmulated()
+ {
+ return this.getXmlElementAttributeValue( "model", "name" );
+ }
+
+ /**
+ * Creates a non-existent PCI controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created PCI controller device instance.
+ */
+ public static ControllerPci createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerPci.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a PCI controller device representing an existing Libvirt XML PCI controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML PCI controller device element.
+ * @return PCI controller device instance.
+ */
+ public static ControllerPci newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerPci( xmlNode );
+ }
+
+ /**
+ * Model of PCI controller device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Model
+ {
+ // @formatter:off
+ PCI_ROOT ( "pci-root" ),
+ PCI_BRDIGE ( "pci-bridge" ),
+ PCIE_ROOT ( "pcie-root" ),
+ PCI_DMI2BRIDGE ( "dmi-to-pci-bridge" ),
+ PCIE_ROOT_PORT ( "pcie-root-port" ),
+ PCIE_SWITCH_UPSTREAM_PORT ( "pcie-switch-upstream-port" ),
+ PCIE_SWITCH_DOWNSTREAM_PORT( "pcie-switch-downstream-port" ),
+ PCI_EXPANDER_BUS ( "pci-expander-bus" ),
+ PCIE_EXPANDER_BUS ( "pcie-expander-bus" ),
+ PCIE2PCI_BRIDGE ( "pcie-to-pci-bridge" );
+ // @formatter:on
+
+ /**
+ * Name of the PCI controller device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates PCI controller device model.
+ *
+ * @param type valid name of the PCI controller device model in a Libvirt domain XML document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates PCI controller device model from its name with error check.
+ *
+ * @param type name of the PCI controller device model in a Libvirt domain XML document.
+ * @return valid PCI controller device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model t : Model.values() ) {
+ if ( t.model.equalsIgnoreCase( model ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerSata.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerSata.java
new file mode 100644
index 0000000..b784ae0
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerSata.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A SATA controller device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerSata extends Controller
+{
+ /**
+ * Creates an empty SATA controller device.
+ */
+ public ControllerSata()
+ {
+ super();
+ }
+
+ /**
+ * Creates a SATA controller device representing an existing Libvirt XML SATA controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML SATA controller device element.
+ */
+ public ControllerSata( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent SATA controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created SATA controller device instance.
+ */
+ public static ControllerSata createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerSata.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a SATA controller device representing an existing Libvirt XML SATA controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML SATA controller device element.
+ * @return SATA controller device instance.
+ */
+ public static ControllerSata newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerSata( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerScsi.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerScsi.java
new file mode 100644
index 0000000..16c3a0f
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerScsi.java
@@ -0,0 +1,138 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A SCSI controller device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerScsi extends Controller
+{
+ /**
+ * Creates an empty SCSI controller device.
+ */
+ public ControllerScsi()
+ {
+ super();
+ }
+
+ /**
+ * Creates a SCSI controller device representing an existing Libvirt XML SCSI controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML SCSI controller device element.
+ */
+ public ControllerScsi( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns hardware model of the PCI controller.
+ *
+ * @return hardware model of the PCI controller.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the PCI controller.
+ *
+ * @param model hardware model for the PCI controller.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", model.toString() );
+ }
+
+ /**
+ * Creates a non-existent SCSI controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created SCSI controller device instance.
+ */
+ public static ControllerScsi createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerScsi.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a SCSI controller device representing an existing Libvirt XML SCSI controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML SCSI controller device element.
+ * @return SCSI controller device instance.
+ */
+ public static ControllerScsi newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerScsi( xmlNode );
+ }
+
+ /**
+ * Model of SCSI controller device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Model
+ {
+ // @formatter:off
+ AUTO ( "auto" ),
+ BUSLOGIC ( "buslogic" ),
+ IBMVSCSI ( "ibmvscsi" ),
+ LSISAS1068 ( "lsisas1068" ),
+ LSISAS1078 ( "lsisas1078" ),
+ VIRTIO_SCSI ( "virtio-scsi" ),
+ VMPVSCSI ( "vmpvscsi" ),
+ VIRTIO_TRANSITIONAL ( "virtio-transitional" ),
+ VIRTIO_NON_TRANSITIONAL( "virtio-non-transitional" ),
+ NCR53C90 ( "ncr53c90" ),
+ AM53C974 ( "am53c974" ),
+ DC390 ( "dc390" );
+ // @formatter:on
+
+ /**
+ * Name of the SCSI controller device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates SCSI controller device model.
+ *
+ * @param type valid name of the SCSI controller device model in a Libvirt domain XML
+ * document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates SCSI controller device model from its name with error check.
+ *
+ * @param type name of the SCSI controller device model in a Libvirt domain XML document.
+ * @return valid SCSI controller device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model t : Model.values() ) {
+ if ( t.model.equalsIgnoreCase( model ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/ControllerUsb.java b/src/main/java/org/openslx/libvirt/domain/device/ControllerUsb.java
new file mode 100644
index 0000000..695167c
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/ControllerUsb.java
@@ -0,0 +1,139 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A USB controller device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class ControllerUsb extends Controller
+{
+ /**
+ * Creates an empty USB controller device.
+ */
+ public ControllerUsb()
+ {
+ super();
+ }
+
+ /**
+ * Creates an USB controller device representing an existing Libvirt XML USB controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML USB controller device element.
+ */
+ public ControllerUsb( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns hardware model of the PCI controller.
+ *
+ * @return hardware model of the PCI controller.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the PCI controller.
+ *
+ * @param model hardware model for the PCI controller.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", model.toString() );
+ }
+
+ /**
+ * Creates a non-existent USB controller device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created USB controller device instance.
+ */
+ public static ControllerUsb createInstance( LibvirtXmlNode xmlNode )
+ {
+ return ControllerUsb.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates an USB controller device representing an existing Libvirt XML USB controller device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML USB controller device element.
+ * @return USB controller device instance.
+ */
+ public static ControllerUsb newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new ControllerUsb( xmlNode );
+ }
+
+ /**
+ * Model of PCI controller device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Model
+ {
+ // @formatter:off
+ NONE ( "none" ),
+ PIIX3_UHCI ( "piix3-uhci" ),
+ PIIX4_UHCI ( "piix4-uhci" ),
+ EHCI ( "ehci" ),
+ ICH9_EHCI1 ( "ich9-ehci1" ),
+ ICH9_UHCI1 ( "ich9-uhci1" ),
+ ICH9_UHCI2 ( "ich9-uhci2" ),
+ ICH9_UHCI3 ( "ich9-uhci3" ),
+ VT82C686B_UHCI( "vt82c686b-uhci" ),
+ PCI_OHCI ( "pci-ohci" ),
+ NEC_XHCI ( "nec-xhci" ),
+ QUSB1 ( "qusb1" ),
+ QUSB2 ( "qusb2" ),
+ QEMU_XHCI ( "qemu-xhci" );
+ // @formatter:on
+
+ /**
+ * Name of the USB controller device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates USB controller device model.
+ *
+ * @param type valid name of the USB controller device model in a Libvirt domain XML document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates USB controller device model from its name with error check.
+ *
+ * @param type name of the USB controller device model in a Libvirt domain XML document.
+ * @return valid USB controller device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model t : Model.values() ) {
+ if ( t.model.equalsIgnoreCase( model ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Device.java b/src/main/java/org/openslx/libvirt/domain/device/Device.java
new file mode 100644
index 0000000..5c26c55
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Device.java
@@ -0,0 +1,205 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * A virtual machines device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Device extends LibvirtXmlNode
+{
+ /**
+ * Creates an empty virtual machine device.
+ */
+ public Device()
+ {
+ super();
+ }
+
+ /**
+ * Creates a virtual machine device representing an existing Libvirt XML device element.
+ *
+ * @param xmlNode existing Libvirt XML device element.
+ */
+ public Device( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Removes device from Libvirt domain XML document.
+ */
+ public void remove()
+ {
+ Node node = this.getXmlElement();
+ node.getParentNode().removeChild( node );
+ }
+
+ /**
+ * Creates a Libvirt XML device element as child of a given Libvirt XML parent node.
+ *
+ * @param xmlParentNode parent Libvirt XML node of the Libvirt XML device element that is
+ * created.
+ * @param deviceType type of the Libvirt XML device element.
+ * @return created Libvirt XML device node.
+ */
+ private static LibvirtXmlNode createDeviceElement( LibvirtXmlNode xmlParentNode, Type deviceType )
+ {
+ // create XML element as part of the Libvirt XML document
+ Document xmlDocument = xmlParentNode.getXmlDocument();
+ Element deviceNode = xmlDocument.createElement( deviceType.toString() );
+
+ // append the created XML element to the Libvirt XML document
+ xmlParentNode.getXmlBaseNode().appendChild( deviceNode );
+
+ return new LibvirtXmlNode( xmlParentNode.getXmlDocument(), deviceNode );
+ }
+
+ /**
+ * Creates a non-existent virtual machine device as Libvirt XML device element.
+ *
+ * @param device virtual machine device that is created.
+ * @param xmlParentNode parent Libvirt XML node of the Libvirt XML device that is created.
+ * @return created virtual machine device instance.
+ */
+ public static Device createInstance( Device device, LibvirtXmlNode xmlParentNode )
+ {
+ Device createdDevice = null;
+
+ if ( device instanceof Controller ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.CONTROLLER );
+ createdDevice = Controller.createInstance( Controller.class.cast( device ), xmlNode );
+ } else if ( device instanceof Disk ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.DISK );
+ createdDevice = Disk.createInstance( Disk.class.cast( device ), xmlNode );
+ } else if ( device instanceof Hostdev ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.HOSTDEV );
+ createdDevice = Hostdev.createInstance( Hostdev.class.cast( device ), xmlNode );
+ } else if ( device instanceof Interface ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.INTERFACE );
+ createdDevice = Interface.createInstance( Interface.class.cast( device ), xmlNode );
+ } else if ( device instanceof Graphics ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.GRAPHICS );
+ createdDevice = Graphics.createInstance( Graphics.class.cast( device ), xmlNode );
+ } else if ( device instanceof Sound ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.SOUND );
+ createdDevice = Sound.createInstance( xmlNode );
+ } else if ( device instanceof Video ) {
+ LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.VIDEO );
+ createdDevice = Video.createInstance( xmlNode );
+ }
+
+ return createdDevice;
+ }
+
+ /**
+ * Creates a virtual machine device representing an existing Libvirt XML device element.
+ *
+ * @param xmlNode existing Libvirt XML device element.
+ * @return virtual machine device instance.
+ */
+ public static Device newInstance( LibvirtXmlNode xmlNode )
+ {
+
+ Node element = xmlNode.getXmlElement();
+
+ if ( element == null ) {
+ return null;
+ } else {
+ Device device = null;
+ Type type = Type.fromString( element.getNodeName() );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case CONTROLLER:
+ device = Controller.newInstance( xmlNode );
+ break;
+ case DISK:
+ device = Disk.newInstance( xmlNode );
+ break;
+ case HOSTDEV:
+ device = Hostdev.newInstance( xmlNode );
+ break;
+ case INTERFACE:
+ device = Interface.newInstance( xmlNode );
+ break;
+ case GRAPHICS:
+ device = Graphics.newInstance( xmlNode );
+ break;
+ case SOUND:
+ device = Sound.newInstance( xmlNode );
+ break;
+ case VIDEO:
+ device = Video.newInstance( xmlNode );
+ break;
+ }
+
+ return device;
+ }
+ }
+
+ /**
+ * Type of virtual machine devices.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Type
+ {
+ // @formatter:off
+ CONTROLLER( "controller" ),
+ DISK ( "disk" ),
+ HOSTDEV ( "hostdev" ),
+ INTERFACE ( "interface" ),
+ GRAPHICS ( "graphics" ),
+ SOUND ( "sound" ),
+ VIDEO ( "video" );
+ // @formatter:on
+
+ /**
+ * Name of the virtual machine device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates virtual machine device type.
+ *
+ * @param type valid name of the virtual machine device type in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates virtual machine device type from its name with error check.
+ *
+ * @param type name of the virtual machine device type in a Libvirt domain XML document.
+ * @return valid virtual machine device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Disk.java b/src/main/java/org/openslx/libvirt/domain/device/Disk.java
new file mode 100644
index 0000000..464e7b6
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Disk.java
@@ -0,0 +1,427 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+import org.w3c.dom.Node;
+
+/**
+ * A disk (floppy, CDROM, ...) device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Disk extends Device
+{
+ /**
+ * Creates an empty disk device.
+ */
+ public Disk()
+ {
+ super();
+ }
+
+ /**
+ * Creates a disk device representing an existing Libvirt XML disk device element.
+ *
+ * @param xmlNode existing Libvirt XML disk device element.
+ */
+ public Disk( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns storage type of the disk device.
+ *
+ * @return storage type of underlying source for the disk device.
+ */
+ public StorageType getStorageType()
+ {
+ String storageType = this.getXmlElementAttributeValue( "type" );
+ return StorageType.fromString( storageType );
+ }
+
+ /**
+ * Sets storage type for the disk device.
+ *
+ * @param storageType storage type of underlying source for the disk device.
+ *
+ * @implNote Please call {@link #setStorageSource(String)} after calling this method, otherwise
+ * the underlying source for the disk device may be invalid.
+ */
+ protected void setStorageType( StorageType storageType )
+ {
+ this.setXmlElementAttributeValue( "type", storageType.toString() );
+ }
+
+ /**
+ * Returns underlying source of disk device.
+ *
+ * @return file path to underlying source of disk device.
+ */
+ public String getStorageSource()
+ {
+ StorageType storageType = this.getStorageType();
+ String storageSource = null;
+
+ switch ( storageType ) {
+ case FILE:
+ storageSource = this.getXmlElementAttributeValue( "source", "file" );
+ break;
+ case BLOCK:
+ storageSource = this.getXmlElementAttributeValue( "source", "bdev" );
+ break;
+ }
+
+ return storageSource;
+ }
+
+ /**
+ * Sets underlying source for disk device.
+ *
+ * @param source file path to underlying source for disk device.
+ *
+ * @implNote Please call {@link #setStorageType(StorageType)} before calling this method,
+ * otherwise the underlying source for the disk device is not set.
+ */
+ protected void setStorageSource( String source )
+ {
+ StorageType storageType = this.getStorageType();
+
+ // remove all attributes from sub-element 'source'
+ this.removeXmlElementAttributes( "source" );
+
+ // rewrite specific attribute depending on the storage type
+ switch ( storageType ) {
+ case FILE:
+ this.setXmlElementAttributeValue( "source", "file", source );
+ break;
+ case BLOCK:
+ this.setXmlElementAttributeValue( "source", "bdev", source );
+ break;
+ }
+ }
+
+ /**
+ * Sets storage type and underlying source for disk device.
+ *
+ * @param storageType storage type of underlying source for the disk device.
+ * @param source file path to underlying source for disk device.
+ */
+ public void setStorage( StorageType storageType, String source )
+ {
+ this.setStorageType( storageType );
+ this.setStorageSource( source );
+ }
+
+ /**
+ * Removes underlying source of the disk device.
+ *
+ * @implNote Calling this method will result in an invalid Libvirt domain XML content.
+ */
+ public void removeStorage()
+ {
+ this.removeXmlElement( "source" );
+ }
+
+ /**
+ * Removes boot oder entry of the disk device.
+ */
+ public void removeBootOrder()
+ {
+ this.removeXmlElement( "boot" );
+ }
+
+ /**
+ * Returns read only state of disk device.
+ *
+ * @return read only state of disk device.
+ */
+ public boolean isReadOnly()
+ {
+ Node readOnly = this.getXmlElement( "readonly" );
+
+ if ( readOnly == null ) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Sets read only state for disk device.
+ *
+ * @param readOnly state for disk device and its read only functionality.
+ */
+ public void setReadOnly( boolean readOnly )
+ {
+ if ( readOnly ) {
+ this.setXmlElement( "readonly" );
+ } else {
+ this.removeXmlElement( "readonly" );
+ }
+ }
+
+ /**
+ * Returns bus type of the disk device.
+ *
+ * @return bus type of the disk device.
+ */
+ public BusType getBusType()
+ {
+ String busType = this.getXmlElementAttributeValue( "target", "bus" );
+ return BusType.fromString( busType );
+ }
+
+ /**
+ * Sets bus type for the disk device.
+ *
+ * @param busType bus type for the disk device.
+ */
+ public void setBusType( BusType busType )
+ {
+ this.setXmlElementAttributeValue( "target", "bus", busType.toString() );
+ }
+
+ /**
+ * Returns target device of the disk device.
+ *
+ * @return target device of the disk device.
+ */
+ public String getTargetDevice()
+ {
+ return this.getXmlElementAttributeValue( "target", "dev" );
+ }
+
+ /**
+ * Sets target device for the disk device.
+ *
+ * @param target device for the disk device.
+ */
+ public void setTargetDevice( String targetDevice )
+ {
+ this.setXmlElementAttributeValue( "target", "dev", targetDevice );
+ }
+
+ /**
+ * Creates a non-existent disk device as Libvirt XML device element.
+ *
+ * @param disk disk device that is created.
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created disk device instance.
+ */
+ public static Disk createInstance( Disk disk, LibvirtXmlNode xmlNode )
+ {
+ Disk addedDisk = null;
+
+ if ( disk instanceof DiskCdrom ) {
+ xmlNode.setXmlElementAttributeValue( "device", Type.CDROM.toString() );
+ addedDisk = DiskCdrom.createInstance( xmlNode );
+ } else if ( disk instanceof DiskFloppy ) {
+ xmlNode.setXmlElementAttributeValue( "device", Type.FLOPPY.toString() );
+ addedDisk = DiskFloppy.createInstance( xmlNode );
+ } else if ( disk instanceof DiskStorage ) {
+ xmlNode.setXmlElementAttributeValue( "device", Type.STORAGE.toString() );
+ addedDisk = DiskStorage.createInstance( xmlNode );
+ }
+
+ return addedDisk;
+ }
+
+ /**
+ * Creates a disk device representing an existing Libvirt XML disk device element.
+ *
+ * @param xmlNode existing Libvirt XML disk device element.
+ * @return disk device instance.
+ */
+ public static Disk newInstance( LibvirtXmlNode xmlNode )
+ {
+ Disk deviceDisk = null;
+ Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "device" ) );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case CDROM:
+ deviceDisk = DiskCdrom.newInstance( xmlNode );
+ break;
+ case FLOPPY:
+ deviceDisk = DiskFloppy.newInstance( xmlNode );
+ break;
+ case STORAGE:
+ deviceDisk = DiskStorage.newInstance( xmlNode );
+ break;
+ }
+
+ return deviceDisk;
+ }
+
+ /**
+ * Type of disk device.
+ *
+ * Indicates how a disk is to be exposed to the guest OS.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Type
+ {
+ // @formatter:off
+ CDROM ( "cdrom" ),
+ FLOPPY ( "floppy" ),
+ STORAGE( "disk" );
+ // @formatter:on
+
+ /**
+ * Name of the disk device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates disk device type.
+ *
+ * @param type valid name of the disk device type in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates disk device type from its name with error check.
+ *
+ * @param type name of the disk device type in a Libvirt domain XML document.
+ * @return valid disk device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Storage type of a disk device.
+ *
+ * The storage type refers to the underlying source for the disk.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum StorageType
+ {
+ // @formatter:off
+ FILE ( "file" ),
+ BLOCK ( "block" );
+ // @formatter:on
+
+ /**
+ * Name of the disk device type.
+ */
+ private String storageType = null;
+
+ /**
+ * Creates disk device storage type.
+ *
+ * @param storageType valid name of the disk device storage type in a Libvirt domain XML
+ * document.
+ */
+ StorageType( String storageType )
+ {
+ this.storageType = storageType;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.storageType;
+ }
+
+ /**
+ * Creates disk device storage type from its name with error check.
+ *
+ * @param storageType name of the disk device storage type in a Libvirt domain XML document.
+ * @return valid disk device storage type.
+ */
+ public static StorageType fromString( String storageType )
+ {
+ for ( StorageType t : StorageType.values() ) {
+ if ( t.storageType.equalsIgnoreCase( storageType ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Bus type (IDE, SATA, ...) of a disk device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum BusType
+ {
+ // @formatter:off
+ IDE ( "ide" ),
+ FDC ( "fdc" ),
+ SATA ( "sata" ),
+ SCSI ( "scsi" ),
+ SD ( "sd" ),
+ USB ( "usb" ),
+ VIRTIO( "virtio" ),
+ XEN ( "xen" );
+ // @formatter:on
+
+ /**
+ * Name of the disk device bus type.
+ */
+ private String busType = null;
+
+ /**
+ * Creates disk device bus type.
+ *
+ * @param busType valid name of the disk device bus type in a Libvirt domain XML document.
+ */
+ BusType( String busType )
+ {
+ this.busType = busType;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.busType;
+ }
+
+ /**
+ * Creates disk device bus type from its name with error check.
+ *
+ * @param busType name of the disk device bus type in a Libvirt domain XML document.
+ * @return valid disk device bus type.
+ */
+ public static BusType fromString( String busType )
+ {
+ for ( BusType t : BusType.values() ) {
+ if ( t.busType.equalsIgnoreCase( busType ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/DiskCdrom.java b/src/main/java/org/openslx/libvirt/domain/device/DiskCdrom.java
new file mode 100644
index 0000000..2ae08e1
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/DiskCdrom.java
@@ -0,0 +1,55 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A CDROM disk device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class DiskCdrom extends Disk
+{
+ /**
+ * Creates an empty CDROM disk device.
+ */
+ public DiskCdrom()
+ {
+ super();
+ }
+
+ /**
+ * Creates a CDROM disk device representing an existing Libvirt XML CDROM disk device element.
+ *
+ * @param xmlNode existing Libvirt XML CDROM disk device element.
+ */
+ public DiskCdrom( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+
+ // restrict CDROM disk device default read/write access always to readonly
+ this.setReadOnly( true );
+ }
+
+ /**
+ * Creates a non-existent CDROM disk device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created CDROM disk device instance.
+ */
+ public static DiskCdrom createInstance( LibvirtXmlNode xmlNode )
+ {
+ return DiskCdrom.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a CDROM disk device representing an existing Libvirt XML CDROM disk device element.
+ *
+ * @param xmlNode existing Libvirt XML CDROM disk device element.
+ * @return CDROM disk device instance.
+ */
+ public static DiskCdrom newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new DiskCdrom( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/DiskFloppy.java b/src/main/java/org/openslx/libvirt/domain/device/DiskFloppy.java
new file mode 100644
index 0000000..ce9733b
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/DiskFloppy.java
@@ -0,0 +1,52 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A floppy disk device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class DiskFloppy extends Disk
+{
+ /**
+ * Creates an empty floppy disk device.
+ */
+ public DiskFloppy()
+ {
+ super();
+ }
+
+ /**
+ * Creates a floppy disk device representing an existing Libvirt XML floppy disk device element.
+ *
+ * @param xmlNode existing Libvirt XML floppy disk device element.
+ */
+ public DiskFloppy( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent floppy disk device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created floppy disk device instance.
+ */
+ public static DiskFloppy createInstance( LibvirtXmlNode xmlNode )
+ {
+ return DiskFloppy.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a floppy disk device representing an existing Libvirt XML floppy disk device element.
+ *
+ * @param xmlNode existing Libvirt XML floppy disk device element.
+ * @return floppy disk device instance.
+ */
+ public static DiskFloppy newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new DiskFloppy( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/DiskStorage.java b/src/main/java/org/openslx/libvirt/domain/device/DiskStorage.java
new file mode 100644
index 0000000..7fca789
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/DiskStorage.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A storage (HDD, SSD, ...) disk device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class DiskStorage extends Disk
+{
+ /**
+ * Creates an empty storage disk device.
+ */
+ public DiskStorage()
+ {
+ super();
+ }
+
+ /**
+ * Creates a storage disk device representing an existing Libvirt XML storage disk device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML storage disk device element.
+ */
+ public DiskStorage( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent storage disk device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created storage disk device instance.
+ */
+ public static DiskStorage createInstance( LibvirtXmlNode xmlNode )
+ {
+ return DiskStorage.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a storage disk device representing an existing Libvirt XML storage disk device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML storage disk device element.
+ * @return storage disk device instance.
+ */
+ public static DiskStorage newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new DiskStorage( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Graphics.java b/src/main/java/org/openslx/libvirt/domain/device/Graphics.java
new file mode 100644
index 0000000..314dba2
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Graphics.java
@@ -0,0 +1,138 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A graphics (display) device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Graphics extends Device
+{
+ /**
+ * Creates an empty graphics device.
+ */
+ public Graphics()
+ {
+ super();
+ }
+
+ /**
+ * Creates a graphics device representing an existing Libvirt XML graphics device element.
+ *
+ * @param xmlNode existing Libvirt XML graphics device element.
+ */
+ public Graphics( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent graphics device as Libvirt XML device element.
+ *
+ * @param graphics graphics device that is created.
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created graphics device instance.
+ */
+ public static Graphics createInstance( Graphics graphics, LibvirtXmlNode xmlNode )
+ {
+ Graphics addedGraphics = null;
+
+ if ( graphics instanceof GraphicsSdl ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.SDL.toString() );
+ addedGraphics = GraphicsSdl.createInstance( xmlNode );
+ } else if ( graphics instanceof GraphicsSpice ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.SPICE.toString() );
+ addedGraphics = GraphicsSpice.createInstance( xmlNode );
+ } else if ( graphics instanceof GraphicsVnc ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.VNC.toString() );
+ addedGraphics = GraphicsVnc.createInstance( xmlNode );
+ }
+
+ return addedGraphics;
+ }
+
+ /**
+ * Creates a graphics device representing an existing Libvirt XML graphics device element.
+ *
+ * @param xmlNode existing Libvirt XML graphics device element.
+ * @return graphics device instance.
+ */
+ public static Graphics newInstance( LibvirtXmlNode xmlNode )
+ {
+ Graphics deviceGraphics = null;
+ Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case SDL:
+ deviceGraphics = GraphicsSdl.newInstance( xmlNode );
+ break;
+ case SPICE:
+ deviceGraphics = GraphicsSpice.newInstance( xmlNode );
+ break;
+ case VNC:
+ deviceGraphics = GraphicsVnc.newInstance( xmlNode );
+ break;
+ }
+
+ return deviceGraphics;
+ }
+
+ /**
+ * Type of graphics device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Type
+ {
+ // @formatter:off
+ SDL ( "sdl" ),
+ SPICE( "spice" ),
+ VNC ( "vnc" );
+ // @formatter:on
+
+ /**
+ * Name of graphics device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates graphics device type.
+ *
+ * @param type valid name of the graphics device type in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates graphics device type from its name with error check.
+ *
+ * @param type name of the graphics device type in a Libvirt domain XML document.
+ * @return valid graphics device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/GraphicsSdl.java b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSdl.java
new file mode 100644
index 0000000..c77f56d
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSdl.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A graphics SDL device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class GraphicsSdl extends Graphics
+{
+ /**
+ * Creates an empty graphics SDL device.
+ */
+ public GraphicsSdl()
+ {
+ super();
+ }
+
+ /**
+ * Creates a graphics SDL device representing an existing Libvirt XML graphics SDL device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics SDL device element.
+ */
+ public GraphicsSdl( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent graphics SDL device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created graphics SDL device instance.
+ */
+ public static GraphicsSdl createInstance( LibvirtXmlNode xmlNode )
+ {
+ return GraphicsSdl.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a graphics SDL device representing an existing Libvirt XML graphics SDL device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics SDL device element.
+ * @return graphics SDL device instance.
+ */
+ public static GraphicsSdl newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new GraphicsSdl( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java
new file mode 100644
index 0000000..f087296
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java
@@ -0,0 +1,74 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A graphics SPICE device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class GraphicsSpice extends Graphics
+{
+ /**
+ * Creates an empty graphics SPICE device.
+ */
+ public GraphicsSpice()
+ {
+ super();
+ }
+
+ /**
+ * Creates a graphics SPICE device representing an existing Libvirt XML graphics SPICE device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics SPCIE device element.
+ */
+ public GraphicsSpice( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns the state whether OpenGL hardware acceleration is enabled or not.
+ *
+ * @return tate whether OpenGL hardware acceleration is enabled or not.
+ */
+ public boolean isOpenGlEnabled()
+ {
+ return this.getXmlElementAttributeValueAsBool( "gl", "enable" );
+ }
+
+ /**
+ * Sets the state whether OpenGL hardware acceleration is enabled or not.
+ *
+ * @param enabled state whether OpenGL hardware acceleration is enabled or not.
+ */
+ public void setOpenGl( boolean enabled )
+ {
+ this.setXmlElementAttributeValue( "gl", "enable", enabled );
+ }
+
+ /**
+ * Creates a non-existent graphics SPICE device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created graphics SPICE device instance.
+ */
+ public static GraphicsSpice createInstance( LibvirtXmlNode xmlNode )
+ {
+ return GraphicsSpice.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a graphics SPICE device representing an existing Libvirt XML graphics SPICE device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics SPICE device element.
+ * @return graphics SPICE device instance.
+ */
+ public static GraphicsSpice newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new GraphicsSpice( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/GraphicsVnc.java b/src/main/java/org/openslx/libvirt/domain/device/GraphicsVnc.java
new file mode 100644
index 0000000..f595699
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/GraphicsVnc.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A graphics VNC device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class GraphicsVnc extends Graphics
+{
+ /**
+ * Creates an empty graphics VNC device.
+ */
+ public GraphicsVnc()
+ {
+ super();
+ }
+
+ /**
+ * Creates a graphics VNC device representing an existing Libvirt XML graphics VNC device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics VNC device element.
+ */
+ public GraphicsVnc( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent graphics VNC device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created graphics VNC device instance.
+ */
+ public static GraphicsVnc createInstance( LibvirtXmlNode xmlNode )
+ {
+ return GraphicsVnc.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a graphics VNC device representing an existing Libvirt XML graphics VNC device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML graphics VNC device element.
+ * @return graphics VNC device instance.
+ */
+ public static GraphicsVnc newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new GraphicsVnc( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java
new file mode 100644
index 0000000..cb09099
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java
@@ -0,0 +1,139 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A hostdev device node in a Libvirt domain XML document for PCI, USB, ... passthrough.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Hostdev extends Device
+{
+ /**
+ * Creates an empty hostdev device.
+ */
+ public Hostdev()
+ {
+ super();
+ }
+
+ /**
+ * Creates a hostdev device representing an existing Libvirt XML hostdev device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev device element.
+ */
+ public Hostdev( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Removes boot oder entry of the hostdev device.
+ */
+ public void removeBootOrder()
+ {
+ this.removeXmlElement( "boot" );
+ }
+
+ /**
+ * Creates a non-existent hostdev device as Libvirt XML device element.
+ *
+ * @param hostdev hostdev device that is created.
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created hostdev device instance.
+ */
+ public static Hostdev createInstance( Hostdev hostdev, LibvirtXmlNode xmlNode )
+ {
+ Hostdev addedHostdev = null;
+
+ if ( hostdev instanceof HostdevPci ) {
+ xmlNode.setXmlElementAttributeValue( "device", Type.PCI.toString() );
+ addedHostdev = HostdevPci.createInstance( xmlNode );
+ } else if ( hostdev instanceof HostdevUsb ) {
+ xmlNode.setXmlElementAttributeValue( "device", Type.USB.toString() );
+ addedHostdev = HostdevUsb.createInstance( xmlNode );
+ }
+
+ return addedHostdev;
+ }
+
+ /**
+ * Creates a hostdev device representing an existing Libvirt XML hostdev device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev device element.
+ * @return hostdev device instance.
+ */
+ public static Hostdev newInstance( LibvirtXmlNode xmlNode )
+ {
+ Hostdev deviceHostdev = null;
+ Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case PCI:
+ deviceHostdev = HostdevPci.newInstance( xmlNode );
+ break;
+ case USB:
+ deviceHostdev = HostdevUsb.newInstance( xmlNode );
+ break;
+ }
+
+ return deviceHostdev;
+ }
+
+ /**
+ * Type of hostdev device subsystem passthrough.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ enum Type
+ {
+ // @formatter:off
+ PCI( "pci" ),
+ USB( "usb" );
+ // @formatter:on
+
+ /**
+ * Name of the hostdev device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates hostdev device type.
+ *
+ * @param type valid name of the hostdev device type in a Libvirt domain XML document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates hostdev device type from its name with error check.
+ *
+ * @param type name of the hostdev device storage in a Libvirt domain XML document.
+ * @return valid hostdev device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java
new file mode 100644
index 0000000..3b26fb0
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java
@@ -0,0 +1,79 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A hostdev PCI device node in a Libvirt domain XML document for PCI passthrough.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class HostdevPci extends Hostdev
+{
+ /**
+ * Creates an empty hostdev PCI device.
+ */
+ public HostdevPci()
+ {
+ super();
+ }
+
+ /**
+ * Creates a hostdev PCI device representing an existing Libvirt XML hostdev PCI device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev PCI device element.
+ */
+ public HostdevPci( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Checks if PCI hostdev device is managed.
+ *
+ * If {@link #isManaged()} returns <code>true</code> the hostdev PCI device is detached from the
+ * host before being passed on to the guest and reattached to the host after the guest exits.
+ *
+ * @return state whether PCI hostdev device is managed.
+ */
+ public boolean isManaged()
+ {
+ return this.getXmlElementAttributeValueAsBool( "managed" );
+ }
+
+ /**
+ * Sets state whether PCI hostdev device is managed.
+ *
+ * If the <code>managed</code> parameter is set to <code>true</code> the PCI hostdev device is
+ * detached from the host before being passed on to the guest and reattached to the host after
+ * the guest exits.
+ *
+ * @return state whether PCI hostdev device is managed.
+ */
+ public void setManaged( boolean managed )
+ {
+ this.setXmlElementAttributeValue( "managed", managed );
+ }
+
+ /**
+ * Creates a non-existent hostdev PCI device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created hostdev PCI device instance.
+ */
+ public static HostdevPci createInstance( LibvirtXmlNode xmlNode )
+ {
+ return HostdevPci.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a hostdev PCI device representing an existing Libvirt XML hostdev PCI device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev PCI device element.
+ * @return hostdev PCI device instance.
+ */
+ public static HostdevPci newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new HostdevPci( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java
new file mode 100644
index 0000000..e1fcc0c
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java
@@ -0,0 +1,52 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A hostdev USB device node in a Libvirt domain XML document for USB passthrough.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class HostdevUsb extends Hostdev
+{
+ /**
+ * Creates an empty hostdev USB device.
+ */
+ public HostdevUsb()
+ {
+ super();
+ }
+
+ /**
+ * Creates a hostdev USB device representing an existing Libvirt XML hostdev USB device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev USB device element.
+ */
+ public HostdevUsb( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent hostdev USB device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created hostdev USB device instance.
+ */
+ public static HostdevUsb createInstance( LibvirtXmlNode xmlNode )
+ {
+ return HostdevUsb.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a hostdev USB device representing an existing Libvirt XML hostdev USB device element.
+ *
+ * @param xmlNode existing Libvirt XML hostdev USB device element.
+ * @return hostdev USB device instance.
+ */
+ public static HostdevUsb newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new HostdevUsb( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Interface.java b/src/main/java/org/openslx/libvirt/domain/device/Interface.java
new file mode 100644
index 0000000..b09c7da
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Interface.java
@@ -0,0 +1,316 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A network interface device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Interface extends Device
+{
+ /**
+ * Creates an empty network device.
+ */
+ public Interface()
+ {
+ super();
+ }
+
+ /**
+ * Creates a network device representing an existing Libvirt XML network interface device
+ * element.
+ *
+ * @param xmlNode existing Libvirt XML network interface device element.
+ */
+ public Interface( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns hardware model of the network device.
+ *
+ * @return hardware model of the network device.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model", "type" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the network device.
+ *
+ * @param model hardware model for the network device.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", "type", model.toString() );
+ }
+
+ /**
+ * Returns type of the network device.
+ *
+ * @return type of the network device.
+ */
+ public Type getType()
+ {
+ return Type.fromString( this.getXmlElementAttributeValue( "type" ) );
+ }
+
+ /**
+ * Sets type of the network device.
+ *
+ * @return type of the network device.
+ */
+ public void setType(Type type)
+ {
+ String source = this.getSource();
+
+ // change type and set source again
+ this.setXmlElementAttributeValue( "type", type.toString() );
+ this.setSource( source );
+ }
+
+ /**
+ * Returns the source of the network device.
+ *
+ * @return source of the network device.
+ */
+ public String getSource()
+ {
+ Type type = this.getType();
+ String source = null;
+
+ switch ( type ) {
+ case BRIDGE:
+ source = this.getXmlElementAttributeValue( "source", "bridge" );
+ break;
+ case NETWORK:
+ source = this.getXmlElementAttributeValue( "source", "network" );
+ break;
+ }
+
+ return source;
+ }
+
+ /**
+ * Sets the source for the network device.
+ *
+ * @param source for the network device.
+ */
+ public void setSource( String source )
+ {
+ Type type = this.getType();
+
+ // remove all attributes from sub-element 'source'
+ this.removeXmlElementAttributes( "source" );
+
+ switch ( type ) {
+ case BRIDGE:
+ this.setXmlElementAttributeValue( "source", "bridge", source );
+ break;
+ case NETWORK:
+ this.setXmlElementAttributeValue( "source", "network", source );
+ break;
+ }
+ }
+
+ /**
+ * Removes boot oder entry of the network interface device.
+ */
+ public void removeBootOrder()
+ {
+ this.removeXmlElement( "boot" );
+ }
+
+ /**
+ * Removes network source of the network interface device.
+ */
+ public void removeSource()
+ {
+ this.removeXmlElement( "source" );
+ }
+
+ /**
+ * Creates a non-existent network interface device as Libvirt XML device element.
+ *
+ * @param iface network device that is created.
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created network device instance.
+ */
+ public static Interface createInstance( Interface iface, LibvirtXmlNode xmlNode )
+ {
+ Interface addedInterface = null;
+
+ if ( iface instanceof InterfaceBridge ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.BRIDGE.toString() );
+ addedInterface = InterfaceBridge.createInstance( xmlNode );
+ } else if ( iface instanceof InterfaceNetwork ) {
+ xmlNode.setXmlElementAttributeValue( "type", Type.NETWORK.toString() );
+ addedInterface = InterfaceNetwork.createInstance( xmlNode );
+ }
+
+ return addedInterface;
+ }
+
+ /**
+ * Creates a network interface device representing an existing Libvirt XML network interface
+ * device element.
+ *
+ * @param xmlNode existing Libvirt XML network interface device element.
+ * @return network interface device instance.
+ */
+ public static Interface newInstance( LibvirtXmlNode xmlNode )
+ {
+ Interface deviceInterface = null;
+ Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );
+
+ if ( type == null ) {
+ return null;
+ }
+
+ switch ( type ) {
+ case BRIDGE:
+ deviceInterface = InterfaceBridge.newInstance( xmlNode );
+ break;
+ case NETWORK:
+ deviceInterface = InterfaceNetwork.newInstance( xmlNode );
+ break;
+ }
+
+ return deviceInterface;
+ }
+
+ /**
+ * Type of network interface device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Type
+ {
+ // @formatter:off
+ BRIDGE ( "bridge" ),
+ NETWORK( "network" );
+ // @formatter:on
+
+ /**
+ * Name of the network interface device type.
+ */
+ private String type = null;
+
+ /**
+ * Creates network interface device type.
+ *
+ * @param type valid name of the network interface device type in a Libvirt domain XML
+ * document.
+ */
+ Type( String type )
+ {
+ this.type = type;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.type;
+ }
+
+ /**
+ * Creates network interface device type from its name with error check.
+ *
+ * @param type name of the network interface device type in a Libvirt domain XML document.
+ * @return valid network interface device type.
+ */
+ public static Type fromString( String type )
+ {
+ for ( Type t : Type.values() ) {
+ if ( t.type.equalsIgnoreCase( type ) ) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * Model of network interface device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Model
+ {
+ // @formatter:off
+ E1000 ( "e1000" ),
+ E1000_82544GC ( "e1000-82544gc" ),
+ E1000_82545EM ( "e1000-82545em" ),
+ E1000E ( "e1000e" ),
+ I82550 ( "i82550" ),
+ I82551 ( "i82551" ),
+ I82557A ( "i82557a" ),
+ I82557B ( "i82557b" ),
+ I82557C ( "i82557c" ),
+ I82558A ( "i82558a" ),
+ I82558B ( "i82558b" ),
+ I82559A ( "i82559a" ),
+ I82559B ( "i82559b" ),
+ I82559C ( "i82559c" ),
+ I82559ER ( "i82559er" ),
+ I82562 ( "i82562" ),
+ I82801 ( "i82801" ),
+ NE2K_PCI ( "ne2k_pci" ),
+ PCNET ( "pcnet" ),
+ RTL8139 ( "rtl8139" ),
+ TULIP ( "tulip" ),
+ VIRTIO ( "virtio" ),
+ VIRTIO_NET_PCI ( "virtio-net-pci" ),
+ VIRTIO_NET_PCI_NON_TRANSITIONAL( "virtio-net-pci-non-transitional" ),
+ VIRTIO_NET_PCI_TRANSITIONAL ( "virtio-net-pci-transitional" ),
+ VMXNET3 ( "vmxnet3" );
+ // @formatter:on
+
+ /**
+ * Name of the network interface device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates network interface device model.
+ *
+ * @param type valid name of the network interface device model in a Libvirt domain XML
+ * document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates network interface device model from its name with error check.
+ *
+ * @param type name of the network interface device model in a Libvirt domain XML document.
+ * @return valid network interface device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model m : Model.values() ) {
+ if ( m.model.equalsIgnoreCase( model ) ) {
+ return m;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/InterfaceBridge.java b/src/main/java/org/openslx/libvirt/domain/device/InterfaceBridge.java
new file mode 100644
index 0000000..02eabb0
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/InterfaceBridge.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A network bridge interface device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class InterfaceBridge extends Interface
+{
+ /**
+ * Creates an empty network bridge interface device.
+ */
+ public InterfaceBridge()
+ {
+ super();
+ }
+
+ /**
+ * Creates a network bridge interface device representing an existing Libvirt XML network bridge
+ * interface device element.
+ *
+ * @param xmlNode existing Libvirt XML network bridge interface device element.
+ */
+ public InterfaceBridge( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent network bridge interface device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created network bridge interface device instance.
+ */
+ public static InterfaceBridge createInstance( LibvirtXmlNode xmlNode )
+ {
+ return InterfaceBridge.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a network bridge interface device representing an existing Libvirt XML network bridge
+ * interface device element.
+ *
+ * @param xmlNode existing Libvirt XML network bridge interface device element.
+ * @return network bridge interface device instance.
+ */
+ public static InterfaceBridge newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new InterfaceBridge( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/InterfaceNetwork.java b/src/main/java/org/openslx/libvirt/domain/device/InterfaceNetwork.java
new file mode 100644
index 0000000..ae1fd40
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/InterfaceNetwork.java
@@ -0,0 +1,54 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A network interface device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class InterfaceNetwork extends Interface
+{
+ /**
+ * Creates an empty network interface device.
+ */
+ public InterfaceNetwork()
+ {
+ super();
+ }
+
+ /**
+ * Creates a network interface device representing an existing Libvirt XML network interface
+ * device element.
+ *
+ * @param xmlNode existing Libvirt XML network interface device element.
+ */
+ public InterfaceNetwork( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Creates a non-existent network interface device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created network interface device device instance.
+ */
+ public static InterfaceNetwork createInstance( LibvirtXmlNode xmlNode )
+ {
+ return InterfaceNetwork.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a network interface device representing an existing Libvirt XML network interface
+ * device element.
+ *
+ * @param xmlNode existing Libvirt XML network interface device element.
+ * @return network interface device instance.
+ */
+ public static InterfaceNetwork newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new InterfaceNetwork( xmlNode );
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Sound.java b/src/main/java/org/openslx/libvirt/domain/device/Sound.java
new file mode 100644
index 0000000..e424ed4
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Sound.java
@@ -0,0 +1,128 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A sound device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Sound extends Device
+{
+ /**
+ * Creates an empty sound device.
+ */
+ public Sound()
+ {
+ super();
+ }
+
+ /**
+ * Creates a sound device representing an existing Libvirt XML sound device element.
+ *
+ * @param xmlNode existing Libvirt XML sound device element.
+ */
+ public Sound( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns hardware model of the sound device.
+ *
+ * @return hardware model of the sound device.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the sound device.
+ *
+ * @param model hardware model for the sound device.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", model.toString() );
+ }
+
+ /**
+ * Creates a non-existent sound device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created sound device instance.
+ */
+ public static Sound createInstance( LibvirtXmlNode xmlNode )
+ {
+ return Sound.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a sound device representing an existing Libvirt XML sound device element.
+ *
+ * @param xmlNode existing Libvirt XML sound device element.
+ * @return sound device instance.
+ */
+ public static Sound newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new Sound( xmlNode );
+ }
+
+ /**
+ * Model of sound device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Model
+ {
+ // @formatter:off
+ AC97 ( "ac97" ),
+ ES1370( "es1370" ),
+ ICH6 ( "ich6" ),
+ ICH9 ( "ich9" ),
+ SB16 ( "sb16" );
+ // @formatter:on
+
+ /**
+ * Name of the sound device model.
+ */
+ private String model;
+
+ /**
+ * Creates sound device model.
+ *
+ * @param type valid name of the sound device model in a Libvirt domain XML document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates sound device model from its name with error check.
+ *
+ * @param type name of the sound device model in a Libvirt domain XML document.
+ * @return valid sound device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model m : Model.values() ) {
+ if ( m.model.equalsIgnoreCase( model ) ) {
+ return m;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Video.java b/src/main/java/org/openslx/libvirt/domain/device/Video.java
new file mode 100644
index 0000000..e901b85
--- /dev/null
+++ b/src/main/java/org/openslx/libvirt/domain/device/Video.java
@@ -0,0 +1,188 @@
+package org.openslx.libvirt.domain.device;
+
+import org.openslx.libvirt.xml.LibvirtXmlNode;
+
+/**
+ * A video (GPU) device node in a Libvirt domain XML document.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class Video extends Device
+{
+ /**
+ * Creates an empty video device.
+ */
+ public Video()
+ {
+ super();
+ }
+
+ /**
+ * Creates a video device representing an existing Libvirt XML video device element.
+ *
+ * @param xmlNode existing Libvirt XML video device element.
+ */
+ public Video( LibvirtXmlNode xmlNode )
+ {
+ super( xmlNode );
+ }
+
+ /**
+ * Returns hardware model of the video device.
+ *
+ * @return hardware model of the video device.
+ */
+ public Model getModel()
+ {
+ String model = this.getXmlElementAttributeValue( "model", "type" );
+ return Model.fromString( model );
+ }
+
+ /**
+ * Sets hardware model for the video device.
+ *
+ * @param model hardware model for the video device.
+ */
+ public void setModel( Model model )
+ {
+ this.setXmlElementAttributeValue( "model", "type", model.toString() );
+ }
+
+ /**
+ * Checks whether 2D hardware video acceleration is turned on or off.
+ *
+ * @return state of 2D hardware video acceleration.
+ */
+ public boolean get2DAcceleration()
+ {
+ return this.getXmlElementAttributeValueAsBool( "model/acceleration", "accel2d" );
+ }
+
+ /**
+ * Turns 2D hardware video acceleration on or off.
+ *
+ * @param acceleration state of 2D hardware video acceleration.
+ */
+ public void set2DAcceleration( boolean acceleration )
+ {
+ Model model = this.getModel();
+
+ if ( model != null ) {
+ if ( model == Model.VIRTIO ) {
+ // only set acceleration on supported Virtio GPUs
+ this.setXmlElementAttributeValue( "model/acceleration", "accel2d", acceleration );
+ } else {
+ String errorMsg = new String(
+ "Video card model '" + model.toString() + "' does not support enabled 2D hardware acceleration." );
+ throw new IllegalArgumentException( errorMsg );
+ }
+ }
+ }
+
+ /**
+ * Checks whether 3D hardware video acceleration is turned on or off.
+ *
+ * @return state of 3D hardware video acceleration.
+ */
+ public boolean get3DAcceleration()
+ {
+ return this.getXmlElementAttributeValueAsBool( "model/acceleration", "accel3d" );
+ }
+
+ /**
+ * Turns 3D hardware video acceleration on or off.
+ *
+ * @param acceleration state of 3D hardware video acceleration.
+ */
+ public void set3DAcceleration( boolean acceleration )
+ {
+ Model model = this.getModel();
+
+ if ( model == Model.VIRTIO ) {
+ // only set acceleration on supported Virtio GPUs
+ this.setXmlElementAttributeValue( "model/acceleration", "accel3d", acceleration );
+ } else {
+ String errorMsg = new String(
+ "Video card model '" + model.toString() + "' does not support enabled 3D hardware acceleration." );
+ throw new IllegalArgumentException( errorMsg );
+ }
+ }
+
+ /**
+ * Creates a non-existent video device as Libvirt XML device element.
+ *
+ * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
+ * @return created video device instance.
+ */
+ public static Video createInstance( LibvirtXmlNode xmlNode )
+ {
+ return Video.newInstance( xmlNode );
+ }
+
+ /**
+ * Creates a video device representing an existing Libvirt XML video device element.
+ *
+ * @param xmlNode existing Libvirt XML video device element.
+ * @return video device instance.
+ */
+ public static Video newInstance( LibvirtXmlNode xmlNode )
+ {
+ return new Video( xmlNode );
+ }
+
+ /**
+ * Model of video device.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+ public enum Model
+ {
+ // @formatter:off
+ NONE ( "none" ),
+ QXL ( "qxl" ),
+ VGA ( "vga" ),
+ VMVGA ( "vmvga" ),
+ VIRTIO( "virtio" );
+ // @formatter:on
+
+ /**
+ * Name of the video device model.
+ */
+ private String model = null;
+
+ /**
+ * Creates video device model.
+ *
+ * @param type valid name of the video device model in a Libvirt domain XML document.
+ */
+ Model( String model )
+ {
+ this.model = model;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.model;
+ }
+
+ /**
+ * Creates video device model from its name with error check.
+ *
+ * @param type name of the video device model in a Libvirt domain XML document.
+ * @return valid video device model.
+ */
+ public static Model fromString( String model )
+ {
+ for ( Model m : Model.values() ) {
+ if ( m.model.equalsIgnoreCase( model ) ) {
+ return m;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java
new file mode 100644
index 0000000..a604b21
--- /dev/null
+++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java
@@ -0,0 +1,294 @@
+package org.openslx.libvirt.domain;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.math.BigInteger;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.openslx.libvirt.domain.Domain.CpuCheck;
+import org.openslx.libvirt.domain.Domain.CpuMode;
+import org.openslx.libvirt.xml.LibvirtXmlDocumentException;
+import org.openslx.libvirt.xml.LibvirtXmlSerializationException;
+import org.openslx.libvirt.xml.LibvirtXmlTestResources;
+import org.openslx.libvirt.xml.LibvirtXmlValidationException;
+
+public class DomainTest
+{
+ @BeforeAll
+ public static void setUp()
+ {
+ // disable logging with log4j
+ LogManager.getRootLogger().setLevel( Level.OFF );
+ }
+
+ private Domain newDomainInstance( String xmlFileName )
+ {
+ Domain domain = null;
+
+ try {
+ domain = new Domain( LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ) );
+ } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) {
+ String errorMsg = new String( "Cannot prepare requested Libvirt domain XML file from the resources folder" );
+ fail( errorMsg );
+ }
+
+ return domain;
+ }
+
+ @Test
+ @DisplayName( "Get VM type from libvirt XML file" )
+ public void testGetType()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( Domain.Type.KVM.toString(), vm.getType().toString() );
+ }
+
+ @Test
+ @DisplayName( "Set VM type from libvirt XML file" )
+ public void testSetType()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setType( Domain.Type.QEMU );
+ assertEquals( Domain.Type.QEMU.toString(), vm.getType().toString() );
+ }
+
+ @Test
+ @DisplayName( "Get VM name from libvirt XML file" )
+ public void testGetName()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( "ubuntu-20-04", vm.getName() );
+ }
+
+ @Test
+ @DisplayName( "Set VM name in libvirt XML file" )
+ public void testSetName()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setName( "ubuntu-18-04" );
+ assertEquals( "ubuntu-18-04", vm.getName() );
+ }
+
+ @Test
+ @DisplayName( "Get VM title from libvirt XML file" )
+ public void testGetTitle()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( "Ubuntu 20.04", vm.getTitle() );
+ }
+
+ @Test
+ @DisplayName( "Set VM title in libvirt XML file" )
+ public void testSetTitle()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setTitle( "Ubuntu 18.04" );
+ assertEquals( "Ubuntu 18.04", vm.getTitle() );
+ }
+
+ @Test
+ @DisplayName( "Get VM description from libvirt XML file" )
+ public void testGetDescription()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( "Ubuntu 20.04 desktop installation", vm.getDescription() );
+ }
+
+ @Test
+ @DisplayName( "Set VM description in libvirt XML file" )
+ public void testSetDescription()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setDescription( "Ubuntu 18.04 server installation" );
+ assertEquals( "Ubuntu 18.04 server installation", vm.getDescription() );
+ }
+
+ @Test
+ @DisplayName( "Get VM UUID from libvirt XML file" )
+ public void testGetUuid()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( "8dc5433c-0228-49e4-b019-fa2b606aa544", vm.getUuid() );
+ }
+
+ @Test
+ @DisplayName( "Set VM UUID in libvirt XML file" )
+ public void testSetUuid()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setUuid( "5ab08167-3d95-400e-ac83-e6af8d150971" );
+ assertEquals( "5ab08167-3d95-400e-ac83-e6af8d150971", vm.getUuid() );
+ }
+
+ @Test
+ @DisplayName( "Get VM memory from libvirt XML file" )
+ public void testGetMemory()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( new BigInteger( "4294967296" ).toString(), vm.getMemory().toString() );
+ }
+
+ @Test
+ @DisplayName( "Set VM memory in libvirt XML file" )
+ public void testSetMemory()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setMemory( new BigInteger( "12073740288" ) );
+ assertEquals( new BigInteger( "12073740288" ).toString(), vm.getMemory().toString() );
+ }
+
+ @Test
+ @DisplayName( "Get current VM memory from libvirt XML file" )
+ public void testGetCurrentMemory()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( new BigInteger( "4294967296" ).toString(), vm.getCurrentMemory().toString() );
+ }
+
+ @Test
+ @DisplayName( "Set current VM memory in libvirt XML file" )
+ public void testSetCurrentMemory()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setCurrentMemory( new BigInteger( "8087237632" ) );
+ assertEquals( new BigInteger( "8087237632" ).toString(), vm.getCurrentMemory().toString() );
+ }
+
+ @Test
+ @DisplayName( "Get VM number of vCpus from libvirt XML file" )
+ public void testGetVCpu()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 2, vm.getVCpu() );
+ }
+
+ @Test
+ @DisplayName( "Set VM number of vCpus in libvirt XML file" )
+ public void testSetVCpu()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setVCpu( 4 );
+ assertEquals( 4, vm.getVCpu() );
+ }
+
+ @Test
+ @DisplayName( "Get VM CPU model from libvirt XML file" )
+ public void testGetCpuModel()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertNull( vm.getCpuModel() );
+ }
+
+ @Test
+ @DisplayName( "Set VM CPU model in libvirt XML file" )
+ public void testSetCpuModel()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setCpuModel( "core2duo" );
+ assertEquals( "core2duo", vm.getCpuModel() );
+ }
+
+ @Test
+ @DisplayName( "Get VM CPU mode from libvirt XML file" )
+ public void testGetCpuModelMode()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( CpuMode.HOST_MODEL.toString(), vm.getCpuMode().toString() );
+ }
+
+ @Test
+ @DisplayName( "Set VM CPU mode in libvirt XML file" )
+ public void testSetCpuModelMode()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setCpuMode( CpuMode.HOST_PASSTHROUGH );
+ assertEquals( CpuMode.HOST_PASSTHROUGH.toString(), vm.getCpuMode().toString() );
+ }
+
+ @Test
+ @DisplayName( "Get VM CPU check from libvirt XML file" )
+ public void testGetCpuCheck()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( CpuCheck.PARTIAL.toString(), vm.getCpuCheck().toString() );
+ }
+
+ @Test
+ @DisplayName( "Set VM CPU check in libvirt XML file" )
+ public void testSetCpuCheck()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ vm.setCpuCheck( CpuCheck.NONE );
+ assertEquals( CpuCheck.NONE.toString(), vm.getCpuCheck().toString() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM devices from libvirt XML file" )
+ public void testGetDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 21, vm.getDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM controller devices from libvirt XML file" )
+ public void testGetControllerDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 14, vm.getControllerDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM disk devices from libvirt XML file" )
+ public void testGetDiskDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 3, vm.getDiskDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM hostdev devices from libvirt XML file" )
+ public void testGetHostdevDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 0, vm.getHostdevDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM interface devices from libvirt XML file" )
+ public void testGetInterfaceDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 1, vm.getInterfaceDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM graphic devices from libvirt XML file" )
+ public void testGetGraphicDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 1, vm.getGraphicDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM sound devices from libvirt XML file" )
+ public void testGetSoundDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 1, vm.getSoundDevices().size() );
+ }
+
+ @Test
+ @DisplayName( "Get all VM video devices from libvirt XML file" )
+ public void testGetVideoDevices()
+ {
+ Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
+ assertEquals( 1, vm.getVideoDevices().size() );
+ }
+}