From 09d48d1daf3243907dd74fd5d8078d96c1ee5597 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 5 Jul 2021 18:44:48 +0200 Subject: [virtualizer] Get rid of --- .../java/org/openslx/virtualization/Version.java | 11 +- .../configuration/VirtualizationConfiguration.java | 210 +++----- .../VirtualizationConfigurationDocker.java | 66 +-- .../VirtualizationConfigurationQemu.java | 557 +++++++++------------ .../VirtualizationConfigurationQemuUtils.java | 78 --- .../VirtualizationConfigurationVirtualBox.java | 378 +++++++------- .../VirtualizationConfigurationVmware.java | 336 +++++++------ .../configuration/logic/ConfigurationLogic.java | 2 +- ...nfigurationLogicDozModClientToDozModServer.java | 4 +- ...nfigurationLogicDozModServerToDozModClient.java | 9 +- ...gurationLogicDozModServerToStatelessClient.java | 8 +- .../hardware/AbstractConfigurableOption.java | 42 ++ .../hardware/ConfigurationGroups.java | 20 + .../openslx/virtualization/hardware/Ethernet.java | 20 + .../openslx/virtualization/hardware/SoundCard.java | 13 + .../org/openslx/virtualization/hardware/Usb.java | 11 + .../VirtualizationConfigurationQemuTest.java | 129 +---- ...urationLogicDozModClientToDozModServerTest.java | 6 +- ...urationLogicDozModServerToDozModClientTest.java | 6 +- ...tionLogicDozModServerToStatelessClientTest.java | 6 +- .../logic/ConfigurationLogicTestUtils.java | 4 +- 21 files changed, 808 insertions(+), 1108 deletions(-) create mode 100644 src/main/java/org/openslx/virtualization/hardware/AbstractConfigurableOption.java create mode 100644 src/main/java/org/openslx/virtualization/hardware/ConfigurationGroups.java create mode 100644 src/main/java/org/openslx/virtualization/hardware/Ethernet.java create mode 100644 src/main/java/org/openslx/virtualization/hardware/SoundCard.java create mode 100644 src/main/java/org/openslx/virtualization/hardware/Usb.java diff --git a/src/main/java/org/openslx/virtualization/Version.java b/src/main/java/org/openslx/virtualization/Version.java index 51dc98b..c823324 100644 --- a/src/main/java/org/openslx/virtualization/Version.java +++ b/src/main/java/org/openslx/virtualization/Version.java @@ -5,6 +5,8 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.openslx.util.Util; + /** * Represents a version information. * @@ -28,7 +30,7 @@ public class Version implements Comparable * 5.10.13 * */ - private static final String VERSION_NUMBER_REGEX = "^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$"; + private static final Pattern VERSION_NUMBER_REGEX = Pattern.compile( "^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$" ); /** * Major number of the version. @@ -236,11 +238,10 @@ public class Version implements Comparable { final Version parsedVersion; - if ( version == null || version.isEmpty() ) { + if ( Util.isEmptyString( version ) ) { parsedVersion = null; } else { - final Pattern versionPattern = Pattern.compile( Version.VERSION_NUMBER_REGEX ); - final Matcher versionMatcher = versionPattern.matcher( version ); + final Matcher versionMatcher = VERSION_NUMBER_REGEX.matcher( version ); if ( versionMatcher.find() ) { final String majorStr = versionMatcher.group( 1 ); @@ -262,7 +263,7 @@ public class Version implements Comparable @Override public String toString() { - if ( this.getName() == null || this.getName().isEmpty() ) { + if ( Util.isEmptyString( this.getName() ) ) { return String.format( "%d.%d", this.getMajor(), this.getMinor() ); } else { return String.format( "%d.%d %s", this.getMajor(), this.getMinor(), this.getName() ); diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfiguration.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfiguration.java index cd8af1e..15a409f 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfiguration.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfiguration.java @@ -5,95 +5,25 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.virtualization.Version; +import org.openslx.virtualization.hardware.AbstractConfigurableOption; +import org.openslx.virtualization.hardware.ConfigurationGroups; import org.openslx.virtualization.virtualizer.Virtualizer; /** * Describes a configured virtual machine. This class is parsed from a machine * description, like a *.vmx for VMware machines. */ -public abstract class VirtualizationConfiguration +public abstract class VirtualizationConfiguration { private static final Logger LOGGER = Logger.getLogger( VirtualizationConfiguration.class ); - /* - * Helper types - */ - protected Map soundCards = new HashMap<>(); - protected Map ddacc = new HashMap<>(); - protected Map networkCards = new HashMap<>(); - protected Map usbSpeeds = new HashMap<>(); - private final Virtualizer virtualizer; - /** - * Virtual sound cards types - */ - public static enum SoundCardType - { - NONE( "None" ), DEFAULT( "(default)" ), SOUND_BLASTER( "Sound Blaster 16" ), ES( "ES 1371" ), HD_AUDIO( - "Intel Integrated HD Audio" ), AC( "Intel ICH Audio Codec 97" ); - - public final String displayName; - - private SoundCardType( String dName ) - { - this.displayName = dName; - } - } - - /** - * 3D acceleration types - */ - public static enum DDAcceleration - { - OFF( "Off" ), ON( "On" ); - - public final String displayName; - - private DDAcceleration( String dName ) - { - this.displayName = dName; - } - } - - /** - * Virtual network cards - */ - public static enum EthernetDevType - { - AUTO( "(default)" ), PCNET32( "AMD PCnet32" ), E1000( "Intel E1000 (PCI)" ), E1000E( - "Intel E1000e (PCI-Express)" ), VMXNET( "VMXnet" ), VMXNET3( "VMXnet 3" ), PCNETPCI2( - "PCnet-PCI II" ), PCNETFAST3( "PCnet-FAST III" ), PRO1000MTD( - "Intel PRO/1000 MT Desktop" ), PRO1000TS( - "Intel PRO/1000 T Server" ), PRO1000MTS( "Intel PRO/1000 MT Server" ), PARAVIRT( - "Paravirtualized Network" ), NONE( "No Network Card" ); - - public final String displayName; - - private EthernetDevType( String dName ) - { - this.displayName = dName; - } - } - - public static enum UsbSpeed - { - NONE( "None" ), USB1_1( "USB 1.1" ), USB2_0( "USB 2.0" ), USB3_0( "USB 3.0" ); - - public final String displayName; - - private UsbSpeed( String dName ) - { - this.displayName = dName; - } - } public static enum DriveBusType { @@ -118,6 +48,30 @@ public abstract class VirtualizationConfiguration { NAT, BRIDGED, HOST_ONLY; } + + public static class ConfigurableOptionGroup + { + public final ConfigurationGroups groupIdentifier; // Enum? + + public final List availableOptions; + + public ConfigurableOptionGroup( ConfigurationGroups groupIdentifier, List availableOptions ) + { + this.groupIdentifier = groupIdentifier; + this.availableOptions = Collections.unmodifiableList( availableOptions ); + } + + public AbstractConfigurableOption getSelected() + { + for (AbstractConfigurableOption hw : availableOptions) { + if ( hw.isActive() ) + return hw; + } + return null; + } + + } + /* * Members */ @@ -131,44 +85,8 @@ public abstract class VirtualizationConfiguration protected String displayName = null; protected boolean isMachineSnapshot; - - /* - * Getters for virtual hardware - */ - public List getSupportedSoundCards() - { - ArrayList availables = new ArrayList( soundCards.keySet() ); - Collections.sort( availables ); - return availables; - } - - public List getSupportedDDAccs() - { - ArrayList availables = new ArrayList( ddacc.keySet() ); - Collections.sort( availables ); - return availables; - } - - public List getSupportedHWVersions() - { - final List availables = this.getVirtualizer().getSupportedVersions(); - Collections.sort( availables ); - return Collections.unmodifiableList( availables ); - } - - public List getSupportedEthernetDevices() - { - ArrayList availables = new ArrayList( networkCards.keySet() ); - Collections.sort( availables ); - return availables; - } - - public List getSupportedUsbSpeeds() - { - ArrayList availables = new ArrayList<>( usbSpeeds.keySet() ); - Collections.sort( availables ); - return availables; - } + + protected final List configurableOptions = new ArrayList<>(); /** * Get operating system of this VM. @@ -217,6 +135,29 @@ public abstract class VirtualizationConfiguration { return isMachineSnapshot; } + + private class VersionOption extends AbstractConfigurableOption { + + private final Version version; + + public VersionOption( Version version ) + { + super( Integer.toString( version.getVersion() ), version.getName() ); + this.version = version; + } + + @Override + public boolean isActive() + { + return getVirtualizerVersion().equals( version ); + } + + @Override + public void apply() + { + setVirtualizerVersion( version ); + } + } /* * Methods @@ -234,6 +175,16 @@ public abstract class VirtualizationConfiguration } // register virtual hardware models for graphical editing of virtual devices (GPU, sound, USB, ...) + final List availables = this.getVirtualizer().getSupportedVersions(); + //Collections.sort( availables ); // XXX WTF? How did this not break before? It's an unmodifiable collection + if ( availables != null ) { + // XXX List is null for qemu? + List list = new ArrayList<>(); + for ( Version ver : availables ) { + list.add( new VersionOption( ver ) ); + } + configurableOptions.add( new ConfigurableOptionGroup( ConfigurationGroups.HW_VERSION, list ) ); + } this.registerVirtualHW(); } @@ -245,7 +196,7 @@ public abstract class VirtualizationConfiguration * @return VmMetaData object representing the relevant parts of the given machine description * @throws IOException failed to read machine description from specified file. */ - public static VirtualizationConfiguration getInstance( List osList, File file ) + public static VirtualizationConfiguration getInstance( List osList, File file ) throws IOException { try { @@ -281,10 +232,11 @@ public abstract class VirtualizationConfiguration * @param length length of the byte array given as vmContent * @return VmMetaData object representing the relevant parts of the given machine description * @throws IOException failed to read machine description from specified byte stream. + * @throws VirtualizationConfigurationException */ - public static VirtualizationConfiguration getInstance( List osList, byte[] vmContent, + public static VirtualizationConfiguration getInstance( List osList, byte[] vmContent, int length ) - throws IOException + throws IOException, VirtualizationConfigurationException { try { return new VirtualizationConfigurationVmware( osList, vmContent, length ); @@ -306,9 +258,7 @@ public abstract class VirtualizationConfiguration } catch ( VirtualizationConfigurationException e ) { LOGGER.debug( "Not a tar.gz file, for docker container", e ); } - - LOGGER.error( "Could not detect any known virtualizer format" ); - return null; + throw new VirtualizationConfigurationException( "Unknown virtualizer config format" ); } /** @@ -337,27 +287,11 @@ public abstract class VirtualizationConfiguration public abstract boolean addCdrom( String image ); public abstract boolean addCpuCoreCount( int nrOfCores ); - - public abstract void setSoundCard( SoundCardType type ); - - public abstract SoundCardType getSoundCard(); - - public abstract void setDDAcceleration( DDAcceleration type ); - - public abstract DDAcceleration getDDAcceleration(); - + public abstract void setVirtualizerVersion( Version type ); public abstract Version getVirtualizerVersion(); - public abstract void setEthernetDevType( int cardIndex, EthernetDevType type ); - - public abstract EthernetDevType getEthernetDevType( int cardIndex ); - - public abstract void setMaxUsbSpeed( UsbSpeed speed ); - - public abstract UsbSpeed getMaxUsbSpeed(); - public abstract byte[] getConfigurationAsByteArray(); public String getConfigurationAsString() @@ -418,4 +352,12 @@ public abstract class VirtualizationConfiguration * Function used to register virtual devices. */ public abstract void registerVirtualHW(); + + /** + * Get all config options this virtualizer supports, with all available options. + */ + public List getConfigurableOptions() + { + return Collections.unmodifiableList( configurableOptions ); + } } diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationDocker.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationDocker.java index d5e9abb..ea888e4 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationDocker.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationDocker.java @@ -1,33 +1,17 @@ package org.openslx.virtualization.configuration; -import org.apache.log4j.Logger; -import org.openslx.bwlp.thrift.iface.OperatingSystem; -import org.openslx.virtualization.Version; -import org.openslx.virtualization.virtualizer.VirtualizerDocker; - import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.List; -class DockerSoundCardMeta -{ -} - -class DockerDDAccelMeta -{ -} - -class DockerEthernetDevTypeMeta -{ -} - -class DockerUsbSpeedMeta -{ -} +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.virtualization.Version; +import org.openslx.virtualization.virtualizer.VirtualizerDocker; -public class VirtualizationConfigurationDocker extends VirtualizationConfiguration { +public class VirtualizationConfigurationDocker extends VirtualizationConfiguration { /** * File name extension for Docker virtualization configuration files. @@ -145,46 +129,15 @@ public class VirtualizationConfigurationDocker extends VirtualizationConfigurati return false; } - @Override public void setSoundCard(SoundCardType type) { - - } - - @Override public SoundCardType getSoundCard() { - return SoundCardType.NONE; - } - - @Override public void setDDAcceleration(DDAcceleration type) { - - } - - @Override public DDAcceleration getDDAcceleration() { - return DDAcceleration.OFF; - } - - @Override public void setVirtualizerVersion(Version type) { - + @Override public void setVirtualizerVersion( Version type ) + { } - @Override public Version getVirtualizerVersion() { + @Override public Version getVirtualizerVersion() + { return null; } - @Override public void setEthernetDevType(int cardIndex, EthernetDevType type) { - - } - - @Override public EthernetDevType getEthernetDevType(int cardIndex) { - return EthernetDevType.NONE; - } - - @Override public void setMaxUsbSpeed(UsbSpeed speed) { - - } - - @Override public UsbSpeed getMaxUsbSpeed() { - return UsbSpeed.NONE; - } - @Override public byte[] getConfigurationAsByteArray() { return this.containerDefinition; } @@ -210,4 +163,5 @@ public class VirtualizationConfigurationDocker extends VirtualizationConfigurati public void validate() throws VirtualizationConfigurationException { } + } diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java index b4bc0ba..19ad406 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java @@ -5,7 +5,6 @@ import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import java.util.Map.Entry; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.libvirt.domain.Domain; @@ -13,8 +12,6 @@ import org.openslx.libvirt.domain.DomainUtils; import org.openslx.libvirt.domain.device.ControllerUsb; import org.openslx.libvirt.domain.device.Disk.BusType; import org.openslx.libvirt.domain.device.Disk.StorageType; -import org.openslx.libvirt.libosinfo.LibOsInfo; -import org.openslx.libvirt.libosinfo.os.Os; import org.openslx.libvirt.domain.device.DiskCdrom; import org.openslx.libvirt.domain.device.DiskFloppy; import org.openslx.libvirt.domain.device.DiskStorage; @@ -23,174 +20,28 @@ import org.openslx.libvirt.domain.device.GraphicsSpice; import org.openslx.libvirt.domain.device.Interface; import org.openslx.libvirt.domain.device.Sound; import org.openslx.libvirt.domain.device.Video; +import org.openslx.libvirt.libosinfo.LibOsInfo; +import org.openslx.libvirt.libosinfo.os.Os; import org.openslx.libvirt.xml.LibvirtXmlDocumentException; import org.openslx.libvirt.xml.LibvirtXmlSerializationException; import org.openslx.libvirt.xml.LibvirtXmlValidationException; import org.openslx.util.LevenshteinDistance; +import org.openslx.util.Util; import org.openslx.virtualization.Version; +import org.openslx.virtualization.hardware.AbstractConfigurableOption; +import org.openslx.virtualization.hardware.ConfigurationGroups; +import org.openslx.virtualization.hardware.Ethernet; +import org.openslx.virtualization.hardware.SoundCard; +import org.openslx.virtualization.hardware.Usb; import org.openslx.virtualization.virtualizer.VirtualizerQemu; -/** - * Metadata to describe the hardware type of a QEMU sound card. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuSoundCardMeta -{ - /** - * Stores the hardware model of the QEMU sound card. - */ - private final Sound.Model model; - - /** - * Creates metadata to describe the hardware model of a QEMU sound card. - * - * @param model hardware model of the QEMU sound card. - */ - public QemuSoundCardMeta( Sound.Model model ) - { - this.model = model; - } - - /** - * Returns hardware model of the QEMU sound card. - * - * @return hardware model of the QEMU sound card. - */ - public Sound.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuDDAccelMeta -{ - /** - * Stores state of the hardware acceleration for QEMU virtual graphics. - */ - private final boolean enabled; - - /** - * Creates metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @param enabled state of the hardware acceleration for QEMU virtual graphics. - */ - public QemuDDAccelMeta( boolean enabled ) - { - this.enabled = enabled; - } - - /** - * Returns state of the hardware acceleration of QEMU virtual graphics. - * - * @return state of the hardware acceleration for QEMU virtual graphics. - */ - public boolean isEnabled() - { - return this.enabled; - } -} - -/** - * Metadata to describe the hardware type of a QEMU ethernet device. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuEthernetDevTypeMeta -{ - /** - * Stores the hardware model of the QEMU ethernet device. - */ - private final Interface.Model model; - - /** - * Creates metadata to describe the hardware type of a QEMU ethernet device. - * - * @param model hardware type of the QEMU ethernet device. - */ - public QemuEthernetDevTypeMeta( Interface.Model model ) - { - this.model = model; - } - - /** - * Returns the hardware type of a QEMU ethernet device. - * - * @return hardware type of the QEMU ethernet device. - */ - public Interface.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe a QEMU USB controller. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuUsbSpeedMeta -{ - /** - * Stores the USB speed of the QEMU USB controller. - */ - private final int speed; - - /** - * Stores the QEMU hardware model of the USB controller. - */ - private final ControllerUsb.Model model; - - /** - * Creates metadata to describe a QEMU USB controller. - * - * @param speed USB speed of the QEMU USB controller. - * @param model QEMU hardware model of the USB controller. - */ - public QemuUsbSpeedMeta( int speed, ControllerUsb.Model model ) - { - this.speed = speed; - this.model = model; - } - - /** - * Returns the speed of the QEMU USB controller. - * - * @return speed of the QEMU USB controller. - */ - public int getSpeed() - { - return this.speed; - } - - /** - * Returns QEMU hardware model of the USB controller. - * - * @return hardware model of the QEMU USB controller. - */ - public ControllerUsb.Model getModel() - { - return this.model; - } -} - /** * Virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. * * @author Manuel Bentele * @version 1.0 */ -public class VirtualizationConfigurationQemu extends - VirtualizationConfiguration +public class VirtualizationConfigurationQemu extends VirtualizationConfiguration { /** * Name of the network bridge for the LAN. @@ -558,129 +409,98 @@ public class VirtualizationConfigurationQemu extends return isVCpuSet; } - - @Override - public void setSoundCard( SoundCardType type ) + + class QemuGfxType extends AbstractConfigurableOption { - QemuSoundCardMeta soundDeviceConfig = this.soundCards.get( type ); - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - Sound.Model soundDeviceModel = soundDeviceConfig.getModel(); - if ( soundDevices.isEmpty() ) { - // create new sound device with 'soundDeviceModel' hardware - Sound soundDevice = this.vmConfig.addSoundDevice(); - soundDevice.setModel( soundDeviceModel ); - } else { - // update sound device model type of existing sound devices - for ( Sound soundDevice : soundDevices ) { - soundDevice.setModel( soundDeviceModel ); - } + public QemuGfxType( String id, String displayName ) + { + super( id, displayName ); } - } - - @Override - public SoundCardType getSoundCard() - { - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - SoundCardType soundDeviceType = SoundCardType.DEFAULT; - - if ( soundDevices.isEmpty() ) { - // the VM configuration does not contain a sound card device - soundDeviceType = SoundCardType.NONE; - } else { - // the VM configuration at least one sound card device, so return the type of the first one - Sound.Model soundDeviceModel = soundDevices.get( 0 ).getModel(); - soundDeviceType = VirtualizationConfigurationQemuUtils.convertSoundDeviceModel( soundDeviceModel ); - } - - return soundDeviceType; - } - @Override - public void setDDAcceleration( DDAcceleration type ) - { - QemuDDAccelMeta accelerationConfig = this.ddacc.get( type ); - ArrayList graphicDevices = this.vmConfig.getGraphicDevices(); - ArrayList