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

import java.util.ArrayList;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.openslx.libvirt.domain.Domain;
import org.openslx.libvirt.domain.device.Serial.Type;
import org.openslx.libvirt.domain.device.Serial;
import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs;
import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu;
import org.openslx.virtualization.configuration.VirtualizationConfigurationQemuUtils;
import org.openslx.virtualization.configuration.transformation.TransformationException;
import org.openslx.virtualization.configuration.transformation.TransformationSpecific;

/**
 * Specific serial device transformation for Libvirt/QEMU virtualization configurations.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class TransformationSpecificQemuSerialDevices
		extends TransformationSpecific<Domain, CommandLineArgs, LibvirtHypervisorQemu>
{
	/**
	 * Name of the configuration transformation.
	 */
	private static final String NAME = "Serial devices";

	/**
	 * Creates a new serial device transformation for Libvirt/QEMU virtualization configurations.
	 * 
	 * @param hypervisor Libvirt/QEMU hypervisor.
	 */
	public TransformationSpecificQemuSerialDevices( LibvirtHypervisorQemu hypervisor )
	{
		super( TransformationSpecificQemuSerialDevices.NAME, hypervisor );
	}

	/**
	 * 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!" );
		}
	}

	/**
	 * Returns all serial devices from a virtualization configuration that link to a host system's
	 * serial device.
	 * 
	 * @param config virtualization configuration.
	 * @return all serial devices that link to a host system's serial device.
	 */
	private ArrayList<Serial> getSerialDevDevices( Domain config )
	{
		final ArrayList<Serial> devices = config.getSerialDevices();
		final Predicate<Serial> byDeviceTypeDev = device -> device.getType() == Type.DEV;

		return devices.stream().filter( byDeviceTypeDev ).collect( Collectors.toCollection( ArrayList::new ) );
	}

	/**
	 * Transforms a serial device in a virtualization configuration selected by its {@code index}.
	 * 
	 * @param config virtualization configuration for the transformation.
	 * @param fileName path to the serial device file on the host system.
	 * @param index number of the serial device in the virtualization configuration that is selected.
	 * @throws TransformationException transformation has failed.
	 */
	private void transformSerialDevice( Domain config, String fileName, int index ) throws TransformationException
	{
		final ArrayList<Serial> devices = this.getSerialDevDevices( config );
		final Serial device = VirtualizationConfigurationQemuUtils.getArrayIndex( devices, index );

		if ( device == null ) {
			// check if device file name is specified
			if ( fileName != null && !fileName.isEmpty() ) {
				// serial port device is not available, so create new serial port device
				final Serial newDevice = config.addSerialDevice();
				newDevice.setType( Type.DEV );
				newDevice.setSource( fileName );
			}
		} else {
			if ( fileName == null || fileName.isEmpty() ) {
				// remove serial port device if device file name is not set
				device.remove();
			} else {
				// set type and source of existing serial port device
				device.setType( Type.DEV );
				device.setSource( fileName );
			}
		}
	}

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

		// alter serial device
		this.transformSerialDevice( config, args.getVmDeviceSerial0(), 0 );

		// remove all additional serial devices
		final ArrayList<Serial> devices = this.getSerialDevDevices( config );
		for ( int i = 1; i < devices.size(); i++ ) {
			devices.get( i ).remove();
		}
	}
}