summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/HostdevUsb.java
blob: 7bf13f4e9fb4caed39a486f64f3a5ec92802e40a (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                                  
                                                                                                        


















                                                                                                        





















                                                                                                            
                                                     






                                                                                             
                                                                   




                                                                                                                





















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