From 7c797230c3e5f043a04f3b08e8cf1ea28396c846 Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Sun, 20 Dec 2020 00:38:40 +0530 Subject: [PATCH 1/4] Fix for mapping guest OS type read from OVF to existing guest OS in CloudStack database while registering VMware template --- .../deployasis/DeployAsIsHelperImpl.java | 24 +++++++++-- .../java/com/cloud/utils/StringUtils.java | 40 ++++++++++++++++++- 2 files changed, 60 insertions(+), 4 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 661096609343..6d05af3337d5 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 @@ -167,8 +167,26 @@ public Long retrieveTemplateGuestOsIdFromGuestOsInfo(long templateId, String gue hypervisor.toString(), minimumHypervisorVersion); if (CollectionUtils.isNotEmpty(guestOsMappings)) { - GuestOSHypervisorVO mapping = guestOsMappings.get(0); - return mapping.getGuestOsId(); + Long guestOsId = null; + if (guestOsMappings.size() == 1) { + GuestOSHypervisorVO mapping = guestOsMappings.get(0); + guestOsId = mapping.getGuestOsId(); + } else { + if (!StringUtils.isEmpty(guestOsDescription)) { + for (GuestOSHypervisorVO guestOSHypervisorVO : guestOsMappings) { + GuestOSVO guestOSVO = guestOSDao.findById(guestOSHypervisorVO.getGuestOsId()); + if (guestOsDescription.equalsIgnoreCase(guestOSVO.getDisplayName())) { + guestOsId = guestOSHypervisorVO.getGuestOsId(); + break; + } + } + } + if (null == guestOsId) { + GuestOSHypervisorVO mapping = guestOsMappings.get(guestOsMappings.size()-1); + guestOsId = mapping.getGuestOsId(); + } + } + return guestOsId; } else { throw new CloudRuntimeException("Did not find a guest OS with type " + guestOsType); } @@ -301,7 +319,7 @@ private void persistTemplateDeployAsIsInformationTOList(long templateId, } String propValue = null; try { - propValue = getValueFromInformationTO(informationTO); + propValue = getValueFromInformationTO(informationTO); } catch (RuntimeException re) { LOGGER.error("gson marshalling of property object fails: " + propKey,re); } catch (IOException e) { diff --git a/utils/src/main/java/com/cloud/utils/StringUtils.java b/utils/src/main/java/com/cloud/utils/StringUtils.java index e858bee74a02..656d744e775f 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 static List csvTagsToList(final String tags) { /** * 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 @@ private static List> partitionList(final List originalList, final public static String toCSVList(final List 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]; + } } From f72f2fdd53f90a84ca9472b80f03ac232fcc8c03 Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Mon, 21 Dec 2020 13:44:54 +0530 Subject: [PATCH 2/4] Added unit tests to String Utils methods and updated the code --- .../java/com/cloud/utils/StringUtils.java | 24 +++++---------- .../java/com/cloud/utils/StringUtilsTest.java | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/utils/src/main/java/com/cloud/utils/StringUtils.java b/utils/src/main/java/com/cloud/utils/StringUtils.java index 656d744e775f..f83667682df5 100644 --- a/utils/src/main/java/com/cloud/utils/StringUtils.java +++ b/utils/src/main/java/com/cloud/utils/StringUtils.java @@ -314,16 +314,6 @@ public static String toCSVList(final List 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 @@ -333,12 +323,12 @@ public static int min(int x, int y, int z) * @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]; + int str1Length = str1.length(); + int str2Length = str2.length(); + int[][] auxiliaryArray = new int[str1Length + 1][str2Length + 1]; - for (int i = 0; i <= m; i++) { - for (int j = 0; j <= n; j++) { + for (int i = 0; i <= str1Length; i++) { + for (int j = 0; j <= str2Length; j++) { if (i == 0) auxiliaryArray[i][j] = j; else if (j == 0) @@ -346,9 +336,9 @@ else if (j == 0) 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]); + auxiliaryArray[i][j] = 1 + Integer.min(Integer.min(auxiliaryArray[i][j - 1], auxiliaryArray[i - 1][j]), auxiliaryArray[i - 1][j - 1]); } } - return auxiliaryArray[m][n]; + return auxiliaryArray[str1Length][str2Length]; } } diff --git a/utils/src/test/java/com/cloud/utils/StringUtilsTest.java b/utils/src/test/java/com/cloud/utils/StringUtilsTest.java index 09a6c71bcf8d..29f9be32f30c 100644 --- a/utils/src/test/java/com/cloud/utils/StringUtilsTest.java +++ b/utils/src/test/java/com/cloud/utils/StringUtilsTest.java @@ -258,4 +258,34 @@ public void testToCSVList() { String output = StringUtils.toCSVList(Arrays.asList(input.split(","))); assertTrue(input.equals(output)); } + + @Test + public void testZeroEditDistance() { + String str1 = "Other 32-bit"; + String str2 = "Other 32-bit"; + int minDistance = StringUtils.minimumEditDistance(str1, str2); + assertEquals(minDistance, 0); + } + + @Test + public void testBestMatchStringWithEditDistance() { + String str1 = "FreeBSD 11 (32bit)"; + String str2 = "FreeBSD 12 (64bit)"; + String targetString = "FreeBSD 64bit"; + int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString); + int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString); + // the best match will be str2, so expecting less edit distance + assertTrue(minDistanceStr2 < minDistanceStr1); + } + + @Test + public void testCompletelyDifferentStringsWithEditDistance() { + String str1 = "Other (32-bit)"; + String str2 = "SCO OpenServer 5"; + String targetString = "Other 32-bit"; + int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString); + int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString); + // the best match will be str1, so expecting less edit distance + assertTrue(minDistanceStr1 < minDistanceStr2); + } } From c1a9f414a70efb12ee66a29615b2b4cabd9bd1a4 Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Mon, 21 Dec 2020 21:27:27 +0530 Subject: [PATCH 3/4] Updated the java doc section --- utils/src/main/java/com/cloud/utils/StringUtils.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/src/main/java/com/cloud/utils/StringUtils.java b/utils/src/main/java/com/cloud/utils/StringUtils.java index f83667682df5..6af3075ee781 100644 --- a/utils/src/main/java/com/cloud/utils/StringUtils.java +++ b/utils/src/main/java/com/cloud/utils/StringUtils.java @@ -316,10 +316,8 @@ public static String toCSVList(final List csvList) { /** * 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 + * @param str1 String that needs editing + * @param str2 Target string that is required/expected after editing * @return minimum number of edits required to convert str1 to str2 */ public static int minimumEditDistance(String str1, String str2) { From 4b8adeb5f034cc04225fe19bc499dcaa185ae00d Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Tue, 22 Dec 2020 19:05:45 +0530 Subject: [PATCH 4/4] Updated os description logic to keep equals ignore match with guest os display name --- .../java/com/cloud/utils/StringUtils.java | 28 +---------------- .../java/com/cloud/utils/StringUtilsTest.java | 30 ------------------- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/utils/src/main/java/com/cloud/utils/StringUtils.java b/utils/src/main/java/com/cloud/utils/StringUtils.java index 6af3075ee781..e858bee74a02 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 static List csvTagsToList(final String tags) { /** * Converts a List of tags to a comma separated list - * @param tagsList + * @param tags * @return String containing a comma separated list of tags */ @@ -313,30 +313,4 @@ private static List> partitionList(final List originalList, final public static String toCSVList(final List csvList) { return join(csvList, ","); } - - /** - * 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 is required/expected after editing - * @return minimum number of edits required to convert str1 to str2 - */ - public static int minimumEditDistance(String str1, String str2) { - int str1Length = str1.length(); - int str2Length = str2.length(); - int[][] auxiliaryArray = new int[str1Length + 1][str2Length + 1]; - - for (int i = 0; i <= str1Length; i++) { - for (int j = 0; j <= str2Length; 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 + Integer.min(Integer.min(auxiliaryArray[i][j - 1], auxiliaryArray[i - 1][j]), auxiliaryArray[i - 1][j - 1]); - } - } - return auxiliaryArray[str1Length][str2Length]; - } } diff --git a/utils/src/test/java/com/cloud/utils/StringUtilsTest.java b/utils/src/test/java/com/cloud/utils/StringUtilsTest.java index 29f9be32f30c..09a6c71bcf8d 100644 --- a/utils/src/test/java/com/cloud/utils/StringUtilsTest.java +++ b/utils/src/test/java/com/cloud/utils/StringUtilsTest.java @@ -258,34 +258,4 @@ public void testToCSVList() { String output = StringUtils.toCSVList(Arrays.asList(input.split(","))); assertTrue(input.equals(output)); } - - @Test - public void testZeroEditDistance() { - String str1 = "Other 32-bit"; - String str2 = "Other 32-bit"; - int minDistance = StringUtils.minimumEditDistance(str1, str2); - assertEquals(minDistance, 0); - } - - @Test - public void testBestMatchStringWithEditDistance() { - String str1 = "FreeBSD 11 (32bit)"; - String str2 = "FreeBSD 12 (64bit)"; - String targetString = "FreeBSD 64bit"; - int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString); - int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString); - // the best match will be str2, so expecting less edit distance - assertTrue(minDistanceStr2 < minDistanceStr1); - } - - @Test - public void testCompletelyDifferentStringsWithEditDistance() { - String str1 = "Other (32-bit)"; - String str2 = "SCO OpenServer 5"; - String targetString = "Other 32-bit"; - int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString); - int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString); - // the best match will be str1, so expecting less edit distance - assertTrue(minDistanceStr1 < minDistanceStr2); - } }