2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openecomp.sdc.be.servlets;
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.components.impl.aaf.AafPermission;
51 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
52 import org.openecomp.sdc.be.config.BeEcompErrorManager;
53 import org.openecomp.sdc.be.dao.api.ActionStatus;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.impl.ServletUtils;
57 import org.openecomp.sdc.be.model.CapabilityDefinition;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
60 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
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;
66 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 @Tags({@Tag(name = "SDCE-2 APIs")})
71 @Servers({@Server(url = "/sdc2/rest")})
73 public class CapabilityServlet extends AbstractValidationsServlet {
75 private static final Logger LOGGER = Logger.getLogger(CapabilityServlet.class);
76 private final CapabilitiesBusinessLogic capabilitiesBusinessLogic;
79 public CapabilityServlet(ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
80 ServletUtils servletUtils, ResourceImportManager resourceImportManager,
81 CapabilitiesBusinessLogic capabilitiesBusinessLogic) {
82 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
83 this.capabilitiesBusinessLogic = capabilitiesBusinessLogic;
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 @Path("/resources/{resourceId}/capabilities")
90 @Operation(description = "Create Capabilities on resource", method = "POST", summary = "Create Capabilities on resource", responses = {
91 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
92 @ApiResponse(responseCode = "201", description = "Create Capabilities"),
93 @ApiResponse(responseCode = "403", description = "Restricted operation"),
94 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
95 @ApiResponse(responseCode = "409", description = "Capability already exist")})
96 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
97 public Response createCapabilitiesOnResource(@Parameter(description = "Capability 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, "createCapabilities");
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces(MediaType.APPLICATION_JSON)
107 @Path("/resources/{resourceId}/capabilities")
108 @Operation(description = "Update Capabilities on resource", method = "PUT", summary = "Update Capabilities on resource", responses = {
109 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
110 @ApiResponse(responseCode = "201", description = "Update Capabilities"),
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 updateCapabilitiesOnResource(@Parameter(description = "Capabilities 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, "updateCapabilities");
122 @Consumes(MediaType.APPLICATION_JSON)
123 @Produces(MediaType.APPLICATION_JSON)
124 @Path("/resources/{resourceId}/capabilities/{capabilityId}")
125 @Operation(description = "Get Capability from resource", method = "GET", summary = "GET Capability from resource", responses = {
126 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
127 @ApiResponse(responseCode = "201", description = "GET Capability"), @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 getCapabilityOnResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
131 @Parameter(description = "Capability Id") @PathParam("capabilityId") String capabilityId,
132 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
133 return get(capabilityId, resourceId, request, userId);
137 @Consumes(MediaType.APPLICATION_JSON)
138 @Produces(MediaType.APPLICATION_JSON)
139 @Path("/resources/{resourceId}/capabilities/{capabilityId}")
140 @Operation(description = "Delete capability from resource", method = "DELETE", summary = "Delete capability from resource", responses = {
141 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
142 @ApiResponse(responseCode = "201", description = "Delete capability"),
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 deleteCapabilityOnResource(@Parameter(description = "capability Id") @PathParam("capabilityId") String capabilityId,
147 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
148 @Context final HttpServletRequest request,
149 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
150 return delete(capabilityId, resourceId, request, userId);
154 @Consumes(MediaType.APPLICATION_JSON)
155 @Produces(MediaType.APPLICATION_JSON)
156 @Path("/services/{serviceId}/capabilities")
157 @Operation(description = "Create Capabilities on service", method = "POST", summary = "Create Capabilities on service", responses = {
158 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
159 @ApiResponse(responseCode = "201", description = "Create Capabilities"),
160 @ApiResponse(responseCode = "403", description = "Restricted operation"),
161 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
162 @ApiResponse(responseCode = "409", description = "Capability already exist")})
163 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
164 public Response createCapabilitiesOnService(@Parameter(description = "Capability 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, "createCapabilities");
172 @Consumes(MediaType.APPLICATION_JSON)
173 @Produces(MediaType.APPLICATION_JSON)
174 @Path("/services/{serviceId}/capabilities")
175 @Operation(description = "Update Capabilities on service", method = "PUT", summary = "Update Capabilities on service", responses = {
176 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
177 @ApiResponse(responseCode = "201", description = "Update Capabilities"),
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 updateCapabilitiesOnService(@Parameter(description = "Capabilities 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, "updateCapabilities");
189 @Consumes(MediaType.APPLICATION_JSON)
190 @Produces(MediaType.APPLICATION_JSON)
191 @Path("/services/{serviceId}/capabilities/{capabilityId}")
192 @Operation(description = "Get Capability from service", method = "GET", summary = "GET Capability from service", responses = {
193 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
194 @ApiResponse(responseCode = "201", description = "GET Capability"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
195 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
196 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
197 public Response getCapabilityOnService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
198 @Parameter(description = "Capability Id") @PathParam("capabilityId") String capabilityId,
199 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
200 return get(capabilityId, serviceId, request, userId);
204 @Consumes(MediaType.APPLICATION_JSON)
205 @Produces(MediaType.APPLICATION_JSON)
206 @Path("/services/{serviceId}/capabilities/{capabilityId}")
207 @Operation(description = "Delete capability from service", method = "DELETE", summary = "Delete capability from service", responses = {
208 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = CapabilityDefinition.class)))),
209 @ApiResponse(responseCode = "201", description = "Delete capability"),
210 @ApiResponse(responseCode = "403", description = "Restricted operation"),
211 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
212 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
213 public Response deleteCapabilityOnService(@Parameter(description = "capability Id") @PathParam("capabilityId") String capabilityId,
214 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
215 @Context final HttpServletRequest request,
216 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
217 return delete(capabilityId, serviceId, request, userId);
220 private Response createOrUpdate(String data, String componentType, String componentId, HttpServletRequest request, String userId,
221 boolean isUpdate, String errorContext) {
222 ServletContext context = request.getSession().getServletContext();
223 String url = request.getMethod() + " " + request.getRequestURI();
224 User modifier = new User();
225 modifier.setUserId(userId);
226 LOGGER.debug("Start create or update request of {} with modifier id {}", url, userId);
228 String componentIdLower = componentId.toLowerCase();
229 Either<List<CapabilityDefinition>, ResponseFormat> mappedCapabilitiesDataEither = getMappedCapabilitiesData(data, modifier,
230 ComponentTypeEnum.findByParamName(componentType));
231 if (mappedCapabilitiesDataEither.isRight()) {
232 LOGGER.error("Failed to create or update capabilities");
233 return buildErrorResponse(mappedCapabilitiesDataEither.right().value());
235 List<CapabilityDefinition> mappedCapabilitiesData = mappedCapabilitiesDataEither.left().value();
236 Either<List<CapabilityDefinition>, ResponseFormat> actionResponse;
238 actionResponse = capabilitiesBusinessLogic.updateCapabilities(componentIdLower, mappedCapabilitiesData, modifier, errorContext, true);
240 actionResponse = capabilitiesBusinessLogic.createCapabilities(componentIdLower, mappedCapabilitiesData, modifier, errorContext, true);
242 if (actionResponse.isRight()) {
243 LOGGER.error("Failed to create or update capabilities");
244 return buildErrorResponse(actionResponse.right().value());
246 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
247 } catch (Exception e) {
248 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Capabilities create or update");
249 LOGGER.error("Failed to create or update capabilities with an error", e);
250 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
254 private Response get(String capabilityIdToGet, String componentId, HttpServletRequest request, String userId) {
255 ServletContext context = request.getSession().getServletContext();
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);
261 String componentIdLower = componentId.toLowerCase();
262 Either<CapabilityDefinition, ResponseFormat> actionResponse = capabilitiesBusinessLogic
263 .getCapability(componentIdLower, capabilityIdToGet, modifier, true);
264 if (actionResponse.isRight()) {
265 LOGGER.error("failed to get capability");
266 return buildErrorResponse(actionResponse.right().value());
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 capability");
272 LOGGER.error("get capabilities failed with exception", e);
273 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
277 private Response delete(String capabilityId, String componentId, HttpServletRequest request, String userId) {
278 ServletContext context = request.getSession().getServletContext();
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);
284 String componentIdLower = componentId.toLowerCase();
285 Either<CapabilityDefinition, ResponseFormat> actionResponse = capabilitiesBusinessLogic
286 .deleteCapability(componentIdLower, capabilityId, modifier, true);
287 if (actionResponse.isRight()) {
288 LOGGER.error("failed to delete capability");
289 return buildErrorResponse(actionResponse.right().value());
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 capability");
295 LOGGER.error("Delete capability failed with an error", e);
296 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
300 private Either<List<CapabilityDefinition>, ResponseFormat> getMappedCapabilitiesData(String inputJson, User user,
301 ComponentTypeEnum componentTypeEnum) {
302 Either<UiComponentDataTransfer, ResponseFormat> mappedData = getComponentsUtils()
303 .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
305 if (mappedData.isRight()) {
306 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
308 Optional<List<CapabilityDefinition>> capabilityDefinitionList = mappedData.left().value().getCapabilities().values().stream().findFirst();
309 return capabilityDefinitionList.<Either<List<CapabilityDefinition>, ResponseFormat>>map(Either::left)
310 .orElseGet(() -> Either.right(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)));