Remove legacy certificate handling
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / RequirementServlet.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.be.servlets;
17
18 import com.jcabi.aspects.Loggable;
19 import fj.data.Either;
20 import io.swagger.v3.oas.annotations.Operation;
21 import io.swagger.v3.oas.annotations.Parameter;
22 import io.swagger.v3.oas.annotations.media.ArraySchema;
23 import io.swagger.v3.oas.annotations.media.Content;
24 import io.swagger.v3.oas.annotations.media.Schema;
25 import io.swagger.v3.oas.annotations.responses.ApiResponse;
26 import io.swagger.v3.oas.annotations.servers.Server;
27 import io.swagger.v3.oas.annotations.servers.Servers;
28 import io.swagger.v3.oas.annotations.tags.Tag;
29 import io.swagger.v3.oas.annotations.tags.Tags;
30 import java.util.List;
31 import java.util.Optional;
32 import javax.inject.Inject;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.GET;
37 import javax.ws.rs.HeaderParam;
38 import javax.ws.rs.POST;
39 import javax.ws.rs.PUT;
40 import javax.ws.rs.Path;
41 import javax.ws.rs.PathParam;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.RequirementBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
49 import org.openecomp.sdc.be.config.BeEcompErrorManager;
50 import org.openecomp.sdc.be.dao.api.ActionStatus;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.impl.ServletUtils;
54 import org.openecomp.sdc.be.model.RequirementDefinition;
55 import org.openecomp.sdc.be.model.User;
56 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
57 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
58 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.log.wrappers.Logger;
60 import org.openecomp.sdc.exception.ResponseFormat;
61 import org.springframework.stereotype.Controller;
62
63 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
64 @Path("/v1/catalog")
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
67 @Tags({@Tag(name = "SDCE-2 APIs")})
68 @Servers({@Server(url = "/sdc2/rest")})
69 @Controller
70 public class RequirementServlet extends AbstractValidationsServlet {
71
72     private static final Logger LOGGER = Logger.getLogger(RequirementServlet.class);
73     private final RequirementBusinessLogic requirementBusinessLogic;
74
75     @Inject
76     public RequirementServlet(ComponentInstanceBusinessLogic componentInstanceBL,
77                               ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
78                               RequirementBusinessLogic requirementBusinessLogic) {
79         super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
80         this.requirementBusinessLogic = requirementBusinessLogic;
81     }
82
83     @POST
84     @Consumes(MediaType.APPLICATION_JSON)
85     @Produces(MediaType.APPLICATION_JSON)
86     @Path("/resources/{resourceId}/requirements")
87     @Operation(description = "Create requirements on resource", method = "POST", summary = "Create requirements on resource", responses = {
88         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
89         @ApiResponse(responseCode = "201", description = "Create requirements"),
90         @ApiResponse(responseCode = "403", description = "Restricted operation"),
91         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
92         @ApiResponse(responseCode = "409", description = "requirement already exist")})
93     public Response createRequirementsOnResource(@Parameter(description = "Requirement to create", required = true) String data,
94                                                  @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
95                                                  @Context final HttpServletRequest request,
96                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
97         return createOrUpdate(data, "resources", resourceId, request, userId, false, "createRequirements");
98     }
99
100     @PUT
101     @Consumes(MediaType.APPLICATION_JSON)
102     @Produces(MediaType.APPLICATION_JSON)
103     @Path("/resources/{resourceId}/requirements")
104     @Operation(description = "Update Requirements on resource", method = "PUT", summary = "Update Requirements on resource", responses = {
105         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
106         @ApiResponse(responseCode = "201", description = "Update Requirements"),
107         @ApiResponse(responseCode = "403", description = "Restricted operation"),
108         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
109     public Response updateRequirementsOnResource(@Parameter(description = "Requirements to update", required = true) String data,
110                                                  @Parameter(description = "Component Id") @PathParam("resourceId") String resourceId,
111                                                  @Context final HttpServletRequest request,
112                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
113         return createOrUpdate(data, "resources", resourceId, request, userId, true, "updateRequirements");
114     }
115
116     @GET
117     @Consumes(MediaType.APPLICATION_JSON)
118     @Produces(MediaType.APPLICATION_JSON)
119     @Path("/resources/{resourceId}/requirements/{requirementId}")
120     @Operation(description = "Get Requirement from resource", method = "GET", summary = "GET Requirement from resource", responses = {
121         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
122         @ApiResponse(responseCode = "201", description = "GET requirement"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
123         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
124     public Response getRequirementsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
125                                                 @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
126                                                 @Context final HttpServletRequest request,
127                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
128         return get(requirementId, resourceId, request, userId);
129     }
130
131     @DELETE
132     @Consumes(MediaType.APPLICATION_JSON)
133     @Produces(MediaType.APPLICATION_JSON)
134     @Path("/resources/{resourceId}/requirements/{requirementId}")
135     @Operation(description = "Delete requirements from resource", method = "DELETE", summary = "Delete requirements from resource", responses = {
136         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
137         @ApiResponse(responseCode = "201", description = "Delete requirement"),
138         @ApiResponse(responseCode = "403", description = "Restricted operation"),
139         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
140     public Response deleteRequirementsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
141                                                    @Parameter(description = "requirement Id") @PathParam("requirementId") String requirementId,
142                                                    @Context final HttpServletRequest request,
143                                                    @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
144         return delete(requirementId, resourceId, request, userId);
145     }
146
147     @POST
148     @Consumes(MediaType.APPLICATION_JSON)
149     @Produces(MediaType.APPLICATION_JSON)
150     @Path("/services/{serviceId}/requirements")
151     @Operation(description = "Create requirements on service", method = "POST", summary = "Create requirements on service", responses = {
152         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
153         @ApiResponse(responseCode = "201", description = "Create Requirements"),
154         @ApiResponse(responseCode = "403", description = "Restricted operation"),
155         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
156         @ApiResponse(responseCode = "409", description = "Requirement already exist")})
157     public Response createRequirementsOnService(@Parameter(description = "Requirements to create", required = true) String data,
158                                                 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
159                                                 @Context final HttpServletRequest request,
160                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
161         return createOrUpdate(data, "services", serviceId, request, userId, false, "createRequirements");
162     }
163
164     @PUT
165     @Consumes(MediaType.APPLICATION_JSON)
166     @Produces(MediaType.APPLICATION_JSON)
167     @Path("/services/{serviceId}/requirements")
168     @Operation(description = "Update requirements on service", method = "PUT", summary = "Update requirements on service", responses = {
169         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
170         @ApiResponse(responseCode = "201", description = "Update requirements"),
171         @ApiResponse(responseCode = "403", description = "Restricted operation"),
172         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
173     public Response updateRequirementsOnService(@Parameter(description = "Requirements to update", required = true) String data,
174                                                 @Parameter(description = "Component Id") @PathParam("serviceId") String serviceId,
175                                                 @Context final HttpServletRequest request,
176                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
177         return createOrUpdate(data, "services", serviceId, request, userId, true, "updateRequirements");
178     }
179
180     @GET
181     @Consumes(MediaType.APPLICATION_JSON)
182     @Produces(MediaType.APPLICATION_JSON)
183     @Path("/services/{serviceId}/requirements/{requirementId}")
184     @Operation(description = "Get requirement from service", method = "GET", summary = "GET requirement from service", responses = {
185         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
186         @ApiResponse(responseCode = "201", description = "GET Requirements"),
187         @ApiResponse(responseCode = "403", description = "Restricted operation"),
188         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
189     public Response getRequirementsOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
190                                              @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
191                                              @Context final HttpServletRequest request,
192                                              @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
193         return get(requirementId, serviceId, request, userId);
194     }
195
196     @DELETE
197     @Consumes(MediaType.APPLICATION_JSON)
198     @Produces(MediaType.APPLICATION_JSON)
199     @Path("/services/{serviceId}/requirements/{requirementId}")
200     @Operation(description = "Delete requirement from service", method = "DELETE", summary = "Delete requirement from service", responses = {
201         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
202         @ApiResponse(responseCode = "201", description = "Delete Requirements"),
203         @ApiResponse(responseCode = "403", description = "Restricted operation"),
204         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
205     public Response deleteRequirementsOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
206                                                 @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
207                                                 @Context final HttpServletRequest request,
208                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
209         return delete(requirementId, serviceId, request, userId);
210     }
211
212     private Response createOrUpdate(String data, String componentType, String componentId, HttpServletRequest request, String userId,
213                                     boolean isUpdate, String errorContext) {
214         String url = request.getMethod() + " " + request.getRequestURI();
215         User modifier = new User();
216         modifier.setUserId(userId);
217         LOGGER.debug("Start create or update request of {} with modifier id {}", url, userId);
218         try {
219             String componentIdLower = componentId.toLowerCase();
220             Either<List<RequirementDefinition>, ResponseFormat> mappedRequirementDataEither = getMappedRequirementData(data, modifier,
221                 ComponentTypeEnum.findByParamName(componentType));
222             if (mappedRequirementDataEither.isRight()) {
223                 LOGGER.error("Failed to create or update requirements");
224                 return buildErrorResponse(mappedRequirementDataEither.right().value());
225             }
226             List<RequirementDefinition> mappedRequirementData = mappedRequirementDataEither.left().value();
227             Either<List<RequirementDefinition>, ResponseFormat> actionResponse;
228             if (isUpdate) {
229                 actionResponse = requirementBusinessLogic.updateRequirements(componentIdLower, mappedRequirementData, modifier, errorContext, true);
230             } else {
231                 actionResponse = requirementBusinessLogic.createRequirements(componentIdLower, mappedRequirementData, modifier, errorContext, true);
232             }
233             if (actionResponse.isRight()) {
234                 LOGGER.error("Failed to create or update requirements");
235                 return buildErrorResponse(actionResponse.right().value());
236             }
237             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
238         } catch (Exception e) {
239             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("requirements create or update");
240             LOGGER.error("Failed to create or update requirements with an error", e);
241             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
242         }
243     }
244
245     private Response get(String requirementIdToGet, String componentId, HttpServletRequest request, String userId) {
246         String url = request.getMethod() + " " + request.getRequestURI();
247         User modifier = new User();
248         modifier.setUserId(userId);
249         LOGGER.debug("Start get request of {} with modifier id {}", url, userId);
250         try {
251             String componentIdLower = componentId.toLowerCase();
252             Either<RequirementDefinition, ResponseFormat> actionResponse = requirementBusinessLogic
253                 .getRequirement(componentIdLower, requirementIdToGet, modifier, true);
254             if (actionResponse.isRight()) {
255                 LOGGER.error("failed to get requirements");
256                 return buildErrorResponse(actionResponse.right().value());
257             }
258             Object result = RepresentationUtils.toFilteredRepresentation(actionResponse.left().value());
259             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
260         } catch (Exception e) {
261             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get requirements");
262             LOGGER.error("get requirements failed with exception", e);
263             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
264         }
265     }
266
267     private Response delete(String requirementId, String componentId, HttpServletRequest request, String userId) {
268         String url = request.getMethod() + " " + request.getRequestURI();
269         User modifier = new User();
270         modifier.setUserId(userId);
271         LOGGER.debug("Start delete request of {} with modifier id {}", url, userId);
272         try {
273             String componentIdLower = componentId.toLowerCase();
274             Either<RequirementDefinition, ResponseFormat> actionResponse = requirementBusinessLogic
275                 .deleteRequirement(componentIdLower, requirementId, modifier, true);
276             if (actionResponse.isRight()) {
277                 LOGGER.error("failed to delete requirements");
278                 return buildErrorResponse(actionResponse.right().value());
279             }
280             Object result = RepresentationUtils.toRepresentation(actionResponse.left().value());
281             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
282         } catch (Exception e) {
283             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete requirements");
284             LOGGER.error("Delete requirements failed with an error", e);
285             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
286         }
287     }
288
289     private Either<List<RequirementDefinition>, ResponseFormat> getMappedRequirementData(String inputJson, User user,
290                                                                                          ComponentTypeEnum componentTypeEnum) {
291         Either<UiComponentDataTransfer, ResponseFormat> mappedData = getComponentsUtils()
292             .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
293                 componentTypeEnum);
294         Optional<List<RequirementDefinition>> requirementDefinitionList = mappedData.left().value().getRequirements().values().stream().findFirst();
295         return requirementDefinitionList.<Either<List<RequirementDefinition>, ResponseFormat>>map(Either::left)
296             .orElseGet(() -> Either.right(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)));
297     }
298 }