Implement Attributes/Outputs BE (part 1)
[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
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
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.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.tags.Tag;
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.be.user.UserBusinessLogic;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.exception.ResponseFormat;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
60 @Path("/v1/catalog")
61 @Tag(name = "SDC Internal APIs")
62 @Server(url = "/sdc2/rest")
63 @Singleton
64 public class ComponentAttributeServlet extends BeGenericServlet {
65
66     private final AttributeBusinessLogic attributeBusinessLogic;
67
68     @Inject
69     public ComponentAttributeServlet(final UserBusinessLogic userBusinessLogic,
70                                      final ComponentsUtils componentsUtils,
71                                      final AttributeBusinessLogic attributeBusinessLogic) {
72         super(userBusinessLogic, componentsUtils);
73         this.attributeBusinessLogic = attributeBusinessLogic;
74     }
75
76     private static final Logger log = LoggerFactory.getLogger(ComponentAttributeServlet.class);
77     private static final String CREATE_ATTRIBUTE = "Create Attribute";
78     private static final String DEBUG_MESSAGE = "Start handle request of {} modifier id is {}";
79
80     @GET
81     @Path("services/{serviceId}/attributes")
82     @Consumes(MediaType.APPLICATION_JSON)
83     @Produces(MediaType.APPLICATION_JSON)
84     @Operation(description = "Get Service Attribute", method = "GET", summary = "Returns attribute list of service",
85         responses = {@ApiResponse(
86             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
87             @ApiResponse(responseCode = "200", description = "attribute"),
88             @ApiResponse(responseCode = "403", description = "Restricted operation"),
89             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
90             @ApiResponse(responseCode = "404", description = "Service attribute not found")})
91     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
92     public Response getAttributeListInService(
93         @Parameter(description = "service id of attribute",
94             required = true) @PathParam("serviceId") final String serviceId,
95         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
96
97         return getAttributeList(serviceId, request, userId);
98     }
99
100     @GET
101     @Path("resources/{resourceId}/attributes")
102     @Consumes(MediaType.APPLICATION_JSON)
103     @Produces(MediaType.APPLICATION_JSON)
104     @Operation(description = "Get Resource Attribute", method = "GET", summary = "Returns attribute list of resource",
105         responses = {@ApiResponse(
106             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
107             @ApiResponse(responseCode = "200", description = "attribute"),
108             @ApiResponse(responseCode = "403", description = "Restricted operation"),
109             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
110             @ApiResponse(responseCode = "404", description = "Resource attribute not found")})
111     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
112     public Response getAttributeListInResource(
113         @Parameter(description = "resource id of attribute",
114             required = true) @PathParam("resourceId") final String resourceId,
115         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
116
117         return getAttributeList(resourceId, request, userId);
118     }
119
120     private Response getAttributeList(final String componentId, final HttpServletRequest request, final String userId) {
121
122         final String url = request.getMethod() + " " + request.getRequestURI();
123         log.debug(DEBUG_MESSAGE, url, userId);
124
125         try {
126             final Either<List<AttributeDefinition>, ResponseFormat> attributesList =
127                 attributeBusinessLogic.getAttributesList(componentId, userId);
128
129             if (attributesList.isRight()) {
130                 return buildErrorResponse(attributesList.right().value());
131             }
132
133             return buildOkResponse(attributesList.left().value());
134
135         } catch (final Exception e) {
136             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE_ATTRIBUTE);
137             log.debug("get attribute failed with exception", e);
138             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
139         }
140     }
141
142 }