2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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=========================================================
19 package org.openecomp.sdc.be.servlets;
21 import io.swagger.v3.oas.annotations.Operation;
22 import io.swagger.v3.oas.annotations.Parameter;
23 import io.swagger.v3.oas.annotations.media.ArraySchema;
24 import io.swagger.v3.oas.annotations.media.Content;
25 import io.swagger.v3.oas.annotations.media.Schema;
26 import io.swagger.v3.oas.annotations.responses.ApiResponse;
27 import io.swagger.v3.oas.annotations.tags.Tag;
28 import io.swagger.v3.oas.annotations.tags.Tags;
29 import java.util.List;
30 import java.util.Optional;
31 import javax.inject.Inject;
32 import javax.inject.Singleton;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.PUT;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ComponentSubstitutionFilterBusinessLogic;
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.datamodel.utils.ConstraintConvertor;
54 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
55 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
56 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
57 import org.openecomp.sdc.be.impl.ComponentsUtils;
58 import org.openecomp.sdc.be.impl.ServletUtils;
59 import org.openecomp.sdc.be.model.User;
60 import org.openecomp.sdc.be.tosca.utils.SubstitutionFilterConverter;
61 import org.openecomp.sdc.be.ui.model.UIConstraint;
62 import org.openecomp.sdc.be.ui.model.UINodeFilter;
63 import org.openecomp.sdc.be.user.UserBusinessLogic;
64 import org.openecomp.sdc.common.api.Constants;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 @Path("/v1/catalog/{componentType}/{componentId}/substitutionFilter/{constraintType}")
69 @Tags({@Tag(name = "SDCE-2 APIs")})
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
73 public class ComponentSubstitutionFilterServlet extends AbstractValidationsServlet {
75 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentSubstitutionFilterServlet.class);
76 private static final String START_HANDLE_REQUEST_OF = "Start handle {} request of {}";
77 private static final String MODIFIER_ID_IS = "Modifier id is {}";
78 private static final String FAILED_TO_PARSE_COMPONENT = "Failed to parse component";
79 private static final String INVALID_CONSTRAINTYPE_ENUM = "Invalid value for NodeFilterConstraintType enum %s";
80 private static final String FAILED_TO_ADD_SUBSTITUTION_FILTER = "Failed to add substitution filter";
81 private static final String ADD_SUBSTITUTION_FILTER = "Add Substitution Filter";
82 private static final String ADD_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Add substitution filter with an error";
83 private static final String FAILED_TO_UPDATE_SUBSTITUTION_FILTER = "Failed to update substitution filter";
84 private static final String SUBSTITUTION_FILTER_UPDATE = "Substitution Filter Update";
85 private static final String UPDATE_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Update substitution filter with an error {}";
86 private static final String FAILED_TO_DELETE_SUBSTITUTION_FILTER = "Failed to delete substitution filter";
87 private static final String SUBSTITUTION_FILTER_DELETE = "Substitution Filter Delete";
88 private static final String DELETE_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Delete substitution filter with an error";
89 private final ComponentSubstitutionFilterBusinessLogic componentSubstitutionFilterBusinessLogic;
92 public ComponentSubstitutionFilterServlet(final UserBusinessLogic userBusinessLogic, final ComponentInstanceBusinessLogic componentInstanceBL,
93 final ComponentsUtils componentsUtils, final ServletUtils servletUtils,
94 final ResourceImportManager resourceImportManager,
95 final ComponentSubstitutionFilterBusinessLogic componentSubstitutionFilterBusinessLogic) {
96 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
97 this.componentSubstitutionFilterBusinessLogic = componentSubstitutionFilterBusinessLogic;
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 @Operation(description = "Add Component Substitution Filter Constraint", method = "POST", summary = "Add Component Substitution Filter Constraint", responses = {
104 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
105 @ApiResponse(responseCode = "201", description = "Add Substitution Filter Constraint"),
106 @ApiResponse(responseCode = "403", description = "Restricted operation"),
107 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
108 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
109 public Response addSubstitutionFilter(@Parameter(description = "UIConstraint data", required = true) String constraintData,
110 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
111 @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = {
112 ComponentTypeEnum.SERVICE_PARAM_NAME,
113 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
114 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
115 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
116 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
117 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
118 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
119 LOGGER.debug(MODIFIER_ID_IS, userId);
120 final User userModifier = componentSubstitutionFilterBusinessLogic.validateUser(userId);
121 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
123 final Optional<UIConstraint> convertResponse = componentsUtils.parseToConstraint(constraintData, userModifier, componentTypeEnum);
124 if (convertResponse.isEmpty()) {
125 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
126 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
128 final UIConstraint uiConstraint = convertResponse.get();
129 final String constraint = new ConstraintConvertor().convert(uiConstraint);
130 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
131 if (nodeFilterConstraintType.isEmpty()) {
132 return buildErrorResponse(
133 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_CONSTRAINTYPE_ENUM, constraintType));
135 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
136 .addSubstitutionFilter(componentId.toLowerCase(), uiConstraint.getServicePropertyName(), constraint, true, componentTypeEnum);
137 if (actionResponse.isEmpty()) {
138 LOGGER.error(FAILED_TO_ADD_SUBSTITUTION_FILTER);
139 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
141 final UINodeFilter uiFilter = new SubstitutionFilterConverter().convertToUi(actionResponse.get());
142 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), uiFilter);
143 } catch (final Exception e) {
144 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(ADD_SUBSTITUTION_FILTER);
145 LOGGER.error(ADD_SUBSTITUTION_FILTER_WITH_AN_ERROR, e);
146 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 @Operation(description = "Update Component Substitution Filter Constraint", method = "PUT", summary = "Update Component Substitution Filter Constraint", responses = {
154 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
155 @ApiResponse(responseCode = "201", description = "Update Substitution Filter Constraint"),
156 @ApiResponse(responseCode = "403", description = "Restricted operation"),
157 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
158 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
159 public Response updateSubstitutionFilter(@Parameter(description = "UIConstraint data", required = true) String constraintData,
160 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
161 @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = {
162 ComponentTypeEnum.SERVICE_PARAM_NAME,
163 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
164 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
165 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
166 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
167 @Context final HttpServletRequest request,
168 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
169 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
170 LOGGER.debug(MODIFIER_ID_IS, userId);
171 final User userModifier = componentSubstitutionFilterBusinessLogic.validateUser(userId);
173 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
174 final List<UIConstraint> uiConstraints = componentsUtils.validateAndParseConstraint(componentTypeEnum, constraintData, userModifier);
175 if (CollectionUtils.isEmpty(uiConstraints)) {
176 LOGGER.error("Failed to Parse Constraint data {} when executing {} ", constraintData, SUBSTITUTION_FILTER_UPDATE);
177 return buildErrorResponse(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, "Failed to parse constraint data"));
179 final List<String> constraints = new ConstraintConvertor().convertToList(uiConstraints);
180 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
181 if (!nodeFilterConstraintType.isPresent()) {
182 return buildErrorResponse(
183 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_CONSTRAINTYPE_ENUM, constraintType));
185 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
186 .updateSubstitutionFilter(componentId.toLowerCase(), constraints, true, componentTypeEnum);
187 if (!actionResponse.isPresent()) {
188 LOGGER.error(FAILED_TO_UPDATE_SUBSTITUTION_FILTER);
189 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
191 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
192 new SubstitutionFilterConverter().convertToUi(actionResponse.get()));
193 } catch (final Exception e) {
194 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(SUBSTITUTION_FILTER_UPDATE);
195 LOGGER.error(UPDATE_SUBSTITUTION_FILTER_WITH_AN_ERROR, e.getMessage(), e);
196 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
201 @Consumes(MediaType.APPLICATION_JSON)
202 @Produces(MediaType.APPLICATION_JSON)
203 @Path("/{constraintIndex}")
204 @Operation(description = "Delete Component Substitution Filter Constraint", method = "Delete", summary = "Delete Component Substitution Filter Constraint", responses = {
205 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
206 @ApiResponse(responseCode = "201", description = "Delete Substitution Filter Constraint"),
207 @ApiResponse(responseCode = "403", description = "Restricted operation"),
208 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
209 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
210 public Response deleteSubstitutionFilterConstraint(@Parameter(description = "Component Id") @PathParam("componentId") String componentId,
211 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
212 @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = {
213 ComponentTypeEnum.SERVICE_PARAM_NAME,
214 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
215 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
216 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
217 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
218 @Context final HttpServletRequest request,
219 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
220 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
221 LOGGER.debug(MODIFIER_ID_IS, userId);
222 componentSubstitutionFilterBusinessLogic.validateUser(userId);
223 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
224 if (!nodeFilterConstraintType.isPresent()) {
225 return buildErrorResponse(
226 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_CONSTRAINTYPE_ENUM, constraintType));
229 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
230 .deleteSubstitutionFilter(componentId.toLowerCase(), index, true, ComponentTypeEnum.findByParamName(componentType));
231 if (!actionResponse.isPresent()) {
232 LOGGER.debug(FAILED_TO_DELETE_SUBSTITUTION_FILTER);
233 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
235 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
236 new SubstitutionFilterConverter().convertToUi(actionResponse.get()));
237 } catch (final Exception e) {
238 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(SUBSTITUTION_FILTER_DELETE);
239 LOGGER.debug(DELETE_SUBSTITUTION_FILTER_WITH_AN_ERROR, e);
240 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));