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=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import io.swagger.v3.oas.annotations.Operation;
23 import io.swagger.v3.oas.annotations.Parameter;
24 import io.swagger.v3.oas.annotations.media.ArraySchema;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
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.inject.Singleton;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.DELETE;
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.apache.commons.collections.CollectionUtils;
47 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.ComponentSubstitutionFilterBusinessLogic;
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.datamodel.utils.ConstraintConvertor;
55 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
56 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
57 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
58 import org.openecomp.sdc.be.impl.ComponentsUtils;
59 import org.openecomp.sdc.be.impl.ServletUtils;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.be.tosca.utils.SubstitutionFilterConverter;
62 import org.openecomp.sdc.be.ui.model.UIConstraint;
63 import org.openecomp.sdc.be.ui.model.UINodeFilter;
64 import org.openecomp.sdc.be.user.UserBusinessLogic;
65 import org.openecomp.sdc.common.api.Constants;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 @Path("/v1/catalog/{componentType}/{componentId}/substitutionFilter/{constraintType}")
70 @Tags({@Tag(name = "SDCE-2 APIs")})
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
74 public class ComponentSubstitutionFilterServlet extends AbstractValidationsServlet {
76 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentSubstitutionFilterServlet.class);
77 private static final String START_HANDLE_REQUEST_OF = "Start handle {} request of {}";
78 private static final String MODIFIER_ID_IS = "Modifier id is {}";
79 private static final String FAILED_TO_PARSE_COMPONENT = "Failed to parse component";
80 private static final String INVALID_CONSTRAINTYPE_ENUM = "Invalid value for NodeFilterConstraintType enum %s";
82 private static final String FAILED_TO_ADD_SUBSTITUTION_FILTER = "Failed to add substitution filter";
83 private static final String ADD_SUBSTITUTION_FILTER = "Add Substitution Filter";
84 private static final String ADD_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Add substitution filter with an error";
86 private static final String FAILED_TO_UPDATE_SUBSTITUTION_FILTER = "Failed to update substitution filter";
87 private static final String SUBSTITUTION_FILTER_UPDATE = "Substitution Filter Update";
88 private static final String UPDATE_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Update substitution filter with an error {}";
90 private static final String FAILED_TO_DELETE_SUBSTITUTION_FILTER = "Failed to delete substitution filter";
91 private static final String SUBSTITUTION_FILTER_DELETE = "Substitution Filter Delete";
92 private static final String DELETE_SUBSTITUTION_FILTER_WITH_AN_ERROR = "Delete substitution filter with an error";
94 private final ComponentSubstitutionFilterBusinessLogic componentSubstitutionFilterBusinessLogic;
97 public ComponentSubstitutionFilterServlet(final UserBusinessLogic userBusinessLogic,
98 final ComponentInstanceBusinessLogic componentInstanceBL,
99 final ComponentsUtils componentsUtils,
100 final ServletUtils servletUtils,
101 final ResourceImportManager resourceImportManager,
102 final ComponentSubstitutionFilterBusinessLogic componentSubstitutionFilterBusinessLogic) {
103 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
104 this.componentSubstitutionFilterBusinessLogic = componentSubstitutionFilterBusinessLogic;
108 @Consumes(MediaType.APPLICATION_JSON)
109 @Produces(MediaType.APPLICATION_JSON)
110 @Operation(description = "Add Component Substitution Filter Constraint", method = "POST",
111 summary = "Add Component Substitution Filter Constraint", responses = {
112 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
113 @ApiResponse(responseCode = "201", description = "Add Substitution Filter Constraint"),
114 @ApiResponse(responseCode = "403", description = "Restricted operation"),
115 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
116 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
117 public Response addSubstitutionFilter(
118 @Parameter(description = "UIConstraint data", required = true) String constraintData,
119 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
120 @Parameter(description = "valid value: resources / services",
121 schema = @Schema(allowableValues = {
122 ComponentTypeEnum.SERVICE_PARAM_NAME,
123 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
124 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
125 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
126 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
127 @PathParam("constraintType") final String constraintType,
128 @Context final HttpServletRequest request,
129 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
131 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
132 LOGGER.debug(MODIFIER_ID_IS, userId);
133 final User userModifier = componentSubstitutionFilterBusinessLogic.validateUser(userId);
135 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
137 final Optional<UIConstraint> convertResponse = componentsUtils
138 .parseToConstraint(constraintData, userModifier, componentTypeEnum);
139 if (convertResponse.isEmpty()) {
140 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
141 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
144 final UIConstraint uiConstraint = convertResponse.get();
145 final String constraint = new ConstraintConvertor().convert(uiConstraint);
147 final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
148 NodeFilterConstraintType.parse(constraintType);
149 if (nodeFilterConstraintType.isEmpty()) {
150 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
151 INVALID_CONSTRAINTYPE_ENUM, constraintType));
154 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
155 .addSubstitutionFilter(componentId.toLowerCase(),
156 uiConstraint.getServicePropertyName(), constraint, true, componentTypeEnum);
158 if (actionResponse.isEmpty()) {
159 LOGGER.error(FAILED_TO_ADD_SUBSTITUTION_FILTER);
160 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
162 final UINodeFilter uiFilter = new SubstitutionFilterConverter().convertToUi(actionResponse.get());
164 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), uiFilter);
166 } catch (final Exception e) {
167 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(ADD_SUBSTITUTION_FILTER);
168 LOGGER.error(ADD_SUBSTITUTION_FILTER_WITH_AN_ERROR, e);
169 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
174 @Consumes(MediaType.APPLICATION_JSON)
175 @Produces(MediaType.APPLICATION_JSON)
176 @Operation(description = "Update Component Substitution Filter Constraint", method = "PUT",
177 summary = "Update Component Substitution Filter Constraint", responses = {
178 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
179 @ApiResponse(responseCode = "201", description = "Update Substitution Filter Constraint"),
180 @ApiResponse(responseCode = "403", description = "Restricted operation"),
181 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
182 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
183 public Response updateSubstitutionFilter(
184 @Parameter(description = "UIConstraint data", required = true) String constraintData,
185 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
186 @Parameter(description = "valid value: resources / services",
187 schema = @Schema(allowableValues = {
188 ComponentTypeEnum.SERVICE_PARAM_NAME,
189 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
190 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
191 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
192 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
193 @PathParam("constraintType") final String constraintType,
194 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
196 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
197 LOGGER.debug(MODIFIER_ID_IS, userId);
198 final User userModifier = componentSubstitutionFilterBusinessLogic.validateUser(userId);
201 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
202 final List<UIConstraint> uiConstraints = componentsUtils
203 .validateAndParseConstraint(componentTypeEnum, constraintData, userModifier);
204 if (CollectionUtils.isEmpty(uiConstraints)) {
205 LOGGER.error("Failed to Parse Constraint data {} when executing {} ",
206 constraintData, SUBSTITUTION_FILTER_UPDATE);
207 return buildErrorResponse(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR,
208 "Failed to parse constraint data"));
211 final List<String> constraints = new ConstraintConvertor().convertToList(uiConstraints);
212 final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
213 NodeFilterConstraintType.parse(constraintType);
214 if (!nodeFilterConstraintType.isPresent()) {
215 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
216 INVALID_CONSTRAINTYPE_ENUM, constraintType));
218 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
219 .updateSubstitutionFilter(componentId.toLowerCase(), constraints,
220 true, componentTypeEnum);
222 if (!actionResponse.isPresent()) {
223 LOGGER.error(FAILED_TO_UPDATE_SUBSTITUTION_FILTER);
224 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
227 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
228 new SubstitutionFilterConverter().convertToUi(actionResponse.get()));
230 } catch (final Exception e) {
231 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(SUBSTITUTION_FILTER_UPDATE);
232 LOGGER.error(UPDATE_SUBSTITUTION_FILTER_WITH_AN_ERROR, e.getMessage(), e);
233 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
238 @Consumes(MediaType.APPLICATION_JSON)
239 @Produces(MediaType.APPLICATION_JSON)
240 @Path("/{constraintIndex}")
241 @Operation(description = "Delete Component Substitution Filter Constraint", method = "Delete",
242 summary = "Delete Component Substitution Filter Constraint", responses = {
243 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
244 @ApiResponse(responseCode = "201", description = "Delete Substitution Filter Constraint"),
245 @ApiResponse(responseCode = "403", description = "Restricted operation"),
246 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
247 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
248 public Response deleteSubstitutionFilterConstraint(
249 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
250 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
251 @Parameter(description = "valid value: resources / services",
252 schema = @Schema(allowableValues = {
253 ComponentTypeEnum.SERVICE_PARAM_NAME,
254 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
255 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
256 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
257 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
258 @PathParam("constraintType") final String constraintType,
259 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
261 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
262 LOGGER.debug(MODIFIER_ID_IS, userId);
263 componentSubstitutionFilterBusinessLogic.validateUser(userId);
265 final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
266 NodeFilterConstraintType.parse(constraintType);
267 if (!nodeFilterConstraintType.isPresent()) {
268 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
269 INVALID_CONSTRAINTYPE_ENUM, constraintType));
273 final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic
274 .deleteSubstitutionFilter(componentId.toLowerCase(), index, true, ComponentTypeEnum.findByParamName(componentType));
276 if (!actionResponse.isPresent()) {
277 LOGGER.debug(FAILED_TO_DELETE_SUBSTITUTION_FILTER);
278 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
281 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
282 new SubstitutionFilterConverter().convertToUi(actionResponse.get()));
284 } catch (final Exception e) {
285 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(SUBSTITUTION_FILTER_DELETE);
286 LOGGER.debug(DELETE_SUBSTITUTION_FILTER_WITH_AN_ERROR, e);
287 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));