2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.openecomp.sdc.be.servlets;
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.config.BeEcompErrorManager;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.model.AttributeDefinition;
51 import org.openecomp.sdc.common.api.Constants;
52 import org.openecomp.sdc.exception.ResponseFormat;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
58 @Tags({@Tag(name = "SDCE-2 APIs")})
59 @Server(url = "/sdc2/rest")
61 public class ComponentAttributeServlet extends BeGenericServlet {
63 private static final Logger log = LoggerFactory.getLogger(ComponentAttributeServlet.class);
64 private static final String CREATE_ATTRIBUTE = "Create Attribute";
65 private static final String DEBUG_MESSAGE = "Start handle request of {} modifier id is {}";
66 private final AttributeBusinessLogic attributeBusinessLogic;
69 public ComponentAttributeServlet(final ComponentsUtils componentsUtils,
70 final AttributeBusinessLogic attributeBusinessLogic) {
71 super(componentsUtils);
72 this.attributeBusinessLogic = attributeBusinessLogic;
76 @Path("services/{serviceId}/attributes")
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 @Operation(description = "Get Service Attribute", method = "GET", summary = "Returns attribute list of service", responses = {
80 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
81 @ApiResponse(responseCode = "200", description = "attribute"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
82 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
83 @ApiResponse(responseCode = "404", description = "Service attribute not found")})
84 public Response getAttributeListInService(
85 @Parameter(description = "service id of attribute", required = true) @PathParam("serviceId") final String serviceId,
86 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
87 return getAttributeList(serviceId, request, userId);
91 @Path("resources/{resourceId}/attributes")
92 @Consumes(MediaType.APPLICATION_JSON)
93 @Produces(MediaType.APPLICATION_JSON)
94 @Operation(description = "Get Resource Attribute", method = "GET", summary = "Returns attribute list of resource", responses = {
95 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
96 @ApiResponse(responseCode = "200", description = "attribute"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
97 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
98 @ApiResponse(responseCode = "404", description = "Resource attribute not found")})
99 public Response getAttributeListInResource(
100 @Parameter(description = "resource id of attribute", required = true) @PathParam("resourceId") final String resourceId,
101 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
102 return getAttributeList(resourceId, request, userId);
105 private Response getAttributeList(final String componentId, final HttpServletRequest request, final String userId) {
106 final String url = request.getMethod() + " " + request.getRequestURI();
107 log.debug(DEBUG_MESSAGE, url, userId);
109 final Either<List<AttributeDefinition>, ResponseFormat> attributesList = attributeBusinessLogic.getAttributesList(componentId, userId);
110 if (attributesList.isRight()) {
111 return buildErrorResponse(attributesList.right().value());
113 return buildOkResponse(attributesList.left().value());
114 } catch (final Exception e) {
115 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE_ATTRIBUTE);
116 log.debug("get attribute failed with exception", e);
117 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));