summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/Interface.java
blob: 44b8e4c0fea8ff05a133f3d7f6bc37df900783be (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package org.openslx.libvirt.domain.device;

import org.openslx.libvirt.xml.LibvirtXmlNode;

/**
 * A network interface device node in a Libvirt domain XML document.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class Interface extends Device
{
	/**
	 * Creates an empty network device.
	 */
	public Interface()
	{
		super();
	}

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

	/**
	 * Returns hardware model of the network device.
	 * 
	 * @return hardware model of the network device.
	 */
	public Model getModel()
	{
		String model = this.getXmlElementAttributeValue( "model", "type" );
		return Model.fromString( model );
	}

	/**
	 * Sets hardware model for the network device.
	 * 
	 * @param model hardware model for the network device.
	 */
	public void setModel( Model model )
	{
		this.setXmlElementAttributeValue( "model", "type", model.toString() );
	}

	/**
	 * Returns type of the network device.
	 * 
	 * @return type of the network device.
	 */
	public Type getType()
	{
		return Type.fromString( this.getXmlElementAttributeValue( "type" ) );
	}

	/**
	 * Sets type of the network device.
	 *  
	 * @param type network device type.
	 */
	public void setType( Type type )
	{
		String source = this.getSource();

		// change type and set source again
		this.setXmlElementAttributeValue( "type", type.toString() );
		this.setSource( source );
	}

	/**
	 * Returns the source of the network device.
	 * 
	 * @return source of the network device.
	 */
	public String getSource()
	{
		Type type = this.getType();
		String source = null;

		switch ( type ) {
		case BRIDGE:
			source = this.getXmlElementAttributeValue( "source", "bridge" );
			break;
		case NETWORK:
			source = this.getXmlElementAttributeValue( "source", "network" );
			break;
		}

		return source;
	}

	/**
	 * Sets the source for the network device.
	 * 
	 * @param source for the network device.
	 */
	public void setSource( String source )
	{
		Type type = this.getType();

		// remove all attributes from sub-element 'source'
		this.removeXmlElementAttributes( "source" );

		switch ( type ) {
		case BRIDGE:
			this.setXmlElementAttributeValue( "source", "bridge", source );
			break;
		case NETWORK:
			this.setXmlElementAttributeValue( "source", "network", source );
			break;
		}
	}

	/**
	 * Returns MAC address of the network device.
	 *
	 * @return MAC address of the network device.
	 */
	public String getMacAddress()
	{
		return this.getXmlElementAttributeValue( "mac", "address" );
	}

	/**
	 * Sets MAC address of the network device.
	 *
	 * @param macAddress MAC address for the network device.
	 */
	public void setMacAddress( String macAddress )
	{
		this.setXmlElementAttributeValue( "mac", "address", macAddress );
	}

	/**
	 * Removes boot oder entry of the network interface device.
	 */
	public void removeBootOrder()
	{
		this.removeXmlElement( "boot" );
	}

	/**
	 * Removes network source of the network interface device.
	 */
	public void removeSource()
	{
		this.removeXmlElement( "source" );
	}

	/**
	 * Removes MAC address of the network interface device.
	 */
	public void removeMacAddress()
	{
		this.removeXmlElement( "mac" );
	}

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

		if ( iface instanceof InterfaceBridge ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.BRIDGE.toString() );
			addedInterface = InterfaceBridge.createInstance( xmlNode );
		} else if ( iface instanceof InterfaceNetwork ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.NETWORK.toString() );
			addedInterface = InterfaceNetwork.createInstance( xmlNode );
		}

		return addedInterface;
	}

	/**
	 * Creates a network interface device representing an existing Libvirt XML network interface
	 * device element.
	 * 
	 * @param xmlNode existing Libvirt XML network interface device element.
	 * @return network interface device instance.
	 */
	public static Interface newInstance( LibvirtXmlNode xmlNode )
	{
		Interface deviceInterface = null;
		Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );

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

		switch ( type ) {
		case BRIDGE:
			deviceInterface = InterfaceBridge.newInstance( xmlNode );
			break;
		case NETWORK:
			deviceInterface = InterfaceNetwork.newInstance( xmlNode );
			break;
		}

		return deviceInterface;
	}

	/**
	 * Type of network interface device.
	 * 
	 * @author Manuel Bentele
	 * @version 1.0
	 */
	public enum Type
	{
		// @formatter:off
		BRIDGE ( "bridge" ),
		NETWORK( "network" );
		// @formatter:on

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

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

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

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

			return null;
		}
	}

	/**
	 * Model of network interface device.
	 * 
	 * @author Manuel Bentele
	 * @version 1.0
	 */
	public enum Model
	{
		// @formatter:off
		E1000                          ( "e1000" ),
		E1000_82544GC                  ( "e1000-82544gc" ),
		E1000_82545EM                  ( "e1000-82545em" ),
		E1000E                         ( "e1000e" ),
		I82550                         ( "i82550" ), 
		I82551                         ( "i82551" ), 
		I82557A                        ( "i82557a" ), 
		I82557B                        ( "i82557b" ), 
		I82557C                        ( "i82557c" ), 
		I82558A                        ( "i82558a" ),
		I82558B                        ( "i82558b" ),
		I82559A                        ( "i82559a" ),
		I82559B                        ( "i82559b" ),
		I82559C                        ( "i82559c" ),
		I82559ER                       ( "i82559er" ),
		I82562                         ( "i82562" ),
		I82801                         ( "i82801" ),
		NE2K_PCI                       ( "ne2k_pci" ),
		PCNET                          ( "pcnet" ),
		RTL8139                        ( "rtl8139" ),
		TULIP                          ( "tulip" ),
		VIRTIO                         ( "virtio" ),
		VIRTIO_NET_PCI                 ( "virtio-net-pci" ),
		VIRTIO_NET_PCI_NON_TRANSITIONAL( "virtio-net-pci-non-transitional" ),
		VIRTIO_NET_PCI_TRANSITIONAL    ( "virtio-net-pci-transitional" ),
		VMXNET3                        ( "vmxnet3" );
		// @formatter:on

		/**
		 * Name of the network interface device model.
		 */
		private String model = null;

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

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

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

			return null;
		}
	}
}