d6efda4b90ec936c0bdf606dc24c749313392320
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ComponentAttributeServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021, 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 package org.openecomp.sdc.be.servlets;
21
22 import com.jcabi.aspects.Loggable;
23 import fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import io.swagger.v3.oas.annotations.tags.Tags;
33 import java.util.List;
34 import javax.inject.Inject;
35 import javax.inject.Singleton;
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.Path;
41 import javax.ws.rs.PathParam;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
48 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
49 import org.openecomp.sdc.be.config.BeEcompErrorManager;
50 import org.openecomp.sdc.be.dao.api.ActionStatus;
51 import org.openecomp.sdc.be.impl.ComponentsUtils;
52 import org.openecomp.sdc.be.model.AttributeDefinition;
53 import org.openecomp.sdc.common.api.Constants;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
59 @Path("/v1/catalog")
60 @Tags({@Tag(name = "SDCE-2 APIs")})
61 @Server(url = "/sdc2/rest")
62 @Singleton
63 public class ComponentAttributeServlet extends BeGenericServlet {
64
65     private static final Logger log = LoggerFactory.getLogger(ComponentAttributeServlet.class);
66     private static final String CREATE_ATTRIBUTE = "Create Attribute";
67     private static final String DEBUG_MESSAGE = "Start handle request of {} modifier id is {}";
68     private final AttributeBusinessLogic attributeBusinessLogic;
69
70     @Inject
71     public ComponentAttributeServlet(final ComponentsUtils componentsUtils,
72                                      final AttributeBusinessLogic attributeBusinessLogic) {
73         super(componentsUtils);
74         this.attributeBusinessLogic = attributeBusinessLogic;
75     }
76
77     @GET
78     @Path("services/{serviceId}/attributes")
79     @Consumes(MediaType.APPLICATION_JSON)
80     @Produces(MediaType.APPLICATION_JSON)
81     @Operation(description = "Get Service Attribute", method = "GET", summary = "Returns attribute list of service", responses = {
82         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
83         @ApiResponse(responseCode = "200", description = "attribute"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
84         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
85         @ApiResponse(responseCode = "404", description = "Service attribute not found")})
86     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
87     public Response getAttributeListInService(
88         @Parameter(description = "service id of attribute", required = true) @PathParam("serviceId") final String serviceId,
89         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
90         return getAttributeList(serviceId, request, userId);
91     }
92
93     @GET
94     @Path("resources/{resourceId}/attributes")
95     @Consumes(MediaType.APPLICATION_JSON)
96     @Produces(MediaType.APPLICATION_JSON)
97     @Operation(description = "Get Resource Attribute", method = "GET", summary = "Returns attribute list of resource", responses = {
98         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
99         @ApiResponse(responseCode = "200", description = "attribute"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
100         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
101         @ApiResponse(responseCode = "404", description = "Resource attribute not found")})
102     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
103     public Response getAttributeListInResource(
104         @Parameter(description = "resource id of attribute", required = true) @PathParam("resourceId") final String resourceId,
105         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
106         return getAttributeList(resourceId, request, userId);
107     }
108
109     private Response getAttributeList(final String componentId, final HttpServletRequest request, final String userId) {
110         final String url = request.getMethod() + " " + request.getRequestURI();
111         log.debug(DEBUG_MESSAGE, url, userId);
112         try {
113             final Either<List<AttributeDefinition>, ResponseFormat> attributesList = attributeBusinessLogic.getAttributesList(componentId, userId);
114             if (attributesList.isRight()) {
115                 return buildErrorResponse(attributesList.right().value());
116             }
117             return buildOkResponse(attributesList.left().value());
118         } catch (final Exception e) {
119             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE_ATTRIBUTE);
120             log.debug("get attribute failed with exception", e);
121             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
122         }
123     }
124 }