/*
 * This file is part of the KubeVirt project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright The KubeVirt Authors.
 */

package cpudedicated

import (
	"encoding/json"

	"libvirt.org/go/libvirtxml"

	v1 "kubevirt.io/api/core/v1"

	cmdv1 "kubevirt.io/kubevirt/pkg/handler-launcher-com/cmd/v1"
	"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
	"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/converter/vcpu"
	convxml "kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/libvirtxml"
)

func GenerateDomainForTargetCPUSetAndTopology(vmi *v1.VirtualMachineInstance, domSpec *api.DomainSpec) (*api.Domain, error) {
	var targetTopology cmdv1.Topology
	targetNodeCPUSet := vmi.Status.MigrationState.TargetCPUSet
	err := json.Unmarshal([]byte(vmi.Status.MigrationState.TargetNodeTopology), &targetTopology)
	if err != nil {
		return nil, err
	}

	domain := api.NewMinimalDomain(vmi.Name)
	domain.Spec = *domSpec
	cpuTopology := vcpu.GetCPUTopology(vmi)
	cpuCount := vcpu.CalculateRequestedVCPUs(cpuTopology)

	// update cpu count to maximum hot plugable CPUs
	vmiCPU := vmi.Spec.Domain.CPU
	if vmiCPU != nil && vmiCPU.MaxSockets != 0 {
		cpuTopology.Sockets = vmiCPU.MaxSockets
		cpuCount = vcpu.CalculateRequestedVCPUs(cpuTopology)
	}
	domain.Spec.CPU.Topology = cpuTopology
	domain.Spec.VCPU = &api.VCPU{
		Placement: "static",
		CPUs:      cpuCount,
	}
	err = vcpu.AdjustDomainForTopologyAndCPUSet(domain, vmi, &targetTopology, targetNodeCPUSet)
	if err != nil {
		return nil, err
	}

	return domain, err
}

func ConvertCPUDedicatedFields(domain *api.Domain, domcfg *libvirtxml.Domain) error {
	if domcfg.CPU == nil {
		domcfg.CPU = &libvirtxml.DomainCPU{}
	}
	domcfg.CPU.Topology = convxml.ConvertKubeVirtCPUTopologyToDomainCPUTopology(domain.Spec.CPU.Topology)
	domcfg.VCPU = convxml.ConvertKubeVirtVCPUToDomainVCPU(domain.Spec.VCPU)
	domcfg.CPUTune = convxml.ConvertKubeVirtCPUTuneToDomainCPUTune(domain.Spec.CPUTune)
	domcfg.NUMATune = convxml.ConvertKubeVirtNUMATuneToDomainNUMATune(domain.Spec.NUMATune)
	domcfg.Features = convxml.ConvertKubeVirtFeaturesToDomainFeatureList(domain.Spec.Features)

	return nil
}
