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.annotations.*;
28 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
29 import org.openecomp.sdc.be.config.BeEcompErrorManager;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.datastructure.Wrapper;
35 import org.openecomp.sdc.exception.ResponseFormat;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import javax.inject.Singleton;
40 import javax.servlet.ServletContext;
41 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
47 * Web Servlet for actions on Attributes
52 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
54 @Api(value = "Resource Attribute Servlet", description = "Resource Attribute Servlet")
56 public class AttributeServlet extends AbstractValidationsServlet {
57 private static final Logger log = LoggerFactory.getLogger(AttributeServlet.class);
60 * Creates new Attribute on a resource with given resource ID
69 @Path("resources/{resourceId}/attributes")
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 @ApiOperation(value = "Create Resource Attribute", httpMethod = "POST", notes = "Returns created resource attribute", response = Response.class)
73 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource property created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
74 @ApiResponse(code = 409, message = "Resource attribute already exist") })
75 public Response createAttribute(@ApiParam(value = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId, @ApiParam(value = "Resource attribute to be created", required = true) String data,
76 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
78 ServletContext context = request.getSession().getServletContext();
80 String url = request.getMethod() + " " + request.getRequestURI();
81 log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
84 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
85 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
86 // convert json to AttributeDefinition
88 buildAttributeFromString(data, attributesWrapper, errorWrapper);
89 if (errorWrapper.isEmpty()) {
90 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
91 Either<PropertyDefinition, ResponseFormat> createAttribute = businessLogic.createAttribute(resourceId, attributesWrapper.getInnerElement(), userId);
92 if (createAttribute.isRight()) {
93 errorWrapper.setInnerElement(createAttribute.right().value());
95 attributesWrapper.setInnerElement(createAttribute.left().value());
100 if (!errorWrapper.isEmpty()) {
101 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
102 response = buildErrorResponse(errorWrapper.getInnerElement());
104 PropertyDefinition createdAttDef = attributesWrapper.getInnerElement();
105 log.debug("Attribute {} created successfully with id {}", createdAttDef.getName(), createdAttDef.getUniqueId());
106 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
107 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(createdAttDef));
112 } catch (Exception e) {
113 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
114 log.debug("create property failed with exception", e);
115 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
116 return buildErrorResponse(responseFormat);
122 * Updates existing Attribute with given attributeID on a resource with given resourceID
132 @Path("resources/{resourceId}/attributes/{attributeId}")
133 @Consumes(MediaType.APPLICATION_JSON)
134 @Produces(MediaType.APPLICATION_JSON)
135 @ApiOperation(value = "Update Resource Attribute", httpMethod = "PUT", notes = "Returns updated attribute", response = Response.class)
136 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource attribute updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
137 public Response updateAttribute(@ApiParam(value = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId,
138 @ApiParam(value = "attribute id to update", required = true) @PathParam("attributeId") final String attributeId, @ApiParam(value = "Resource attribute to update", required = true) String data, @Context final HttpServletRequest request,
139 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
141 ServletContext context = request.getSession().getServletContext();
143 String url = request.getMethod() + " " + request.getRequestURI();
144 log.debug("Start handle request of {}", url);
147 User modifier = new User();
148 modifier.setUserId(userId);
149 log.debug("modifier id is {}", userId);
152 // convert json to PropertyDefinition
153 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
154 Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
155 // convert json to AttributeDefinition
157 buildAttributeFromString(data, attributesWrapper, errorWrapper);
159 if (errorWrapper.isEmpty()) {
160 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
161 Either<PropertyDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic.updateAttribute(resourceId, attributeId, attributesWrapper.getInnerElement(), userId);
163 if (eitherUpdateAttribute.isRight()) {
164 errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
166 attributesWrapper.setInnerElement(eitherUpdateAttribute.left().value());
171 if (!errorWrapper.isEmpty()) {
172 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
173 response = buildErrorResponse(errorWrapper.getInnerElement());
175 PropertyDefinition updatedAttribute = attributesWrapper.getInnerElement();
176 log.debug("Attribute id {} updated successfully ", updatedAttribute.getUniqueId());
177 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
178 response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(updatedAttribute));
183 } catch (Exception e) {
184 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
185 log.debug("update attribute failed with exception", e);
186 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
187 return buildErrorResponse(responseFormat);
193 * Deletes existing Attribute with given attributeID on a resource with given resourceID
202 @Path("resources/{resourceId}/attributes/{attributeId}")
203 @Consumes(MediaType.APPLICATION_JSON)
204 @Produces(MediaType.APPLICATION_JSON)
205 @ApiOperation(value = "Create Resource Attribute", httpMethod = "DELETE", notes = "Returns deleted attribute", response = Response.class)
206 @ApiResponses(value = { @ApiResponse(code = 204, message = "deleted attribute"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
207 @ApiResponse(code = 404, message = "Resource property not found") })
208 public Response deleteAttribute(@ApiParam(value = "resource id of attribute", required = true) @PathParam("resourceId") final String resourceId,
209 @ApiParam(value = "Attribute id to delete", required = true) @PathParam("attributeId") final String attributeId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
211 ServletContext context = request.getSession().getServletContext();
213 String url = request.getMethod() + " " + request.getRequestURI();
214 log.debug("Start handle request of {}", url);
215 log.debug("modifier id is {}", userId);
219 // delete the property
220 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
221 Either<PropertyDefinition, ResponseFormat> eitherAttribute = businessLogic.deleteAttribute(resourceId, attributeId, userId);
222 if (eitherAttribute.isRight()) {
223 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
224 return buildErrorResponse(eitherAttribute.right().value());
226 PropertyDefinition attributeDefinition = eitherAttribute.left().value();
227 String name = attributeDefinition.getName();
229 log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
230 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
231 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
233 } catch (Exception e) {
234 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
235 log.debug("delete attribute failed with exception", e);
236 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
237 return buildErrorResponse(responseFormat);
242 private void buildAttributeFromString(String data, Wrapper<PropertyDefinition> attributesWrapper, Wrapper<ResponseFormat> errorWrapper) {
245 Gson gson = new GsonBuilder().setPrettyPrinting().create();
246 final PropertyDefinition attribute = gson.fromJson(data, PropertyDefinition.class);
247 if (attribute == null) {
248 log.info("Attribute content is invalid - {}", data);
249 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
250 errorWrapper.setInnerElement(responseFormat);
252 attributesWrapper.setInnerElement(attribute);
255 } catch (Exception e) {
256 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
257 errorWrapper.setInnerElement(responseFormat);
258 log.debug("Attribute content is invalid - {}", data, e);
259 log.info("Attribute content is invalid - {}", data);