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
|
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>,
HostdevAddressableTarget<HostdevUsbDeviceAddress>
{
/**
* 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 );
}
@Override
public HostdevUsbDeviceAddress getTarget()
{
final String usbBus = this.getXmlElementAttributeValue( "address", "bus" );
final String usbPort = this.getXmlElementAttributeValue( "address", "port" );
return HostdevUsbDeviceAddress.valueOf( usbBus, usbPort );
}
@Override
public void setTarget( 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 );
}
}
|