summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java
blob: 7bf13f4e9fb4caed39a486f64f3a5ec92802e40a (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
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 implements HostdevAddressableSource<HostdevUsbDeviceDescription>
{
	/**
	 * 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 );
	}

	@Override
	public HostdevUsbDeviceDescription getSource()
	{
		String vendorId = this.getXmlElementAttributeValue( "source/address/vendor", "id" );
		String productId = this.getXmlElementAttributeValue( "source/address/product", "id" );

		vendorId = HostdevUtils.removeHexPrefix( vendorId );
		productId = HostdevUtils.removeHexPrefix( productId );

		return HostdevUsbDeviceDescription.valueOf( vendorId + ":" + productId );
	}

	@Override
	public void setSource( HostdevUsbDeviceDescription description )
	{
		final String vendorId = HostdevUtils.appendHexPrefix( description.getVendorIdAsString() );
		final String productId = HostdevUtils.appendHexPrefix( description.getProductIdAsString() );

		this.setXmlElementAttributeValue( "source/address/vendor", "id", vendorId );
		this.setXmlElementAttributeValue( "source/address/product", "id", productId );
	}

	public HostdevUsbDeviceAddress getUsbTarget()
	{
		final String usbBus = this.getXmlElementAttributeValue( "address", "bus" );
		final String usbPort = this.getXmlElementAttributeValue( "address", "port" );

		return HostdevUsbDeviceAddress.valueOf( usbBus, usbPort );
	}

	public void setUsbTarget( HostdevUsbDeviceAddress address )
	{
		this.setXmlElementAttributeValue( "address", "bus", Integer.toString( address.getUsbBus() ) );
		this.setXmlElementAttributeValue( "address", "port", Integer.toString( address.getUsbPort() ) );
	}

	/**
	 * 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 );
	}
}