summaryrefslogtreecommitdiffstats
path: root/catalog-be
diff options
context:
space:
mode:
authorKrupaNagabhushan <krupa.nagabhushan@est.tech>2022-10-26 17:26:54 +0100
committerKrupaNagabhushan <krupa.nagabhushan@est.tech>2023-01-19 15:23:12 +0000
commit3943534cd3409c0cca6588f36527df34345d74f2 (patch)
treefc10c05a93fe853f21f1f3beddf5d0557d7bad1d /catalog-be
parent8edfa4dee9fed9be5b38133847c60136294eba80 (diff)
Download data type from UI
Issue-ID: SDC-4332 Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech> Change-Id: If31f7a95eb2d568e716760a33ef2641bad09cf68
Diffstat (limited to 'catalog-be')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java65
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java37
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java12
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java10
4 files changed, 123 insertions, 1 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
index fd8b61887a..efdae4de96 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
@@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@@ -46,6 +47,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.ListUtils;
import org.openecomp.sdc.be.components.impl.ArtifactTypeBusinessLogic;
import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic;
@@ -57,6 +59,8 @@ import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
import org.openecomp.sdc.be.components.impl.ResourceImportManager;
import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
+import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
+import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
@@ -65,6 +69,7 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.exception.BusinessException;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.impl.ServletUtils;
+import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.DataTypeDefinition;
@@ -72,7 +77,9 @@ import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.RelationshipTypeDefinition;
import org.openecomp.sdc.be.model.User;
+import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
+import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.Wrapper;
@@ -92,12 +99,15 @@ public class TypesFetchServlet extends AbstractValidationsServlet {
private static final Logger log = Logger.getLogger(TypesFetchServlet.class);
private static final String FAILED_TO_GET_ALL_NON_ABSTRACT = "failed to get all non abstract {}";
private static final String START_HANDLE_REQUEST_OF_MODIFIER_ID_IS = "Start handle request of {} | modifier id is {}";
+ private static final String DATATYPE_FILE_TYPE = ".yml";
private final RelationshipTypeBusinessLogic relationshipTypeBusinessLogic;
private final CapabilitiesBusinessLogic capabilitiesBusinessLogic;
private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
private final ResourceBusinessLogic resourceBusinessLogic;
private final ArtifactTypeBusinessLogic artifactTypeBusinessLogic;
private final ModelOperation modelOperation;
+ private final DataTypeOperation dataTypeOperation;
+ private final ToscaExportHandler toscaExportUtils;
@Inject
public TypesFetchServlet(
@@ -110,7 +120,9 @@ public class TypesFetchServlet extends AbstractValidationsServlet {
InterfaceOperationBusinessLogic interfaceOperationBusinessLogic,
ResourceBusinessLogic resourceBusinessLogic,
ArtifactTypeBusinessLogic artifactTypeBusinessLogic,
- ModelOperation modelOperation
+ ModelOperation modelOperation,
+ DataTypeOperation dataTypeOperation,
+ ToscaExportHandler toscaExportUtils
) {
super(
componentInstanceBL,
@@ -124,6 +136,8 @@ public class TypesFetchServlet extends AbstractValidationsServlet {
this.resourceBusinessLogic = resourceBusinessLogic;
this.artifactTypeBusinessLogic = artifactTypeBusinessLogic;
this.modelOperation = modelOperation;
+ this.dataTypeOperation = dataTypeOperation;
+ this.toscaExportUtils = toscaExportUtils;
}
@GET
@@ -198,6 +212,35 @@ public class TypesFetchServlet extends AbstractValidationsServlet {
}
@GET
+ @Path("downloadDataType")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "Get data types", method = "GET", summary = "Returns all data types from all models", responses = {
+ @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
+ @ApiResponse(responseCode = "200", description = "allDataTypes"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
+ @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
+ @ApiResponse(responseCode = "404", description = "Data types not found")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public Response downloadDataType(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
+ @Parameter(description = "dataTypeId") @QueryParam("dataTypeId") String dataTypeId) {
+ Wrapper<Response> responseWrapper = new Wrapper<>();
+ Wrapper<User> userWrapper = new Wrapper<>();
+ init();
+ validateUserExist(responseWrapper, userWrapper, userId);
+ Response response;
+ try {
+ String url = request.getMethod() + " " + request.getRequestURI();
+ log.info(START_HANDLE_REQUEST_OF_MODIFIER_ID_IS, url, userId);
+ response = handleDataTypeDownloadRequest(dataTypeId);
+ } catch (Exception e) {
+ BeEcompErrorManager.getInstance().logBeRestApiGeneralError("downloadResourceArtifactBase64");
+ log.debug("downloadResourceArtifactBase64 unexpected exception", e);
+ throw e;
+ }
+ return response;
+ }
+
+ @GET
@Path("interfaceLifecycleTypes")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@@ -422,4 +465,24 @@ public class TypesFetchServlet extends AbstractValidationsServlet {
component -> ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
.getToscaResourceName(), component -> component, (component1, component2) -> component1)));
}
+
+ private Response handleDataTypeDownloadRequest(final String dataTypeId) {
+ Optional<DataTypeDefinition> dataTypeDefinition = dataTypeOperation.handleDataTypeDownloadRequestById(dataTypeId);
+ Either<byte[], ComponentException> toscaExportDataType = toscaExportUtils.exportDataType(dataTypeDefinition.get()).left()
+ .map(toscaRepresentation -> {
+ log.debug("Tosca yaml exported for Datatype {} ", dataTypeDefinition.get().getUniqueId());
+ return toscaRepresentation.getMainYaml();
+ }).right().map(toscaError -> {
+ log.debug("Failed export tosca yaml for DataType {} error {}", dataTypeDefinition.get().getUniqueId(), toscaError);
+ return new ByActionStatusComponentException(componentsUtils.convertFromToscaError(toscaError));
+ });
+ byte[] file = toscaExportDataType.left().value();
+ String base64Contents = new String(Base64.encodeBase64(file));
+ String artifactName = dataTypeDefinition.get().getName() + DATATYPE_FILE_TYPE;
+ ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
+ ArtifactUiDownloadData artifactUiDownloadData = new ArtifactUiDownloadData();
+ artifactUiDownloadData.setArtifactName(artifactName);
+ artifactUiDownloadData.setBase64Contents(base64Contents);
+ return buildOkResponse(responseFormat, artifactUiDownloadData);
+ }
}
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 68795e1003..313bef57aa 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
@@ -226,6 +226,10 @@ public class ToscaExportHandler {
return convertToToscaTemplate(component).left().map(this::createToscaRepresentation);
}
+ public Either<ToscaRepresentation, ToscaError> exportDataType(DataTypeDefinition dataTypeDefinition) {
+ return convertDataTypeToToscaTemplate(dataTypeDefinition).left().map(this::createToscaRepresentation);
+ }
+
public Either<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component, final boolean isAssociatedComponent) {
final List<Map<String, Map<String, String>>> imports = new ArrayList<>(getDefaultToscaImports(component.getModel()));
if (CollectionUtils.isEmpty(imports)) {
@@ -316,6 +320,39 @@ public class ToscaExportHandler {
}
}
+ private Either<ToscaTemplate, ToscaError> convertDataTypeToToscaTemplate(final DataTypeDefinition dataTypeDefinition) {
+ final ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
+ return convertDataTypeTosca(dataTypeDefinition, toscaTemplate);
+ }
+
+ private Either<ToscaTemplate, ToscaError> convertDataTypeTosca(final DataTypeDefinition dataTypeDefinition, final ToscaTemplate toscaTemplate) {
+ final var dataTypesEither = applicationDataTypeCache.getAll(dataTypeDefinition.getModel());
+ if (dataTypesEither.isRight()) {
+ log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
+ return Either.right(ToscaError.GENERAL_ERROR);
+ }
+ Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
+ if (!dataTypeDefinition.isEmpty()) {
+ Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
+ ToscaDataType toscaDataType = new ToscaDataType();
+ toscaDataType.setDerived_from(dataTypeDefinition.getDerivedFromName());
+ toscaDataType.setDescription(dataTypeDefinition.getDescription());
+ toscaDataType.setVersion(dataTypeDefinition.getVersion());
+ if (CollectionUtils.isNotEmpty(dataTypeDefinition.getProperties())) {
+ toscaDataType.setProperties(dataTypeDefinition.getProperties().stream()
+ .collect(Collectors.toMap(
+ PropertyDataDefinition::getName,
+ s -> propertyConvertor.convertProperty(dataTypes, s, PropertyType.PROPERTY),
+ (toscaPropertyTobeValidated, toscaProperty) -> validateToscaProperty((List<DataTypeDefinition>) dataTypeDefinition, toscaPropertyTobeValidated,
+ toscaProperty)
+ )));
+ }
+ toscaDataTypeMap.put(dataTypeDefinition.getName(), toscaDataType);
+ toscaTemplate.setData_types(toscaDataTypeMap);
+ }
+ return Either.left(toscaTemplate);
+ }
+
private List<Map<String, Map<String, String>>> getDefaultToscaImports(final String modelId) {
if (modelId == null) {
return getDefaultToscaImportConfig();
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java
index d7f23f1b21..3de087ff32 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java
@@ -63,9 +63,11 @@ import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.User;
+import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
import org.openecomp.sdc.be.servlets.builder.ServletResponseBuilder;
import org.openecomp.sdc.be.servlets.exception.OperationExceptionMapper;
+import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.ConfigurationSource;
@@ -119,6 +121,10 @@ class TypesFetchServletTest extends JerseyTest {
private ResponseFormatManager responseFormatManager;
@Mock
private ModelOperation modelOperation;
+ @Mock
+ private DataTypeOperation dataTypeOperation;
+ @Mock
+ private ToscaExportHandler toscaExportHandler;
private final Path rootPath = Path.of("/v1/catalog");
private final Path nodeTypesPath = rootPath.resolve("nodeTypes");
@@ -218,6 +224,8 @@ class TypesFetchServletTest extends JerseyTest {
bind(interfaceOperationBusinessLogic).to(InterfaceOperationBusinessLogic.class);
bind(artifactTypeBusinessLogic).to(ArtifactTypeBusinessLogic.class);
bind(modelOperation).to(ModelOperation.class);
+ bind(dataTypeOperation).to(DataTypeOperation.class);
+ bind(toscaExportHandler).to(ToscaExportHandler.class);
}
})
.register(new OperationExceptionMapper(
@@ -264,6 +272,10 @@ class TypesFetchServletTest extends JerseyTest {
.thenReturn(artifactTypeBusinessLogic);
when(webApplicationContext.getBean(ModelOperation.class))
.thenReturn(modelOperation);
+ when(webApplicationContext.getBean(DataTypeOperation.class))
+ .thenReturn(dataTypeOperation);
+ when(webApplicationContext.getBean(ToscaExportHandler.class))
+ .thenReturn(toscaExportHandler);
}
void initConfig() {
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 736fcbcd7f..7d16c906c4 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
@@ -291,6 +291,16 @@ class ToscaExportHandlerTest extends BaseConfDependent {
}
@Test
+ void testExportDataType() {
+ DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
+ dataTypeDefinition.setUniqueId("uniqueId");
+ Either<ToscaRepresentation, ToscaError> result;
+ when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
+ result = testSubject.exportDataType(dataTypeDefinition);
+ assertNotNull(result);
+ }
+
+ @Test
void testConvertInterfaceNodeTypeProperties() {
Resource component = getNewResource();