summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java
blob: cb090991d8bbca46bd8bbf10e71751e36d29b364 (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
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;
		}
	}
}