fcfe37ac11d0a5bba8c42ce399f3fc7bda9b4e59
[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.components.impl.aaf.AafPermission;
50 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
51 import org.openecomp.sdc.be.config.BeEcompErrorManager;
52 import org.openecomp.sdc.be.dao.api.ActionStatus;
53 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
54 import org.openecomp.sdc.be.impl.ComponentsUtils;
55 import org.openecomp.sdc.be.impl.ServletUtils;
56 import org.openecomp.sdc.be.model.RequirementDefinition;
57 import org.openecomp.sdc.be.model.User;
58 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
59 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
60 import org.openecomp.sdc.be.user.UserBusinessLogic;
61 import org.openecomp.sdc.common.api.Constants;
62 import org.openecomp.sdc.common.log.wrappers.Logger;
63 import org.openecomp.sdc.exception.ResponseFormat;
64 import org.springframework.stereotype.Controller;
65
66 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
67 @Path("/v1/catalog")
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 @Tags({@Tag(name = "SDCE-2 APIs")})
71 @Servers({@Server(url = "/sdc2/rest")})
72 @Controller
73 public class RequirementServlet extends AbstractValidationsServlet {
74
75     private static final Logger LOGGER = Logger.getLogger(RequirementServlet.class);
76     private final RequirementBusinessLogic requirementBusinessLogic;
77
78     @Inject
79     public RequirementServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL,
80                               ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
81                               RequirementBusinessLogic requirementBusinessLogic) {
82         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
83         this.requirementBusinessLogic = requirementBusinessLogic;
84     }
85
86     @POST
87     @Consumes(MediaType.APPLICATION_JSON)
88     @Produces(MediaType.APPLICATION_JSON)
89     @Path("/resources/{resourceId}/requirements")
90     @Operation(description = "Create requirements on resource", method = "POST", summary = "Create requirements on resource", responses = {
91         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
92         @ApiResponse(responseCode = "201", description = "Create requirements"),
93         @ApiResponse(responseCode = "403", description = "Restricted operation"),
94         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
95         @ApiResponse(responseCode = "409", description = "requirement already exist")})
96     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
97     public Response createRequirementsOnResource(@Parameter(description = "Requirement to create", required = true) String data,
98                                                  @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
99                                                  @Context final HttpServletRequest request,
100                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
101         return createOrUpdate(data, "resources", resourceId, request, userId, false, "createRequirements");
102     }
103
104     @PUT
105     @Consumes(MediaType.APPLICATION_JSON)
106     @Produces(MediaType.APPLICATION_JSON)
107     @Path("/resources/{resourceId}/requirements")
108     @Operation(description = "Update Requirements on resource", method = "PUT", summary = "Update Requirements on resource", responses = {
109         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
110         @ApiResponse(responseCode = "201", description = "Update Requirements"),
111         @ApiResponse(responseCode = "403", description = "Restricted operation"),
112         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
113     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
114     public Response updateRequirementsOnResource(@Parameter(description = "Requirements to update", required = true) String data,
115                                                  @Parameter(description = "Component Id") @PathParam("resourceId") String resourceId,
116                                                  @Context final HttpServletRequest request,
117                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
118         return createOrUpdate(data, "resources", resourceId, request, userId, true, "updateRequirements");
119     }
120
121     @GET
122     @Consumes(MediaType.APPLICATION_JSON)
123     @Produces(MediaType.APPLICATION_JSON)
124     @Path("/resources/{resourceId}/requirements/{requirementId}")
125     @Operation(description = "Get Requirement from resource", method = "GET", summary = "GET Requirement from resource", responses = {
126         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
127         @ApiResponse(responseCode = "201", description = "GET requirement"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
128         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
129     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
130     public Response getRequirementsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
131                                                 @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
132                                                 @Context final HttpServletRequest request,
133                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
134         return get(requirementId, resourceId, request, userId);
135     }
136
137     @DELETE
138     @Consumes(MediaType.APPLICATION_JSON)
139     @Produces(MediaType.APPLICATION_JSON)
140     @Path("/resources/{resourceId}/requirements/{requirementId}")
141     @Operation(description = "Delete requirements from resource", method = "DELETE", summary = "Delete requirements from resource", responses = {
142         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
143         @ApiResponse(responseCode = "201", description = "Delete requirement"),
144         @ApiResponse(responseCode = "403", description = "Restricted operation"),
145         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
146     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
147     public Response deleteRequirementsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
148                                                    @Parameter(description = "requirement Id") @PathParam("requirementId") String requirementId,
149                                                    @Context final HttpServletRequest request,
150                                                    @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
151         return delete(requirementId, resourceId, request, userId);
152     }
153
154     @POST
155     @Consumes(MediaType.APPLICATION_JSON)
156     @Produces(MediaType.APPLICATION_JSON)
157     @Path("/services/{serviceId}/requirements")
158     @Operation(description = "Create requirements on service", method = "POST", summary = "Create requirements on service", responses = {
159         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
160         @ApiResponse(responseCode = "201", description = "Create Requirements"),
161         @ApiResponse(responseCode = "403", description = "Restricted operation"),
162         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
163         @ApiResponse(responseCode = "409", description = "Requirement already exist")})
164     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
165     public Response createRequirementsOnService(@Parameter(description = "Requirements to create", required = true) String data,
166                                                 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
167                                                 @Context final HttpServletRequest request,
168                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
169         return createOrUpdate(data, "services", serviceId, request, userId, false, "createRequirements");
170     }
171
172     @PUT
173     @Consumes(MediaType.APPLICATION_JSON)
174     @Produces(MediaType.APPLICATION_JSON)
175     @Path("/services/{serviceId}/requirements")
176     @Operation(description = "Update requirements on service", method = "PUT", summary = "Update requirements on service", responses = {
177         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
178         @ApiResponse(responseCode = "201", description = "Update requirements"),
179         @ApiResponse(responseCode = "403", description = "Restricted operation"),
180         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
181     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
182     public Response updateRequirementsOnService(@Parameter(description = "Requirements to update", required = true) String data,
183                                                 @Parameter(description = "Component Id") @PathParam("serviceId") String serviceId,
184                                                 @Context final HttpServletRequest request,
185                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
186         return createOrUpdate(data, "services", serviceId, request, userId, true, "updateRequirements");
187     }
188
189     @GET
190     @Consumes(MediaType.APPLICATION_JSON)
191     @Produces(MediaType.APPLICATION_JSON)
192     @Path("/services/{serviceId}/requirements/{requirementId}")
193     @Operation(description = "Get requirement from service", method = "GET", summary = "GET requirement from service", responses = {
194         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
195         @ApiResponse(responseCode = "201", description = "GET Requirements"),
196         @ApiResponse(responseCode = "403", description = "Restricted operation"),
197         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
198     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
199     public Response getRequirementsOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
200                                              @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
201                                              @Context final HttpServletRequest request,
202                                              @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
203         return get(requirementId, serviceId, request, userId);
204     }
205
206     @DELETE
207     @Consumes(MediaType.APPLICATION_JSON)
208     @Produces(MediaType.APPLICATION_JSON)
209     @Path("/services/{serviceId}/requirements/{requirementId}")
210     @Operation(description = "Delete requirement from service", method = "DELETE", summary = "Delete requirement from service", responses = {
211         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RequirementDefinition.class)))),
212         @ApiResponse(responseCode = "201", description = "Delete Requirements"),
213         @ApiResponse(responseCode = "403", description = "Restricted operation"),
214         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
215     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
216     public Response deleteRequirementsOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
217                                                 @Parameter(description = "Requirement Id") @PathParam("requirementId") String requirementId,
218                                                 @Context final HttpServletRequest request,
219                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
220         return delete(requirementId, serviceId, request, userId);
221     }
222
223     private Response createOrUpdate(String data, String componentType, String componentId, HttpServletRequest request, String userId,
224                                     boolean isUpdate, String errorContext) {
225         String url = request.getMethod() + " " + request.getRequestURI();
226         User modifier = new User();
227         modifier.setUserId(userId);
228         LOGGER.debug("Start create or update request of {} with modifier id {}", url, userId);
229         try {
230             String componentIdLower = componentId.toLowerCase();
231             Either<List<RequirementDefinition>, ResponseFormat> mappedRequirementDataEither = getMappedRequirementData(data, modifier,
232                 ComponentTypeEnum.findByParamName(componentType));
233             if (mappedRequirementDataEither.isRight()) {
234                 LOGGER.error("Failed to create or update requirements");
235                 return buildErrorResponse(mappedRequirementDataEither.right().value());
236             }
237             List<RequirementDefinition> mappedRequirementData = mappedRequirementDataEither.left().value();
238             Either<List<RequirementDefinition>, ResponseFormat> actionResponse;
239             if (isUpdate) {
240                 actionResponse = requirementBusinessLogic.updateRequirements(componentIdLower, mappedRequirementData, modifier, errorContext, true);
241             } else {
242                 actionResponse = requirementBusinessLogic.createRequirements(componentIdLower, mappedRequirementData, modifier, errorContext, true);
243             }
244             if (actionResponse.isRight()) {
245                 LOGGER.error("Failed to create or update requirements");
246                 return buildErrorResponse(actionResponse.right().value());
247             }
248             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
249         } catch (Exception e) {
250             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("requirements create or update");
251             LOGGER.error("Failed to create or update requirements with an error", e);
252             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
253         }
254     }
255
256     private Response get(String requirementIdToGet, String componentId, HttpServletRequest request, String userId) {
257         String url = request.getMethod() + " " + request.getRequestURI();
258         User modifier = new User();
259         modifier.setUserId(userId);
260         LOGGER.debug("Start get request of {} with modifier id {}", url, userId);
261         try {
262             String componentIdLower = componentId.toLowerCase();
263             Either<RequirementDefinition, ResponseFormat> actionResponse = requirementBusinessLogic
264                 .getRequirement(componentIdLower, requirementIdToGet, modifier, true);
265             if (actionResponse.isRight()) {
266                 LOGGER.error("failed to get requirements");
267                 return buildErrorResponse(actionResponse.right().value());
268             }
269             Object result = RepresentationUtils.toFilteredRepresentation(actionResponse.left().value());
270             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
271         } catch (Exception e) {
272             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get requirements");
273             LOGGER.error("get requirements failed with exception", e);
274             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
275         }
276     }
277
278     private Response delete(String requirementId, String componentId, HttpServletRequest request, String userId) {
279         String url = request.getMethod() + " " + request.getRequestURI();
280         User modifier = new User();
281         modifier.setUserId(userId);
282         LOGGER.debug("Start delete request of {} with modifier id {}", url, userId);
283         try {
284             String componentIdLower = componentId.toLowerCase();
285             Either<RequirementDefinition, ResponseFormat> actionResponse = requirementBusinessLogic
286                 .deleteRequirement(componentIdLower, requirementId, modifier, true);
287             if (actionResponse.isRight()) {
288                 LOGGER.error("failed to delete requirements");
289                 return buildErrorResponse(actionResponse.right().value());
290             }
291             Object result = RepresentationUtils.toRepresentation(actionResponse.left().value());
292             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
293         } catch (Exception e) {
294             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete requirements");
295             LOGGER.error("Delete requirements failed with an error", e);
296             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
297         }
298     }
299
300     private Either<List<RequirementDefinition>, ResponseFormat> getMappedRequirementData(String inputJson, User user,
301                                                                                          ComponentTypeEnum componentTypeEnum) {
302         Either<UiComponentDataTransfer, ResponseFormat> mappedData = getComponentsUtils()
303             .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
304                 componentTypeEnum);
305         Optional<List<RequirementDefinition>> requirementDefinitionList = mappedData.left().value().getRequirements().values().stream().findFirst();
306         return requirementDefinitionList.<Either<List<RequirementDefinition>, ResponseFormat>>map(Either::left)
307             .orElseGet(() -> Either.right(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)));
308     }
309 }