From 4b978c9cc115c4f4032d5f3dbc4d3cde002449cc Mon Sep 17 00:00:00 2001 From: vasraz Date: Sat, 26 Feb 2022 17:22:58 +0000 Subject: Implement improved MinIo client Change-Id: Ic9abd6b0bdaa17e9deff2279a64416d81f7ad606 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3886 --- .../services/OrchestrationTemplateCandidateImpl.java | 14 ++++++++++++-- .../services/OrchestrationTemplateCandidateImplTest.java | 16 ++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java index b51583980d..01d2a59e45 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java @@ -32,6 +32,7 @@ import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR; import static org.openecomp.sdc.common.errors.Messages.UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING; import java.io.ByteArrayInputStream; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -102,6 +103,7 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate private final VendorSoftwareProductManager vendorSoftwareProductManager; private final ActivityLogManager activityLogManager; private final ArtifactStorageManager artifactStorageManager; + private final StorageFactory storageFactory; private final PackageSizeReducer packageSizeReducer; private final OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager; @@ -111,7 +113,7 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface(); this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); LOGGER.info("Instantiating artifactStorageManager"); - final StorageFactory storageFactory = new StorageFactory(); + this.storageFactory = new StorageFactory(); this.artifactStorageManager = storageFactory.createArtifactStorageManager(); LOGGER.info("Instantiating packageSizeReducer"); this.packageSizeReducer = storageFactory.createPackageSizeReducer().orElse(null); @@ -129,12 +131,14 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate this.vendorSoftwareProductManager = vendorSoftwareProductManager; this.activityLogManager = activityLogManager; this.artifactStorageManager = artifactStorageManager; + this.storageFactory = new StorageFactory(); this.packageSizeReducer = packageSizeReducer; this.orchestrationTemplateCandidateUploadManager = orchestrationTemplateCandidateUploadManager; } @Override public Response upload(String vspId, String versionId, final Attachment fileToUpload, final String user) { + LOGGER.debug("STARTED -> OrchestrationTemplateCandidateImpl.upload"); vspId = ValidationUtils.sanitizeInputString(vspId); versionId = ValidationUtils.sanitizeInputString(versionId); final Response response; @@ -145,6 +149,7 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate final DataHandler dataHandler = fileToUpload.getDataHandler(); final var filename = ValidationUtils.sanitizeInputString(dataHandler.getName()); ArtifactInfo artifactInfo = null; + final ArtifactStorageManager artifactStorageManager = storageFactory.createArtifactStorageManager(); if (artifactStorageManager.isEnabled()) { artifactInfo = handleArtifactStorage(vspId, versionId, filename, dataHandler); fileToUploadBytes = artifactInfo.getBytes(); @@ -190,6 +195,7 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate } throw ex; } + LOGGER.debug("FINISHED -> OrchestrationTemplateCandidateImpl.upload"); return response; } @@ -202,22 +208,26 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate final Path folder = Path.of(storageConfiguration.getTempPath()).resolve(vspId).resolve(versionId); tempArtifactPath = folder.resolve(UUID.randomUUID().toString()); Files.createDirectories(folder); + LOGGER.debug("STARTED -> Transfer to '{}'", tempArtifactPath.toString()); try (final InputStream packageInputStream = artifactDataHandler.getInputStream(); final var fileOutputStream = new FileOutputStream(tempArtifactPath.toFile())) { packageInputStream.transferTo(fileOutputStream); } + LOGGER.debug("FINISHED -> Transfer to '{}'", tempArtifactPath.toString()); } catch (final Exception e) { throw new ArtifactStorageException(UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename)); } final ArtifactInfo artifactInfo; - try (final InputStream inputStream = Files.newInputStream(tempArtifactPath)) { + try (final InputStream inputStream = new FileInputStream(tempArtifactPath.toFile())) { artifactInfo = artifactStorageManager.upload(vspId, versionId, inputStream); } catch (final Exception e) { LOGGER.error("Package Size Reducer not configured", e); throw new ArtifactStorageException(ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT.formatMessage(filename)); } try { + LOGGER.debug("STARTED -> reducing '{}'", tempArtifactPath.toString()); artifactInfo.setBytes(packageSizeReducer.reduce(tempArtifactPath)); + LOGGER.debug("FINISHED -> reducing '{}'", tempArtifactPath.toString()); Files.delete(tempArtifactPath); } catch (final Exception e) { LOGGER.error("Package Size Reducer not configured", e); diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImplTest.java index 6c21cc471e..802d6d885a 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImplTest.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImplTest.java @@ -65,6 +65,7 @@ import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig; import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials; import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint; import org.openecomp.sdc.be.csar.storage.PackageSizeReducer; +import org.openecomp.sdc.be.csar.storage.StorageFactory; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager; @@ -100,6 +101,10 @@ class OrchestrationTemplateCandidateImplTest { private PackageSizeReducer packageSizeReducer; @Mock private OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager; + @Mock + private StorageFactory storageFactory; + @Mock + private Attachment fileToUpload; @InjectMocks private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate; @@ -178,9 +183,10 @@ class OrchestrationTemplateCandidateImplTest { @Test void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException { + when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager); when(artifactStorageManager.isEnabled()).thenReturn(true); - when(artifactStorageManager.getStorageConfiguration()).thenReturn( - new MinIoStorageArtifactStorageConfig(true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "tempPath")); + when(artifactStorageManager.getStorageConfiguration()).thenReturn(new MinIoStorageArtifactStorageConfig + (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "tempPath", 10_000_000)); final Path path = Path.of("src/test/resources/files/sample-not-signed.csar"); final String vspId = "vspId"; @@ -316,12 +322,10 @@ class OrchestrationTemplateCandidateImplTest { vspUploadStatusDto.setLockId(UUID.randomUUID()); when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto); final RuntimeException forcedException = new RuntimeException(); - when(artifactStorageManager.isEnabled()).thenThrow(forcedException); - final Attachment mock = Mockito.mock(Attachment.class); - when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class)); + when(fileToUpload.getDataHandler()).thenThrow(forcedException); //when final RuntimeException actualException = assertThrows(RuntimeException.class, - () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user)); + () -> orchestrationTemplateCandidate.upload(candidateId, versionId, fileToUpload, user)); //then assertEquals(forcedException, actualException); verify(orchestrationTemplateCandidateUploadManager) -- cgit 1.2.3-korg