summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/libosinfo/LibOsInfo.java
blob: f506d742ae230c81c485f3151cddd75650f5f373 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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;
	}
}