From ccc1f364baea63733e93c6f4eb26ea97eeb77960 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Tue, 3 Sep 2019 08:37:55 +0000 Subject: Onboard Package Handling Retain the original onboarded package when it comes in zip/csar format. Issue-ID: SDC-2510 Change-Id: Ie293db25eca8872be26ffc8475b25ccace1aab6f Signed-off-by: aribeiro --- .../OrchestrationTemplateCandidateImpl.java | 114 +++++++++++++++------ .../OrchestrationTemplateCandidateImplTest.java | 50 ++++----- .../rest/services/VnfPackageRepositoryImpl.java | 33 +++--- 3 files changed, 126 insertions(+), 71 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 5cf6f40c37..10f96e9d3b 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 @@ -19,8 +19,24 @@ package org.openecomp.sdcrests.vsp.rest.services; +import static org.openecomp.core.utilities.file.FileUtils.getFileExtension; +import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName; +import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import javax.activation.DataHandler; +import javax.inject.Named; +import javax.ws.rs.core.Response; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.cxf.jaxrs.ext.multipart.Attachment; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.activitylog.ActivityLogManager; import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; @@ -35,7 +51,10 @@ import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateMan import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory; import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException; +import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage; +import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo; import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse; @@ -53,20 +72,6 @@ import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; -import javax.inject.Named; -import javax.ws.rs.core.Response; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static org.openecomp.core.utilities.file.FileUtils.getFileExtension; -import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName; -import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; - @Named @Service("orchestrationTemplateCandidate") @Scope(value = "prototype") @@ -81,35 +86,86 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate ActivityLogManagerFactory.getInstance().createInterface(); @Override - public Response upload(String vspId, String versionId, Attachment fileToUpload, String user) { - PackageArchive archive = new PackageArchive(fileToUpload.getObject(byte[].class)); + public Response upload(final String vspId, final String versionId, + final Attachment fileToUpload, final String user) { + final byte[] fileToUploadBytes = fileToUpload.getObject(byte[].class); + String fileToUploadName = ""; + String fileToUploadExtension = ""; + final DataHandler dataHandler = fileToUpload.getDataHandler(); + if(dataHandler != null) { + final String filename = dataHandler.getName(); + fileToUploadName = FilenameUtils.removeExtension(filename); + fileToUploadExtension = FilenameUtils.getExtension(filename); + } + final PackageArchive archive = new PackageArchive(fileToUploadBytes); + final Optional validatePackageArchiveResponse = + validatePackageArchive(archive); + if (!validatePackageArchiveResponse.isPresent()) { + final VspDetails vspDetails = new VspDetails(vspId, new Version(versionId)); + return processOnboardPackage(fileToUpload, fileToUploadBytes, fileToUploadName, + fileToUploadExtension, archive, vspDetails); + } else { + return Response.ok(validatePackageArchiveResponse.get()).build(); + } + } + + private Optional validatePackageArchive(final PackageArchive archive) { UploadFileResponseDto uploadFileResponseDto; try { if (archive.isSigned() && !archive.isSignatureValid()) { - ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, - getErrorWithParameters(Messages.FAILED_TO_VERIFY_SIGNATURE.getErrorMessage(), "")); + final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.FAILED_TO_VERIFY_SIGNATURE.getErrorMessage(), "")); LOGGER.error(errorMessage.getMessage()); uploadFileResponseDto = buildUploadResponseWithError(errorMessage); //returning OK as SDC UI won't show error message if NOT OK error code. - return Response.ok(uploadFileResponseDto).build(); + return Optional.of(uploadFileResponseDto); } + } catch (final SecurityManagerException e) { + final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(e.getMessage(), "")); + LOGGER.error(errorMessage.getMessage(), e); + uploadFileResponseDto = buildUploadResponseWithError(errorMessage); + //returning OK as SDC UI won't show error message if NOT OK error code. + return Optional.of(uploadFileResponseDto); + } + return Optional.empty(); + } - String filename = archive.getArchiveFileName().orElse(fileToUpload.getContentDisposition().getFilename()); - UploadFileResponse uploadFileResponse = candidateManager - .upload(vspId, new Version(versionId), new ByteArrayInputStream(archive.getPackageFileContents()), - getFileExtension(filename), getNetworkPackageName(filename)); - + private Response processOnboardPackage(final Attachment fileToUpload, + final byte[] fileToUploadBytes, + final String fileToUploadName, + final String fileToUploadExtension, + final PackageArchive archive, + final VspDetails vspDetails) { + final String filename = archive.getArchiveFileName() + .orElse(fileToUpload.getContentDisposition().getFilename()); + UploadFileResponseDto uploadFileResponseDto; + try { + final String archiveFileExtension = getFileExtension(filename); + final OnboardPackageInfo onboardPackageInfo; + if (OnboardingTypesEnum.CSAR.toString().equalsIgnoreCase(archiveFileExtension)) { + final OnboardPackage onboardPackage = new OnboardPackage(getNetworkPackageName(filename), + archiveFileExtension, ByteBuffer.wrap(archive.getPackageFileContents())); + onboardPackageInfo = new OnboardPackageInfo(fileToUploadName, + fileToUploadExtension, ByteBuffer.wrap(fileToUploadBytes), onboardPackage); + } else { + onboardPackageInfo = new OnboardPackageInfo(fileToUploadName, + fileToUploadExtension, ByteBuffer.wrap(fileToUploadBytes)); + } + final UploadFileResponse uploadFileResponse = candidateManager + .upload(vspDetails, onboardPackageInfo); uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto() - .applyMapping(uploadFileResponse, UploadFileResponseDto.class); - } catch (SecurityManagerException e) { - ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, - getErrorWithParameters(e.getMessage(), "")); + .applyMapping(uploadFileResponse, UploadFileResponseDto.class); + + return Response.ok(uploadFileResponseDto).build(); + } catch (final SecurityManagerException e) { + final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(e.getMessage(), "")); LOGGER.error(errorMessage.getMessage(), e); uploadFileResponseDto = buildUploadResponseWithError(errorMessage); //returning OK as SDC UI won't show error message if NOT OK error code. return Response.ok(uploadFileResponseDto).build(); } - return Response.ok(uploadFileResponseDto).build(); } private UploadFileResponseDto buildUploadResponseWithError(ErrorMessage errorMessage) { 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 0ebd014dbe..a9dfbb8449 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 @@ -20,6 +20,20 @@ package org.openecomp.sdcrests.vsp.rest.services; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.powermock.api.mockito.PowerMockito.whenNew; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Optional; +import java.util.UUID; +import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition; @@ -52,18 +66,6 @@ import org.openecomp.sdcrests.vsp.rest.data.PackageArchive; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.util.Arrays; -import java.util.Optional; -import java.util.UUID; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertFalse; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.*; - @RunWith(PowerMockRunner.class) @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class, OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class}) @@ -114,7 +116,7 @@ public class OrchestrationTemplateCandidateImplTest { UploadFileResponse uploadFileResponse = new UploadFileResponse(); uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP); uploadFileResponse.setNetworkPackageName("test"); - when(candidateManager.upload(any(), any(), any(), any(), any())).thenReturn(uploadFileResponse); + when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse); // get using the candidate manager. @@ -165,22 +167,24 @@ public class OrchestrationTemplateCandidateImplTest { when(packageArchive.isSigned()).thenReturn(true); when(packageArchive.isSignatureValid()).thenReturn(true); orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl(); - Attachment attachment = mock(Attachment.class); - when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test")); - Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1"); + Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1"); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - } @Test public void uploadNotSignedTest(){ when(packageArchive.isSigned()).thenReturn(false); orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl(); - Attachment attachment = mock(Attachment.class); - when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test")); - Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1"); + Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1"); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + private Attachment mockAttachment() { + final Attachment attachment = mock(Attachment.class); + when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test")); + final byte[] bytes = "upload package Test".getBytes(); + when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes); + return attachment; } @Test @@ -188,12 +192,9 @@ public class OrchestrationTemplateCandidateImplTest { when(packageArchive.isSigned()).thenReturn(true); when(packageArchive.isSignatureValid()).thenReturn(false); orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl(); - Attachment attachment = mock(Attachment.class); - when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test")); - Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1"); + Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1"); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty()); - } @Test @@ -204,7 +205,6 @@ public class OrchestrationTemplateCandidateImplTest { Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1); byte[] content = (byte[])rsp.getEntity(); Assert.assertEquals("World", new String(content)); - } @Test diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vnf-repository-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VnfPackageRepositoryImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vnf-repository-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VnfPackageRepositoryImpl.java index 6f76bf4527..5bcd3d3a39 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vnf-repository-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VnfPackageRepositoryImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vnf-repository-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VnfPackageRepositoryImpl.java @@ -21,10 +21,8 @@ import static javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION; import static org.openecomp.core.utilities.file.FileUtils.getFileExtension; import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName; -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.InputStream; import java.net.URI; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -49,6 +47,8 @@ import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager; import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo; import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.VersioningManagerFactory; @@ -135,24 +135,23 @@ public class VnfPackageRepositoryImpl implements VnfPackageRepository { } } - private Response uploadVnfPackage(String vspId, String versionId, String csarId, byte[] payload) { - - try (InputStream fileStream = new BufferedInputStream(new ByteArrayInputStream(payload))) { - - OrchestrationTemplateCandidateManager candidateManager = + private Response uploadVnfPackage(final String vspId, final String versionId, + final String csarId, final byte[] payload) { + try { + final OrchestrationTemplateCandidateManager candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface(); - - String filename = formatFilename(csarId); - Version version = getVersion(vspId, versionId); - UploadFileResponse response = candidateManager.upload(vspId, version, fileStream, - getFileExtension(filename), getNetworkPackageName(filename)); - - UploadFileResponseDto uploadFileResponse = new MapUploadFileResponseToUploadFileResponseDto() - .applyMapping(response, UploadFileResponseDto.class); + final String filename = formatFilename(csarId); + final OnboardPackageInfo onboardPackageInfo = new OnboardPackageInfo(getNetworkPackageName(filename), + getFileExtension(filename), ByteBuffer.wrap(payload)); + final VspDetails vspDetails = new VspDetails(vspId, getVersion(vspId, versionId)); + final UploadFileResponse response = candidateManager.upload(vspDetails, onboardPackageInfo); + final UploadFileResponseDto uploadFileResponse = + new MapUploadFileResponseToUploadFileResponseDto() + .applyMapping(response, UploadFileResponseDto.class); return Response.ok(uploadFileResponse).build(); - } catch (Exception e) { + } catch (final Exception e) { ErrorCode error = new GeneralErrorBuilder().build(); LOGGER.error("Exception while uploading package received from VNF Repository", new CoreException(error, e)); return generateInternalServerError(error); -- cgit 1.2.3-korg