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