summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/TransformationGenericInterfaceDevices.java
blob: 6cf12ce203d3d3205ec1b6027d8af6f437f37a9f (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
package org.openslx.runvirt.plugin.qemu.configuration;

import java.util.ArrayList;

import org.openslx.libvirt.domain.Domain;
import org.openslx.libvirt.domain.device.Interface;
import org.openslx.libvirt.domain.device.InterfaceBridge;
import org.openslx.libvirt.domain.device.Interface.Model;
import org.openslx.libvirt.domain.device.Interface.Type;
import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs;
import org.openslx.virtualization.configuration.VirtualizationConfigurationQemu;
import org.openslx.virtualization.configuration.VirtualizationConfigurationQemuUtils;
import org.openslx.virtualization.configuration.transformation.TransformationException;
import org.openslx.virtualization.configuration.transformation.TransformationGeneric;

/**
 * Generic network interface transformation for Libvirt/QEMU virtualization configurations.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class TransformationGenericInterfaceDevices extends TransformationGeneric<Domain, CommandLineArgs>
{
	/**
	 * Name of the configuration transformation.
	 */
	private static final String NAME = "Network interface devices";

	/**
	 * Creates a new network interface transformation for Libvirt/QEMU virtualization configurations.
	 */
	public TransformationGenericInterfaceDevices()
	{
		super( TransformationGenericInterfaceDevices.NAME );
	}

	/**
	 * Validates a virtualization configuration and input arguments for this transformation.
	 * 
	 * @param config virtualization configuration for the validation.
	 * @param args input arguments for the validation.
	 * @throws TransformationException validation has failed.
	 */
	private void validateInputs( Domain config, CommandLineArgs args ) throws TransformationException
	{
		if ( config == null || args == null ) {
			throw new TransformationException( "Virtualization configuration or input arguments are missing!" );
		}
	}

	/**
	 * Transforms a network interface in a virtualization configuration selected by its
	 * {@code index}.
	 * 
	 * @param config virtualization configuration for the transformation.
	 * @param macAddress MAC address for the network interface.
	 * @param index number of the network interface in the virtualization configuration that is
	 *           selected.
	 * @throws TransformationException transformation has failed.
	 */
	private void transformInterfaceDevice( Domain config, String macAddress, int index ) throws TransformationException
	{
		final ArrayList<Interface> devices = config.getInterfaceDevices();
		final Interface device = VirtualizationConfigurationQemuUtils.getArrayIndex( devices, index );

		if ( device == null ) {
			if ( macAddress != null && !macAddress.isEmpty() ) {
				// create network interface if it does not exists
				final InterfaceBridge newDevice = config.addInterfaceBridgeDevice();
				newDevice.setType( Type.BRIDGE );
				newDevice.setModel( Model.VIRTIO );
				newDevice.setMacAddress( macAddress );
				newDevice.setSource( VirtualizationConfigurationQemu.NETWORK_BRIDGE_NAT_DEFAULT );
			}
		} else {
			if ( macAddress == null || macAddress.isEmpty() ) {
				// remove network interface device if MAC address is not set
				device.remove();
			} else {
				// set MAC address of network interface device if network interface device is available
				device.setMacAddress( macAddress );
			}
		}
	}

	@Override
	public void transform( Domain config, CommandLineArgs args ) throws TransformationException
	{
		// validate configuration and input arguments
		this.validateInputs( config, args );

		// alter network interface
		this.transformInterfaceDevice( config, args.getVmMacAddress0(), 0 );

		// remove all additional disk storage devices
		final ArrayList<Interface> devices = config.getInterfaceDevices();
		for ( int i = 1; i < devices.size(); i++ ) {
			devices.get( i ).remove();
		}
	}
}