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.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.lang.StringUtils;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
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.components.impl.utils.NodeFilterConstraintAction;
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.CINodeFilterDataDefinition;
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.NodeFilterConverter;
62 import org.openecomp.sdc.be.ui.model.UIConstraint;
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}/componentInstance/{componentInstanceId}/{constraintType}")
69 @Tags({@Tag(name = "SDCE-2 APIs")})
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
73 public class ComponentNodeFilterServlet extends AbstractValidationsServlet {
75 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentNodeFilterServlet.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 {}";
79 private static final String FAILED_TO_PARSE_COMPONENT = "failed to parse component";
81 private static final String FAILED_TO_CREATE_NODE_FILTER = "failed to create node filter";
82 private static final String NODE_FILTER_CREATION = "Node Filter Creation";
83 private static final String CREATE_NODE_FILTER_WITH_AN_ERROR = "create node filter with an error";
85 private static final String FAILED_TO_UPDATE_NODE_FILTER = "failed to update node filter";
86 private static final String NODE_FILTER_UPDATE = "Node Filter Update";
87 private static final String UPDATE_NODE_FILTER_WITH_AN_ERROR = "update node filter with an error";
89 private static final String FAILED_TO_DELETE_NODE_FILTER = "failed to delete node filter";
90 private static final String NODE_FILTER_DELETE = "Node Filter Delete";
91 private static final String DELETE_NODE_FILTER_WITH_AN_ERROR = "delete node filter with an error";
93 private static final String INVALID_NODE_FILTER_CONSTRAINT_TYPE = "Invalid value for NodeFilterConstraintType enum {}";
95 private final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic;
98 public ComponentNodeFilterServlet(final UserBusinessLogic userBusinessLogic,
99 final ComponentInstanceBusinessLogic componentInstanceBL,
100 final ComponentsUtils componentsUtils,
101 final ServletUtils servletUtils,
102 final ResourceImportManager resourceImportManager,
103 final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic) {
104 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
105 this.componentNodeFilterBusinessLogic = componentNodeFilterBusinessLogic;
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
112 @Operation(description = "Add Component Filter Constraint", method = "POST",
113 summary = "Add Component Filter Constraint", responses = {
114 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
115 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
116 @ApiResponse(responseCode = "403", description = "Restricted operation"),
117 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
118 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
119 public Response addComponentFilterConstraint(
120 @Parameter(description = "UIConstraint data", required = true) String constraintData,
121 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
122 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
123 @Parameter(description = "valid values: resources / services",
124 schema = @Schema(allowableValues = {
125 ComponentTypeEnum.RESOURCE_PARAM_NAME,
126 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
127 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
128 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
129 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
130 @PathParam("constraintType") final String constraintType,
131 @Context final HttpServletRequest request,
132 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
134 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
135 LOGGER.debug(MODIFIER_ID_IS, userId);
136 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
138 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
140 final Optional<UIConstraint> convertResponse = componentsUtils
141 .parseToConstraint(constraintData, userModifier, componentTypeEnum);
142 if (convertResponse.isEmpty()) {
143 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
144 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
146 final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
147 NodeFilterConstraintType.parse(constraintType);
148 if (nodeFilterConstraintType.isEmpty()) {
149 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
150 INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
152 final UIConstraint uiConstraint = convertResponse.get();
153 final String constraint = new ConstraintConvertor().convert(uiConstraint);
154 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
155 .addNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.ADD,
156 uiConstraint.getServicePropertyName(), constraint, true, componentTypeEnum,
157 nodeFilterConstraintType.get(),
158 StringUtils.isEmpty(uiConstraint.getCapabilityName()) ? "" : uiConstraint.getCapabilityName());
160 if (actionResponse.isEmpty()) {
161 LOGGER.error(FAILED_TO_CREATE_NODE_FILTER);
162 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
165 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
166 new NodeFilterConverter().convertToUi(actionResponse.get()));
168 } catch (final Exception e) {
169 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_CREATION);
170 LOGGER.error(CREATE_NODE_FILTER_WITH_AN_ERROR, e);
171 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
176 @Consumes(MediaType.APPLICATION_JSON)
177 @Produces(MediaType.APPLICATION_JSON)
178 @Path("/{constraintIndex}/nodeFilter")
179 @Operation(description = "Update Component Filter Constraint", method = "PUT",
180 summary = "Update Component Filter Constraint", responses = {
181 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
182 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
183 @ApiResponse(responseCode = "403", description = "Restricted operation"),
184 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
185 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
186 public Response updateComponentFilterConstraint(
187 @Parameter(description = "UIConstraint data", required = true) String constraintData,
188 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
189 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
190 @Parameter(description = "valid values: resources / services",
191 schema = @Schema(allowableValues = {
192 ComponentTypeEnum.RESOURCE_PARAM_NAME,
193 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
194 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
195 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
196 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
197 @PathParam("constraintType") final String constraintType,
198 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
199 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
201 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
202 LOGGER.debug(MODIFIER_ID_IS, userId);
203 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
206 final Optional<NodeFilterConstraintType> nodeFilterConstraintTypeOptional =
207 NodeFilterConstraintType.parse(constraintType);
208 if (nodeFilterConstraintTypeOptional.isEmpty()) {
209 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
210 INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
213 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
214 final Optional<UIConstraint> convertResponse = componentsUtils
215 .parseToConstraint(constraintData, userModifier, componentTypeEnum);
216 if (convertResponse.isEmpty()) {
217 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
218 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
220 final NodeFilterConstraintType nodeFilterConstraintType = nodeFilterConstraintTypeOptional.get();
221 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
222 .updateNodeFilter(componentId.toLowerCase(), componentInstanceId, convertResponse.get(),
223 componentTypeEnum, nodeFilterConstraintType, index);
225 if (actionResponse.isEmpty()) {
226 LOGGER.error(FAILED_TO_UPDATE_NODE_FILTER);
227 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
230 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
231 new NodeFilterConverter().convertToUi(actionResponse.get()));
233 } catch (final Exception e) {
234 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_UPDATE);
235 LOGGER.error(UPDATE_NODE_FILTER_WITH_AN_ERROR, e);
236 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
241 @Consumes(MediaType.APPLICATION_JSON)
242 @Produces(MediaType.APPLICATION_JSON)
243 @Path("{constraintIndex}/nodeFilter")
244 @Operation(description = "Delete Component Filter Constraint", method = "Delete",
245 summary = "Delete Component Filter Constraint", responses = {
246 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
247 @ApiResponse(responseCode = "201", description = "Delete Component Filter Constraint"),
248 @ApiResponse(responseCode = "403", description = "Restricted operation"),
249 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
250 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
251 public Response deleteComponentFilterConstraint(
252 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
253 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
254 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
255 @Parameter(description = "valid values: resources / services",
256 schema = @Schema(allowableValues = {
257 ComponentTypeEnum.RESOURCE_PARAM_NAME,
258 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
259 @Parameter(description = "Constraint type. Valid values: properties / capabilities",
260 schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
261 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME}))
262 @PathParam("constraintType") final String constraintType,
263 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
265 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
266 LOGGER.debug(MODIFIER_ID_IS, userId);
267 componentNodeFilterBusinessLogic.validateUser(userId);
270 final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
271 NodeFilterConstraintType.parse(constraintType);
272 if (nodeFilterConstraintType.isEmpty()) {
273 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
274 INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
276 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
277 .deleteNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.DELETE,
278 null, index, true, ComponentTypeEnum.findByParamName(componentType),
279 nodeFilterConstraintType.get());
281 if (actionResponse.isEmpty()) {
282 LOGGER.debug(FAILED_TO_DELETE_NODE_FILTER);
283 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
286 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
287 new NodeFilterConverter().convertToUi(actionResponse.get()));
289 } catch (final Exception e) {
290 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_DELETE);
291 LOGGER.debug(DELETE_NODE_FILTER_WITH_AN_ERROR, e);
292 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));