2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.fasterxml.jackson.databind.ObjectMapper;
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 io.swagger.v3.oas.annotations.tags.Tags;
34 import java.io.IOException;
35 import javax.inject.Inject;
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.Consumes;
39 import javax.ws.rs.DELETE;
40 import javax.ws.rs.HeaderParam;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.PUT;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
52 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
53 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
54 import org.openecomp.sdc.be.config.BeEcompErrorManager;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.impl.ComponentsUtils;
57 import org.openecomp.sdc.be.impl.ServletUtils;
58 import org.openecomp.sdc.be.model.AttributeDefinition;
59 import org.openecomp.sdc.be.model.User;
60 import org.openecomp.sdc.common.api.Constants;
61 import org.openecomp.sdc.common.datastructure.Wrapper;
62 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
63 import org.openecomp.sdc.common.log.wrappers.Logger;
64 import org.openecomp.sdc.exception.ResponseFormat;
65 import org.springframework.stereotype.Controller;
68 * Web Servlet for actions on Attributes
72 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
74 @Tags({@Tag(name = "SDCE-2 APIs")})
75 @Server(url = "/sdc2/rest")
77 public class AttributeServlet extends AbstractValidationsServlet {
79 private static final Logger log = Logger.getLogger(AttributeServlet.class);
80 private static final String ATTRIBUTE_CONTENT_IS_INVALID = "Attribute content is invalid - {}";
83 public AttributeServlet(ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
84 ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
85 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
89 * Creates new Attribute on a resource with given resource ID
98 @Path("resources/{resourceId}/attributes")
99 @Consumes(MediaType.APPLICATION_JSON)
100 @Produces(MediaType.APPLICATION_JSON)
101 @Operation(description = "Create Resource Attribute", method = "POST", summary = "Returns created resource attribute", responses = {
102 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
103 @ApiResponse(responseCode = "201", description = "Resource property created"),
104 @ApiResponse(responseCode = "403", description = "Restricted operation"),
105 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
106 @ApiResponse(responseCode = "409", description = "Resource attribute already exist")})
107 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
108 public Response createAttribute(
109 @Parameter(description = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId,
110 @Parameter(description = "Resource attribute to be created", required = true) String data, @Context final HttpServletRequest request,
111 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
112 ServletContext context = request.getSession().getServletContext();
113 String url = request.getMethod() + " " + request.getRequestURI();
114 log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
116 final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
117 AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
118 if (attributeDataDefinition == null) {
119 log.error("Unexpected null value for `attributeDataDefinition`");
120 return buildErrorResponse(errorWrapper.getInnerElement());
122 if (errorWrapper.isEmpty()) {
123 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
124 Either<AttributeDefinition, ResponseFormat> createAttribute = businessLogic
125 .createAttribute(resourceId, attributeDataDefinition, userId);
126 if (createAttribute.isRight()) {
127 errorWrapper.setInnerElement(createAttribute.right().value());
129 attributeDataDefinition = createAttribute.left().value();
132 if (!errorWrapper.isEmpty()) {
133 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
134 return buildErrorResponse(errorWrapper.getInnerElement());
136 log.debug("Attribute {} created successfully with id {}", attributeDataDefinition.getName(), attributeDataDefinition.getUniqueId());
137 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
138 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
140 } catch (Exception e) {
141 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
142 log.debug("create property failed with exception", e);
148 * Updates existing Attribute with given attributeID on a resource with given resourceID
158 @Path("resources/{resourceId}/attributes/{attributeId}")
159 @Consumes(MediaType.APPLICATION_JSON)
160 @Produces(MediaType.APPLICATION_JSON)
161 @Operation(description = "Update Resource Attribute", method = "PUT", summary = "Returns updated attribute", responses = {
162 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
163 @ApiResponse(responseCode = "200", description = "Resource attribute updated"),
164 @ApiResponse(responseCode = "403", description = "Restricted operation"),
165 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
166 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
167 public Response updateAttribute(
168 @Parameter(description = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId,
169 @Parameter(description = "attribute id to update", required = true) @PathParam("attributeId") final String attributeId,
170 @Parameter(description = "Resource attribute to update", required = true) String data, @Context final HttpServletRequest request,
171 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
172 ServletContext context = request.getSession().getServletContext();
173 String url = request.getMethod() + " " + request.getRequestURI();
174 log.debug("Start handle request of {}", url);
176 User modifier = new User();
177 modifier.setUserId(userId);
178 log.debug("modifier id is {}", userId);
180 final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
181 AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
182 if (attributeDataDefinition == null) {
183 log.error("Unexpected null value for `attributeDataDefinition`");
184 return buildErrorResponse(errorWrapper.getInnerElement());
186 if (errorWrapper.isEmpty()) {
187 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
188 Either<AttributeDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic
189 .updateAttribute(resourceId, attributeId, attributeDataDefinition, userId);
190 if (eitherUpdateAttribute.isRight()) {
191 errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
193 attributeDataDefinition = eitherUpdateAttribute.left().value();
196 if (!errorWrapper.isEmpty()) {
197 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
198 return buildErrorResponse(errorWrapper.getInnerElement());
200 log.debug("Attribute id {} updated successfully ", attributeDataDefinition.getUniqueId());
201 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
202 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
204 } catch (Exception e) {
205 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
206 log.debug("update attribute failed with exception", e);
212 * Deletes existing Attribute with given attributeID on a resource with given resourceID
221 @Path("resources/{resourceId}/attributes/{attributeId}")
222 @Consumes(MediaType.APPLICATION_JSON)
223 @Produces(MediaType.APPLICATION_JSON)
224 @Operation(description = "Create Resource Attribute", method = "DELETE", summary = "Returns deleted attribute", responses = {
225 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
226 @ApiResponse(responseCode = "204", description = "deleted attribute"),
227 @ApiResponse(responseCode = "403", description = "Restricted operation"),
228 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
229 @ApiResponse(responseCode = "404", description = "Resource property not found")})
230 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
231 public Response deleteAttribute(
232 @Parameter(description = "resource id of attribute", required = true) @PathParam("resourceId") final String resourceId,
233 @Parameter(description = "Attribute id to delete", required = true) @PathParam("attributeId") final String attributeId,
234 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
235 ServletContext context = request.getSession().getServletContext();
236 String url = request.getMethod() + " " + request.getRequestURI();
237 log.debug("Start handle request of {}", url);
238 log.debug("modifier id is {}", userId);
240 // delete the property
241 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
242 Either<AttributeDefinition, ResponseFormat> eitherAttribute = businessLogic.deleteAttribute(resourceId, attributeId, userId);
243 if (eitherAttribute.isRight()) {
244 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
245 return buildErrorResponse(eitherAttribute.right().value());
247 AttributeDefinition attributeDefinition = eitherAttribute.left().value();
248 String name = attributeDefinition.getName();
249 log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
250 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
251 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
252 } catch (Exception e) {
253 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
254 log.debug("delete attribute failed with exception", e);
259 private AttributeDefinition convertJsonToObject(final String data, final Wrapper<ResponseFormat> errorWrapper) {
260 final ObjectMapper mapper = new ObjectMapper();
262 return mapper.readValue(data, AttributeDefinition.class);
263 } catch (final IOException e) {
264 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ATTRIBUTE_CONTENT_IS_INVALID, data);
265 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
266 errorWrapper.setInnerElement(responseFormat);