diff options
Diffstat (limited to 'catalog-be')
11 files changed, 168 insertions, 18 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogic.java index 499a6127a1..3b92e62e72 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogic.java @@ -67,6 +67,7 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.BaseType; import org.openecomp.sdc.be.model.CatalogUpdateTimestamp; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentParametersView; @@ -1286,4 +1287,12 @@ public class ElementBusinessLogic extends BaseBusinessLogic { janusGraphDao.commit(); } } + + public Either<List<BaseType>, ActionStatus> getBaseTypes(final String categoryName, final String userId) { + final ActionStatus status = validateUserExistsActionStatus(userId); + if (ActionStatus.OK != status) { + return Either.right(status); + } + return Either.left(elementOperation.getBaseTypes(categoryName)); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/generic/GenericTypeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/generic/GenericTypeBusinessLogic.java index a00f44ce39..313d345da1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/generic/GenericTypeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/generic/GenericTypeBusinessLogic.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; @@ -62,13 +63,19 @@ public class GenericTypeBusinessLogic { log.debug("Failed to fetch certified generic node type for component {}", component.getName()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } - Either<Resource, StorageOperationStatus> findLatestGeneric = toscaOperationFacade + Either<Resource, StorageOperationStatus> genericType; + if (StringUtils.isEmpty(component.getDerivedFromGenericVersion())){ + genericType = toscaOperationFacade .getLatestCertifiedNodeTypeByToscaResourceName(genericTypeToscaName); - if (findLatestGeneric.isRight()) { - log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName)); + if (genericType.isRight()) { + log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName)); + } + } else { + genericType = toscaOperationFacade.getByToscaResourceNameAndVersion(genericTypeToscaName, component.getDerivedFromGenericVersion()); } - Resource genericTypeResource = findLatestGeneric.left().value(); + + Resource genericTypeResource = genericType.left().value(); return Either.left(genericTypeResource); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java index 5bbf84906f..8439ffbefd 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java @@ -445,6 +445,8 @@ public class UiComponentDataConverter { UiServiceMetadata metadata = new UiServiceMetadata(service.getCategories(), (ServiceMetadataDataDefinition) service.getComponentMetadataDefinition().getMetadataDataDefinition()); dataTransfer.setMetadata(metadata); + dataTransfer.setDerivedFromGenericType(service.getDerivedFromGenericType()); + dataTransfer.setDerivedFromGenericVersion(service.getDerivedFromGenericVersion()); break; case NODE_FILTER: if (service.getNodeFilterComponents() == null) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ElementServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ElementServlet.java index 395c610e34..4efca8a2b8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ElementServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ElementServlet.java @@ -65,6 +65,7 @@ import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.info.ArtifactTypesInfo; import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.BaseType; import org.openecomp.sdc.be.model.CatalogUpdateTimestamp; import org.openecomp.sdc.be.model.Category; import org.openecomp.sdc.be.model.Component; @@ -189,6 +190,39 @@ public class ElementServlet extends BeGenericServlet { throw e; } } + + @GET + @Path("/category/{componentType}/{categoryName}/baseTypes") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Get base types for category", method = "GET", summary = "Get base types for category", + responses = {@ApiResponse(responseCode = "200", description = "Returns base types Ok"), + @ApiResponse(responseCode = "404", description = "No base types were found"), + @ApiResponse(responseCode = "500", description = "Internal Server Error")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response getCategoryBaseTypes(@PathParam(value = "categoryName") final String categoryName, + @PathParam(value = "componentType") final String componentType, @Context final HttpServletRequest request, + @HeaderParam(value = Constants.USER_ID_HEADER) String userId) { + + try { + final ElementBusinessLogic elementBL = getElementBL(request.getSession().getServletContext()); + final Either<List<BaseType>, ActionStatus> either = elementBL.getBaseTypes(categoryName, userId); + + if (either.isRight() || either.left().value() == null) { + log.debug("No base types were found"); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT)); + } else { + final Map<String, Object> baseTypesMap = new HashMap<>(); + baseTypesMap.put("baseTypes", either.left().value()); + + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), baseTypesMap); + } + } catch (Exception e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get base types of category"); + log.debug("getCategoryBaseTypes failed with exception", e); + throw e; + } + } @DELETE @Path("/category/{componentType}/{categoryUniqueId}") diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java index 3597c5c23c..b083fbfe2a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java @@ -418,7 +418,7 @@ public class CsarUtils { //US798487 - Abstraction of complex types if (!ModelConverter.isAtomicComponent(component)) { log.debug("Component {} is complex - generating abstract type for it..", component.getName()); - writeComponentInterface(component, zip, fileName, false); + dependencies.addAll(writeComponentInterface(component, zip, fileName, false)); } //UID <cassandraId,filename,component> Either<ZipOutputStream, ResponseFormat> zipOutputStreamOrResponseFormat = getZipOutputStreamResponseFormatEither(zip, dependencies); @@ -747,18 +747,31 @@ public class CsarUtils { return componentRI; } - private Either<ZipOutputStream, ResponseFormat> writeComponentInterface(Component component, ZipOutputStream zip, String fileName, + private List<Triple<String, String, Component>> writeComponentInterface(Component component, + ZipOutputStream zip, + String fileName, + boolean isAssociatedComponent + ){ + final Either<ToscaRepresentation, ToscaError> interfaceRepresentation = toscaExportUtils.exportComponentInterface(component, false); + writeComponentInterface(interfaceRepresentation, zip, fileName, false); + return interfaceRepresentation.left().value().getDependencies().getOrElse(new ArrayList<>()); + } + + + private Either<ZipOutputStream, ResponseFormat> writeComponentInterface( + Either<ToscaRepresentation,ToscaError> interfaceRepresentation, ZipOutputStream zip, String fileName, boolean isAssociatedComponent) { // TODO: This should not be done but we need this to keep the refactoring small enough to be easily reviewable - return writeComponentInterface(component, fileName, isAssociatedComponent, ZipWriter.live(zip)) + return writeComponentInterface(interfaceRepresentation, fileName, isAssociatedComponent, ZipWriter.live(zip)) .map(void0 -> Either.<ZipOutputStream, ResponseFormat>left(zip)).recover(th -> { log.error("#writeComponentInterface - zip writing failed with error: ", th); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); }).get(); } - private Try<Void> writeComponentInterface(Component component, String fileName, boolean isAssociatedComponent, ZipWriter zw) { - Either<byte[], ToscaError> yml = toscaExportUtils.exportComponentInterface(component, isAssociatedComponent).left() + private Try<Void> writeComponentInterface( + Either<ToscaRepresentation,ToscaError> interfaceRepresentation, String fileName, boolean isAssociatedComponent, ZipWriter zw) { + Either<byte[], ToscaError> yml = interfaceRepresentation.left() .map(ToscaRepresentation::getMainYaml); return fromEither(yml, ToscaErrorException::new).flatMap(zw.write(DEFINITIONS_PATH + ToscaExportHandler.getInterfaceFilename(fileName))); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 988f709bfc..efb3f49d03 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -199,17 +199,27 @@ public class ToscaExportHandler { } public Either<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component, final boolean isAssociatedComponent) { - final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig(); - if (CollectionUtils.isEmpty(defaultToscaImportConfig)) { + final List<Map<String, Map<String, String>>> imports = getDefaultToscaImportConfig(); + if (CollectionUtils.isEmpty(imports)) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); } + List<Triple<String, String, Component>> dependencies = new ArrayList<>(); + if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().startsWith("org.openecomp.resource.abstract.nodes.")) { + final Either<Component, StorageOperationStatus> baseType = toscaOperationFacade.getByToscaResourceNameAndVersion(component.getDerivedFromGenericType(), component.getDerivedFromGenericVersion()); + if (baseType.isLeft() && baseType.left().value() != null) { + addDependencies(imports, dependencies , baseType.left().value()); + } else { + log.debug("Failed to fetch derived from type {}", component.getDerivedFromGenericType()); + } + } + String toscaVersion = null; if (component instanceof Resource) { toscaVersion = ((Resource) component).getToscaVersion(); } ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION); - toscaTemplate.setImports(new ArrayList<>(defaultToscaImportConfig)); + toscaTemplate.setImports(new ArrayList<>(imports)); final Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); final Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes, isAssociatedComponent); @@ -217,6 +227,7 @@ public class ToscaExportHandler { return Either.right(toscaTemplateRes.right().value()); } toscaTemplate = toscaTemplateRes.left().value(); + toscaTemplate.setDependencies(dependencies); ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate); return Either.left(toscaRepresentation); } @@ -493,7 +504,7 @@ public class ToscaExportHandler { if (!ModelConverter.isAtomicComponent(component)) { final List<Map<String, Map<String, String>>> additionalImports = toscaTemplate.getImports() == null ? new ArrayList<>(defaultToscaImportConfig) : new ArrayList<>(toscaTemplate.getImports()); - List<Triple<String, String, Component>> dependecies = new ArrayList<>(); + List<Triple<String, String, Component>> dependencies = new ArrayList<>(); Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts(); if (isNotEmpty(toscaArtifacts)) { ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); @@ -510,9 +521,9 @@ public class ToscaExportHandler { } List<ComponentInstance> componentInstances = component.getComponentInstances(); if (componentInstances != null && !componentInstances.isEmpty()) { - componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci)); + componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependencies, ci)); } - toscaTemplate.setDependencies(dependecies); + toscaTemplate.setDependencies(dependencies); toscaTemplate.setImports(additionalImports); } else { log.debug("currently imports supported for VF and service only"); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/ElementOperationMock.java b/catalog-be/src/test/java/org/openecomp/sdc/ElementOperationMock.java index f9605c1aed..5d05e65886 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/ElementOperationMock.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/ElementOperationMock.java @@ -26,6 +26,7 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.BaseType; import org.openecomp.sdc.be.model.Category; import org.openecomp.sdc.be.model.PropertyScope; import org.openecomp.sdc.be.model.Tag; @@ -280,4 +281,10 @@ public class ElementOperationMock implements IElementOperation { return null; } + @Override + public List<BaseType> getBaseTypes(String categoryName) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java index c87bb2481d..36eee96848 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java @@ -39,6 +39,7 @@ import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.BaseType; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.Product; import org.openecomp.sdc.be.model.Resource; @@ -304,4 +305,23 @@ public class ElementBusinessLogicTest extends BaseBusinessLogicMock { Assert.assertTrue(response.isRight()); Assert.assertEquals((Integer) 9, response.right().value().getStatus()); } + + @Test + public void testGetBaseTypes_givenValidUserAndComponentType_thenReturnsSuccessful() { + + List<BaseType> baseTypes = new ArrayList<>(); + baseTypes.add(new BaseType("org.openecomp.type")); + String categoryName = "CAT01"; + + when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.OK); + when(elementDao.getBaseTypes(categoryName)).thenReturn(baseTypes); + Assert.assertTrue(elementBusinessLogic.getBaseTypes(categoryName, user.getUserId()) + .isLeft()); + } + + @Test + public void testGetBaseTypes_givenUserValidationFails_thenReturnsException() { + when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.RESTRICTED_OPERATION); + Assert.assertTrue(elementBusinessLogic.getBaseTypes("CAT01", user.getUserId()).isRight()); + } }
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ElementServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ElementServletTest.java index cd8c0c57ab..4d2c3621d9 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ElementServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ElementServletTest.java @@ -68,6 +68,7 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.impl.WebAppContextWrapper; import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.BaseType; import org.openecomp.sdc.be.model.PropertyScope; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Tag; @@ -1104,4 +1105,40 @@ class ElementServletTest extends JerseyTest { }) .property("contextConfig", context); } + + @Test + void getBaseTypesTest() { + String path = "/v1/category/services/CAT1/baseTypes"; + Either<List<BaseType>, ActionStatus> baseTypesEither = Either.left(new ArrayList<>()); + when(elementBusinessLogic.getBaseTypes("CAT1", designerUser.getUserId())) + .thenReturn(baseTypesEither); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); + } + + @Test + void getBaseTypesNoBaseTypesFoundTest() { + String path = "/v1/category/services/CAT1/baseTypes"; + Either<List<BaseType>, ActionStatus> baseTypesEither = Either.right(ActionStatus.NO_CONTENT); + + when(elementBusinessLogic.getBaseTypes("CAT1", designerUser.getUserId())) + .thenReturn(baseTypesEither); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT); + } + }
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java index 4aa967be47..ce7a10eb28 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java @@ -582,10 +582,9 @@ public class CsarUtilsTest extends BeConfDependentTest { try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) { - Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false); + List<Triple<String, String, Component>> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false); assertNotNull(output); - assertTrue(output.isLeft()); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java index bca8827736..1b38a245c4 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java @@ -521,6 +521,17 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(toscaOperationFacade.getToscaFullElement(any(String.class))) .thenReturn(Either.left(component)); + + Resource baseType = getNewResource(); + Map<String, ArtifactDefinition> baseTypeToscaArtifacts = new HashMap<>(); + ArtifactDefinition baseTypeArtifact = new ArtifactDefinition(); + baseTypeArtifact.setArtifactName("typeA"); + baseTypeToscaArtifacts.put("assettoscatemplate", baseTypeArtifact); + baseType.setToscaArtifacts(baseTypeToscaArtifacts); + + component.setDerivedFromGenericType("org.typeA"); + component.setDerivedFromGenericVersion("1.0"); + when(toscaOperationFacade.getByToscaResourceNameAndVersion("org.typeA", "1.0")).thenReturn(Either.left(baseType)); // default test result = Deencapsulation.invoke(testSubject, "fillImports", component, toscaTemplate); |