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.Optional;
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.POST;
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.apache.commons.lang3.StringUtils;
45 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
48 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
49 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
50 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
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.CINodeFilterDataDefinition;
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.NodeFilterConverter;
61 import org.openecomp.sdc.be.ui.model.UIConstraint;
62 import org.openecomp.sdc.be.user.UserBusinessLogic;
63 import org.openecomp.sdc.common.api.Constants;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 @Path("/v1/catalog/{componentType}/{componentId}/componentInstance/{componentInstanceId}/{constraintType}")
68 @Tags({@Tag(name = "SDCE-2 APIs")})
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
72 public class ComponentNodeFilterServlet extends AbstractValidationsServlet {
74 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentNodeFilterServlet.class);
75 private static final String START_HANDLE_REQUEST_OF = "Start handle {} request of {}";
76 private static final String MODIFIER_ID_IS = "modifier id is {}";
77 private static final String FAILED_TO_PARSE_COMPONENT = "failed to parse component";
78 private static final String FAILED_TO_CREATE_NODE_FILTER = "failed to create node filter";
79 private static final String NODE_FILTER_CREATION = "Node Filter Creation";
80 private static final String CREATE_NODE_FILTER_WITH_AN_ERROR = "create node filter with an error";
81 private static final String FAILED_TO_UPDATE_NODE_FILTER = "failed to update node filter";
82 private static final String NODE_FILTER_UPDATE = "Node Filter Update";
83 private static final String UPDATE_NODE_FILTER_WITH_AN_ERROR = "update node filter with an error";
84 private static final String FAILED_TO_DELETE_NODE_FILTER = "failed to delete node filter";
85 private static final String NODE_FILTER_DELETE = "Node Filter Delete";
86 private static final String DELETE_NODE_FILTER_WITH_AN_ERROR = "delete node filter with an error";
87 private static final String INVALID_NODE_FILTER_CONSTRAINT_TYPE = "Invalid value for NodeFilterConstraintType enum {}";
88 private final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic;
91 public ComponentNodeFilterServlet(final UserBusinessLogic userBusinessLogic, final ComponentInstanceBusinessLogic componentInstanceBL,
92 final ComponentsUtils componentsUtils, final ServletUtils servletUtils,
93 final ResourceImportManager resourceImportManager,
94 final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic) {
95 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
96 this.componentNodeFilterBusinessLogic = componentNodeFilterBusinessLogic;
100 @Consumes(MediaType.APPLICATION_JSON)
101 @Produces(MediaType.APPLICATION_JSON)
103 @Operation(description = "Add Component Filter Constraint", method = "POST", summary = "Add Component Filter Constraint", responses = {
104 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
105 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
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 addComponentFilterConstraint(@Parameter(description = "UIConstraint data", required = true) String constraintData,
110 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
111 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
112 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
113 ComponentTypeEnum.RESOURCE_PARAM_NAME,
114 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
115 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
116 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
117 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
118 @Context final HttpServletRequest request,
119 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
120 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
121 LOGGER.debug(MODIFIER_ID_IS, userId);
122 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
123 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
125 final Optional<UIConstraint> convertResponse = componentsUtils.parseToConstraint(constraintData, userModifier, componentTypeEnum);
126 if (convertResponse.isEmpty()) {
127 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
128 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
130 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
131 if (nodeFilterConstraintType.isEmpty()) {
132 return buildErrorResponse(
133 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
135 final UIConstraint uiConstraint = convertResponse.get();
136 final String constraint = new ConstraintConvertor().convert(uiConstraint);
137 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
138 .addNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.ADD, uiConstraint.getServicePropertyName(),
139 constraint, true, componentTypeEnum, nodeFilterConstraintType.get(),
140 StringUtils.isEmpty(uiConstraint.getCapabilityName()) ? "" : uiConstraint.getCapabilityName());
141 if (actionResponse.isEmpty()) {
142 LOGGER.error(FAILED_TO_CREATE_NODE_FILTER);
143 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
145 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
146 new NodeFilterConverter().convertToUi(actionResponse.get()));
147 } catch (final Exception e) {
148 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_CREATION);
149 LOGGER.error(CREATE_NODE_FILTER_WITH_AN_ERROR, e);
150 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
155 @Consumes(MediaType.APPLICATION_JSON)
156 @Produces(MediaType.APPLICATION_JSON)
157 @Path("/{constraintIndex}/nodeFilter")
158 @Operation(description = "Update Component Filter Constraint", method = "PUT", summary = "Update Component Filter Constraint", responses = {
159 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
160 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
161 @ApiResponse(responseCode = "403", description = "Restricted operation"),
162 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
163 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
164 public Response updateComponentFilterConstraint(@Parameter(description = "UIConstraint data", required = true) String constraintData,
165 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
166 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
167 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
168 ComponentTypeEnum.RESOURCE_PARAM_NAME,
169 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
170 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
171 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
172 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
173 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
174 @Context final HttpServletRequest request,
175 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
176 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
177 LOGGER.debug(MODIFIER_ID_IS, userId);
178 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
180 final Optional<NodeFilterConstraintType> nodeFilterConstraintTypeOptional = NodeFilterConstraintType.parse(constraintType);
181 if (nodeFilterConstraintTypeOptional.isEmpty()) {
182 return buildErrorResponse(
183 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
185 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
186 final Optional<UIConstraint> convertResponse = componentsUtils.parseToConstraint(constraintData, userModifier, componentTypeEnum);
187 if (convertResponse.isEmpty()) {
188 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
189 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
191 final NodeFilterConstraintType nodeFilterConstraintType = nodeFilterConstraintTypeOptional.get();
192 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
193 .updateNodeFilter(componentId.toLowerCase(), componentInstanceId, convertResponse.get(), componentTypeEnum, nodeFilterConstraintType,
195 if (actionResponse.isEmpty()) {
196 LOGGER.error(FAILED_TO_UPDATE_NODE_FILTER);
197 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
199 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
200 new NodeFilterConverter().convertToUi(actionResponse.get()));
201 } catch (final Exception e) {
202 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_UPDATE);
203 LOGGER.error(UPDATE_NODE_FILTER_WITH_AN_ERROR, e);
204 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
209 @Consumes(MediaType.APPLICATION_JSON)
210 @Produces(MediaType.APPLICATION_JSON)
211 @Path("{constraintIndex}/nodeFilter")
212 @Operation(description = "Delete Component Filter Constraint", method = "Delete", summary = "Delete Component Filter Constraint", responses = {
213 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
214 @ApiResponse(responseCode = "201", description = "Delete Component Filter Constraint"),
215 @ApiResponse(responseCode = "403", description = "Restricted operation"),
216 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
217 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
218 public Response deleteComponentFilterConstraint(@Parameter(description = "Component Id") @PathParam("componentId") String componentId,
219 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
220 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
221 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
222 ComponentTypeEnum.RESOURCE_PARAM_NAME,
223 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
224 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
225 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
226 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
227 @Context final HttpServletRequest request,
228 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
229 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
230 LOGGER.debug(MODIFIER_ID_IS, userId);
231 componentNodeFilterBusinessLogic.validateUser(userId);
233 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
234 if (nodeFilterConstraintType.isEmpty()) {
235 return buildErrorResponse(
236 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
238 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
239 .deleteNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.DELETE, null, index, true,
240 ComponentTypeEnum.findByParamName(componentType), nodeFilterConstraintType.get());
241 if (actionResponse.isEmpty()) {
242 LOGGER.debug(FAILED_TO_DELETE_NODE_FILTER);
243 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
245 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
246 new NodeFilterConverter().convertToUi(actionResponse.get()));
247 } catch (final Exception e) {
248 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_DELETE);
249 LOGGER.debug(DELETE_NODE_FILTER_WITH_AN_ERROR, e);
250 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));