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/java/org/openslx/util/vm/QemuConfig.java | |
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/java/org/openslx/util/vm/QemuConfig.java')
-rw-r--r-- | src/main/java/org/openslx/util/vm/QemuConfig.java | 275 |
1 files changed, 158 insertions, 117 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; - } - } - } |