2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * 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 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import com.jcabi.aspects.Loggable;
23 import fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.validation.Valid;
34 import javax.validation.constraints.NotNull;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
46 import org.openecomp.sdc.be.config.BeEcompErrorManager;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.exception.BusinessException;
50 import org.openecomp.sdc.be.model.CapabilityDefinition;
51 import org.openecomp.sdc.be.servlets.builder.ServletResponseBuilder;
52 import org.openecomp.sdc.be.ui.mapper.CapabilityMapper;
53 import org.openecomp.sdc.be.ui.model.ComponentInstanceCapabilityUpdateModel;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
56 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
57 import org.openecomp.sdc.common.log.enums.StatusCode;
58 import org.openecomp.sdc.common.log.wrappers.Logger;
59 import org.openecomp.sdc.exception.ResponseFormat;
60 import org.springframework.stereotype.Controller;
61 import org.springframework.web.bind.annotation.RequestBody;
64 * Handles component instance capabilities operations
66 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
68 @Tag(name = "SDCE-2 APIs")
69 @Server(url = "/sdc2/rest")
71 public class ComponentInstanceCapabilityServlet {
73 private static final Logger LOGGER = Logger.getLogger(ComponentInstanceCapabilityServlet.class);
74 private static final LoggerSupportability LOGGER_SUPPORTABILITY = LoggerSupportability.getLogger(ComponentInstanceCapabilityServlet.class);
76 private final ResponseFormatManager responseFormatManager;
77 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
78 private final CapabilityMapper capabilityMapper;
79 private final ServletResponseBuilder servletResponseBuilder;
81 public ComponentInstanceCapabilityServlet(final ComponentInstanceBusinessLogic componentInstanceBusinessLogic,
82 final CapabilityMapper capabilityMapper, final ServletResponseBuilder servletResponseBuilder) {
83 this.capabilityMapper = capabilityMapper;
84 this.servletResponseBuilder = servletResponseBuilder;
85 this.responseFormatManager = ResponseFormatManager.getInstance();
86 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
90 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/")
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 @Operation(description = "Update Component Instance Capability", method = "PUT", summary = "Returns updated Component Instance Capability",
95 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
96 @ApiResponse(responseCode = "200", description = "Resource instance capability successfully updated"),
97 @ApiResponse(responseCode = "403", description = "Restricted operation"),
98 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
99 @ApiResponse(responseCode = "404", description = "Component/Component Instance/Capability not found")})
100 public Response updateInstanceRequirement(@PathParam("containerComponentType") final String containerComponentType,
101 @PathParam("containerComponentId") final String containerComponentId,
102 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
103 @Parameter(description = "Component instance capability to update", required = true)
104 @Valid @RequestBody @NotNull final ComponentInstanceCapabilityUpdateModel capabilityUpdateModel,
105 @Context final HttpServletRequest request,
106 @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
107 if (LOGGER.isDebugEnabled()) {
108 final var url = request.getMethod() + " " + request.getRequestURI();
109 LOGGER.debug("Start handle request of {}", url);
112 var componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
113 if (componentTypeEnum == null) {
114 LOGGER.debug("Unsupported component type {}", containerComponentType);
115 return servletResponseBuilder
116 .buildErrorResponse(responseFormatManager.getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
118 final var capabilityDefinition = capabilityMapper.mapToCapabilityDefinition(capabilityUpdateModel);
119 LOGGER_SUPPORTABILITY.log(LoggerSupportabilityActions.UPDATE_INSTANCE_CAPABILITY, StatusCode.STARTED,
120 "Starting to update capability '{}' in component instance '{}' by '{}'",
121 capabilityDefinition.getName(), componentInstanceUniqueId, userId);
122 final Either<CapabilityDefinition, ResponseFormat> response = componentInstanceBusinessLogic
123 .updateInstanceCapability(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityDefinition, userId);
124 if (response.isRight()) {
125 return servletResponseBuilder.buildErrorResponse(response.right().value());
127 return servletResponseBuilder.buildOkResponse(responseFormatManager.getResponseFormat(ActionStatus.OK), response.left().value());
128 } catch (final BusinessException e) {
129 //leave to the handlers deal with it
131 } catch (final Exception e) {
132 var errorMsg = String
133 .format("Unexpected error while updating component '%s' of type '%s' instance '%s'. Payload '%s'",
134 containerComponentId, containerComponentType, componentInstanceUniqueId, capabilityUpdateModel);
135 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
136 LOGGER.debug(errorMsg, e);
137 return servletResponseBuilder.buildErrorResponse(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR));