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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.jcabi.aspects.Loggable;
26 import fj.data.Either;
27 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
28 import io.swagger.v3.oas.annotations.Operation;
29 import io.swagger.v3.oas.annotations.Parameter;
30 import io.swagger.v3.oas.annotations.info.Info;
31 import io.swagger.v3.oas.annotations.media.ArraySchema;
32 import io.swagger.v3.oas.annotations.media.Content;
33 import io.swagger.v3.oas.annotations.media.Schema;
34 import io.swagger.v3.oas.annotations.responses.ApiResponse;
35 import io.swagger.v3.oas.annotations.responses.ApiResponses;
36 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
39 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
40 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
41 import org.openecomp.sdc.be.config.BeEcompErrorManager;
42 import org.openecomp.sdc.be.dao.api.ActionStatus;
43 import org.openecomp.sdc.be.impl.ComponentsUtils;
44 import org.openecomp.sdc.be.impl.ServletUtils;
45 import org.openecomp.sdc.be.model.PropertyDefinition;
46 import org.openecomp.sdc.be.model.User;
47 import org.openecomp.sdc.be.user.UserBusinessLogic;
48 import org.openecomp.sdc.common.api.Constants;
49 import org.openecomp.sdc.common.datastructure.Wrapper;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.openecomp.sdc.exception.ResponseFormat;
52 import org.springframework.stereotype.Controller;
54 import javax.inject.Inject;
55 import javax.servlet.ServletContext;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.ws.rs.Consumes;
58 import javax.ws.rs.DELETE;
59 import javax.ws.rs.HeaderParam;
60 import javax.ws.rs.POST;
61 import javax.ws.rs.PUT;
62 import javax.ws.rs.Path;
63 import javax.ws.rs.PathParam;
64 import javax.ws.rs.Produces;
65 import javax.ws.rs.core.Context;
66 import javax.ws.rs.core.MediaType;
67 import javax.ws.rs.core.Response;
68 import java.io.IOException;
71 * Web Servlet for actions on Attributes
76 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
78 @OpenAPIDefinition(info = @Info(title = "Resource Attribute Servlet", description = "Resource Attribute Servlet"))
80 public class AttributeServlet extends AbstractValidationsServlet {
81 private static final Logger log = Logger.getLogger(AttributeServlet.class);
82 private static final String ATTRIBUTE_CONTENT_IS_INVALID = "Attribute content is invalid - {}";
84 public AttributeServlet(UserBusinessLogic userBusinessLogic,
85 ComponentInstanceBusinessLogic componentInstanceBL,
86 ComponentsUtils componentsUtils, ServletUtils servletUtils,
87 ResourceImportManager resourceImportManager) {
88 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
92 * Creates new Attribute on a resource with given resource ID
101 @Path("resources/{resourceId}/attributes")
102 @Consumes(MediaType.APPLICATION_JSON)
103 @Produces(MediaType.APPLICATION_JSON)
104 @Operation(description = "Create Resource Attribute", method = "POST",
105 summary = "Returns created resource attribute", responses = @ApiResponse(
106 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
107 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Resource property created"),
108 @ApiResponse(responseCode = "403", description = "Restricted operation"),
109 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
110 @ApiResponse(responseCode = "409", description = "Resource attribute already exist")})
111 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
112 public Response createAttribute(
113 @Parameter(description = "resource id to update with new attribute",
114 required = true) @PathParam("resourceId") final String resourceId,
115 @Parameter(description = "Resource attribute to be created", required = true) String data,
116 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
118 ServletContext context = request.getSession().getServletContext();
120 String url = request.getMethod() + " " + request.getRequestURI();
121 log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
124 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
125 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
126 // convert json to AttributeDefinition
128 buildAttributeFromString(data, attributesWrapper, errorWrapper);
129 if (errorWrapper.isEmpty()) {
130 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
131 Either<PropertyDefinition, ResponseFormat> createAttribute = businessLogic.createAttribute(resourceId, attributesWrapper.getInnerElement(), userId);
132 if (createAttribute.isRight()) {
133 errorWrapper.setInnerElement(createAttribute.right().value());
135 attributesWrapper.setInnerElement(createAttribute.left().value());
140 if (!errorWrapper.isEmpty()) {
141 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
142 response = buildErrorResponse(errorWrapper.getInnerElement());
144 PropertyDefinition createdAttDef = attributesWrapper.getInnerElement();
145 log.debug("Attribute {} created successfully with id {}", createdAttDef.getName(), createdAttDef.getUniqueId());
146 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
147 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(createdAttDef));
152 } catch (Exception e) {
153 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
154 log.debug("create property failed with exception", e);
160 * Updates existing Attribute with given attributeID on a resource with given resourceID
170 @Path("resources/{resourceId}/attributes/{attributeId}")
171 @Consumes(MediaType.APPLICATION_JSON)
172 @Produces(MediaType.APPLICATION_JSON)
173 @Operation(description = "Update Resource Attribute", method = "PUT", summary = "Returns updated attribute",
174 responses = @ApiResponse(
175 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
176 @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Resource attribute updated"),
177 @ApiResponse(responseCode = "403", description = "Restricted operation"),
178 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
179 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
180 public Response updateAttribute(
181 @Parameter(description = "resource id to update with new attribute",
182 required = true) @PathParam("resourceId") final String resourceId,
183 @Parameter(description = "attribute id to update",
184 required = true) @PathParam("attributeId") final String attributeId,
185 @Parameter(description = "Resource attribute to update", required = true) String data,
186 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
188 ServletContext context = request.getSession().getServletContext();
190 String url = request.getMethod() + " " + request.getRequestURI();
191 log.debug("Start handle request of {}", url);
194 User modifier = new User();
195 modifier.setUserId(userId);
196 log.debug("modifier id is {}", userId);
199 // convert json to PropertyDefinition
200 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
201 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
202 // convert json to AttributeDefinition
204 buildAttributeFromString(data, attributesWrapper, errorWrapper);
206 if (errorWrapper.isEmpty()) {
207 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
208 Either<PropertyDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic.updateAttribute(resourceId, attributeId, attributesWrapper.getInnerElement(), userId);
210 if (eitherUpdateAttribute.isRight()) {
211 errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
213 attributesWrapper.setInnerElement(eitherUpdateAttribute.left().value());
218 if (!errorWrapper.isEmpty()) {
219 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
220 response = buildErrorResponse(errorWrapper.getInnerElement());
222 PropertyDefinition updatedAttribute = attributesWrapper.getInnerElement();
223 log.debug("Attribute id {} updated successfully ", updatedAttribute.getUniqueId());
224 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
225 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(updatedAttribute));
230 } catch (Exception e) {
231 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
232 log.debug("update attribute failed with exception", e);
238 * Deletes existing Attribute with given attributeID on a resource with given resourceID
247 @Path("resources/{resourceId}/attributes/{attributeId}")
248 @Consumes(MediaType.APPLICATION_JSON)
249 @Produces(MediaType.APPLICATION_JSON)
250 @Operation(description = "Create Resource Attribute", method = "DELETE", summary = "Returns deleted attribute",
251 responses = @ApiResponse(
252 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
253 @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "deleted attribute"),
254 @ApiResponse(responseCode = "403", description = "Restricted operation"),
255 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
256 @ApiResponse(responseCode = "404", description = "Resource property not found")})
257 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
258 public Response deleteAttribute(
259 @Parameter(description = "resource id of attribute",
260 required = true) @PathParam("resourceId") final String resourceId,
261 @Parameter(description = "Attribute id to delete",
262 required = true) @PathParam("attributeId") final String attributeId,
263 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
265 ServletContext context = request.getSession().getServletContext();
267 String url = request.getMethod() + " " + request.getRequestURI();
268 log.debug("Start handle request of {}", url);
269 log.debug("modifier id is {}", userId);
273 // delete the property
274 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
275 Either<PropertyDefinition, ResponseFormat> eitherAttribute = businessLogic.deleteAttribute(resourceId, attributeId, userId);
276 if (eitherAttribute.isRight()) {
277 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
278 return buildErrorResponse(eitherAttribute.right().value());
280 PropertyDefinition attributeDefinition = eitherAttribute.left().value();
281 String name = attributeDefinition.getName();
283 log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
284 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
285 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
287 } catch (Exception e) {
288 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
289 log.debug("delete attribute failed with exception", e);
294 private void buildAttributeFromString(String data, Wrapper<PropertyDefinition> attributesWrapper,
295 Wrapper<ResponseFormat> errorWrapper) {
297 Gson gson = new GsonBuilder().setPrettyPrinting().create();
298 final PropertyDefinition attribute = gson.fromJson(data, PropertyDefinition.class);
299 if (attribute == null) {
300 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);
301 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
302 errorWrapper.setInnerElement(responseFormat);
304 attributesWrapper.setInnerElement(attribute);
307 } catch (Exception e) {
308 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
309 errorWrapper.setInnerElement(responseFormat);
310 log.debug(ATTRIBUTE_CONTENT_IS_INVALID, data, e);
311 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);