Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / AttributeServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
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;
53
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;
69
70 /**
71  * Web Servlet for actions on Attributes
72  * 
73  * @author mshitrit
74  *
75  */
76 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
77 @Path("/v1/catalog")
78 @OpenAPIDefinition(info = @Info(title = "Resource Attribute Servlet", description = "Resource Attribute Servlet"))
79 @Controller
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 - {}";
83     @Inject
84     public AttributeServlet(UserBusinessLogic userBusinessLogic,
85         ComponentInstanceBusinessLogic componentInstanceBL,
86         ComponentsUtils componentsUtils, ServletUtils servletUtils,
87         ResourceImportManager resourceImportManager) {
88         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
89     }
90
91     /**
92      * Creates new Attribute on a resource with given resource ID
93      *
94      * @param resourceId
95      * @param data
96      * @param request
97      * @param userId
98      * @return
99      */
100     @POST
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 {
117
118         ServletContext context = request.getSession().getServletContext();
119
120         String url = request.getMethod() + " " + request.getRequestURI();
121         log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
122
123         try {
124             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
125             Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
126             // convert json to AttributeDefinition
127
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());
134                 } else {
135                     attributesWrapper.setInnerElement(createAttribute.left().value());
136                 }
137             }
138
139             Response response;
140             if (!errorWrapper.isEmpty()) {
141                 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
142                 response = buildErrorResponse(errorWrapper.getInnerElement());
143             } else {
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));
148             }
149
150             return response;
151
152         } catch (Exception e) {
153             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
154             log.debug("create property failed with exception", e);
155             throw e;
156         }
157     }
158
159     /**
160      * Updates existing Attribute with given attributeID on a resource with given resourceID
161      *
162      * @param resourceId
163      * @param attributeId
164      * @param data
165      * @param request
166      * @param userId
167      * @return
168      */
169     @PUT
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 {
187
188         ServletContext context = request.getSession().getServletContext();
189
190         String url = request.getMethod() + " " + request.getRequestURI();
191         log.debug("Start handle request of {}", url);
192
193         // get modifier id
194         User modifier = new User();
195         modifier.setUserId(userId);
196         log.debug("modifier id is {}", userId);
197
198         try {
199             // convert json to PropertyDefinition
200             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
201             Wrapper<PropertyDefinition> attributesWrapper = new Wrapper<>();
202             // convert json to AttributeDefinition
203
204             buildAttributeFromString(data, attributesWrapper, errorWrapper);
205
206             if (errorWrapper.isEmpty()) {
207                 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
208                 Either<PropertyDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic.updateAttribute(resourceId, attributeId, attributesWrapper.getInnerElement(), userId);
209                 // update property
210                 if (eitherUpdateAttribute.isRight()) {
211                     errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
212                 } else {
213                     attributesWrapper.setInnerElement(eitherUpdateAttribute.left().value());
214                 }
215             }
216
217             Response response;
218             if (!errorWrapper.isEmpty()) {
219                 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
220                 response = buildErrorResponse(errorWrapper.getInnerElement());
221             } else {
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));
226             }
227
228             return response;
229
230         } catch (Exception e) {
231             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
232             log.debug("update attribute failed with exception", e);
233             throw e;
234         }
235     }
236
237     /**
238      * Deletes existing Attribute with given attributeID on a resource with given resourceID
239      *
240      * @param resourceId
241      * @param attributeId
242      * @param request
243      * @param userId
244      * @return
245      */
246     @DELETE
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 {
264
265         ServletContext context = request.getSession().getServletContext();
266
267         String url = request.getMethod() + " " + request.getRequestURI();
268         log.debug("Start handle request of {}", url);
269         log.debug("modifier id is {}", userId);
270
271         try {
272
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());
279             }
280             PropertyDefinition attributeDefinition = eitherAttribute.left().value();
281             String name = attributeDefinition.getName();
282
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));
286
287         } catch (Exception e) {
288             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
289             log.debug("delete attribute failed with exception", e);
290             throw e;
291         }
292     }
293
294     private void buildAttributeFromString(String data, Wrapper<PropertyDefinition> attributesWrapper,
295             Wrapper<ResponseFormat> errorWrapper) {
296         try {
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);
303             } else {
304                 attributesWrapper.setInnerElement(attribute);
305             }
306
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);
312         }
313     }
314 }