diff options
Diffstat (limited to 'src/main/java/org/openslx/libvirt')
4 files changed, 317 insertions, 4 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index c8f5303..50d0811 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -261,6 +261,17 @@ public class Domain extends LibvirtXmlDocument } /** + * Returns the libosinfo operating system identifier. + * + * @return libosinfo operating system identifier. + */ + public String getLibOsInfoOsId() + { + return this.getRootXmlNode() + .getXmlElementAttributeValue( "metadata/*[local-name()='libosinfo']/*[local-name()='os']", "id" ); + } + + /** * Returns virtual machine UUID defined in the Libvirt domain XML document. * * @return UUID of virtual machine. diff --git a/src/main/java/org/openslx/libvirt/libosinfo/LibOsInfo.java b/src/main/java/org/openslx/libvirt/libosinfo/LibOsInfo.java new file mode 100644 index 0000000..f506d74 --- /dev/null +++ b/src/main/java/org/openslx/libvirt/libosinfo/LibOsInfo.java @@ -0,0 +1,158 @@ +package org.openslx.libvirt.libosinfo; + +import java.io.File; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.function.Predicate; + +import org.openslx.libvirt.libosinfo.os.Os; +import org.openslx.libvirt.xml.LibvirtXmlDocument; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlNode; +import org.openslx.libvirt.xml.LibvirtXmlResources; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.openslx.virtualization.Version; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * Implementation of a libosinfo XML document. + * + * The libosinfo XML document is used to describe existing operating systems, devices and their + * configuration possibilities. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class LibOsInfo extends LibvirtXmlDocument +{ + /** + * Creates a libosinfo XML document from a {@link String} providing libosinfo XML content. + * + * @param xml {@link String} with libosinfo XML content. + * + * @throws LibvirtXmlDocumentException creation of XML context failed. + * @throws LibvirtXmlSerializationException serialization of the libosinfo XML content failed. + * @throws LibvirtXmlValidationException XML content is not a valid libosinfo XML document. + */ + public LibOsInfo( String xml ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml, LibvirtXmlResources.getLibOsInfoRng( "osinfo.rng" ) ); + } + + /** + * Creates a libosinfo XML document from a {@link File} containing libosinfo XML content. + * + * @param xml existing {@link File} containing libosinfo XML content. + * + * @throws LibvirtXmlDocumentException creation of XML context failed. + * @throws LibvirtXmlSerializationException serialization of the libosinfo XML content failed. + * @throws LibvirtXmlValidationException XML content is not a valid libosinfo XML document. + */ + public LibOsInfo( File xml ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml, LibvirtXmlResources.getLibOsInfoRng( "osinfo.rng" ) ); + } + + /** + * Creates a libosinfo XML document from an {@link InputStream} providing libosinfo XML content. + * + * @param xml {@link InputStream} providing libosinfo XML content. + * + * @throws LibvirtXmlDocumentException creation of XML context failed. + * @throws LibvirtXmlSerializationException serialization of the libosinfo XML content failed. + * @throws LibvirtXmlValidationException XML content is not a valid libosinfo XML document. + */ + public LibOsInfo( InputStream xml ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml, LibvirtXmlResources.getLibOsInfoRng( "osinfo.rng" ) ); + } + + /** + * Creates libosinfo XML document from {@link InputSource} providing libosinfo XML content. + * + * @param xml {@link InputSource} providing libosinfo XML content. + * + * @throws LibvirtXmlDocumentException creation of XML context failed. + * @throws LibvirtXmlSerializationException serialization of the libosinfo XML content failed. + * @throws LibvirtXmlValidationException XML content is not a valid libosinfo XML document. + */ + public LibOsInfo( InputSource xml ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml, LibvirtXmlResources.getLibOsInfoRng( "osinfo.rng" ) ); + } + + /** + * Returns the version of the libosinfo database. + * + * @return version of the libosinfo database. + */ + public Version getVersion() + { + final String version = this.getRootXmlNode().getXmlElementAttributeValue( "version" ); + return Version.valueOf( version ); + } + + /** + * Returns a list of all defined operating systems. + * + * @return list of all defined operating systems. + */ + public ArrayList<Os> getOses() + { + final ArrayList<Os> oses = new ArrayList<Os>(); + final NodeList osNodes = this.getRootXmlNode().getXmlNodes( "os" ); + + if ( osNodes != null ) { + for ( int i = 0; i < osNodes.getLength(); i++ ) { + final Node childNode = osNodes.item( i ); + if ( childNode.getNodeType() == Node.ELEMENT_NODE ) { + final LibvirtXmlNode osNode = new LibvirtXmlNode( this.getRootXmlNode().getXmlDocument(), childNode ); + final Os os = Os.newInstance( osNode ); + + if ( os != null ) { + oses.add( os ); + } + } + } + } + + return oses; + } + + /** + * Lookups an operating system in the libosinfo database specified by the operating system + * identifier. + * + * @param osId identifier of the operating system to lookup in the libosinfo database. + * @return found operating system from the libosinfo database. + */ + public static Os lookupOs( String osId ) + { + Os os = null; + + if ( osId != null && !osId.isEmpty() ) { + ArrayList<Os> oses = null; + + try { + final LibOsInfo osInfo = new LibOsInfo( LibvirtXmlResources.getLibOsInfoXml( "osinfo.xml" ) ); + oses = osInfo.getOses(); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + oses = null; + } + + if ( oses != null ) { + final Predicate<Os> byOsId = osCandidate -> osId.equals( osCandidate.getId() ); + os = oses.stream().filter( byOsId ).findFirst().orElse( null ); + } + } + + return os; + } +} diff --git a/src/main/java/org/openslx/libvirt/libosinfo/os/Os.java b/src/main/java/org/openslx/libvirt/libosinfo/os/Os.java new file mode 100644 index 0000000..37a0a2e --- /dev/null +++ b/src/main/java/org/openslx/libvirt/libosinfo/os/Os.java @@ -0,0 +1,93 @@ +package org.openslx.libvirt.libosinfo.os; + +import org.openslx.libvirt.xml.LibvirtXmlNode; +import org.openslx.virtualization.Version; + +/** + * A operating system node in a libosinfo XML document. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class Os extends LibvirtXmlNode +{ + /** + * Creates an empty operating system. + */ + public Os() + { + super(); + } + + /** + * Creates a operating system representing an existing libosinfo XML operating system element. + * + * @param xmlNode existing libosinfo XML operating system element. + */ + public Os( LibvirtXmlNode xmlNode ) + { + super( xmlNode ); + } + + /** + * Returns the identifier of the operating system. + * + * @return identifier of the operating system. + */ + public String getId() + { + return this.getXmlElementAttributeValue( "id" ); + } + + /** + * Returns the name of the operating system. + * + * @return name of the operating system. + */ + public String getName() + { + return this.getXmlElementValue( "name" ); + } + + /** + * Returns the version of the operating system. + * + * @return version of the operating system. + */ + public Version getVersion() + { + final String version = this.getXmlElementValue( "version" ); + return Version.valueOf( version ); + } + + /** + * Returns the system family of the operating system. + * + * @return system family of the operating system. + */ + public String getFamily() + { + return this.getXmlElementValue( "family" ); + } + + /** + * Returns the distribution name of the operating system. + * + * @return distribution name of the operating system. + */ + public String getDistro() + { + return this.getXmlElementValue( "distro" ); + } + + /** + * Creates a operating system representing an existing libosinfo XML operating system element. + * + * @param xmlNode existing libosinfo XML operating system element. + * @return libosinfo XML operating system instance. + */ + public static Os newInstance( LibvirtXmlNode node ) + { + return new Os( node ); + } +} diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java index 38c818b..a6b3e39 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java @@ -17,6 +17,11 @@ public final class LibvirtXmlResources private static final String LIBVIRT_PREFIX_PATH = File.separator + "libvirt"; /** + * File path prefix of the absolute path to the libosinfo resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH = File.separator + "libvirt" + File.separator + "libosinfo"; + + /** * File path prefix of the absolute path to the libvirt XSL resource folder in a *.jar file. */ private static final String LIBVIRT_PREFIX_PATH_XSL = LIBVIRT_PREFIX_PATH + File.separator + "xsl"; @@ -27,6 +32,29 @@ public final class LibvirtXmlResources private static final String LIBVIRT_PREFIX_PATH_RNG = LIBVIRT_PREFIX_PATH + File.separator + "rng"; /** + * File path prefix of the absolute path to the libosinfo RNG resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH_RNG = LIBOSINFO_PREFIX_PATH + File.separator + "rng"; + + /** + * File path prefix of the absolute path to the libosinfo XML resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH_XML = LIBOSINFO_PREFIX_PATH + File.separator + "xml"; + + /** + * Returns a Libvirt resource as stream. + * + * @param prefix file path of the Libvirt resource in the resources *.jar folder. + * @param fileName file name of the Libvirt resource in the resources *.jar folder. + * @return Libvirt resource as stream. + */ + private static InputStream getLibvirtResource( String prefix, String fileName ) + { + final String path = prefix + File.separator + fileName; + return LibvirtXmlResources.class.getResourceAsStream( path ); + } + + /** * Returns a Libvirt XSL resource as stream. * * @param libvirtXslFileName file name of the XSL resource in the resources *.jar folder. @@ -34,8 +62,7 @@ public final class LibvirtXmlResources */ public static InputStream getLibvirtXsl( String libvirtXslFileName ) { - String libvirtXslPath = LibvirtXmlResources.LIBVIRT_PREFIX_PATH_XSL + File.separator + libvirtXslFileName; - return LibvirtXmlResources.class.getResourceAsStream( libvirtXslPath ); + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBVIRT_PREFIX_PATH_XSL, libvirtXslFileName ); } /** @@ -46,7 +73,31 @@ public final class LibvirtXmlResources */ public static InputStream getLibvirtRng( String libvirtRngFileName ) { - String libvirtRngPath = LibvirtXmlResources.LIBVIRT_PREFIX_PATH_RNG + File.separator + libvirtRngFileName; - return LibvirtXmlResources.class.getResourceAsStream( libvirtRngPath ); + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBVIRT_PREFIX_PATH_RNG, libvirtRngFileName ); + } + + /** + * Returns a libosinfo RNG schema resource as stream. + * + * @param libosInfoRngFileName file name of the RNG schema resource in the resources *.jar + * folder. + * @return libosinfo RNG schema resource as stream. + */ + public static InputStream getLibOsInfoRng( String libosInfoRngFileName ) + { + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBOSINFO_PREFIX_PATH_RNG, + libosInfoRngFileName ); + } + + /** + * Returns a libosinfo XML resource as stream. + * + * @param libosInfoXmlFileName file name of the XML resource in the resources *.jar folder. + * @return libosinfo XML resource as stream. + */ + public static InputStream getLibOsInfoXml( String libosInfoXmlFileName ) + { + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBOSINFO_PREFIX_PATH_XML, + libosInfoXmlFileName ); } } |