<commit_before>package com.github.K0zka.kerub.hypervisor.kvm

import com.github.K0zka.kerub.model.VirtualMachine
import com.github.K0zka.kerub.model.VirtualStorageDevice
import com.github.K0zka.kerub.model.VirtualStorageLink
import com.github.K0zka.kerub.model.dynamic.VirtualStorageAllocation
import com.github.K0zka.kerub.model.dynamic.VirtualStorageDeviceDynamic
import com.github.K0zka.kerub.model.dynamic.VirtualStorageFsAllocation
import com.github.K0zka.kerub.model.dynamic.VirtualStorageLvmAllocation

fun storagesToXml(disks: Map<VirtualStorageLink, Pair<VirtualStorageDevice, VirtualStorageDeviceDynamic> >): String {
	val ret = StringBuilder()
	for (device in disks) {
		ret.append(storageToXml(device.value.first, device.key, device.value.second.allocation))
	}
	return ret.toString()
}

val allocationTypeToDiskType = mapOf(
		VirtualStorageFsAllocation:: class to "file",
		VirtualStorageLvmAllocation::class to "block"
)

private fun storageToXml(disk : VirtualStorageDevice, link: VirtualStorageLink, allocation : VirtualStorageAllocation): String {
	return """
		<disk type='${allocationTypeToDiskType[allocation.javaClass.kotlin]}' device='disk'>
            <driver />
            ${allocationToXml(allocation, disk)}
            <target dev='sda' bus='${link.bus}'/>
		</disk>
"""
}

fun allocationToXml(allocation: VirtualStorageAllocation, disk : VirtualStorageDevice): String {
	when(allocation) {
		is VirtualStorageFsAllocation -> return "<source file='${allocation.mountPoint}/${disk.id}'/>"
		is VirtualStorageLvmAllocation -> return "<source dev='${allocation.path}'/>"
		else -> return TODO()
	}
}

fun escapeXmlText(str: String): String {
	return str.replace("<".toRegex(), "&lt;").replace(">".toRegex(), "&gt;")
}

fun vmDefinitiontoXml(vm: VirtualMachine, disks: Map<VirtualStorageLink, Pair<VirtualStorageDevice, VirtualStorageDeviceDynamic> >): String {
	return """
<domain type='kvm'>
    <name>${vm.id}</name>
    <uuid>${vm.id}</uuid>
    <memory unit='B'>${vm.memory.min}</memory>
    <vcpu>${vm.nrOfCpus}</vcpu>
    <os><commit_msg>added readonly option to the kvm output<commit_after>package com.github.K0zka.kerub.hypervisor.kvm

import com.github.K0zka.kerub.model.VirtualMachine
import com.github.K0zka.kerub.model.VirtualStorageDevice
import com.github.K0zka.kerub.model.VirtualStorageLink
import com.github.K0zka.kerub.model.dynamic.VirtualStorageAllocation
import com.github.K0zka.kerub.model.dynamic.VirtualStorageDeviceDynamic
import com.github.K0zka.kerub.model.dynamic.VirtualStorageFsAllocation
import com.github.K0zka.kerub.model.dynamic.VirtualStorageLvmAllocation

fun storagesToXml(disks: Map<VirtualStorageLink, Pair<VirtualStorageDevice, VirtualStorageDeviceDynamic> >): String {
	val ret = StringBuilder()
	for (device in disks) {
		ret.append(storageToXml(device.value.first, device.key, device.value.second.allocation))
	}
	return ret.toString()
}

val allocationTypeToDiskType = mapOf(
		VirtualStorageFsAllocation:: class to "file",
		VirtualStorageLvmAllocation::class to "block"
)

private fun storageToXml(disk : VirtualStorageDevice, link: VirtualStorageLink, allocation : VirtualStorageAllocation): String {
	return """
		<disk type='${allocationTypeToDiskType[allocation.javaClass.kotlin]}' device='${link.device.name.toLowerCase()}'>
            <driver />
            ${if(link.readOnly || disk.readOnly) "<readonly/>" else ""}
            ${allocationToXml(allocation, disk)}
            <target dev='sda' bus='${link.bus}'/>
		</disk>
"""
}

fun allocationToXml(allocation: VirtualStorageAllocation, disk : VirtualStorageDevice): String {
	when(allocation) {
		is VirtualStorageFsAllocation -> return "<source file='${allocation.mountPoint}/${disk.id}'/>"
		is VirtualStorageLvmAllocation -> return "<source dev='${allocation.path}'/>"
		else -> return TODO()
	}
}

fun escapeXmlText(str: String): String {
	return str.replace("<".toRegex(), "&lt;").replace(">".toRegex(), "&gt;")
}

fun vmDefinitiontoXml(vm: VirtualMachine, disks: Map<VirtualStorageLink, Pair<VirtualStorageDevice, VirtualStorageDeviceDynamic> >): String {
	return """
<domain type='kvm'>
    <name>${vm.id}</name>
    <uuid>${vm.id}</uuid>
    <memory unit='B'>${vm.memory.min}</memory>
    <vcpu>${vm.nrOfCpus}</vcpu>
    <os>
{'fraction_non_alphanumeric': 0.14429608807683297, 'fraction_numerical': 0.0037479503396579995, 'mean_word_length': 5.682316118935837, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5830665', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1355, 'n_words': 227}