This is an automated email from the ASF dual-hosted git repository.

harikrishna pushed a commit to branch guestOSMappingfromOVFinVMWare
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit e88f526afbc22c51b53fc7c61d166e43b7cdb418
Author: Harikrishna Patnala <[email protected]>
AuthorDate: Sun Dec 20 00:38:40 2020 +0530

    Fix for mapping guest OS type read from OVF to existing guest OS in 
CloudStack database  while registering VMware template
---
 .../image/deployasis/DeployAsIsHelperImpl.java     | 22 ++++++++++--
 .../src/main/java/com/cloud/utils/StringUtils.java | 40 +++++++++++++++++++++-
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git 
a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java
 
b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java
index 0d52f57..7cb01b6 100644
--- 
a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java
+++ 
b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java
@@ -174,8 +174,26 @@ public class DeployAsIsHelperImpl implements 
DeployAsIsHelper {
                 hypervisor.toString(), minimumHypervisorVersion);
 
         if (CollectionUtils.isNotEmpty(guestOsMappings)) {
-            GuestOSHypervisorVO mapping = guestOsMappings.get(0);
-            long guestOsId = mapping.getGuestOsId();
+            Long guestOsId = null;
+            if (guestOsMappings.size() == 1) {
+                GuestOSHypervisorVO mapping = guestOsMappings.get(0);
+                guestOsId = mapping.getGuestOsId();
+            } else {
+                if (!StringUtils.isEmpty(guestOsDescription)) {
+                    int minimumEdits = Integer.MAX_VALUE;
+                    for (GuestOSHypervisorVO guestOSHypervisorVO : 
guestOsMappings) {
+                        GuestOSVO guestOSVO = 
guestOSDao.findById(guestOSHypervisorVO.getGuestOsId());
+                        int temporaryMin = 
com.cloud.utils.StringUtils.minimumEditDistance(guestOsDescription, 
guestOSVO.getDisplayName());
+                        if (temporaryMin < minimumEdits) {
+                            minimumEdits = temporaryMin;
+                            guestOsId = guestOSHypervisorVO.getGuestOsId();
+                        }
+                    }
+                } else {
+                    GuestOSHypervisorVO mapping = 
guestOsMappings.get(guestOsMappings.size()-1);
+                    guestOsId = mapping.getGuestOsId();
+                }
+            }
             LOGGER.info("Updating deploy-as-is template guest OS to " + 
guestOsType);
             updateTemplateGuestOsId(template, guestOsId);
         } else {
diff --git a/utils/src/main/java/com/cloud/utils/StringUtils.java 
b/utils/src/main/java/com/cloud/utils/StringUtils.java
index e858bee..656d744 100644
--- a/utils/src/main/java/com/cloud/utils/StringUtils.java
+++ b/utils/src/main/java/com/cloud/utils/StringUtils.java
@@ -124,7 +124,7 @@ public class StringUtils {
 
     /**
      * Converts a List of tags to a comma separated list
-     * @param tags
+     * @param tagsList
      * @return String containing a comma separated list of tags
      */
 
@@ -313,4 +313,42 @@ public class StringUtils {
     public static String toCSVList(final List<String> csvList) {
         return join(csvList, ",");
     }
+
+    public static int min(int x, int y, int z)
+    {
+        if (x <= y && x <= z)
+            return x;
+        if (y <= x && y <= z)
+            return y;
+        else
+            return z;
+    }
+
+    /**
+     * Calculates minimum number of edits required to convert from one string 
to another string.
+     * @param str1
+     *      String that needs editing
+     * @param str2
+     *      Target string that needs after editing
+     * @return minimum number of edits required to convert str1 to str2
+     */
+    public static int minimumEditDistance(String str1, String str2) {
+        int m = str1.length();
+        int n = str2.length();
+        int[][] auxiliaryArray = new int[m + 1][n + 1];
+
+        for (int i = 0; i <= m; i++) {
+            for (int j = 0; j <= n; j++) {
+                if (i == 0)
+                    auxiliaryArray[i][j] = j;
+                else if (j == 0)
+                    auxiliaryArray[i][j] = i;
+                else if (str1.charAt(i - 1) == str2.charAt(j - 1))
+                    auxiliaryArray[i][j] = auxiliaryArray[i - 1][j - 1];
+                else
+                    auxiliaryArray[i][j] = 1 + min(auxiliaryArray[i][j - 1], 
auxiliaryArray[i - 1][j], auxiliaryArray[i - 1][j - 1]);
+            }
+        }
+        return auxiliaryArray[m][n];
+    }
 }

Reply via email to