From 4248a443e716f429c9625dcb61be3c5adce52ed7 Mon Sep 17 00:00:00 2001
From: wangge <wangge20@huawei.com>
Date: Sat, 9 Oct 2021 15:11:00 +0800
Subject: [PATCH] fit cluster can't find error under aarch64 platform

---
 .../engine/core/bll/UpdateClusterCommand.java |  3 +-
 .../core/bll/validator/ClusterValidator.java  |  7 +--
 .../common/utils/ClusterEmulatedMachines.java | 56 +++++++++++++++++--
 .../builder/vminfo/LibvirtVmXmlBuilder.java   |  7 ++-
 .../monitoring/VirtMonitoringStrategy.java    |  5 +-
 5 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateClusterCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateClusterCommand.java
index e23c76a..c66de52 100644
--- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateClusterCommand.java
+++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateClusterCommand.java
@@ -618,7 +618,8 @@ public class UpdateClusterCommand<T extends ClusterOperationParameters> extends
         List<String> available = Config.getValue(ConfigValues.ClusterEmulatedMachines, version.getValue());
         return ClusterEmulatedMachines.build(
                 EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.I440FX, supported, available),
-                EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.Q35, supported, available));
+                EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.Q35, supported, available),
+                EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.VIRT, supported, available));
     }
 
     private void addOrUpdateAddtionalClusterFeatures() {
diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ClusterValidator.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ClusterValidator.java
index 9e13a3d..4f43154 100644
--- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ClusterValidator.java
+++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ClusterValidator.java
@@ -131,12 +131,11 @@ public class ClusterValidator {
      */
     public ValidationResult cpuTypeSupportsVirtService() {
         return ValidationResult.failWith(EngineMessage.ACTION_TYPE_FAILED_CPU_NOT_FOUND)
-                .when(cluster.supportsVirtService() && !cpuExists());
+                .when(!cluster.supportsVirtService() && !cpuExists());
     }
 
     protected boolean cpuExists() {
-        return cluster.getCpuName() == null || "".equals(cluster.getCpuName()) ||
-                cpuFlagsManagerHandler.checkIfCpusExist(cluster.getCpuName(), cluster.getCompatibilityVersion());
+        return cluster.getCpuName() == null || "".equals(cluster.getCpuName()) || cpuFlagsManagerHandler.checkIfCpusExist(cluster.getCpuName(), cluster.getCompatibilityVersion());
     }
 
     public ValidationResult versionSupported() {
@@ -283,7 +282,7 @@ public class ClusterValidator {
      */
     public ValidationResult cpuNotFound(boolean cpusExist) {
         return ValidationResult.failWith(EngineMessage.CLUSTER_CANNOT_UPDATE_CPU_ILLEGAL)
-                .when(newCluster.supportsVirtService()
+                .when(!newCluster.supportsVirtService()
                         && (!"".equals(cluster.getCpuName()) || !"".equals(newCluster.getCpuName()))
                         && !cpusExist);
     }
diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ClusterEmulatedMachines.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ClusterEmulatedMachines.java
index 2928da4..36c4946 100644
--- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ClusterEmulatedMachines.java
+++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ClusterEmulatedMachines.java
@@ -6,23 +6,50 @@ public class ClusterEmulatedMachines {
 
     private static final String I440FX_CHIPSET_NAME = ChipsetType.I440FX.getChipsetName();
     private static final String Q35_CHIPSET_NAME = ChipsetType.Q35.getChipsetName();
+    private static final String VIRT_CHIPSET_NAME = ChipsetType.VIRT.getChipsetName();
 
     private String i440fxType = "";
     private String q35Type = "";
+    private String virtType = "";
 
     private ClusterEmulatedMachines() {
     }
 
+    private ClusterEmulatedMachines(String emulatedMachine) {
+        if (emulatedMachine.contains(VIRT_CHIPSET_NAME)) {
+            this.virtType = VIRT_CHIPSET_NAME;
+        }
+    }
+
     private ClusterEmulatedMachines(String i440fxType, String q35Type) {
         this.i440fxType = i440fxType;
         this.q35Type = q35Type;
     }
 
-    public static String build(String i440fxType, String q35Type) {
-        if (i440fxType == null) {
-            return q35Type;
+    public static String build(String i440fxType, String q35Type, String virtType) {
+        String matchedType = new String();
+        if (i440fxType != null) {
+            if (!matchedType.isEmpty()) {
+                matchedType = matchedType + ";";
+            }
+            matchedType = matchedType + i440fxType;
+        }
+        if (q35Type != null) {
+            if (!matchedType.isEmpty()) {
+                matchedType = matchedType + ";";
+            }
+            matchedType = matchedType + q35Type;
+        }
+        if (virtType != null) {
+            if (!matchedType.isEmpty()) {
+                matchedType = matchedType + ";";
+            }
+            matchedType = matchedType + virtType;
+        }
+        if (matchedType.isEmpty()) {
+            return null;
         } else {
-            return q35Type == null ? i440fxType : i440fxType + ";" + q35Type;
+            return matchedType;
         }
     }
 
@@ -41,7 +68,13 @@ public class ClusterEmulatedMachines {
         if (em.length == 2) {
             return new ClusterEmulatedMachines(em[0], em[1]);
         }
+        if (em.length > 2) {
+            return new ClusterEmulatedMachines(emulatedMachine);
+        }
         ChipsetType chipsetType = ChipsetType.fromMachineType(emulatedMachine);
+        if (chipsetType == ChipsetType.VIRT) {
+            return new ClusterEmulatedMachines(VIRT_CHIPSET_NAME);
+        }
         if (chipsetType == ChipsetType.Q35) {
             return new ClusterEmulatedMachines(replaceChipset(emulatedMachine, ChipsetType.I440FX), emulatedMachine);
         } else {
@@ -80,8 +113,21 @@ public class ClusterEmulatedMachines {
         return q35Type;
     }
 
+    public String getVirtType() {
+        return virtType;
+    }
+
     public String getTypeByChipset(ChipsetType chipsetType) {
-        return chipsetType == ChipsetType.Q35 ? q35Type : i440fxType;
+        switch (chipsetType) {
+            case Q35:
+                return q35Type;
+            case I440FX:
+                return i440fxType;
+            case VIRT:
+                return virtType;
+            default:
+                return q35Type;
+        }
     }
 
     public static String forChipset(String emulatedMachine, ChipsetType chipsetType) {
diff --git a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/builder/vminfo/LibvirtVmXmlBuilder.java b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/builder/vminfo/LibvirtVmXmlBuilder.java
index 4e7794a..712d3c3 100644
--- a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/builder/vminfo/LibvirtVmXmlBuilder.java
+++ b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/builder/vminfo/LibvirtVmXmlBuilder.java
@@ -692,7 +692,7 @@ public class LibvirtVmXmlBuilder {
             writer.writeAttributeString("secure", secureBoot ? "yes" : "no");
             writer.writeAttributeString("type", "pflash");
             if (vm.getClusterArch().getFamily() == ArchitectureType.aarch64) {
-                writer.writeRaw("/usr/share/AAVMF/AAVMF_CODE.fd");
+                writer.writeRaw("/usr/share/edk2/aarch64/QEMU_EFI-pflash.raw");
             } else {
                 writer.writeRaw("/usr/share/OVMF/OVMF_CODE.secboot.fd");
             }
@@ -701,7 +701,7 @@ public class LibvirtVmXmlBuilder {
             String nvramTemplate = vmCustomProperties.get("nvram_template");
             if (nvramTemplate == null) {
                if(vm.getClusterArch().getFamily() == ArchitectureType.aarch64){
-                   nvramTemplate = "/usr/share/AAVMF/AAVMF_CODE.fd";
+                   nvramTemplate = "/usr/share/edk2/aarch64/vars-template-pflash.raw";
                } else {
                    nvramTemplate = String.format("/usr/share/OVMF/%s",
                         secureBoot ? "OVMF_VARS.secboot.fd" : "OVMF_VARS.fd");
@@ -1047,6 +1047,9 @@ public class LibvirtVmXmlBuilder {
         case x86:
             writeInput();
             break;
+        case aarch64:
+            writeInput();
+            break;
         case ppc:
             if (vmInfoBuildUtils.hasUsbController(vm)) {
                 writeInput();
diff --git a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/monitoring/VirtMonitoringStrategy.java b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/monitoring/VirtMonitoringStrategy.java
index b889773..b027275 100644
--- a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/monitoring/VirtMonitoringStrategy.java
+++ b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/monitoring/VirtMonitoringStrategy.java
@@ -228,7 +228,7 @@ public class VirtMonitoringStrategy implements MonitoringStrategy {
                 return supported.contains(cluster.getEmulatedMachine());
             } else {
                 ClusterEmulatedMachines ems = ClusterEmulatedMachines.parse(cluster.getEmulatedMachine());
-                return supported.contains(ems.getI440fxType()) && supported.contains(ems.getQ35Type());
+                return supported.contains(ems.getVirtType()) || (supported.contains(ems.getI440fxType()) && supported.contains(ems.getQ35Type()));
             }
         }
     }
@@ -237,7 +237,8 @@ public class VirtMonitoringStrategy implements MonitoringStrategy {
         String matchedI440fx =
                 EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.I440FX, supported, available);
         String matchedQ35 = EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.Q35, supported, available);
-        String matchedEmulatedMachine = ClusterEmulatedMachines.build(matchedI440fx, matchedQ35);
+        String matchedVirt = EmulatedMachineCommonUtils.getSupportedByChipset(ChipsetType.VIRT, supported, available);
+        String matchedEmulatedMachine = ClusterEmulatedMachines.build(matchedI440fx, matchedQ35, matchedVirt);
 
         if (!StringUtils.isEmpty(matchedEmulatedMachine)) {
             setClusterEmulatedMachine(vds, matchedEmulatedMachine);
-- 
2.27.0

