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