Remove legacy certificate handling
[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 package org.openecomp.sdc.be.servlets;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import java.io.IOException;
35 import javax.inject.Inject;
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.Consumes;
39 import javax.ws.rs.DELETE;
40 import javax.ws.rs.HeaderParam;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.PUT;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
52 import org.openecomp.sdc.be.config.BeEcompErrorManager;
53 import org.openecomp.sdc.be.dao.api.ActionStatus;
54 import org.openecomp.sdc.be.impl.ComponentsUtils;
55 import org.openecomp.sdc.be.impl.ServletUtils;
56 import org.openecomp.sdc.be.model.AttributeDefinition;
57 import org.openecomp.sdc.be.model.User;
58 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.datastructure.Wrapper;
60 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
61 import org.openecomp.sdc.common.log.wrappers.Logger;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.springframework.stereotype.Controller;
64
65 /**
66  * Web Servlet for actions on Attributes
67  *
68  * @author mshitrit
69  */
70 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Path("/v1/catalog")
72 @Tags({@Tag(name = "SDCE-2 APIs")})
73 @Server(url = "/sdc2/rest")
74 @Controller
75 public class AttributeServlet extends AbstractValidationsServlet {
76
77     private static final Logger log = Logger.getLogger(AttributeServlet.class);
78     private static final String ATTRIBUTE_CONTENT_IS_INVALID = "Attribute content is invalid - {}";
79
80     @Inject
81     public AttributeServlet(ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
82                             ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
83         super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
84     }
85
86     /**
87      * Creates new Attribute on a resource with given resource ID
88      *
89      * @param resourceId
90      * @param data
91      * @param request
92      * @param userId
93      * @return
94      */
95     @POST
96     @Path("resources/{resourceId}/attributes")
97     @Consumes(MediaType.APPLICATION_JSON)
98     @Produces(MediaType.APPLICATION_JSON)
99     @Operation(description = "Create Resource Attribute", method = "POST", summary = "Returns created resource attribute", responses = {
100         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
101         @ApiResponse(responseCode = "201", description = "Resource property created"),
102         @ApiResponse(responseCode = "403", description = "Restricted operation"),
103         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
104         @ApiResponse(responseCode = "409", description = "Resource attribute already exist")})
105     public Response createAttribute(
106         @Parameter(description = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId,
107         @Parameter(description = "Resource attribute to be created", required = true) String data, @Context final HttpServletRequest request,
108         @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
109         ServletContext context = request.getSession().getServletContext();
110         String url = request.getMethod() + " " + request.getRequestURI();
111         log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
112         try {
113             final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
114             AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
115             if (attributeDataDefinition == null) {
116                 log.error("Unexpected null value for `attributeDataDefinition`");
117                 return buildErrorResponse(errorWrapper.getInnerElement());
118             }
119             if (errorWrapper.isEmpty()) {
120                 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
121                 Either<AttributeDefinition, ResponseFormat> createAttribute = businessLogic
122                     .createAttribute(resourceId, attributeDataDefinition, userId);
123                 if (createAttribute.isRight()) {
124                     errorWrapper.setInnerElement(createAttribute.right().value());
125                 } else {
126                     attributeDataDefinition = createAttribute.left().value();
127                 }
128             }
129             if (!errorWrapper.isEmpty()) {
130                 log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement());
131                 return buildErrorResponse(errorWrapper.getInnerElement());
132             } else {
133                 log.debug("Attribute {} created successfully with id {}", attributeDataDefinition.getName(), attributeDataDefinition.getUniqueId());
134                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
135                 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
136             }
137         } catch (Exception e) {
138             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Attribute");
139             log.debug("create property failed with exception", e);
140             throw e;
141         }
142     }
143
144     /**
145      * Updates existing Attribute with given attributeID on a resource with given resourceID
146      *
147      * @param resourceId
148      * @param attributeId
149      * @param data
150      * @param request
151      * @param userId
152      * @return
153      */
154     @PUT
155     @Path("resources/{resourceId}/attributes/{attributeId}")
156     @Consumes(MediaType.APPLICATION_JSON)
157     @Produces(MediaType.APPLICATION_JSON)
158     @Operation(description = "Update Resource Attribute", method = "PUT", summary = "Returns updated attribute", responses = {
159         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
160         @ApiResponse(responseCode = "200", description = "Resource attribute updated"),
161         @ApiResponse(responseCode = "403", description = "Restricted operation"),
162         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
163     public Response updateAttribute(
164         @Parameter(description = "resource id to update with new attribute", required = true) @PathParam("resourceId") final String resourceId,
165         @Parameter(description = "attribute id to update", required = true) @PathParam("attributeId") final String attributeId,
166         @Parameter(description = "Resource attribute to update", required = true) String data, @Context final HttpServletRequest request,
167         @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
168         ServletContext context = request.getSession().getServletContext();
169         String url = request.getMethod() + " " + request.getRequestURI();
170         log.debug("Start handle request of {}", url);
171         // get modifier id
172         User modifier = new User();
173         modifier.setUserId(userId);
174         log.debug("modifier id is {}", userId);
175         try {
176             final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
177             AttributeDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper);
178             if (attributeDataDefinition == null) {
179                 log.error("Unexpected null value for `attributeDataDefinition`");
180                 return buildErrorResponse(errorWrapper.getInnerElement());
181             }
182             if (errorWrapper.isEmpty()) {
183                 AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
184                 Either<AttributeDefinition, ResponseFormat> eitherUpdateAttribute = businessLogic
185                     .updateAttribute(resourceId, attributeId, attributeDataDefinition, userId);
186                 if (eitherUpdateAttribute.isRight()) {
187                     errorWrapper.setInnerElement(eitherUpdateAttribute.right().value());
188                 } else {
189                     attributeDataDefinition = eitherUpdateAttribute.left().value();
190                 }
191             }
192             if (!errorWrapper.isEmpty()) {
193                 log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement());
194                 return buildErrorResponse(errorWrapper.getInnerElement());
195             } else {
196                 log.debug("Attribute id {} updated successfully ", attributeDataDefinition.getUniqueId());
197                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
198                 return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition));
199             }
200         } catch (Exception e) {
201             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Attribute");
202             log.debug("update attribute failed with exception", e);
203             throw e;
204         }
205     }
206
207     /**
208      * Deletes existing Attribute with given attributeID on a resource with given resourceID
209      *
210      * @param resourceId
211      * @param attributeId
212      * @param request
213      * @param userId
214      * @return
215      */
216     @DELETE
217     @Path("resources/{resourceId}/attributes/{attributeId}")
218     @Consumes(MediaType.APPLICATION_JSON)
219     @Produces(MediaType.APPLICATION_JSON)
220     @Operation(description = "Create Resource Attribute", method = "DELETE", summary = "Returns deleted attribute", responses = {
221         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
222         @ApiResponse(responseCode = "204", description = "deleted attribute"),
223         @ApiResponse(responseCode = "403", description = "Restricted operation"),
224         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
225         @ApiResponse(responseCode = "404", description = "Resource property not found")})
226     public Response deleteAttribute(
227         @Parameter(description = "resource id of attribute", required = true) @PathParam("resourceId") final String resourceId,
228         @Parameter(description = "Attribute id to delete", required = true) @PathParam("attributeId") final String attributeId,
229         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
230         ServletContext context = request.getSession().getServletContext();
231         String url = request.getMethod() + " " + request.getRequestURI();
232         log.debug("Start handle request of {}", url);
233         log.debug("modifier id is {}", userId);
234         try {
235             // delete the property
236             AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class);
237             Either<AttributeDefinition, ResponseFormat> eitherAttribute = businessLogic.deleteAttribute(resourceId, attributeId, userId);
238             if (eitherAttribute.isRight()) {
239                 log.debug("Failed to delete Attribute. Reason - ", eitherAttribute.right().value());
240                 return buildErrorResponse(eitherAttribute.right().value());
241             }
242             AttributeDefinition attributeDefinition = eitherAttribute.left().value();
243             String name = attributeDefinition.getName();
244             log.debug("Attribute {} deleted successfully with id {}", name, attributeDefinition.getUniqueId());
245             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
246             return buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDefinition));
247         } catch (Exception e) {
248             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Attribute");
249             log.debug("delete attribute failed with exception", e);
250             throw e;
251         }
252     }
253
254     private AttributeDefinition convertJsonToObject(final String data, final Wrapper<ResponseFormat> errorWrapper) {
255         final ObjectMapper mapper = new ObjectMapper();
256         try {
257             return mapper.readValue(data, AttributeDefinition.class);
258         } catch (final IOException e) {
259             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ATTRIBUTE_CONTENT_IS_INVALID, data);
260             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
261             errorWrapper.setInnerElement(responseFormat);
262             return null;
263         }
264     }
265 }