2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.enums.ParameterIn;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.parameters.RequestBody;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.servers.Server;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import java.util.List;
35 import java.util.Optional;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.HeaderParam;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import javax.ws.rs.core.Response.Status;
48 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
49 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
50 import org.openecomp.sdc.be.config.BeEcompErrorManager;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
53 import org.openecomp.sdc.be.exception.BusinessException;
54 import org.openecomp.sdc.be.impl.ComponentsUtils;
55 import org.openecomp.sdc.be.model.PropertyDefinition;
56 import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
57 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
58 import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
59 import org.openecomp.sdc.common.api.Constants;
60 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
61 import org.openecomp.sdc.common.log.wrappers.Logger;
62 import org.springframework.stereotype.Controller;
64 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
65 @Path("/v1/catalog/data-types")
66 @Tag(name = "SDCE-2 APIs")
67 @Server(url = "/sdc2/rest")
69 public class DataTypeServlet extends BeGenericServlet {
71 private static final Logger log = Logger.getLogger(DataTypeServlet.class);
72 private final DataTypeOperation dataTypeOperation;
74 public DataTypeServlet(final ComponentsUtils componentsUtils,
75 final DataTypeOperation dataTypeOperation) {
76 super(componentsUtils);
77 this.dataTypeOperation = dataTypeOperation;
81 @Path("{dataTypeUid}")
82 @Consumes(MediaType.APPLICATION_JSON)
83 @Produces(MediaType.APPLICATION_JSON)
84 @Operation(description = "Get data types", method = "GET", summary = "Returns data types", responses = {
85 @ApiResponse(content = @Content(schema = @Schema(implementation = DataTypeDataDefinition.class))),
86 @ApiResponse(responseCode = "200", description = "Data type found"),
87 @ApiResponse(responseCode = "403", description = "Restricted operation"),
88 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
89 @ApiResponse(responseCode = "404", description = "Data types not found")})
90 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
91 public Response fetchDataType(@Context final HttpServletRequest request,
92 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
93 @PathParam("dataTypeUid") String dataTypeUid) {
94 final String url = request.getMethod() + " " + request.getRequestURI();
95 log.debug("Start handle request of {} - modifier id is {}", url, userId);
96 final Optional<DataTypeDataDefinition> dataTypeFoundOptional;
98 dataTypeFoundOptional = dataTypeOperation.getDataTypeByUid(dataTypeUid);
99 } catch (final BusinessException e) {
101 } catch (final Exception ex) {
102 final String errorMsg = "Unexpected error while listing the Tosca DataType";
103 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
104 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, this.getClass().getName(), errorMsg, ex);
105 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
107 var dataType = dataTypeFoundOptional.orElseThrow(() -> new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, dataTypeUid));
108 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataType);
112 @Path("{id}/properties")
113 @Consumes(MediaType.APPLICATION_JSON)
114 @Produces(MediaType.APPLICATION_JSON)
115 @Operation(description = "Get a data type properties", method = "GET", summary = "Returns the data type properties", responses = {
116 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = PropertyDefinition.class)))),
117 @ApiResponse(responseCode = "200", description = "Data type found, properties may be empty"),
118 @ApiResponse(responseCode = "403", description = "Restricted operation"),
119 @ApiResponse(responseCode = "404", description = "Data type not found")})
120 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
121 public Response fetchProperties(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
122 @PathParam("id") final String id) {
123 final List<PropertyDefinition> allProperties = dataTypeOperation.findAllProperties(id);
124 return buildOkResponse(allProperties);
128 @Path("{id}/properties")
129 @Consumes(MediaType.APPLICATION_JSON)
130 @Produces(MediaType.APPLICATION_JSON)
131 @Operation(summary = "Create a property in the given data type", method = "POST", description = "Create a property in the given data type",
133 @ApiResponse(content = @Content(schema = @Schema(implementation = PropertyDefinitionDto.class))),
134 @ApiResponse(responseCode = "201", description = "Property created in the data type"),
135 @ApiResponse(responseCode = "400", description = "Invalid payload"),
136 @ApiResponse(responseCode = "409", description = "Property already exists in the data type"),
137 @ApiResponse(responseCode = "403", description = "Restricted operation"),
138 @ApiResponse(responseCode = "404", description = "Data type not found")
140 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
141 public Response createProperty(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
142 @PathParam("id") final String id,
143 @RequestBody(description = "Property to add", required = true) final PropertyDefinitionDto propertyDefinitionDto) {
144 final PropertyDefinitionDto property = dataTypeOperation.createProperty(id, propertyDefinitionDto);
145 return Response.status(Status.CREATED).entity(property).build();