diff options
author | Christopher Lucas | 2018-12-06 17:44:42 +0100 |
---|---|---|
committer | Christopher Lucas | 2018-12-06 17:44:42 +0100 |
commit | 50c0a1ad622f39f5e9e5d7e5654be86c2086bfee (patch) | |
tree | c6a3248129058711a591d3b3855a794f73c8ea85 /src/main | |
parent | Learning git (diff) | |
download | master-sync-shared-50c0a1ad622f39f5e9e5d7e5654be86c2086bfee.tar.gz master-sync-shared-50c0a1ad622f39f5e9e5d7e5654be86c2086bfee.tar.xz master-sync-shared-50c0a1ad622f39f5e9e5d7e5654be86c2086bfee.zip |
Optimized Upload with new Map and Enum system
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/openslx/util/vm/QemuConfig.java | 275 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/QemuMetaData.java | 132 |
2 files changed, 223 insertions, 184 deletions
diff --git a/src/main/java/org/openslx/util/vm/QemuConfig.java b/src/main/java/org/openslx/util/vm/QemuConfig.java index 358fa1f..8348790 100644 --- a/src/main/java/org/openslx/util/vm/QemuConfig.java +++ b/src/main/java/org/openslx/util/vm/QemuConfig.java @@ -20,29 +20,47 @@ public final class QemuConfig { private static final Logger LOGGER = Logger.getLogger(QemuConfig.class); - private Map<String, TreeMap<String, String>> entries = new LinkedHashMap<>(); + private Map<LinkedHashMap<String, String>, TreeMap<String, String>> entries = new LinkedHashMap<>(); private String osName = new String(); + private final String quote = "\""; + private ArrayList<VmMetaData.HardDisk> hddsArray = new ArrayList<>(); - + // BOOT("netdev", "net0") -> toString -> "[netdev "net0"]" public static enum Header { - BOOT("[boot-opts]", "asd"), DEV("[dev"), DEVICE("[device]"), DRIVE("[drive"), - MACHINE("[machine]"), MEMORY("[memory]"), NAME("[name]"), - NETDEV("[netdev "), SMP("[smp-opts]"); - + BOOT("boot-opts", null), DEVICE("device", ""), DRIVE("drive", ""), + MACHINE("machine", null), MEMORY("memory", null), NAME("name", null), + NETDEV("netdev", ""), SMP("smp-opts", null); + private final String header; - - private Header(String header. String id){ + + private String id; + + private Header(String header, String id) { this.header = header; + this.id = id; } - - public String value(){ + + public String getHeader() { return this.header; } + + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + @Override + public String toString() { + return "[" + header + " \"" + id + "\"]"; + } } - + public QemuConfig(byte[] vmContent, int length) { //transform byte[] to arralist sorted ArrayList<String> result; @@ -57,8 +75,8 @@ public final class QemuConfig { while ((line = stream.readLine()) != null) { if (line.startsWith("#") == false) { if (line.isEmpty() == false) { - if (line.contains(DEV.value())) { - result.add(DEVICE.value()); + if (line.contains(DEVICE.getHeader())) { + result.add(DEVICE.getHeader()); } else { result.add(line.trim()); } @@ -118,73 +136,95 @@ public final class QemuConfig { public void init(ArrayList<String> lines) { int index = -1; int nbDev = 0; - Device dev; - TreeMap<String, String> options = new TreeMap<>(); - if (lines != null) { + int nbDrive = 0; + ArrayList<String> listID = new ArrayList<>(); + LinkedHashMap<String, String> headers = null; + TreeMap<String, String> options = null; + boolean exist = false; + if (lines != null) { + //Adding all the lines to a Map for (String option : lines) { - - if (option.startsWith("[")) { //key only - if (option.equals(Header.DEVICE.value())) { - dev = new Device(); - dev.setName(DEV.value()+ nbDev + "]"); - option = dev.getName(); + if (option.startsWith("[") && option.endsWith("]")) { + option = option.replaceAll("\\[|\\]", ""); + headers = new LinkedHashMap<>(); + if (option.contains(DRIVE.getHeader()) && option.contains(quote)) { //Check drive with id + String[] drive = option.split(quote); + for (String id : listID) { + if (drive[1].equals(id)) { + DRIVE.setID("id-disk-" + nbDrive); + listID.add(DRIVE.getID()); + exist = true; + } + } + if (!exist) { + DRIVE.setID(drive[1]); + listID.add(DRIVE.getID()); + } + headers.put(DRIVE.getHeader(), DRIVE.getID()); + nbDrive++; + } else if (option.equals(DRIVE.getHeader())) {//Check drive without id + DRIVE.setID("id-disk-" + nbDrive); + listID.add(DRIVE.getID()); + headers.put(option, DRIVE.getID()); + nbDrive++; + // } else if (option.equals(NETDEV.toString())){//Check drive without id TODO + } else if (option.equals(DEVICE.getHeader())) {//This will alwas come as [device] + DRIVE.setID("dev" + nbDev); + headers.put(option, DRIVE.getID()); nbDev++; + } else { + headers.put(option, null); } - entries.put(option, null); - options = new TreeMap(); + options = new TreeMap<>(); index++; } else if (index == -1) { - // In case file doesnt begin with header - LOGGER.error("This config file is invalid. Check syntax. Must begin with [...]"); + //In case file doesn't begin with a header + LOGGER.error("This config file is invalid. Chech syntax. Must begin with [..]"); } else { - // adding value to key - String[] opt = option.split("="); - options.put(opt[0].trim(), opt[1].trim()); - entries.keySet(); - String key = (String) entries.keySet().toArray()[index]; - entries.put(key, options); + String[] parameter = option.split("="); + options.put(parameter[0].trim(), parameter[1].trim().replace("\"", "")); + entries.put(headers, options); } } } } - public TreeMap<String, String> get(String key) { + public TreeMap<String, String> get(String key, String id) { TreeMap<String, String> value = null; - if (entries.containsKey(key)) { - value = entries.get(key); + LinkedHashMap keys = new LinkedHashMap(); + keys.put(key, id); + if (entries.containsKey(keys)) { + value = entries.get(keys); } return value; //value of key } - public void set(String key, TreeMap value) { + public void set(LinkedHashMap<String, String> key, TreeMap value) { entries.put(key, value); } - public TreeMap<String, TreeMap<String, String>> get() { - return (TreeMap<String, TreeMap<String, String>>) entries; + public TreeMap<LinkedHashMap<String, String>, TreeMap<String, String>> get() { + return (TreeMap<LinkedHashMap<String, String>, TreeMap<String, String>>) entries; } public String getDisplayName() { String result = ""; - if (entries.containsKey(NAME.value())) { - result = (String) entries.get(NAME.value()).get("guest"); - result = result.replace("\"", ""); - } + LinkedHashMap key = new LinkedHashMap(); + key.put(NAME.getHeader(), null); + if (entries.containsKey(key)) { + result = entries.get(key).get("guest"); + } return result; } public boolean isMachineSnapshot() { boolean isSnapshot = false; - String[] idDrive = null; - for (String key : entries.keySet()) { - if (key.contains(DRIVE.value())) { - idDrive = key.split("\""); - } - } - String active = entries.get(DRIVE.value()+" \"" + idDrive[1] + "\"]").get("snapshot"); - active = active.replace("\"", ""); + LinkedHashMap key = new LinkedHashMap(); + key.put(DRIVE.getHeader(), DRIVE.getID()); + String active = entries.get(key).get("snapshot"); + if (active != null && active.equals("on")) { isSnapshot = true; } @@ -201,59 +241,65 @@ public final class QemuConfig { } public void setHdds() { - String[] idDrive = null; + int dev = 0; String filename = null; DriveBusType bus = null; String controllerDevice = null; - for (String entry : entries.keySet()) { - - if (entry.contains(DRIVE.value())) { - idDrive = entry.split("\""); - - //Get Path - filename = entries.get(entry).get("file"); - filename = filename.replace("\"", ""); - if (filename == null) { - LOGGER.error("Please make sure your harddrive has a path"); - } - - //Bus Type - String busType = entries.get(entry).get("if"); - busType = busType.replace("\"", ""); - if (busType == null) { - LOGGER.error("Please make sure your harddrive has a bus"); - } else { - switch (busType) { - case "ide": + TreeMap<String, String> options = new TreeMap<>(); + for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : entries.entrySet()) { + if (entry.getKey().containsKey(DRIVE.getHeader())) { + if (entry.getValue().containsKey("index")) { + if (entry.getValue().get("index").equals("0")) { + DRIVE.setID(entry.getKey().get(DRIVE.getHeader())); + //Get Path + filename = entry.getValue().get("file"); + if (filename == null) { + LOGGER.error("Please make sure your harddrive has a path"); + } + //Get Bus Type + String busType = entry.getValue().get("if"); + if (busType == null) { bus = DriveBusType.IDE; - break; - case "scsi": - bus = DriveBusType.SCSI; - break; - case "sata": - //not available for Qemu. Others : sd, mtd, floppy, pflash, virtio - break; - default: - break; + } else { + switch (busType) { + case "ide": + bus = DriveBusType.IDE; + break; + case "scsi": + bus = DriveBusType.SCSI; + break; + case "sata": + //not available for Qemu. Others : sd, mtd, floppy, pflash, virtio + break; + default: + break; + } + } } } + } //TODO set default device if no device found + if (entry.getKey().containsKey(DEVICE.getHeader())) { + String drive = entry.getValue().get("drive"); + if (drive != null && drive.equals(DRIVE.getID())) { + controllerDevice = entry.getValue().get("driver"); + } } - - if (entry.contains(DEV.value())) { - String drive = entries.get(entry).get("drive"); - drive = drive.replace("\"", ""); - - if (drive != null && drive.equals(idDrive[1])) { - controllerDevice = entries.get(entry).get("driver"); + for (String key : entry.getKey().keySet()) { + if (key.contains(DEVICE.getHeader())) { + dev++; + } + } + //No device + if (dev == 0) { + if (bus.equals(DriveBusType.IDE)) { + controllerDevice = "ide-hd"; + }else if (bus.equals(DriveBusType.SCSI)) { + controllerDevice = "scsi-hd"; } } - if ((bus != null) && (filename != null) && (controllerDevice != null)) { hddsArray.add(new VmMetaData.HardDisk(controllerDevice, bus, filename)); - idDrive = null; - filename = null; - bus = null; - controllerDevice = null; + break; } } } @@ -264,23 +310,35 @@ public final class QemuConfig { public String toString(boolean filtered) { StringBuilder sb = new StringBuilder(300); - LinkedHashMap<String, TreeMap<String, String>> sortedArray = null; + LinkedHashMap<LinkedHashMap<String, String>, TreeMap<String, String>> sortedArray = null; if (filtered) { sortedArray = new LinkedHashMap<>(); - for (Map.Entry<String, TreeMap<String, String>> entry : entries.entrySet()) { - - if ((entry.getKey().equals(NAME.value())) || (entry.getKey().contains(DRIVE.value())) || entry.getKey().contains(DEV.value())) { + for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : entries.entrySet()) { + + if ((entry.getKey().keySet().equals(NAME.getHeader())) || (entry.getKey().keySet().equals(DRIVE.getHeader()) || entry.getKey().keySet().equals(DEVICE.getHeader()))) { sortedArray.put(entry.getKey(), entry.getValue()); } } } - for (Map.Entry<String, TreeMap<String, String>> entry : sortedArray == null ? entries.entrySet() : sortedArray.entrySet()) { + for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : sortedArray == null ? entries.entrySet() : sortedArray.entrySet()) { - String header = entry.getKey(); + String header = entry.getKey().keySet().toString();; - if (header.contains(DEV.value())) { - header = DEVICE.value(); + if (entry.getKey().containsKey(DRIVE.getHeader())) { + if ((entry.getValue().get("index") != null)) { + if ((entry.getValue().get("index").equals("0"))) { + header = DRIVE.toString(); + } else { + continue; + } + } else { + continue; + } + } + + if (entry.getKey().containsKey(DEVICE.getHeader())) { + header = "[" + DEVICE.getHeader() + "]"; } sb.append(header + "\n"); @@ -291,24 +349,7 @@ public final class QemuConfig { sb.append(" " + key + " = " + value + "\n"); } } - LOGGER.debug(sb); return sb.toString(); - - } - - public static class Device { - - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - } diff --git a/src/main/java/org/openslx/util/vm/QemuMetaData.java b/src/main/java/org/openslx/util/vm/QemuMetaData.java index cf3b550..c1a062b 100644 --- a/src/main/java/org/openslx/util/vm/QemuMetaData.java +++ b/src/main/java/org/openslx/util/vm/QemuMetaData.java @@ -12,8 +12,7 @@ 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.QemuConfig.Header; -import static org.openslx.util.vm.QemuConfig.Header.MACHINE; +import static org.openslx.util.vm.QemuConfig.Header.*; class QemuDDAccelMeta { @@ -98,10 +97,10 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce public void init() { registerVirtualHW(); + config.setHdds(); displayName = config.getDisplayName(); setOs(config.getOsName()); - isMachineSnapshot = config.isMachineSnapshot(); - config.setHdds(); + isMachineSnapshot = config.isMachineSnapshot(); for (HardDisk hardDisk : config.getHdds()) { hdds.add(hardDisk); } @@ -128,8 +127,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce option = new TreeMap<>(); String bus = "anychipset"; DriveBusType busType = DriveBusType.IDE; - //drive - option = config.get("[drive \"disk0\"]"); + option = config.get(DRIVE.getHeader(), DRIVE.getID()); if (option != null) { option.replace("file", "\"" + diskImage.getAbsolutePath() + "\""); bus = option.get("if"); @@ -148,7 +146,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce break; } //device - option = config.get("[dev0]"); +// option = config.get("[dev0]"); hdds.add(new HardDisk(option.get("driver"), busType, diskImage.getAbsolutePath())); return true; } @@ -162,12 +160,12 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce option.put("if", "\"scsi\""); String status = (isMachineSnapshot == true ? "on" : "off"); option.put("snapshot", "\"" + status + "\""); - config.set("[drive \"drive" + driveCounter + "\"]", option); +// config.set("[drive \"drive" + driveCounter + "\"]", option); //device option.put("drive", "\"drive" + driveCounter + "\""); option.put("driver", "scsi-hd"); - config.set("[device]", option); +////// config.set("[device]", option); hdds.add(new HardDisk("anychipset", DriveBusType.IDE, diskImagePath)); driveCounter++; @@ -189,7 +187,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce public boolean addDisplayName(String name) { option = new TreeMap<>(); option.put("guest", "\"" + name + "\""); - config.set("[name]", option); +// config.set("[name]", option); return true; } @@ -197,7 +195,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce public boolean addRam(int mem) { option = new TreeMap<>(); option.put("size", "\"" + mem + "\""); - config.set("[memory]", option); +// config.set("[memory]", option); return true; } @@ -211,7 +209,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce } option.put("if", "\"floppy\""); option.put("file", image); - config.set("[drive \"floppy" + floppyCounter + "\"]", option); +// config.set("[drive \"floppy" + floppyCounter + "\"]", option); floppyCounter++; } @@ -220,7 +218,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce option = new TreeMap<>(); option.put("media", "\"cdrom\""); option.put("file", image); - config.set("[drive \"cdrom" + cdromCounter + "\"]", option); +// config.set("[drive \"cdrom" + cdromCounter + "\"]", option); cdromCounter++; return true; } @@ -229,7 +227,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce public boolean addCpuCoreCount(int nrOfCores) { option = new TreeMap<>(); option.put("cpus", "\"" + nrOfCores + "\""); - config.set("[smp-opts]", option); +// config.set("[smp-opts]", option); return true; } @@ -271,29 +269,29 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce option = new TreeMap<>(); option.put("driver", "\"" + dev.value + "\""); option.put("netdev", "\"net" + cardIndex + "\""); - config.set("[device]", option); +// config.set("[device]", option); } @Override public VmMetaData.EthernetDevType getEthernetDevType(int cardIndex) { - QemuEthernetDevTypeMeta ethernetDevTypeMeta = null; - for (String key : config.get().keySet()) { - if (key.equals("[dev" + cardIndex + "]")) {//wont work dev0 - if (config.get(key).get("netdev").equals("\"net" + cardIndex + "\"")) { - String devs = config.get(key).get("driver"); - - for (EthernetDevType type : VmMetaData.EthernetDevType.values()) { - ethernetDevTypeMeta = networkCards.get(type); - if (ethernetDevTypeMeta == null) { - continue; - } - if (devs.equals(ethernetDevTypeMeta.value)) { - return type; - } - } - } - } - } +// QemuEthernetDevTypeMeta ethernetDevTypeMeta = null; +// for (String key : config.get().keySet()) { +// if (key.equals("[dev" + cardIndex + "]")) {//wont work dev0 +// if (config.get(key).get("netdev").equals("\"net" + cardIndex + "\"")) { +// String devs = config.get(key).get("driver"); +// +// for (EthernetDevType type : VmMetaData.EthernetDevType.values()) { +// ethernetDevTypeMeta = networkCards.get(type); +// if (ethernetDevTypeMeta == null) { +// continue; +// } +// if (devs.equals(ethernetDevTypeMeta.value)) { +// return type; +// } +// } +// } +// } +// } return EthernetDevType.AUTO; } @@ -301,40 +299,40 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce public boolean addEthernet(VmMetaData.EtherType type) { boolean ret = false; int index = 0; - for (;; ++index) { - TreeMap<String, String> dev = config.get("[dev" + index + "]"); +// for (;; ++index) { +// TreeMap<String, String> dev = config.get("[dev" + index + "]"); - if (dev == null) { - break; - } else { - if (dev.get("netdev") == null) { - break; - } - } - } - switch (type) { - case NAT: - //netdev - option = new TreeMap<>(); - option.put("br", "nat1"); - option.put("type", "bridge"); - config.set("[netdev \"net" + index + "\"]", option); - //device - option = new TreeMap<>(); - break; - case BRIDGED: - option = new TreeMap<>(); - option.put("br", "'br0'"); - option.put("type", "'bridge'"); - config.set("[netdev \"net" + index + "\"]", option); - break; - case HOST_ONLY: - //Dont know how to do it... - break; - default: - // Should not come to this... - break; - } +// if (dev == null) { +// break; +// } else { +// if (dev.get("netdev") == null) { +// break; +// } +// } +// } +// switch (type) { +// case NAT: +// //netdev +// option = new TreeMap<>(); +// option.put("br", "nat1"); +// option.put("type", "bridge"); +//// config.set("[netdev \"net" + index + "\"]", option); +// //device +// option = new TreeMap<>(); +// break; +// case BRIDGED: +// option = new TreeMap<>(); +// option.put("br", "'br0'"); +// option.put("type", "'bridge'"); +//// config.set("[netdev \"net" + index + "\"]", option); +// break; +// case HOST_ONLY: +// //Dont know how to do it... +// break; +// default: +// // Should not come to this... +// break; +// } return ret; } @@ -351,7 +349,7 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce } else { option.put("usb", "\"off\""); } - config.set(MACHINE.value(), option); +// config.set(MACHINE.value(), option); } @Override |