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.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.jcabi.aspects.Loggable;
27 import fj.data.Either;
28 import io.swagger.v3.oas.annotations.Operation;
29 import io.swagger.v3.oas.annotations.Parameter;
30 import io.swagger.v3.oas.annotations.media.ArraySchema;
31 import io.swagger.v3.oas.annotations.media.Content;
32 import io.swagger.v3.oas.annotations.media.Schema;
33 import io.swagger.v3.oas.annotations.responses.ApiResponse;
34 import io.swagger.v3.oas.annotations.servers.Server;
35 import io.swagger.v3.oas.annotations.tags.Tag;
36 import java.io.IOException;
37 import javax.inject.Inject;
38 import javax.servlet.ServletContext;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.Consumes;
41 import javax.ws.rs.DELETE;
42 import javax.ws.rs.HeaderParam;
43 import javax.ws.rs.POST;
44 import javax.ws.rs.PUT;
45 import javax.ws.rs.Path;
46 import javax.ws.rs.PathParam;
47 import javax.ws.rs.Produces;
48 import javax.ws.rs.core.Context;
49 import javax.ws.rs.core.MediaType;
50 import javax.ws.rs.core.Response;
51 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
53 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
54 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
55 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
56 import org.openecomp.sdc.be.config.BeEcompErrorManager;
57 import org.openecomp.sdc.be.dao.api.ActionStatus;
58 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
59 import org.openecomp.sdc.be.impl.ComponentsUtils;
60 import org.openecomp.sdc.be.impl.ServletUtils;
61 import org.openecomp.sdc.be.model.AttributeDefinition;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.be.user.UserBusinessLogic;
64 import org.openecomp.sdc.common.api.Constants;
65 import org.openecomp.sdc.common.datastructure.Wrapper;
66 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
67 import org.openecomp.sdc.common.log.wrappers.Logger;
68 import org.openecomp.sdc.exception.ResponseFormat;
69 import org.springframework.stereotype.Controller;
72 * Web Servlet for actions on Attributes
76 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
78 @Tag(name = "SDC Internal APIs")
79 @Server(url = "/sdc2/rest")
81 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 - {}";
87 public AttributeServlet(UserBusinessLogic userBusinessLogic,
88 ComponentInstanceBusinessLogic componentInstanceBL,
89 ComponentsUtils componentsUtils, ServletUtils servletUtils,
90 ResourceImportManager resourceImportManager) {
91 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
95 * Creates new Attribute on a resource with given resource ID
104 @Path("resources/{resourceId}/attributes")
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces(MediaType.APPLICATION_JSON)
107 @Operation(description = "Create Resource Attribute", method = "POST",
108 summary = "Returns created resource attribute", responses = {
109 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
110 @ApiResponse(responseCode = "201", description = "Resource property created"),
111 @ApiResponse(responseCode = "403", description = "Restricted operation"),
112 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
113 @ApiResponse(responseCode = "409", description = "Resource attribute already exist")})
114 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
115 public Response createAttribute(
116 @Parameter(description = "resource id to update with new attribute",
117 required = true) @PathParam("resourceId") final String resourceId,
118 @Parameter(description = "Resource attribute to be created", required = true) String data,
119 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
122 ServletContext context = request.getSession().getServletContext();
124 String url = request.getMethod() + " " + request.getRequestURI();
125 log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
128 final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
129 AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
131 if (errorWrapper.isEmpty()) {
132 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
133 Either<AttributeDefinition, ResponseFormat> createAttribute = businessLogic.createAttribute(resourceId, attributeDataDefinition, userId);
134 if (createAttribute.isRight()) {
135 errorWrapper.setInnerElement(createAttribute.right().value());
137 attributeDataDefinition = createAttribute.left().value();
141 if (!errorWrapper.isEmpty()) {
142 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
143 return buildErrorResponse(errorWrapper.getInnerElement());
145 log.debug("Attribute {} created successfully with id {}", attributeDataDefinition.getName(), attributeDataDefinition.getUniqueId());
146 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
147 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
150 } catch (Exception e) {
151 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
152 log.debug("create property failed with exception", e);
158 * Updates existing Attribute with given attributeID on a resource with given resourceID
168 @Path("resources/{resourceId}/attributes/{attributeId}")
169 @Consumes(MediaType.APPLICATION_JSON)
170 @Produces(MediaType.APPLICATION_JSON)
171 @Operation(description = "Update Resource Attribute", method = "PUT", summary = "Returns updated attribute",
172 responses = {@ApiResponse(
173 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
174 @ApiResponse(responseCode = "200", description = "Resource attribute updated"),
175 @ApiResponse(responseCode = "403", description = "Restricted operation"),
176 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
177 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
178 public Response updateAttribute(
179 @Parameter(description = "resource id to update with new attribute",
180 required = true) @PathParam("resourceId") final String resourceId,
181 @Parameter(description = "attribute id to update",
182 required = true) @PathParam("attributeId") final String attributeId,
183 @Parameter(description = "Resource attribute to update", required = true) String data,
184 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
187 ServletContext context = request.getSession().getServletContext();
189 String url = request.getMethod() + " " + request.getRequestURI();
190 log.debug("Start handle request of {}", url);
193 User modifier = new User();
194 modifier.setUserId(userId);
195 log.debug("modifier id is {}", userId);
198 final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
199 AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
200 if (errorWrapper.isEmpty()) {
201 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
202 Either<AttributeDefinition, ResponseFormat> eitherUpdateAttribute
203 = businessLogic.updateAttribute(resourceId, attributeId, attributeDataDefinition, userId);
204 if (eitherUpdateAttribute.isRight()) {
205 errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
207 attributeDataDefinition = eitherUpdateAttribute.left().value();
211 if (!errorWrapper.isEmpty()) {
212 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
213 return buildErrorResponse(errorWrapper.getInnerElement());
215 log.debug("Attribute id {} updated successfully ", attributeDataDefinition.getUniqueId());
216 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
217 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
220 } catch (Exception e) {
221 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
222 log.debug("update attribute failed with exception", e);
228 * Deletes existing Attribute with given attributeID on a resource with given resourceID
237 @Path("resources/{resourceId}/attributes/{attributeId}")
238 @Consumes(MediaType.APPLICATION_JSON)
239 @Produces(MediaType.APPLICATION_JSON)
240 @Operation(description = "Create Resource Attribute", method = "DELETE", summary = "Returns deleted attribute",
241 responses = {@ApiResponse(
242 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
243 @ApiResponse(responseCode = "204", description = "deleted attribute"),
244 @ApiResponse(responseCode = "403", description = "Restricted operation"),
245 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
246 @ApiResponse(responseCode = "404", description = "Resource property not found")})
247 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
248 public Response deleteAttribute(
249 @Parameter(description = "resource id of attribute",
250 required = true) @PathParam("resourceId") final String resourceId,
251 @Parameter(description = "Attribute id to delete",
252 required = true) @PathParam("attributeId") final String attributeId,
253 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
256 ServletContext context = request.getSession().getServletContext();
258 String url = request.getMethod() + " " + request.getRequestURI();
259 log.debug("Start handle request of {}", url);
260 log.debug("modifier id is {}", userId);
263 // delete the property
264 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context,
265 () -> AttributeBusinessLogic.class);
266 Either<AttributeDefinition, ResponseFormat> eitherAttribute = businessLogic
267 .deleteAttribute(resourceId, attributeId, userId);
268 if (eitherAttribute.isRight()) {
269 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
270 return buildErrorResponse(eitherAttribute.right().value());
272 AttributeDefinition attributeDefinition = eitherAttribute.left().value();
273 String name = attributeDefinition.getName();
275 log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
276 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
277 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
279 } catch (Exception e) {
280 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
281 log.debug("delete attribute failed with exception", e);
286 private void buildAttributeFromString(String data, Wrapper<AttributeDataDefinition> attributesWrapper,
287 Wrapper<ResponseFormat> errorWrapper) {
289 Gson gson = new GsonBuilder().setPrettyPrinting().create();
290 final AttributeDataDefinition attribute = gson.fromJson(data, AttributeDataDefinition.class);
291 if (attribute == null) {
292 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);
293 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
294 errorWrapper.setInnerElement(responseFormat);
296 attributesWrapper.setInnerElement(attribute);
299 } catch (Exception e) {
300 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
301 errorWrapper.setInnerElement(responseFormat);
302 log.debug(ATTRIBUTE_CONTENT_IS_INVALID, data, e);
303 log.info(ATTRIBUTE_CONTENT_IS_INVALID, data);
307 private AttributeDefinition convertJsonToObject(final String data,
308 final Wrapper<ResponseFormat> errorWrapper) {
310 final ObjectMapper mapper = new ObjectMapper();
312 return mapper.readValue(data, AttributeDefinition.class);
313 } catch (final IOException e) {
314 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ATTRIBUTE_CONTENT_IS_INVALID, data);
315 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
316 errorWrapper.setInnerElement(responseFormat);