Fix response of fetch data type endpoint
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / DataTypeServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.servers.Server;
29 import io.swagger.v3.oas.annotations.tags.Tag;
30 import java.util.Optional;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
42 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
43 import org.openecomp.sdc.be.config.BeEcompErrorManager;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
46 import org.openecomp.sdc.be.exception.BusinessException;
47 import org.openecomp.sdc.be.impl.ComponentsUtils;
48 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
49 import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
50 import org.openecomp.sdc.common.api.Constants;
51 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
52 import org.openecomp.sdc.common.log.wrappers.Logger;
53 import org.springframework.stereotype.Controller;
54
55 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
56 @Path("/v1/catalog/data-types")
57 @Tag(name = "SDCE-2 APIs")
58 @Server(url = "/sdc2/rest")
59 @Controller
60 public class DataTypeServlet extends BeGenericServlet {
61
62     private static final Logger log = Logger.getLogger(DataTypeServlet.class);
63     private final DataTypeOperation dataTypeOperation;
64
65     public DataTypeServlet(final ComponentsUtils componentsUtils,
66                            final DataTypeOperation dataTypeOperation) {
67         super(componentsUtils);
68         this.dataTypeOperation = dataTypeOperation;
69     }
70
71     @GET
72     @Path("{dataTypeUid}")
73     @Consumes(MediaType.APPLICATION_JSON)
74     @Produces(MediaType.APPLICATION_JSON)
75     @Operation(description = "Get data types", method = "GET", summary = "Returns data types", responses = {
76         @ApiResponse(content = @Content(schema = @Schema(implementation = DataTypeDataDefinition.class))),
77         @ApiResponse(responseCode = "200", description = "Data type found"),
78         @ApiResponse(responseCode = "403", description = "Restricted operation"),
79         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
80         @ApiResponse(responseCode = "404", description = "Data types not found")})
81     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
82     public Response fetchDataType(@Context final HttpServletRequest request,
83                                   @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
84                                   @PathParam("dataTypeUid") String dataTypeUid) {
85         final String url = request.getMethod() + " " + request.getRequestURI();
86         log.debug("Start handle request of {} - modifier id is {}", url, userId);
87         final Optional<DataTypeDataDefinition> dataTypeFoundOptional;
88         try {
89             dataTypeFoundOptional = dataTypeOperation.getDataTypeByUid(dataTypeUid);
90         } catch (final BusinessException e) {
91             throw e;
92         } catch (final Exception ex) {
93             final String errorMsg = "Unexpected error while listing the Tosca DataType";
94             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
95             log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, this.getClass().getName(), errorMsg, ex);
96             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
97         }
98         var dataType = dataTypeFoundOptional.orElseThrow(() -> new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, dataTypeUid));
99         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataType);
100     }
101
102 }