summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/Controller.java
blob: 626462bc0833b68cad2ac5ef3e5f55870263d74a (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.openslx.libvirt.domain.device;

import org.openslx.libvirt.xml.LibvirtXmlNode;

/**
 * A controller (PCI, USB, ...) device node in a Libvirt domain XML document.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class Controller extends Device
{
	/**
	 * Creates an empty controller device.
	 */
	public Controller()
	{
		super();
	}

	/**
	 * Creates a controller device representing an existing Libvirt XML controller device element.
	 * 
	 * @param xmlNode existing Libvirt XML controller device element.
	 */
	public Controller( LibvirtXmlNode xmlNode )
	{
		super( xmlNode );
	}

	/**
	 * Returns index of the controller.
	 * 
	 * @return index of the controller.
	 */
	public int getIndex()
	{
		String index = this.getXmlElementAttributeValue( "index" );
		return Integer.parseInt( index );
	}

	/**
	 * Sets index for the controller.
	 * 
	 * @param index index for the controller.
	 */
	public void setIndex( int index )
	{
		this.setXmlElementAttributeValue( "index", Integer.toString( index ) );
	}

	/**
	 * Creates a non-existent controller device as Libvirt XML device element.
	 * 
	 * @param controller controller device that is created.
	 * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
	 * @return created controller device instance.
	 */
	public static Controller createInstance( Controller controller, LibvirtXmlNode xmlNode )
	{
		Controller addedController = null;

		if ( controller instanceof ControllerFloppy ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.FLOPPY.toString() );
			addedController = ControllerFloppy.createInstance( xmlNode );
		} else if ( controller instanceof ControllerIde ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.IDE.toString() );
			addedController = ControllerIde.createInstance( xmlNode );
		} else if ( controller instanceof ControllerPci ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.PCI.toString() );
			addedController = ControllerPci.createInstance( xmlNode );
		} else if ( controller instanceof ControllerSata ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.SATA.toString() );
			addedController = ControllerSata.createInstance( xmlNode );
		} else if ( controller instanceof ControllerScsi ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.SCSI.toString() );
			addedController = ControllerScsi.createInstance( xmlNode );
		} else if ( controller instanceof ControllerUsb ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.USB.toString() );
			addedController = ControllerUsb.createInstance( xmlNode );
		}

		return addedController;
	}

	/**
	 * Creates a controller device representing an existing Libvirt XML controller device element.
	 * 
	 * @param xmlNode existing Libvirt XML controller device element.
	 * @return controller device instance.
	 */
	public static Controller newInstance( LibvirtXmlNode xmlNode )
	{

		Controller deviceController = null;
		Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );

		if ( type == null ) {
			return null;
		}

		switch ( type ) {
		case FLOPPY:
			deviceController = ControllerFloppy.newInstance( xmlNode );
			break;
		case IDE:
			deviceController = ControllerIde.newInstance( xmlNode );
			break;
		case PCI:
			deviceController = ControllerPci.newInstance( xmlNode );
			break;
		case SATA:
			deviceController = ControllerSata.newInstance( xmlNode );
			break;
		case SCSI:
			deviceController = ControllerScsi.newInstance( xmlNode );
			break;
		case USB:
			deviceController = ControllerUsb.newInstance( xmlNode );
			break;
		}

		return deviceController;
	}

	/**
	 * Type of controller device.
	 * 
	 * @author Manuel Bentele
	 * @version 1.0
	 */
	enum Type
	{
		// @formatter:off
		FLOPPY( "fdc" ),
		IDE   ( "ide" ),
		PCI   ( "pci" ),
		SATA  ( "sata" ),
		SCSI  ( "scsi" ),
		USB   ( "usb" );
		// @formatter:on

		/**
		 * Name of the controller device type.
		 */
		private String type = null;

		/**
		 * Creates controller device type.
		 * 
		 * @param type valid name of the controller device type in a Libvirt domain XML document.
		 */
		Type( String type )
		{
			this.type = type;
		}

		@Override
		public String toString()
		{
			return this.type;
		}

		/**
		 * Creates controller device type from its name with error check.
		 * 
		 * @param type name of the controller device type in a Libvirt domain XML document.
		 * @return valid controller device type.
		 */
		public static Type fromString( String type )
		{
			for ( Type t : Type.values() ) {
				if ( t.type.equalsIgnoreCase( type ) ) {
					return t;
				}
			}

			return null;
		}
	}
}