From 2bfddcf3155679e50ff188abe377a20232128632 Mon Sep 17 00:00:00 2001 From: Stephan Schwär Date: Thu, 8 Oct 2020 09:34:02 +0900 Subject: Add meta and config files for ovf format --- src/main/java/org/openslx/util/vm/OvfConfig.java | 107 +++++++++++ src/main/java/org/openslx/util/vm/OvfMetaData.java | 207 +++++++++++++++++++++ src/main/java/org/openslx/util/vm/VmMetaData.java | 5 + 3 files changed, 319 insertions(+) create mode 100644 src/main/java/org/openslx/util/vm/OvfConfig.java create mode 100644 src/main/java/org/openslx/util/vm/OvfMetaData.java diff --git a/src/main/java/org/openslx/util/vm/OvfConfig.java b/src/main/java/org/openslx/util/vm/OvfConfig.java new file mode 100644 index 0000000..3055075 --- /dev/null +++ b/src/main/java/org/openslx/util/vm/OvfConfig.java @@ -0,0 +1,107 @@ +package org.openslx.util.vm; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; +import javax.xml.xpath.XPathExpressionException; + +import org.apache.log4j.Logger; +import org.openslx.util.Util; +import org.openslx.util.XmlHelper; +import org.openslx.util.vm.VmMetaData.HardDisk; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + + +/** + * Class handling the parsing of a .ovf machine description file + * For now, only used to parse the xml to get the path of the vmdk + * from the ovf for sanity checking and cleanup after conversion. + */ +public class OvfConfig +{ + private static final Logger LOGGER = Logger.getLogger( OvfConfig.class ); + + // key information set during initial parsing of the XML file + private String osName = new String(); + private ArrayList hddsArray = new ArrayList(); + + // XPath and DOM parsing related members + private Document doc = null; + + + public OvfConfig( File file ) throws IOException, UnsupportedVirtualizerFormatException + { + LOGGER.info("Entering OvfConfig class creation"); + // first validate xml + // try { + // SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); + // InputStream xsdStream = OvfConfig.class.getResourceAsStream( "/master-sync-shared/xml/ovf_1.1.1.xsd" ); + // LOGGER.info("xsdStream creation worked"); + // if ( xsdStream == null ) { + // LOGGER.warn( "Cannot validate ovf XML: No XSD found in JAR" ); + // } else { + // LOGGER.info("Before schema creation"); + // Schema schema = factory.newSchema( new StreamSource( xsdStream ) ); + // LOGGER.info("Created schema class"); + // Validator validator = schema.newValidator(); + // LOGGER.info("Created validator class"); + // validator.validate( new StreamSource( file ) ); + // LOGGER.info("Validated"); + // } + // } catch ( SAXException e ) { + // LOGGER.error( "Selected ovf file was not validated against the XSD schema: " + e.getMessage() ); + // } + LOGGER.info("XSD validation worked."); + // valid xml, try to create the DOM + doc = XmlHelper.parseDocumentFromStream( new FileInputStream( file ) ); + doc = XmlHelper.removeFormattingNodes( doc ); + if ( doc == null ) + throw new UnsupportedVirtualizerFormatException( "Could not create DOM from given ovf machine configuration file!" ); + LOGGER.info("DOM creation worked"); + init(); + } + + /** + * Main initialization functions parsing the document created during the constructor. + * @throws UnsupportedVirtualizerFormatException + */ + private void init() throws UnsupportedVirtualizerFormatException + { + LOGGER.info("Entering OvfConfig init"); + if ( Util.isEmptyString( getDisplayName() ) ) { + throw new UnsupportedVirtualizerFormatException( "Machine doesn't have a name" ); + } + LOGGER.info( getDisplayName() ); + // try { + + // setHdds(); + // } catch ( XPathExpressionException e ) { + // LOGGER.debug( "Could not initialize VBoxConfig", e ); + // return; + // } + } + + /** + * Getter for the display name + * + * @return the display name of this VM + */ + public String getDisplayName() + { + try { + return XmlHelper.XPath.compile( "/Envelope/VirtualSystem/Name" ).evaluate( this.doc ); + } catch ( XPathExpressionException e ) { + return ""; + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/openslx/util/vm/OvfMetaData.java b/src/main/java/org/openslx/util/vm/OvfMetaData.java new file mode 100644 index 0000000..f5a45bf --- /dev/null +++ b/src/main/java/org/openslx/util/vm/OvfMetaData.java @@ -0,0 +1,207 @@ +package org.openslx.util.vm; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import java.util.Map.Entry; +import java.util.UUID; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.thrifthelper.TConst; +import org.openslx.util.vm.VboxConfig.PlaceHolder; +import org.w3c.dom.Attr; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + + +public class OvfMetaData extends VmMetaData { + + private static final Logger LOGGER = Logger.getLogger( OvfMetaData.class ); + + private final OvfConfig config; + + public OvfMetaData( List osList, File file ) throws IOException, UnsupportedVirtualizerFormatException + { + super( osList ); + LOGGER.info("Constructor worked"); + this.config = new OvfConfig( file ); + LOGGER.info("Creating config worked"); + init(); + LOGGER.info("Init worked"); + } + + private void init() + { + registerVirtualHW(); + displayName = config.getDisplayName(); + // setOs( config.getOsName() ); + // this.isMachineSnapshot = config.isMachineSnapshot(); + // for ( HardDisk hardDisk : config.getHdds() ) { + // hdds.add( hardDisk ); + // } + } + + @Override + public byte[] getFilteredDefinitionArray() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void applySettingsForLocalEdit() { + // TODO Auto-generated method stub + + } + + @Override + public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addDefaultNat() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void setOs(String vendorOsId) { + // TODO Auto-generated method stub + + } + + @Override + public boolean addDisplayName(String name) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addRam(int mem) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void addFloppy(int index, String image, boolean readOnly) { + // TODO Auto-generated method stub + + } + + @Override + public boolean addCdrom(String image) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addCpuCoreCount(int nrOfCores) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void setSoundCard(SoundCardType type) { + // TODO Auto-generated method stub + + } + + @Override + public SoundCardType getSoundCard() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setDDAcceleration(DDAcceleration type) { + // TODO Auto-generated method stub + + } + + @Override + public DDAcceleration getDDAcceleration() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setHWVersion(HWVersion type) { + // TODO Auto-generated method stub + + } + + @Override + public HWVersion getHWVersion() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setEthernetDevType(int cardIndex, EthernetDevType type) { + // TODO Auto-generated method stub + + } + + @Override + public EthernetDevType getEthernetDevType(int cardIndex) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setMaxUsbSpeed(UsbSpeed speed) { + // TODO Auto-generated method stub + + } + + @Override + public UsbSpeed getMaxUsbSpeed() { + // TODO Auto-generated method stub + return null; + } + + @Override + public byte[] getDefinitionArray() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean addEthernet(EtherType type) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Virtualizer getVirtualizer() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean tweakForNonPersistent() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void registerVirtualHW() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java index c836697..e3b78c8 100644 --- a/src/main/java/org/openslx/util/vm/VmMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmMetaData.java @@ -312,6 +312,11 @@ public abstract class VmMetaData } catch ( Exception e ) { LOGGER.info( "Not a QEmu file", e ); } + try { + return new OvfMetaData( osList, file ); + } catch ( Exception e ) { + LOGGER.info( "Not an ovf file", e ); + } try { // TODO This will work for each file because simple read as byte array // TODO No checks if file is a dockerfile --- THIS SHOOULD NOT BE IN PRODUCTION -- cgit v1.2.3-55-g7522