2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import com.jcabi.aspects.Loggable;
23 import fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.servers.Servers;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.stream.Collectors;
38 import javax.inject.Inject;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.Consumes;
41 import javax.ws.rs.DELETE;
42 import javax.ws.rs.GET;
43 import javax.ws.rs.HeaderParam;
44 import javax.ws.rs.POST;
45 import javax.ws.rs.PUT;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.MediaType;
51 import javax.ws.rs.core.Response;
52 import org.apache.commons.lang3.StringUtils;
53 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
55 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
56 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
57 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
58 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
59 import org.openecomp.sdc.be.config.BeEcompErrorManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
62 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
63 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
64 import org.openecomp.sdc.be.datatypes.enums.DeclarationTypeEnum;
65 import org.openecomp.sdc.be.impl.ComponentsUtils;
66 import org.openecomp.sdc.be.impl.ServletUtils;
67 import org.openecomp.sdc.be.model.PolicyDefinition;
68 import org.openecomp.sdc.be.model.PolicyTargetDTO;
69 import org.openecomp.sdc.be.model.Resource;
70 import org.openecomp.sdc.be.user.UserBusinessLogic;
71 import org.openecomp.sdc.common.api.Constants;
72 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
73 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
74 import org.openecomp.sdc.common.log.enums.StatusCode;
75 import org.openecomp.sdc.common.log.wrappers.Logger;
76 import org.openecomp.sdc.exception.ResponseFormat;
77 import org.springframework.stereotype.Controller;
80 * Provides REST API to create, retrieve, update, delete a policy
82 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
84 @Tags({@Tag(name = "SDCE-2 APIs")})
85 @Servers({@Server(url = "/sdc2/rest")})
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 public class PolicyServlet extends AbstractValidationsServlet {
91 private static final Logger log = Logger.getLogger(PolicyServlet.class);
92 private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ServiceServlet.class.getName());
93 private final PolicyBusinessLogic policyBusinessLogic;
96 public PolicyServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
97 ServletUtils servletUtils, ResourceImportManager resourceImportManager, PolicyBusinessLogic policyBusinessLogic) {
98 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
99 this.policyBusinessLogic = policyBusinessLogic;
100 this.servletUtils = servletUtils;
101 this.resourceImportManager = resourceImportManager;
102 this.componentsUtils = componentsUtils;
106 @Path("/{containerComponentType}/{componentId}/policies/{policyTypeName}")
107 @Operation(description = "Create Policy", method = "POST", summary = "Returns created Policy", responses = {
108 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
109 @ApiResponse(responseCode = "201", description = "Policy created"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
110 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
111 @ApiResponse(responseCode = "409", description = "Policy already exist"),
112 @ApiResponse(responseCode = "404", description = "Component not found")})
113 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
114 public Response createPolicy(@PathParam("componentId") final String containerComponentId,
115 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
116 ComponentTypeEnum.RESOURCE_PARAM_NAME,
117 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
118 @PathParam("policyTypeName") final String policyTypeName,
119 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
120 @Context final HttpServletRequest request) {
123 .log(LoggerSupportabilityActions.CREATE_POLICIES, StatusCode.STARTED, "Starting to create Policy by user {} containerComponentId={}",
124 userId, containerComponentId);
125 ComponentTypeEnum componentType = validateComponentTypeAndUserId(containerComponentType, userId);
126 PolicyDefinition policy = policyBusinessLogic.createPolicy(componentType, containerComponentId, policyTypeName, userId, true);
128 .log(LoggerSupportabilityActions.CREATE_POLICIES, StatusCode.COMPLETE, "Ended create Policy by user {} containerComponentId={}", userId,
129 containerComponentId);
130 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), policy);
134 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
135 @Operation(description = "Update Policy metadata", method = "PUT", summary = "Returns updated Policy", responses = {
136 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
137 @ApiResponse(responseCode = "200", description = "Policy updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
138 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
139 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
140 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
141 public Response updatePolicy(@PathParam("componentId") final String containerComponentId,
142 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
143 ComponentTypeEnum.RESOURCE_PARAM_NAME,
144 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
145 @PathParam("policyId") final String policyId,
146 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
147 @Parameter(description = "PolicyDefinition", required = true) String policyData,
148 @Context final HttpServletRequest request) {
151 .log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.STARTED, "Starting to update Policy by user {} containerComponentId={}",
152 userId, containerComponentId);
153 PolicyDefinition policyDefinition = convertJsonToObjectOfClass(policyData, PolicyDefinition.class);
154 policyDefinition.setUniqueId(policyId);
155 policyDefinition = policyBusinessLogic
156 .updatePolicy(validateComponentTypeAndUserId(containerComponentType, userId), containerComponentId, policyDefinition, userId, true);
158 .log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.COMPLETE, "Ended update Policy by user {} containerComponentId={}",
159 userId, containerComponentId);
160 return buildOkResponse(policyDefinition);
164 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
165 @Operation(description = "Get Policy", method = "GET", summary = "Returns Policy", responses = {
166 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
167 @ApiResponse(responseCode = "200", description = "Policy was found"),
168 @ApiResponse(responseCode = "403", description = "Restricted operation"),
169 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
170 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
171 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
172 public Response getPolicy(@PathParam("componentId") final String containerComponentId,
173 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
174 ComponentTypeEnum.RESOURCE_PARAM_NAME,
175 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
176 @PathParam("policyId") final String policyId,
177 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
178 @Context final HttpServletRequest request) {
180 PolicyDefinition policy = policyBusinessLogic
181 .getPolicy(validateComponentTypeAndUserId(containerComponentType, userId), containerComponentId, policyId, userId);
182 return buildOkResponse(policy);
186 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
187 @Operation(description = "Delete Policy", method = "DELETE", summary = "No body", responses = {
188 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
189 @ApiResponse(responseCode = "204", description = "Policy was deleted"),
190 @ApiResponse(responseCode = "403", description = "Restricted operation"),
191 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
192 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
193 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
194 public Response deletePolicy(@PathParam("componentId") final String containerComponentId,
195 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
196 ComponentTypeEnum.RESOURCE_PARAM_NAME,
197 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
198 @PathParam("policyId") final String policyId,
199 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
200 @Context final HttpServletRequest request) {
202 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
203 PolicyDefinition policyDefinition = policyBusinessLogic.deletePolicy(componentTypeEnum, containerComponentId, policyId, userId, true);
204 return buildOkResponse(policyDefinition);
208 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/undeclare")
209 @Operation(description = "undeclare Policy", method = "PUT", summary = "No body", responses = {
210 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
211 @ApiResponse(responseCode = "204", description = "Policy was undeclared"),
212 @ApiResponse(responseCode = "403", description = "Restricted operation"),
213 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
214 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
215 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
216 public Response undeclarePolicy(@PathParam("componentId") final String containerComponentId,
217 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
218 ComponentTypeEnum.RESOURCE_PARAM_NAME,
219 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
220 @PathParam("policyId") final String policyId,
221 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
222 @Context final HttpServletRequest request) {
224 Response response = null;
226 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
227 Either<PolicyDefinition, ResponseFormat> undeclarePolicy = policyBusinessLogic
228 .undeclarePolicy(componentTypeEnum, containerComponentId, policyId, userId, true);
229 if (undeclarePolicy.isLeft()) {
230 response = buildOkResponse(undeclarePolicy.left().value());
232 response = buildErrorResponse(undeclarePolicy.right().value());
234 } catch (Exception e) {
235 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Undeclare Policy");
236 log.error("Failed to undeclare policy. The exception {} occurred. ", e);
242 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/properties")
243 @Operation(description = "Get component policy properties", method = "GET", summary = "Returns component policy properties", responses = {
244 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = PropertyDataDefinition.class)))),
245 @ApiResponse(responseCode = "200", description = "Properties found"),
246 @ApiResponse(responseCode = "400", description = "invalid content - Error: containerComponentType is invalid"),
247 @ApiResponse(responseCode = "403", description = "Restricted operation"),
248 @ApiResponse(responseCode = "404", description = "Componentorpolicy not found"),
249 @ApiResponse(responseCode = "500", description = "The GET request failed due to internal SDC problem.")})
250 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
251 public Response getPolicyProperties(
252 @Parameter(description = "the id of the component which is the container of the policy") @PathParam("componentId") final String containerComponentId,
253 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME,
254 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
255 @Parameter(description = "the id of the policy which its properties are to return") @PathParam("policyId") final String policyId,
256 @Parameter(description = "the userid", required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
257 @Context final HttpServletRequest request) {
259 List<PropertyDataDefinition> propertyDataDefinitionList = policyBusinessLogic
260 .getPolicyProperties(convertToComponentType(containerComponentType), containerComponentId, policyId, userId);
261 return buildOkResponse(propertyDataDefinitionList);
265 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/properties")
266 @Operation(description = "Update Policy properties", method = "PUT", summary = "Returns updated Policy", responses = {
267 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
268 @ApiResponse(responseCode = "200", description = "Policy properties updated"),
269 @ApiResponse(responseCode = "403", description = "Restricted operation"),
270 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
271 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
272 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
273 public Response updatePolicyProperties(@PathParam("componentId") final String containerComponentId,
274 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
275 ComponentTypeEnum.RESOURCE_PARAM_NAME,
276 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
277 @PathParam("policyId") final String policyId,
278 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
279 @Parameter(description = "PolicyDefinition", required = true) String policyData,
280 @Context final HttpServletRequest request) {
282 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICIES_PROPERTIES, StatusCode.STARTED,
283 "Starting to update Policy Properties by user {} containerComponentId={}", userId, containerComponentId);
284 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
285 PropertyDataDefinition[] propertyDataDefinitions = convertJsonToObjectOfClass(policyData, PropertyDataDefinition[].class);
286 List<PropertyDataDefinition> propertyDataDefinitionList = policyBusinessLogic
287 .updatePolicyProperties(componentTypeEnum, containerComponentId, policyId, propertyDataDefinitions, userId, true);
288 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICIES_PROPERTIES, StatusCode.STARTED,
289 "Starting to update Policy Properties by user {} containerComponentId={}", userId, containerComponentId);
290 return buildOkResponse(propertyDataDefinitionList);
293 private ComponentTypeEnum validateComponentTypeAndUserId(final String containerComponentType, String userId) {
294 if (StringUtils.isEmpty(userId)) {
295 log.error("Missing userId HTTP header. ");
296 throw new ByActionStatusComponentException(ActionStatus.MISSING_USER_ID);
298 return validateComponentType(containerComponentType);
302 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/targets")
303 @Consumes(MediaType.APPLICATION_JSON)
304 @Produces(MediaType.APPLICATION_JSON)
305 @Operation(description = "update policy targets", method = "POST", summary = "Returns updated Policy", responses = {
306 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
307 @ApiResponse(responseCode = "201", description = "Policy target updated"),
308 @ApiResponse(responseCode = "403", description = "Restricted operation"),
309 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
310 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
311 public Response updatePolicyTargets(@PathParam("componentId") final String containerComponentId,
312 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
313 ComponentTypeEnum.RESOURCE_PARAM_NAME,
314 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
315 @PathParam("policyId") final String policyId,
316 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
317 @Context final HttpServletRequest request, List<PolicyTargetDTO> requestJson) {
318 Map<PolicyTargetType, List<String>> policyTargetTypeListMap = updatePolicyTargetsFromDTO(requestJson);
319 PolicyDefinition policyDefinition = updatePolicyTargetsFromMap(policyTargetTypeListMap, containerComponentType, containerComponentId,
321 return buildOkResponse(policyDefinition);
325 @Path("/{componentType}/{componentId}/create/policies")
326 @Operation(description = "Create policies on service", method = "POST", summary = "Return policies list", responses = {
327 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
328 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
329 @ApiResponse(responseCode = "404", description = "Component not found")})
330 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
331 public Response declareProperties(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
332 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
333 @Parameter(description = "ComponentIns policies Object to be created", required = true) String componentInstPoliciesMapObj) {
334 return super.declareProperties(userId, componentId, componentType, componentInstPoliciesMapObj, DeclarationTypeEnum.POLICY, request);
337 private PolicyDefinition updatePolicyTargetsFromMap(Map<PolicyTargetType, List<String>> policyTarget, String containerComponentType,
338 String containerComponentId, String policyId, String userId) {
339 ComponentTypeEnum componentTypeEnum = convertToComponentType(containerComponentType);
340 return policyBusinessLogic.updatePolicyTargets(componentTypeEnum, containerComponentId, policyId, policyTarget, userId);
343 private Map<PolicyTargetType, List<String>> updatePolicyTargetsFromDTO(List<PolicyTargetDTO> targetDTOList) {
344 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.STARTED, "Starting to update Policy target");
345 Map<PolicyTargetType, List<String>> policyTarget = new HashMap<>();
346 for (PolicyTargetDTO currentTarget : targetDTOList) {
347 if (!addTargetsByType(policyTarget, currentTarget.getType(), currentTarget.getUniqueIds())) {
348 throw new ByActionStatusComponentException(ActionStatus.POLICY_TARGET_TYPE_DOES_NOT_EXIST, currentTarget.getType());
351 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.COMPLETE, "Ended update Policy target");
355 public boolean addTargetsByType(Map<PolicyTargetType, List<String>> policyTarget, String type, List<String> uniqueIds) {
356 PolicyTargetType targetTypeEnum = PolicyTargetType.getByNameIgnoreCase(type);
357 if (targetTypeEnum != null) {
358 policyTarget.put(targetTypeEnum, validateUniquenessOfIds(uniqueIds));
365 private List<String> validateUniquenessOfIds(List<String> uniqueIds) {
366 return uniqueIds.stream().distinct().collect(Collectors.toList());