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 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.lang3.StringUtils;
44 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
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.components.impl.exceptions.BusinessLogicException;
50 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
51 import org.openecomp.sdc.be.config.BeEcompErrorManager;
52 import org.openecomp.sdc.be.dao.api.ActionStatus;
53 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
56 import org.openecomp.sdc.be.impl.ComponentsUtils;
57 import org.openecomp.sdc.be.impl.ServletUtils;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.model.dto.FilterConstraintDto;
60 import org.openecomp.sdc.be.tosca.utils.NodeFilterConverter;
61 import org.openecomp.sdc.be.ui.mapper.FilterConstraintMapper;
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;
69 @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 {}";
78 private static final String FAILED_TO_PARSE_COMPONENT = "failed to parse component";
79 private static final String FAILED_TO_CREATE_NODE_FILTER = "failed to create node filter";
80 private static final String NODE_FILTER_CREATION = "Node Filter Creation";
81 private static final String CREATE_NODE_FILTER_WITH_AN_ERROR = "create node filter with an error";
82 private static final String FAILED_TO_UPDATE_NODE_FILTER = "failed to update node filter";
83 private static final String NODE_FILTER_UPDATE = "Node Filter Update";
84 private static final String UPDATE_NODE_FILTER_WITH_AN_ERROR = "update node filter with an error";
85 private static final String FAILED_TO_DELETE_NODE_FILTER = "failed to delete node filter";
86 private static final String NODE_FILTER_DELETE = "Node Filter Delete";
87 private static final String DELETE_NODE_FILTER_WITH_AN_ERROR = "delete node filter with an error";
88 private static final String INVALID_NODE_FILTER_CONSTRAINT_TYPE = "Invalid value for NodeFilterConstraintType enum {}";
89 private final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic;
92 public ComponentNodeFilterServlet(final UserBusinessLogic userBusinessLogic, final ComponentInstanceBusinessLogic componentInstanceBL,
93 final ComponentsUtils componentsUtils, final ServletUtils servletUtils,
94 final ResourceImportManager resourceImportManager,
95 final ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic) {
96 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
97 this.componentNodeFilterBusinessLogic = componentNodeFilterBusinessLogic;
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 @Path("/{componentType}/{componentId}/componentInstance/{componentInstanceId}/{constraintType}/nodeFilter")
104 @Operation(description = "Add Component Filter Constraint", method = "POST", summary = "Add Component Filter Constraint", responses = {
105 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
106 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
107 @ApiResponse(responseCode = "403", description = "Restricted operation"),
108 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
109 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
110 public Response addComponentFilterConstraint(@Parameter(description = "UIConstraint data", required = true) String constraintData,
111 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
112 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
113 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
114 ComponentTypeEnum.RESOURCE_PARAM_NAME,
115 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
116 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
117 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
118 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
119 @Context final HttpServletRequest request,
120 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
121 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
122 LOGGER.debug(MODIFIER_ID_IS, userId);
123 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
124 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
126 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
127 if (nodeFilterConstraintType.isEmpty()) {
128 return buildErrorResponse(
129 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
131 final UIConstraint uiConstraint = componentsUtils.parseToConstraint(constraintData, userModifier, componentTypeEnum).orElse(null);
132 if (uiConstraint == null) {
133 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
134 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
136 final FilterConstraintDto filterConstraintDto = new FilterConstraintMapper().mapFrom(uiConstraint);
137 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
138 .addNodeFilter(componentId.toLowerCase(), componentInstanceId,
139 filterConstraintDto, 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 ComponentException e) {
149 } catch (final BusinessLogicException e) {
150 return buildErrorResponse(e.getResponseFormat());
151 } catch (final Exception e) {
152 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_CREATION);
153 LOGGER.error(CREATE_NODE_FILTER_WITH_AN_ERROR, e);
154 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
159 @Consumes(MediaType.APPLICATION_JSON)
160 @Produces(MediaType.APPLICATION_JSON)
161 @Path("/{componentType}/{componentId}/componentInstance/{componentInstanceId}/{constraintType}/{constraintIndex}/nodeFilter")
162 @Operation(description = "Update Component Filter Constraint", method = "PUT", summary = "Update Component Filter Constraint", responses = {
163 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
164 @ApiResponse(responseCode = "201", description = "Create Component Filter"),
165 @ApiResponse(responseCode = "403", description = "Restricted operation"),
166 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
167 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
168 public Response updateComponentFilterConstraint(@Parameter(description = "UIConstraint data", required = true) String constraintData,
169 @Parameter(description = "Component Id") @PathParam("componentId") String componentId,
170 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
171 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
172 ComponentTypeEnum.RESOURCE_PARAM_NAME,
173 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
174 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
175 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
176 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
177 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
178 @Context final HttpServletRequest request,
179 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
180 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
181 LOGGER.debug(MODIFIER_ID_IS, userId);
182 final User userModifier = componentNodeFilterBusinessLogic.validateUser(userId);
184 final Optional<NodeFilterConstraintType> nodeFilterConstraintTypeOptional = NodeFilterConstraintType.parse(constraintType);
185 if (nodeFilterConstraintTypeOptional.isEmpty()) {
186 return buildErrorResponse(
187 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
189 final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
190 final Optional<UIConstraint> convertResponse = componentsUtils.parseToConstraint(constraintData, userModifier, componentTypeEnum);
191 if (convertResponse.isEmpty()) {
192 LOGGER.error(FAILED_TO_PARSE_COMPONENT);
193 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
195 final NodeFilterConstraintType nodeFilterConstraintType = nodeFilterConstraintTypeOptional.get();
196 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
197 .updateNodeFilter(componentId.toLowerCase(), componentInstanceId, convertResponse.get(), componentTypeEnum, nodeFilterConstraintType,
199 if (actionResponse.isEmpty()) {
200 LOGGER.error(FAILED_TO_UPDATE_NODE_FILTER);
201 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
203 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
204 new NodeFilterConverter().convertToUi(actionResponse.get()));
205 } catch (final Exception e) {
206 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_UPDATE);
207 LOGGER.error(UPDATE_NODE_FILTER_WITH_AN_ERROR, e);
208 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
213 @Consumes(MediaType.APPLICATION_JSON)
214 @Produces(MediaType.APPLICATION_JSON)
215 @Path("/{componentType}/{componentId}/componentInstance/{componentInstanceId}/{constraintType}/{constraintIndex}/nodeFilter")
216 @Operation(description = "Delete Component Filter Constraint", method = "Delete", summary = "Delete Component Filter Constraint", responses = {
217 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
218 @ApiResponse(responseCode = "201", description = "Delete Component Filter Constraint"),
219 @ApiResponse(responseCode = "403", description = "Restricted operation"),
220 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
221 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
222 public Response deleteComponentFilterConstraint(@Parameter(description = "Component Id") @PathParam("componentId") String componentId,
223 @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId,
224 @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index,
225 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
226 ComponentTypeEnum.RESOURCE_PARAM_NAME,
227 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType,
228 @Parameter(description = "Constraint type. Valid values: properties / capabilities", schema = @Schema(allowableValues = {
229 NodeFilterConstraintType.PROPERTIES_PARAM_NAME,
230 NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) @PathParam("constraintType") final String constraintType,
231 @Context final HttpServletRequest request,
232 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
233 LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI());
234 LOGGER.debug(MODIFIER_ID_IS, userId);
235 componentNodeFilterBusinessLogic.validateUser(userId);
237 final Optional<NodeFilterConstraintType> nodeFilterConstraintType = NodeFilterConstraintType.parse(constraintType);
238 if (nodeFilterConstraintType.isEmpty()) {
239 return buildErrorResponse(
240 getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, INVALID_NODE_FILTER_CONSTRAINT_TYPE, constraintType));
242 final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
243 .deleteNodeFilter(componentId.toLowerCase(), componentInstanceId, index, true,
244 ComponentTypeEnum.findByParamName(componentType), nodeFilterConstraintType.get());
245 if (actionResponse.isEmpty()) {
246 LOGGER.debug(FAILED_TO_DELETE_NODE_FILTER);
247 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
249 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
250 new NodeFilterConverter().convertToUi(actionResponse.get()));
251 } catch (final Exception e) {
252 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_DELETE);
253 LOGGER.debug(DELETE_NODE_FILTER_WITH_AN_ERROR, e);
254 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));