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.Operation;
28 import io.swagger.v3.oas.annotations.Parameter;
29 import io.swagger.v3.oas.annotations.media.ArraySchema;
30 import io.swagger.v3.oas.annotations.media.Content;
31 import io.swagger.v3.oas.annotations.media.Schema;
32 import io.swagger.v3.oas.annotations.responses.ApiResponse;
33 import io.swagger.v3.oas.annotations.servers.Server;
34 import io.swagger.v3.oas.annotations.servers.Servers;
35 import io.swagger.v3.oas.annotations.tags.Tag;
36 import io.swagger.v3.oas.annotations.tags.Tags;
37 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
40 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
41 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.impl.ServletUtils;
46 import org.openecomp.sdc.be.model.PropertyDefinition;
47 import org.openecomp.sdc.be.model.User;
48 import org.openecomp.sdc.be.user.UserBusinessLogic;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.openecomp.sdc.common.datastructure.Wrapper;
51 import org.openecomp.sdc.common.log.wrappers.Logger;
52 import org.openecomp.sdc.exception.ResponseFormat;
53 import org.springframework.stereotype.Controller;
55 import javax.inject.Inject;
56 import javax.servlet.ServletContext;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.ws.rs.Consumes;
59 import javax.ws.rs.DELETE;
60 import javax.ws.rs.HeaderParam;
61 import javax.ws.rs.POST;
62 import javax.ws.rs.PUT;
63 import javax.ws.rs.Path;
64 import javax.ws.rs.PathParam;
65 import javax.ws.rs.Produces;
66 import javax.ws.rs.core.Context;
67 import javax.ws.rs.core.MediaType;
68 import javax.ws.rs.core.Response;
69 import java.io.IOException;
72 * Web Servlet for actions on Attributes
77 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
79 @Tags({@Tag(name = "SDC Internal APIs")})
80 @Servers({@Server(url = "/sdc2/rest")})
82 public class AttributeServlet extends AbstractValidationsServlet {
83 private static final Logger log = Logger.getLogger(AttributeServlet.class);
84 private static final String ATTRIBUTE_CONTENT_IS_INVALID = "Attribute content is invalid - {}";
86 public AttributeServlet(UserBusinessLogic userBusinessLogic,
87 ComponentInstanceBusinessLogic componentInstanceBL,
88 ComponentsUtils componentsUtils, ServletUtils servletUtils,
89 ResourceImportManager resourceImportManager) {
90 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
94 * Creates new Attribute on a resource with given resource ID
103 @Path("resources/{resourceId}/attributes")
104 @Consumes(MediaType.APPLICATION_JSON)
105 @Produces(MediaType.APPLICATION_JSON)
106 @Operation(description = "Create Resource Attribute", method = "POST",
107 summary = "Returns created resource attribute", responses = {
108 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
109 @ApiResponse(responseCode = "201", description = "Resource property created"),
110 @ApiResponse(responseCode = "403", description = "Restricted operation"),
111 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
112 @ApiResponse(responseCode = "409", description = "Resource attribute already exist")})
113 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
114 public Response createAttribute(
115 @Parameter(description = "resource id to update with new attribute",
116 required = true) @PathParam("resourceId") final String resourceId,
117 @Parameter(description = "Resource attribute to be created", required = true) String data,
118 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
120 ServletContext context = request.getSession().getServletContext();
122 String url = request.getMethod() + " " + request.getRequestURI();
123 log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
126 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
127 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
128 // convert json to AttributeDefinition
130 buildAttributeFromString(data, attributesWrapper, errorWrapper);
131 if (errorWrapper.isEmpty()) {
132 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
133 Either<PropertyDefinition, ResponseFormat> createAttribute = businessLogic.createAttribute(resourceId, attributesWrapper.getInnerElement(), userId);
134 if (createAttribute.isRight()) {
135 errorWrapper.setInnerElement(createAttribute.right().value());
137 attributesWrapper.setInnerElement(createAttribute.left().value());
142 if (!errorWrapper.isEmpty()) {
143 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
144 response = buildErrorResponse(errorWrapper.getInnerElement());
146 PropertyDefinition createdAttDef = attributesWrapper.getInnerElement();
147 log.debug("Attribute {} created successfully with id {}", createdAttDef.getName(), createdAttDef.getUniqueId());
148 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
149 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(createdAttDef));
154 } catch (Exception e) {
155 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
156 log.debug("create property failed with exception", e);
162 * Updates existing Attribute with given attributeID on a resource with given resourceID
172 @Path("resources/{resourceId}/attributes/{attributeId}")
173 @Consumes(MediaType.APPLICATION_JSON)
174 @Produces(MediaType.APPLICATION_JSON)
175 @Operation(description = "Update Resource Attribute", method = "PUT", summary = "Returns updated attribute",
176 responses = {@ApiResponse(
177 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
178 @ApiResponse(responseCode = "200", description = "Resource attribute updated"),
179 @ApiResponse(responseCode = "403", description = "Restricted operation"),
180 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
181 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
182 public Response updateAttribute(
183 @Parameter(description = "resource id to update with new attribute",
184 required = true) @PathParam("resourceId") final String resourceId,
185 @Parameter(description = "attribute id to update",
186 required = true) @PathParam("attributeId") final String attributeId,
187 @Parameter(description = "Resource attribute to update", required = true) String data,
188 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
190 ServletContext context = request.getSession().getServletContext();
192 String url = request.getMethod() + " " + request.getRequestURI();
193 log.debug("Start handle request of {}", url);
196 User modifier = new User();
197 modifier.setUserId(userId);
198 log.debug("modifier id is {}", userId);
201 // convert json to PropertyDefinition
202 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
203 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
204 // convert json to AttributeDefinition
206 buildAttributeFromString(data, attributesWrapper, errorWrapper);
208 if (errorWrapper.isEmpty()) {
209 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
210 Either<PropertyDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic.updateAttribute(resourceId, attributeId, attributesWrapper.getInnerElement(), userId);
212 if (eitherUpdateAttribute.isRight()) {
213 errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
215 attributesWrapper.setInnerElement(eitherUpdateAttribute.left().value());
220 if (!errorWrapper.isEmpty()) {
221 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
222 response = buildErrorResponse(errorWrapper.getInnerElement());
224 PropertyDefinition updatedAttribute = attributesWrapper.getInnerElement();
225 log.debug("Attribute id {} updated successfully ", updatedAttribute.getUniqueId());
226 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
227 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(updatedAttribute));
232 } catch (Exception e) {
233 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
234 log.debug("update attribute failed with exception", e);
240 * Deletes existing Attribute with given attributeID on a resource with given resourceID
249 @Path("resources/{resourceId}/attributes/{attributeId}")
250 @Consumes(MediaType.APPLICATION_JSON)
251 @Produces(MediaType.APPLICATION_JSON)
252 @Operation(description = "Create Resource Attribute", method = "DELETE", summary = "Returns deleted attribute",
253 responses = {@ApiResponse(
254 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
255 @ApiResponse(responseCode = "204", description = "deleted attribute"),
256 @ApiResponse(responseCode = "403", description = "Restricted operation"),
257 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
258 @ApiResponse(responseCode = "404", description = "Resource property not found")})
259 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
260 public Response deleteAttribute(
261 @Parameter(description = "resource id of attribute",
262 required = true) @PathParam("resourceId") final String resourceId,
263 @Parameter(description = "Attribute id to delete",
264 required = true) @PathParam("attributeId") final String attributeId,
265 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
267 ServletContext context = request.getSession().getServletContext();
269 String url = request.getMethod() + " " + request.getRequestURI();
270 log.debug("Start handle request of {}", url);
271 log.debug("modifier id is {}", userId);
275 // delete the property
276 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
277 Either<PropertyDefinition, ResponseFormat> eitherAttribute = businessLogic.deleteAttribute(resourceId, attributeId, userId);
278 if (eitherAttribute.isRight()) {
279 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
280 return buildErrorResponse(eitherAttribute.right().value());
282 PropertyDefinition attributeDefinition = eitherAttribute.left().value();
283 String name = attributeDefinition.getName();
285 log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
286 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
287 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
289 } catch (Exception e) {
290 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
291 log.debug("delete attribute failed with exception", e);
296 private void buildAttributeFromString(String data, Wrapper<PropertyDefinition> attributesWrapper,
297 Wrapper<ResponseFormat> errorWrapper) {
299 Gson gson = new GsonBuilder().setPrettyPrinting().create();
300 final PropertyDefinition attribute = gson.fromJson(data, PropertyDefinition.class);
301 if (attribute == null) {
302 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);
303 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
304 errorWrapper.setInnerElement(responseFormat);
306 attributesWrapper.setInnerElement(attribute);
309 } catch (Exception e) {
310 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
311 errorWrapper.setInnerElement(responseFormat);
312 log.debug(ATTRIBUTE_CONTENT_IS_INVALID, data, e);
313 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);