[VMware][Deploy-as-is] OVF properties not importing when template is uploaded from local (#5861)

* Fix ova upload missing details

* Refactor and cleanup

* Unused import
diff --git a/core/src/main/java/org/apache/cloudstack/storage/command/UploadStatusAnswer.java b/core/src/main/java/org/apache/cloudstack/storage/command/UploadStatusAnswer.java
index 5c880c3..ee89ec0 100644
--- a/core/src/main/java/org/apache/cloudstack/storage/command/UploadStatusAnswer.java
+++ b/core/src/main/java/org/apache/cloudstack/storage/command/UploadStatusAnswer.java
@@ -20,7 +20,7 @@
 package org.apache.cloudstack.storage.command;
 
 import com.cloud.agent.api.Answer;
-import com.cloud.utils.Pair;
+import com.cloud.agent.api.to.OVFInformationTO;
 
 public class UploadStatusAnswer extends Answer {
     public static enum UploadStatus {
@@ -32,8 +32,7 @@
     private long physicalSize = 0;
     private String installPath = null;
     private int downloadPercent = 0;
-    private Pair<String, String> guestOsInfo;
-    private String minimumHardwareVersion;
+    private OVFInformationTO ovfInformationTO;
 
     protected UploadStatusAnswer() {
     }
@@ -89,19 +88,11 @@
         this.downloadPercent = downloadPercent;
     }
 
-    public Pair<String, String> getGuestOsInfo() {
-        return guestOsInfo;
+    public OVFInformationTO getOvfInformationTO() {
+        return ovfInformationTO;
     }
 
-    public void setGuestOsInfo(Pair<String, String> guestOsInfo) {
-        this.guestOsInfo = guestOsInfo;
-    }
-
-    public void setMinimumHardwareVersion(String minimumHardwareVersion) {
-        this.minimumHardwareVersion = minimumHardwareVersion;
-    }
-
-    public String getMinimumHardwareVersion() {
-        return minimumHardwareVersion;
+    public void setOvfInformationTO(OVFInformationTO ovfInformationTO) {
+        this.ovfInformationTO = ovfInformationTO;
     }
 }
diff --git a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java
index 0c55545..3ef9fbc 100644
--- a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java
+++ b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java
@@ -33,6 +33,7 @@
 import javax.inject.Inject;
 
 import com.cloud.agent.api.to.NfsTO;
+import com.cloud.agent.api.to.OVFInformationTO;
 import com.cloud.storage.DataStoreRole;
 import com.cloud.storage.Upload;
 import org.apache.cloudstack.storage.image.deployasis.DeployAsIsHelper;
@@ -207,8 +208,9 @@
         TemplateDataStoreVO tmpltStoreVO = _templateStoreDao.findByStoreTemplate(store.getId(), obj.getId());
         if (tmpltStoreVO != null) {
             if (tmpltStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
-                if (template.isDeployAsIs()) {
-                    boolean persistDeployAsIs = deployAsIsHelper.persistTemplateDeployAsIsDetails(template.getId(), answer, tmpltStoreVO);
+                if (template.isDeployAsIs() && answer != null) {
+                    OVFInformationTO ovfInformationTO = answer.getOvfInformationTO();
+                    boolean persistDeployAsIs = deployAsIsHelper.persistTemplateOVFInformationAndUpdateGuestOS(template.getId(), ovfInformationTO, tmpltStoreVO);
                     if (!persistDeployAsIs) {
                         LOGGER.info("Failed persisting deploy-as-is template details for template " + template.getName());
                         return null;
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 9d26da8..0fac479 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
@@ -18,7 +18,6 @@
  */
 package org.apache.cloudstack.storage.image.deployasis;
 
-import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.OVFInformationTO;
 import com.cloud.agent.api.to.deployasis.OVFConfigurationTO;
@@ -100,41 +99,44 @@
         gson = builder.create();
     }
 
-    public boolean persistTemplateDeployAsIsDetails(long templateId, DownloadAnswer answer, TemplateDataStoreVO tmpltStoreVO) {
-        try {
-            OVFInformationTO ovfInformationTO = answer.getOvfInformationTO();
-            if (ovfInformationTO != null) {
-                List<OVFPropertyTO> ovfProperties = ovfInformationTO.getProperties();
-                List<OVFNetworkTO> networkRequirements = ovfInformationTO.getNetworks();
-                OVFVirtualHardwareSectionTO ovfHardwareSection = ovfInformationTO.getHardwareSection();
-                List<OVFEulaSectionTO> eulaSections = ovfInformationTO.getEulaSections();
-                Pair<String, String> guestOsInfo = ovfInformationTO.getGuestOsInfo();
+    private void persistTemplateOVFInformation(long templateId, OVFInformationTO ovfInformationTO) {
+        List<OVFPropertyTO> ovfProperties = ovfInformationTO.getProperties();
+        List<OVFNetworkTO> networkRequirements = ovfInformationTO.getNetworks();
+        OVFVirtualHardwareSectionTO ovfHardwareSection = ovfInformationTO.getHardwareSection();
+        List<OVFEulaSectionTO> eulaSections = ovfInformationTO.getEulaSections();
+        Pair<String, String> guestOsInfo = ovfInformationTO.getGuestOsInfo();
 
-                if (CollectionUtils.isNotEmpty(ovfProperties)) {
-                    persistTemplateDeployAsIsInformationTOList(templateId, ovfProperties);
-                }
-                if (CollectionUtils.isNotEmpty(networkRequirements)) {
-                    persistTemplateDeployAsIsInformationTOList(templateId, networkRequirements);
-                }
-                if (CollectionUtils.isNotEmpty(eulaSections)) {
-                    persistTemplateDeployAsIsInformationTOList(templateId, eulaSections);
-                }
-                String minimumHardwareVersion = null;
-                if (ovfHardwareSection != null) {
-                    if (CollectionUtils.isNotEmpty(ovfHardwareSection.getConfigurations())) {
-                        persistTemplateDeployAsIsInformationTOList(templateId, ovfHardwareSection.getConfigurations());
-                    }
-                    if (CollectionUtils.isNotEmpty(ovfHardwareSection.getCommonHardwareItems())) {
-                        persistTemplateDeployAsIsInformationTOList(templateId, ovfHardwareSection.getCommonHardwareItems());
-                    }
-                    minimumHardwareVersion = ovfHardwareSection.getMinimiumHardwareVersion();
-                }
-                if (guestOsInfo != null) {
-                    String osType = guestOsInfo.first();
-                    String osDescription = guestOsInfo.second();
-                    LOGGER.info("Guest OS information retrieved from the template: " + osType + " - " + osDescription);
-                    handleGuestOsFromOVFDescriptor(templateId, osType, osDescription, minimumHardwareVersion);
-                }
+        if (CollectionUtils.isNotEmpty(ovfProperties)) {
+            persistTemplateDeployAsIsInformationTOList(templateId, ovfProperties);
+        }
+        if (CollectionUtils.isNotEmpty(networkRequirements)) {
+            persistTemplateDeployAsIsInformationTOList(templateId, networkRequirements);
+        }
+        if (CollectionUtils.isNotEmpty(eulaSections)) {
+            persistTemplateDeployAsIsInformationTOList(templateId, eulaSections);
+        }
+        String minimumHardwareVersion = null;
+        if (ovfHardwareSection != null) {
+            if (CollectionUtils.isNotEmpty(ovfHardwareSection.getConfigurations())) {
+                persistTemplateDeployAsIsInformationTOList(templateId, ovfHardwareSection.getConfigurations());
+            }
+            if (CollectionUtils.isNotEmpty(ovfHardwareSection.getCommonHardwareItems())) {
+                persistTemplateDeployAsIsInformationTOList(templateId, ovfHardwareSection.getCommonHardwareItems());
+            }
+            minimumHardwareVersion = ovfHardwareSection.getMinimiumHardwareVersion();
+        }
+        if (guestOsInfo != null) {
+            String osType = guestOsInfo.first();
+            String osDescription = guestOsInfo.second();
+            LOGGER.info("Guest OS information retrieved from the template: " + osType + " - " + osDescription);
+            handleGuestOsFromOVFDescriptor(templateId, osType, osDescription, minimumHardwareVersion);
+        }
+    }
+
+    public boolean persistTemplateOVFInformationAndUpdateGuestOS(long templateId, OVFInformationTO ovfInformationTO, TemplateDataStoreVO tmpltStoreVO) {
+        try {
+            if (ovfInformationTO != null) {
+                persistTemplateOVFInformation(templateId, ovfInformationTO);
             }
         } catch (Exception e) {
             LOGGER.error("Error persisting deploy-as-is details for template " + templateId, e);
@@ -151,7 +153,7 @@
     /**
      * Returns the mapped guest OS from the OVF file of the template to the CloudStack database OS ID
      */
-    public Long retrieveTemplateGuestOsIdFromGuestOsInfo(long templateId, String guestOsType, String guestOsDescription,
+    private Long retrieveTemplateGuestOsIdFromGuestOsInfo(long templateId, String guestOsType, String guestOsDescription,
                                                          String minimumHardwareVersion) {
         VMTemplateVO template = templateDao.findById(templateId);
         Hypervisor.HypervisorType hypervisor = template.getHypervisorType();
@@ -337,7 +339,7 @@
         return map;
     }
 
-    private void persistTemplateDeployAsIsInformationTOList(long templateId,
+    public void persistTemplateDeployAsIsInformationTOList(long templateId,
                                                             List<? extends TemplateDeployAsIsInformationTO> informationTOList) {
         for (TemplateDeployAsIsInformationTO informationTO : informationTOList) {
             String propKey = getKeyFromInformationTO(informationTO);
diff --git a/server/src/main/java/com/cloud/storage/ImageStoreUploadMonitorImpl.java b/server/src/main/java/com/cloud/storage/ImageStoreUploadMonitorImpl.java
index 0720927..7916f4a 100755
--- a/server/src/main/java/com/cloud/storage/ImageStoreUploadMonitorImpl.java
+++ b/server/src/main/java/com/cloud/storage/ImageStoreUploadMonitorImpl.java
@@ -25,8 +25,7 @@
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
-import com.cloud.hypervisor.Hypervisor;
-import com.cloud.utils.Pair;
+import com.cloud.agent.api.to.OVFInformationTO;
 import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
 import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
 import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint;
@@ -412,18 +411,14 @@
 
                             VMTemplateVO templateUpdate = _templateDao.createForUpdate();
                             templateUpdate.setSize(answer.getVirtualSize());
-                            if (template.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
-                                Pair<String, String> guestOsInfo = answer.getGuestOsInfo();
-                                String minimumHardwareVersion = answer.getMinimumHardwareVersion();
-                                String osType = guestOsInfo.first();
-                                String osDescription = guestOsInfo.second();
-                                s_logger.info("Guest OS information retrieved from the template: " + osType + " - " + osDescription);
-                                try {
-                                    Long guestOsId = deployAsIsHelper.retrieveTemplateGuestOsIdFromGuestOsInfo(template.getId(),
-                                            osType, osDescription, minimumHardwareVersion);
-                                    templateUpdate.setGuestOSId(guestOsId);
-                                } catch (CloudRuntimeException e) {
-                                    s_logger.error("Could not map the guest OS to a CloudStack guest OS", e);
+
+                            OVFInformationTO ovfInformationTO = answer.getOvfInformationTO();
+                            if (template.isDeployAsIs() && ovfInformationTO != null) {
+                                s_logger.debug("Received OVF information from the uploaded template");
+                                boolean persistDeployAsIs = deployAsIsHelper.persistTemplateOVFInformationAndUpdateGuestOS(tmpTemplate.getId(), ovfInformationTO, tmpTemplateDataStore);
+                                if (!persistDeployAsIs) {
+                                    s_logger.info("Failed persisting deploy-as-is template details for template " + template.getName());
+                                    break;
                                 }
                             }
                             _templateDao.update(tmpTemplate.getId(), templateUpdate);
diff --git a/server/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java b/server/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java
index a04b043..c1be0d8 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java
@@ -16,8 +16,8 @@
 // under the License.
 package org.apache.cloudstack.storage.image.deployasis;
 
-import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.agent.api.to.NicTO;
+import com.cloud.agent.api.to.OVFInformationTO;
 import com.cloud.vm.VirtualMachineProfile;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
 
@@ -25,9 +25,7 @@
 
 public interface DeployAsIsHelper {
 
-    boolean persistTemplateDeployAsIsDetails(long templateId, DownloadAnswer answer, TemplateDataStoreVO tmpltStoreVO);
+    boolean persistTemplateOVFInformationAndUpdateGuestOS(long templateId, OVFInformationTO ovfInformationTO, TemplateDataStoreVO tmpltStoreVO);
     Map<String, String> getVirtualMachineDeployAsIsProperties(VirtualMachineProfile vmId);
     Map<Integer, String> getAllocatedVirtualMachineNicsAdapterMapping(VirtualMachineProfile vm, NicTO[] nics);
-    Long retrieveTemplateGuestOsIdFromGuestOsInfo(long templateId, String guestOsType, String guestOsDescription,
-                                                  String minimumHardwareVersion);
 }
diff --git a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
index 565c4df..ed5530c 100644
--- a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
+++ b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
@@ -2280,8 +2280,9 @@
                 answer.setInstallPath(uploadEntity.getTmpltPath());
                 answer.setPhysicalSize(uploadEntity.getPhysicalSize());
                 answer.setDownloadPercent(100);
-                answer.setGuestOsInfo(uploadEntity.getGuestOsInfo());
-                answer.setMinimumHardwareVersion(uploadEntity.getMinimumHardwareVersion());
+                if (uploadEntity.getOvfInformationTO() != null) {
+                    answer.setOvfInformationTO(uploadEntity.getOvfInformationTO());
+                }
                 uploadEntityStateMap.remove(entityUuid);
                 return answer;
             } else if (uploadEntity.getUploadState() == UploadEntity.Status.IN_PROGRESS) {
@@ -3424,12 +3425,7 @@
                 uploadEntity.setVirtualSize(info.virtualSize);
                 uploadEntity.setPhysicalSize(info.size);
                 if (info.ovfInformationTO != null) {
-                    if (info.ovfInformationTO.getGuestOsInfo() != null) {
-                        uploadEntity.setGuestOsInfo(info.ovfInformationTO.getGuestOsInfo());
-                    }
-                    if (info.ovfInformationTO.getHardwareSection() != null) {
-                        uploadEntity.setMinimumHardwareVersion(info.ovfInformationTO.getHardwareSection().getMinimiumHardwareVersion());
-                    }
+                    uploadEntity.setOvfInformationTO(info.ovfInformationTO);
                 }
                 break;
             }
diff --git a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadEntity.java b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadEntity.java
index 6d5154e..8acf4d7 100644
--- a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadEntity.java
+++ b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadEntity.java
@@ -18,8 +18,8 @@
 package org.apache.cloudstack.storage.template;
 
 
+import com.cloud.agent.api.to.OVFInformationTO;
 import com.cloud.storage.Storage;
-import com.cloud.utils.Pair;
 
 public class UploadEntity {
     private long downloadedsize;
@@ -36,8 +36,7 @@
     private String description;
     private long contentLength;
     private long processTimeout;
-    private Pair<String, String> guestOsInfo;
-    private String minimumHardwareVersion;
+    private OVFInformationTO ovfInformationTO;
 
     public static enum ResourceType {
         VOLUME, TEMPLATE
@@ -211,19 +210,11 @@
         this.contentLength = contentLength;
     }
 
-    public Pair<String, String> getGuestOsInfo() {
-        return guestOsInfo;
+    public OVFInformationTO getOvfInformationTO() {
+        return ovfInformationTO;
     }
 
-    public void setGuestOsInfo(Pair<String, String> guestOsInfo) {
-        this.guestOsInfo = guestOsInfo;
-    }
-
-    public void setMinimumHardwareVersion(String minimumHardwareVersion) {
-        this.minimumHardwareVersion = minimumHardwareVersion;
-    }
-
-    public String getMinimumHardwareVersion() {
-        return minimumHardwareVersion;
+    public void setOvfInformationTO(OVFInformationTO ovfInformationTO) {
+        this.ovfInformationTO = ovfInformationTO;
     }
 }