Remove legacy certificate handling
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / CapabilityServlet.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.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.DELETE;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.HeaderParam;
39 import javax.ws.rs.POST;
40 import javax.ws.rs.PUT;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
50 import org.openecomp.sdc.be.config.BeEcompErrorManager;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.be.impl.ServletUtils;
55 import org.openecomp.sdc.be.model.CapabilityDefinition;
56 import org.openecomp.sdc.be.model.User;
57 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
58 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
59 import org.openecomp.sdc.common.api.Constants;
60 import org.openecomp.sdc.common.log.wrappers.Logger;
61 import org.openecomp.sdc.exception.ResponseFormat;
62 import org.springframework.stereotype.Controller;
63
64 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
65 @Path("/v1/catalog")
66 @Consumes(MediaType.APPLICATION_JSON)
67 @Produces(MediaType.APPLICATION_JSON)
68 @Tags({@Tag(name = "SDCE-2 APIs")})
69 @Servers({@Server(url = "/sdc2/rest")})
70 @Controller
71 public class CapabilityServlet extends AbstractValidationsServlet {
72
73     private static final Logger LOGGER = Logger.getLogger(CapabilityServlet.class);
74     private final CapabilitiesBusinessLogic capabilitiesBusinessLogic;
75
76     @Inject
77     public CapabilityServlet(ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
78                              ServletUtils servletUtils, ResourceImportManager resourceImportManager,
79                              CapabilitiesBusinessLogic capabilitiesBusinessLogic) {
80         super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
81         this.capabilitiesBusinessLogic = capabilitiesBusinessLogic;
82     }
83
84     @POST
85     @Consumes(MediaType.APPLICATION_JSON)
86     @Produces(MediaType.APPLICATION_JSON)
87     @Path("/resources/{resourceId}/capabilities")
88     @Operation(description = "Create Capabilities on resource", method = "POST", summary = "Create Capabilities on resource", responses = {
89         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
90         @ApiResponse(responseCode = "201", description = "Create Capabilities"),
91         @ApiResponse(responseCode = "403", description = "Restricted operation"),
92         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
93         @ApiResponse(responseCode = "409", description = "Capability already exist")})
94     public Response createCapabilitiesOnResource(@Parameter(description = "Capability to create", required = true) String data,
95                                                  @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
96                                                  @Context final HttpServletRequest request,
97                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
98         return createOrUpdate(data, "resources", resourceId, request, userId, false, "createCapabilities");
99     }
100
101     @PUT
102     @Consumes(MediaType.APPLICATION_JSON)
103     @Produces(MediaType.APPLICATION_JSON)
104     @Path("/resources/{resourceId}/capabilities")
105     @Operation(description = "Update Capabilities on resource", method = "PUT", summary = "Update Capabilities on resource", responses = {
106         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
107         @ApiResponse(responseCode = "201", description = "Update Capabilities"),
108         @ApiResponse(responseCode = "403", description = "Restricted operation"),
109         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
110     public Response updateCapabilitiesOnResource(@Parameter(description = "Capabilities to update", required = true) String data,
111                                                  @Parameter(description = "Component Id") @PathParam("resourceId") String resourceId,
112                                                  @Context final HttpServletRequest request,
113                                                  @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
114         return createOrUpdate(data, "resources", resourceId, request, userId, true, "updateCapabilities");
115     }
116
117     @GET
118     @Consumes(MediaType.APPLICATION_JSON)
119     @Produces(MediaType.APPLICATION_JSON)
120     @Path("/resources/{resourceId}/capabilities/{capabilityId}")
121     @Operation(description = "Get Capability from resource", method = "GET", summary = "GET Capability from resource", responses = {
122         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
123         @ApiResponse(responseCode = "201", description = "GET Capability"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
124         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
125     public Response getCapabilityOnResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
126                                             @Parameter(description = "Capability Id") @PathParam("capabilityId") String capabilityId,
127                                             @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
128         return get(capabilityId, resourceId, request, userId);
129     }
130
131     @DELETE
132     @Consumes(MediaType.APPLICATION_JSON)
133     @Produces(MediaType.APPLICATION_JSON)
134     @Path("/resources/{resourceId}/capabilities/{capabilityId}")
135     @Operation(description = "Delete capability from resource", method = "DELETE", summary = "Delete capability from resource", responses = {
136         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
137         @ApiResponse(responseCode = "201", description = "Delete capability"),
138         @ApiResponse(responseCode = "403", description = "Restricted operation"),
139         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
140     public Response deleteCapabilityOnResource(@Parameter(description = "capability Id") @PathParam("capabilityId") String capabilityId,
141                                                @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
142                                                @Context final HttpServletRequest request,
143                                                @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
144         return delete(capabilityId, resourceId, request, userId);
145     }
146
147     @POST
148     @Consumes(MediaType.APPLICATION_JSON)
149     @Produces(MediaType.APPLICATION_JSON)
150     @Path("/services/{serviceId}/capabilities")
151     @Operation(description = "Create Capabilities on service", method = "POST", summary = "Create Capabilities on service", responses = {
152         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
153         @ApiResponse(responseCode = "201", description = "Create Capabilities"),
154         @ApiResponse(responseCode = "403", description = "Restricted operation"),
155         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
156         @ApiResponse(responseCode = "409", description = "Capability already exist")})
157     public Response createCapabilitiesOnService(@Parameter(description = "Capability 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, "createCapabilities");
162     }
163
164     @PUT
165     @Consumes(MediaType.APPLICATION_JSON)
166     @Produces(MediaType.APPLICATION_JSON)
167     @Path("/services/{serviceId}/capabilities")
168     @Operation(description = "Update Capabilities on service", method = "PUT", summary = "Update Capabilities on service", responses = {
169         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
170         @ApiResponse(responseCode = "201", description = "Update Capabilities"),
171         @ApiResponse(responseCode = "403", description = "Restricted operation"),
172         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
173     public Response updateCapabilitiesOnService(@Parameter(description = "Capabilities 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, "updateCapabilities");
178     }
179
180     @GET
181     @Consumes(MediaType.APPLICATION_JSON)
182     @Produces(MediaType.APPLICATION_JSON)
183     @Path("/services/{serviceId}/capabilities/{capabilityId}")
184     @Operation(description = "Get Capability from service", method = "GET", summary = "GET Capability from service", responses = {
185         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
186         @ApiResponse(responseCode = "201", description = "GET Capability"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
187         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
188     public Response getCapabilityOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
189                                            @Parameter(description = "Capability Id") @PathParam("capabilityId") String capabilityId,
190                                            @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
191         return get(capabilityId, serviceId, request, userId);
192     }
193
194     @DELETE
195     @Consumes(MediaType.APPLICATION_JSON)
196     @Produces(MediaType.APPLICATION_JSON)
197     @Path("/services/{serviceId}/capabilities/{capabilityId}")
198     @Operation(description = "Delete capability from service", method = "DELETE", summary = "Delete capability from service", responses = {
199         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
200         @ApiResponse(responseCode = "201", description = "Delete capability"),
201         @ApiResponse(responseCode = "403", description = "Restricted operation"),
202         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
203     public Response deleteCapabilityOnService(@Parameter(description = "capability Id") @PathParam("capabilityId") String capabilityId,
204                                               @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
205                                               @Context final HttpServletRequest request,
206                                               @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
207         return delete(capabilityId, serviceId, request, userId);
208     }
209
210     private Response createOrUpdate(String data, String componentType, String componentId, HttpServletRequest request, String userId,
211                                     boolean isUpdate, String errorContext) {
212         ServletContext context = request.getSession().getServletContext();
213         String url = request.getMethod() + " " + request.getRequestURI();
214         User modifier = new User();
215         modifier.setUserId(userId);
216         LOGGER.debug("Start create or update request of {} with modifier id {}", url, userId);
217         try {
218             String componentIdLower = componentId.toLowerCase();
219             Either<List<CapabilityDefinition>, ResponseFormat> mappedCapabilitiesDataEither = getMappedCapabilitiesData(data, modifier,
220                 ComponentTypeEnum.findByParamName(componentType));
221             if (mappedCapabilitiesDataEither.isRight()) {
222                 LOGGER.error("Failed to create or update capabilities");
223                 return buildErrorResponse(mappedCapabilitiesDataEither.right().value());
224             }
225             List<CapabilityDefinition> mappedCapabilitiesData = mappedCapabilitiesDataEither.left().value();
226             Either<List<CapabilityDefinition>, ResponseFormat> actionResponse;
227             if (isUpdate) {
228                 actionResponse = capabilitiesBusinessLogic.updateCapabilities(componentIdLower, mappedCapabilitiesData, modifier, errorContext, true);
229             } else {
230                 actionResponse = capabilitiesBusinessLogic.createCapabilities(componentIdLower, mappedCapabilitiesData, modifier, errorContext, true);
231             }
232             if (actionResponse.isRight()) {
233                 LOGGER.error("Failed to create or update capabilities");
234                 return buildErrorResponse(actionResponse.right().value());
235             }
236             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
237         } catch (Exception e) {
238             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Capabilities create or update");
239             LOGGER.error("Failed to create or update capabilities with an error", e);
240             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
241         }
242     }
243
244     private Response get(String capabilityIdToGet, String componentId, HttpServletRequest request, String userId) {
245         ServletContext context = request.getSession().getServletContext();
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<CapabilityDefinition, ResponseFormat> actionResponse = capabilitiesBusinessLogic
253                 .getCapability(componentIdLower, capabilityIdToGet, modifier, true);
254             if (actionResponse.isRight()) {
255                 LOGGER.error("failed to get capability");
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 capability");
262             LOGGER.error("get capabilities failed with exception", e);
263             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
264         }
265     }
266
267     private Response delete(String capabilityId, String componentId, HttpServletRequest request, String userId) {
268         ServletContext context = request.getSession().getServletContext();
269         String url = request.getMethod() + " " + request.getRequestURI();
270         User modifier = new User();
271         modifier.setUserId(userId);
272         LOGGER.debug("Start delete request of {} with modifier id {}", url, userId);
273         try {
274             String componentIdLower = componentId.toLowerCase();
275             Either<CapabilityDefinition, ResponseFormat> actionResponse = capabilitiesBusinessLogic
276                 .deleteCapability(componentIdLower, capabilityId, modifier, true);
277             if (actionResponse.isRight()) {
278                 LOGGER.error("failed to delete capability");
279                 return buildErrorResponse(actionResponse.right().value());
280             }
281             Object result = RepresentationUtils.toRepresentation(actionResponse.left().value());
282             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
283         } catch (Exception e) {
284             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete capability");
285             LOGGER.error("Delete capability failed with an error", e);
286             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
287         }
288     }
289
290     private Either<List<CapabilityDefinition>, ResponseFormat> getMappedCapabilitiesData(String inputJson, User user,
291                                                                                          ComponentTypeEnum componentTypeEnum) {
292         Either<UiComponentDataTransfer, ResponseFormat> mappedData = getComponentsUtils()
293             .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
294                 componentTypeEnum);
295         if (mappedData.isRight()) {
296             return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
297         }
298         Optional<List<CapabilityDefinition>> capabilityDefinitionList = mappedData.left().value().getCapabilities().values().stream().findFirst();
299         return capabilityDefinitionList.<Either<List<CapabilityDefinition>, ResponseFormat>>map(Either::left)
300             .orElseGet(() -> Either.right(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)));
301     }
302 }