Remove legacy certificate handling
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / DataTypeServlet.java
index 711ebcb..9ca7255 100644 (file)
@@ -35,9 +35,11 @@ import java.util.List;
 import java.util.Optional;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -45,8 +47,8 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
-import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
-import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
+import org.apache.commons.lang3.StringUtils;
+import org.openecomp.sdc.be.components.impl.DataTypeBusinessLogic;
 import org.openecomp.sdc.be.config.BeEcompErrorManager;
 import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
@@ -55,6 +57,7 @@ import org.openecomp.sdc.be.impl.ComponentsUtils;
 import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
+import org.openecomp.sdc.be.model.normatives.ElementTypeEnum;
 import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
 import org.openecomp.sdc.common.api.Constants;
 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
@@ -70,11 +73,13 @@ public class DataTypeServlet extends BeGenericServlet {
 
     private static final Logger log = Logger.getLogger(DataTypeServlet.class);
     private final DataTypeOperation dataTypeOperation;
+    private final DataTypeBusinessLogic dataTypeBusinessLogic;
 
     public DataTypeServlet(final ComponentsUtils componentsUtils,
-                           final DataTypeOperation dataTypeOperation) {
+                           final DataTypeOperation dataTypeOperation, DataTypeBusinessLogic dataTypeBusinessLogic) {
         super(componentsUtils);
         this.dataTypeOperation = dataTypeOperation;
+        this.dataTypeBusinessLogic = dataTypeBusinessLogic;
     }
 
     @GET
@@ -87,7 +92,6 @@ public class DataTypeServlet extends BeGenericServlet {
         @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 fetchDataType(@Context final HttpServletRequest request,
                                   @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
                                   @PathParam("dataTypeUid") String dataTypeUid) {
@@ -117,7 +121,6 @@ public class DataTypeServlet extends BeGenericServlet {
         @ApiResponse(responseCode = "200", description = "Data type found, properties may be empty"),
         @ApiResponse(responseCode = "403", description = "Restricted operation"),
         @ApiResponse(responseCode = "404", description = "Data type not found")})
-    @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
     public Response fetchProperties(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
                                     @PathParam("id") final String id) {
         final List<PropertyDefinition> allProperties = dataTypeOperation.findAllProperties(id);
@@ -137,12 +140,144 @@ public class DataTypeServlet extends BeGenericServlet {
             @ApiResponse(responseCode = "403", description = "Restricted operation"),
             @ApiResponse(responseCode = "404", description = "Data type not found")
         })
-    @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
     public Response createProperty(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
-                                       @PathParam("id") final String id,
+                                   @PathParam("id") final String id,
                                    @RequestBody(description = "Property to add", required = true) final PropertyDefinitionDto propertyDefinitionDto) {
+        Optional<DataTypeDataDefinition> dataTypeOptional = dataTypeOperation.getDataTypeByUid(id);
+        dataTypeOptional.orElseThrow(() -> {
+            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, String.format("Failed to find data type '%s'", id));
+        });
+        DataTypeDataDefinition dataType = dataTypeOptional.get();
+        String model = dataType.getModel();
+        Optional<DataTypeDataDefinition> propertyDataType = dataTypeOperation.getDataTypeByNameAndModel(propertyDefinitionDto.getType(), model);
+        if (propertyDataType.isEmpty()) {
+            if (StringUtils.isEmpty(model)) {
+                model = Constants.DEFAULT_MODEL_NAME;
+            }
+            throw new OperationException(ActionStatus.INVALID_MODEL,
+                String.format("Property model is not the same as the data type model. Must be '%s'", model));
+        }
+        if (StringUtils.isEmpty(dataType.getModel())) {
+            dataType.setModel(Constants.DEFAULT_MODEL_NAME);
+        }
         final PropertyDefinitionDto property = dataTypeOperation.createProperty(id, propertyDefinitionDto);
+        dataTypeOperation.updatePropertyInAdditionalTypeDataType(dataType, property, true);
+        dataTypeBusinessLogic.updateApplicationDataTypeCache(id);
         return Response.status(Status.CREATED).entity(property).build();
     }
 
+    @PUT
+    @Path("{id}/properties")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    @Operation(summary = "Update a property in the given data type", method = "POST", description = "Update a property in the given data type",
+        responses = {
+            @ApiResponse(content = @Content(schema = @Schema(implementation = PropertyDefinitionDto.class))),
+            @ApiResponse(responseCode = "201", description = "Property updated in the data type"),
+            @ApiResponse(responseCode = "400", description = "Invalid payload"),
+            @ApiResponse(responseCode = "403", description = "Restricted operation"),
+            @ApiResponse(responseCode = "404", description = "Data type not found")
+        })
+    public Response updateProperty(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
+                                   @PathParam("id") final String id,
+                                   @RequestBody(description = "Property to update", required = true)
+                                   final PropertyDefinitionDto propertyDefinitionDto) {
+        Optional<DataTypeDataDefinition> dataTypeOptional = dataTypeOperation.getDataTypeByUid(id);
+        dataTypeOptional.orElseThrow(() -> {
+            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, String.format("Failed to find data type '%s'", id));
+        });
+        DataTypeDataDefinition dataType = dataTypeOptional.get();
+        String model = dataType.getModel();
+        Optional<DataTypeDataDefinition> propertyDataType = dataTypeOperation.getDataTypeByNameAndModel(propertyDefinitionDto.getType(), model);
+        if (propertyDataType.isEmpty()) {
+            if (StringUtils.isEmpty(model)) {
+                model = Constants.DEFAULT_MODEL_NAME;
+            }
+            throw new OperationException(ActionStatus.INVALID_MODEL,
+                String.format("Property model is not the same as the data type model. Must be '%s'", model));
+        }
+        if (StringUtils.isEmpty(dataType.getModel())) {
+            dataType.setModel(Constants.DEFAULT_MODEL_NAME);
+        }
+        final PropertyDefinitionDto property = dataTypeOperation.updateProperty(id, propertyDefinitionDto);
+        dataTypeOperation.updatePropertyInAdditionalTypeDataType(dataType, property, true);
+        dataTypeBusinessLogic.updateApplicationDataTypeCache(id);
+        return Response.status(Status.CREATED).entity(property).build();
+    }
+
+    @GET
+    @Path("{dataTypeName}/models")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    @Operation(description = "Get models for type", method = "GET", summary = "Returns list of models for type", responses = {
+        @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
+        @ApiResponse(responseCode = "200", description = "dataTypeModels"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
+        @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
+        @ApiResponse(responseCode = "403", description = "Restricted operation"),
+        @ApiResponse(responseCode = "404", description = "Data type not found")})
+    public Response getDataTypeModels(@PathParam("dataTypeName") String dataTypeName) {
+        return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
+            gson.toJson(dataTypeOperation.getAllDataTypeModels(dataTypeName)));
+    }
+
+    @DELETE
+    @Path("{dataTypeId}/{propertyId}")
+    public Response deleteProperty(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
+                                   @PathParam("dataTypeId") final String dataTypeId,
+                                   @Parameter(in = ParameterIn.PATH, required = true, description = "The property id to delete")
+                                   @PathParam("propertyId") final String propertyId) {
+        final Optional<DataTypeDataDefinition> dataTypeOptional = dataTypeOperation.getDataTypeByUid(dataTypeId);
+        dataTypeOptional.orElseThrow(() -> {
+            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, String.format("Failed to find data type '%s'", dataTypeId));
+        });
+        final DataTypeDataDefinition dataTypeDataDefinition = dataTypeOptional.get();
+        if (StringUtils.isEmpty(dataTypeDataDefinition.getModel())) {
+            dataTypeDataDefinition.setModel(Constants.DEFAULT_MODEL_NAME);
+        }
+        final PropertyDefinitionDto propertyDefinitionDto;
+        try {
+            propertyDefinitionDto = dataTypeOperation.deleteProperty(dataTypeDataDefinition, propertyId);
+            dataTypeOperation.updatePropertyInAdditionalTypeDataType(dataTypeDataDefinition, propertyDefinitionDto, false);
+        } catch (OperationException e) {
+            final PropertyDefinitionDto dto = new PropertyDefinitionDto();
+            dto.setName(extractNameFromPropertyId(propertyId));
+            dataTypeOperation.updatePropertyInAdditionalTypeDataType(dataTypeDataDefinition, dto, false);
+            throw e;
+        } finally {
+            dataTypeBusinessLogic.updateApplicationDataTypeCache(dataTypeId);
+        }
+        return Response.status(Status.OK).entity(propertyDefinitionDto).build();
+    }
+
+    @DELETE
+    @Path("{dataTypeId}")
+    public Response deleteDatatype(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
+                                   @PathParam("dataTypeId") final String dataTypeId) {
+        final Optional<DataTypeDataDefinition> dataTypeOptional = dataTypeOperation.getDataTypeByUid(dataTypeId);
+        dataTypeOptional.orElseThrow(() -> {
+            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, String.format("Failed to find data type '%s'", dataTypeId));
+        });
+        final DataTypeDataDefinition dataTypeDataDefinition = dataTypeOptional.get();
+        if (dataTypeDataDefinition.isNormative()) {
+            throw new OperationException(ActionStatus.CANNOT_DELETE_SYSTEM_DEPLOYED_RESOURCES, ElementTypeEnum.DATA_TYPE.getToscaEntryName(),
+                dataTypeId);
+        }
+        if (StringUtils.isEmpty(dataTypeDataDefinition.getModel())) {
+            dataTypeDataDefinition.setModel(Constants.DEFAULT_MODEL_NAME);
+        }
+        try {
+            dataTypeOperation.deleteDataTypesByDataTypeId(dataTypeId);
+            dataTypeOperation.removeDataTypeFromAdditionalType(dataTypeDataDefinition);
+        } catch (Exception e) {
+            BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Datatype");
+            log.debug("delete datatype failed with exception ", e);
+            throw e;
+        }
+        return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
+    }
+
+    private String extractNameFromPropertyId(final String propertyId) {
+        final String[] split = propertyId.split("\\.");
+        return split[split.length - 1];
+    }
 }