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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.servers.Servers;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import io.swagger.v3.oas.annotations.tags.Tags;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
39 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
40 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
41 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
47 import org.openecomp.sdc.be.datatypes.enums.DeclarationTypeEnum;
48 import org.openecomp.sdc.be.impl.ComponentsUtils;
49 import org.openecomp.sdc.be.impl.ServletUtils;
50 import org.openecomp.sdc.be.model.PolicyDefinition;
51 import org.openecomp.sdc.be.model.PolicyTargetDTO;
52 import org.openecomp.sdc.be.model.Resource;
53 import org.openecomp.sdc.be.user.UserBusinessLogic;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
56 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
57 import org.openecomp.sdc.common.log.enums.StatusCode;
58 import org.openecomp.sdc.common.log.wrappers.Logger;
59 import org.openecomp.sdc.exception.ResponseFormat;
60 import org.springframework.stereotype.Controller;
62 import javax.inject.Inject;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.ws.rs.Consumes;
65 import javax.ws.rs.DELETE;
66 import javax.ws.rs.GET;
67 import javax.ws.rs.HeaderParam;
68 import javax.ws.rs.POST;
69 import javax.ws.rs.PUT;
70 import javax.ws.rs.Path;
71 import javax.ws.rs.PathParam;
72 import javax.ws.rs.Produces;
73 import javax.ws.rs.core.Context;
74 import javax.ws.rs.core.MediaType;
75 import javax.ws.rs.core.Response;
76 import java.util.HashMap;
77 import java.util.List;
79 import java.util.stream.Collectors;
81 * Provides REST API to create, retrieve, update, delete a policy
83 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
85 @Tags({@Tag(name = "SDC Internal APIs")})
86 @Servers({@Server(url = "/sdc2/rest")})
88 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces(MediaType.APPLICATION_JSON)
90 public class PolicyServlet extends AbstractValidationsServlet {
92 private static final Logger log = Logger.getLogger(PolicyServlet.class);
93 private final PolicyBusinessLogic policyBusinessLogic;
94 private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ServiceServlet.class.getName());
97 public PolicyServlet(UserBusinessLogic userBusinessLogic,
98 ComponentInstanceBusinessLogic componentInstanceBL,
99 ComponentsUtils componentsUtils, ServletUtils servletUtils,
100 ResourceImportManager resourceImportManager,
101 PolicyBusinessLogic policyBusinessLogic) {
102 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
103 this.policyBusinessLogic = policyBusinessLogic;
104 this.servletUtils = servletUtils;
105 this.resourceImportManager = resourceImportManager;
106 this.componentsUtils = componentsUtils;
110 @Path("/{containerComponentType}/{componentId}/policies/{policyTypeName}")
111 @Operation(description = "Create Policy", method = "POST", summary = "Returns created Policy", responses = {
112 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
113 @ApiResponse(responseCode = "201", description = "Policy created"),
114 @ApiResponse(responseCode = "403", description = "Restricted operation"),
115 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
116 @ApiResponse(responseCode = "409", description = "Policy already exist"),
117 @ApiResponse(responseCode = "404", description = "Component not found")})
118 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
119 public Response createPolicy(@PathParam("componentId") final String containerComponentId, @Parameter(description = "valid values: resources / services",
120 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
121 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
122 @PathParam("policyTypeName") final String policyTypeName,
123 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
124 required = true) String userId,
125 @Context final HttpServletRequest request) {
128 loggerSupportability.log(LoggerSupportabilityActions.CREATE_POLICIES, StatusCode.STARTED,"Starting to create Policy by user {} containerComponentId={}" , userId , containerComponentId );
129 ComponentTypeEnum componentType = validateComponentTypeAndUserId(containerComponentType, userId);
130 PolicyDefinition policy = policyBusinessLogic.createPolicy(componentType, containerComponentId, policyTypeName, userId, true);
131 loggerSupportability.log(LoggerSupportabilityActions.CREATE_POLICIES, StatusCode.COMPLETE,"Ended create Policy by user {} containerComponentId={}" , userId , containerComponentId);
132 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), policy);
136 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
137 @Operation(description = "Update Policy metadata", method = "PUT", summary = "Returns updated Policy", responses = {
138 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
139 @ApiResponse(responseCode = "200", description = "Policy updated"),
140 @ApiResponse(responseCode = "403", description = "Restricted operation"),
141 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
142 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
143 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
144 public Response updatePolicy(@PathParam("componentId") final String containerComponentId, @Parameter(
145 description = "valid values: resources / services",
146 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME,
147 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
148 @PathParam("policyId") final String policyId,
149 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
150 required = true) String userId,
151 @Parameter(description = "PolicyDefinition", required = true) String policyData,
152 @Context final HttpServletRequest request) {
155 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.STARTED,"Starting to update Policy by user {} containerComponentId={}" , userId , containerComponentId);
156 PolicyDefinition policyDefinition = convertJsonToObjectOfClass(policyData, PolicyDefinition.class);
157 policyDefinition.setUniqueId(policyId);
158 policyDefinition = policyBusinessLogic.updatePolicy(validateComponentTypeAndUserId(containerComponentType, userId), containerComponentId, policyDefinition, userId, true);
159 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.COMPLETE,"Ended update Policy by user {} containerComponentId={}" , userId , containerComponentId);
160 return buildOkResponse(policyDefinition);
165 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
166 @Operation(description = "Get Policy", method = "GET", summary = "Returns Policy", responses = {
167 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
168 @ApiResponse(responseCode = "200", description = "Policy was found"),
169 @ApiResponse(responseCode = "403", description = "Restricted operation"),
170 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
171 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
172 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
173 public Response getPolicy(@PathParam("componentId") final String containerComponentId, @Parameter(
174 description = "valid values: resources / services",
175 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
176 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
177 @PathParam("policyId") final String policyId,
178 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
179 required = true) String userId,
180 @Context final HttpServletRequest request) {
183 PolicyDefinition policy = policyBusinessLogic.getPolicy(validateComponentTypeAndUserId(containerComponentType,
184 userId), containerComponentId, policyId, userId);
185 return buildOkResponse(policy);
189 @Path("/{containerComponentType}/{componentId}/policies/{policyId}")
190 @Operation(description = "Delete Policy", method = "DELETE", summary = "No body", responses = {
191 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
192 @ApiResponse(responseCode = "204", description = "Policy was deleted"),
193 @ApiResponse(responseCode = "403", description = "Restricted operation"),
194 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
195 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
196 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
197 public Response deletePolicy(@PathParam("componentId") final String containerComponentId, @Parameter(
198 description = "valid values: resources / services",
199 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
200 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
201 @PathParam("policyId") final String policyId,
202 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
203 required = true) String userId,
204 @Context final HttpServletRequest request) {
207 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
208 PolicyDefinition policyDefinition = policyBusinessLogic.deletePolicy(componentTypeEnum, containerComponentId, policyId, userId, true);
209 return buildOkResponse(policyDefinition);
213 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/undeclare")
214 @Operation(description = "undeclare Policy", method = "PUT", summary = "No body", responses = {
215 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
216 @ApiResponse(responseCode = "204", description = "Policy was undeclared"),
217 @ApiResponse(responseCode = "403", description = "Restricted operation"),
218 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
219 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
220 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
221 public Response undeclarePolicy(@PathParam("componentId") final String containerComponentId, @Parameter(
222 description = "valid values: resources / services",
223 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
224 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
225 @PathParam("policyId") final String policyId,
226 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
227 required = true) String userId,
228 @Context final HttpServletRequest request) {
230 Response response = null;
232 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
233 Either<PolicyDefinition, ResponseFormat> undeclarePolicy = policyBusinessLogic.undeclarePolicy(componentTypeEnum, containerComponentId, policyId, userId, true);
234 if (undeclarePolicy.isLeft()){
235 response = buildOkResponse(undeclarePolicy.left().value());
237 response = buildErrorResponse(undeclarePolicy.right().value());
239 } catch (Exception e) {
240 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Undeclare Policy");
241 log.error("Failed to undeclare policy. The exception {} occurred. ", e);
247 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/properties")
248 @Operation(description = "Get component policy properties", method = "GET",
249 summary = "Returns component policy properties", responses = {@ApiResponse(
250 content = @Content(array = @ArraySchema(schema = @Schema(implementation = PropertyDataDefinition.class)))),
251 @ApiResponse(responseCode = "200", description = "Properties found"), @ApiResponse(responseCode = "400",
252 description = "invalid content - Error: containerComponentType is invalid"),
253 @ApiResponse(responseCode = "403", description = "Restricted operation"),
254 @ApiResponse(responseCode = "404", description = "Componentorpolicy not found"),
255 @ApiResponse(responseCode = "500", description = "The GET request failed due to internal SDC problem.")})
256 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
257 public Response getPolicyProperties(@Parameter(
258 description = "the id of the component which is the container of the policy") @PathParam("componentId") final String containerComponentId,
259 @Parameter(description = "valid values: resources / services",
260 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
261 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
263 description = "the id of the policy which its properties are to return") @PathParam("policyId") final String policyId,
264 @Parameter(description = "the userid",
265 required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
266 @Context final HttpServletRequest request) {
268 List<PropertyDataDefinition> propertyDataDefinitionList = policyBusinessLogic.getPolicyProperties(
269 convertToComponentType(containerComponentType), containerComponentId, policyId, userId);
270 return buildOkResponse(propertyDataDefinitionList);
274 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/properties")
275 @Operation(description = "Update Policy properties", method = "PUT", summary = "Returns updated Policy",
276 responses = {@ApiResponse(
277 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
278 @ApiResponse(responseCode = "200", description = "Policy properties updated"),
279 @ApiResponse(responseCode = "403", description = "Restricted operation"),
280 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
281 @ApiResponse(responseCode = "404", description = "component / policy Not found")})
282 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
283 public Response updatePolicyProperties(@PathParam("componentId") final String containerComponentId, @Parameter(
284 description = "valid values: resources / services",
285 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
286 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
287 @PathParam("policyId") final String policyId,
288 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
289 required = true) String userId,
290 @Parameter(description = "PolicyDefinition", required = true) String policyData,
291 @Context final HttpServletRequest request) {
293 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICIES_PROPERTIES, StatusCode.STARTED,"Starting to update Policy Properties by user {} containerComponentId={}" , userId , containerComponentId);
295 ComponentTypeEnum componentTypeEnum = validateComponentTypeAndUserId(containerComponentType, userId);
296 PropertyDataDefinition[] propertyDataDefinitions = convertJsonToObjectOfClass(policyData, PropertyDataDefinition[].class);
297 List<PropertyDataDefinition> propertyDataDefinitionList = policyBusinessLogic.updatePolicyProperties(componentTypeEnum,
298 containerComponentId, policyId, propertyDataDefinitions, userId, true);
299 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICIES_PROPERTIES, StatusCode.STARTED,"Starting to update Policy Properties by user {} containerComponentId={}" , userId , containerComponentId);
300 return buildOkResponse(propertyDataDefinitionList);
303 private ComponentTypeEnum validateComponentTypeAndUserId(final String containerComponentType, String userId) {
304 if (StringUtils.isEmpty(userId)) {
305 log.error("Missing userId HTTP header. ");
306 throw new ByActionStatusComponentException(ActionStatus.MISSING_USER_ID);
308 return validateComponentType(containerComponentType);
312 @Path("/{containerComponentType}/{componentId}/policies/{policyId}/targets")
313 @Consumes(MediaType.APPLICATION_JSON)
314 @Produces(MediaType.APPLICATION_JSON)
315 @Operation(description = "update policy targets", method = "POST", summary = "Returns updated Policy", responses = {
316 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
317 @ApiResponse(responseCode = "201", description = "Policy target updated"),
318 @ApiResponse(responseCode = "403", description = "Restricted operation"),
319 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
320 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
321 public Response updatePolicyTargets(@PathParam("componentId") final String containerComponentId, @Parameter(
322 description = "valid values: resources / services",
323 schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME ,
324 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
325 @PathParam("policyId") final String policyId,
326 @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user",
327 required = true) String userId,
328 @Context final HttpServletRequest request, List<PolicyTargetDTO> requestJson) {
329 Map<PolicyTargetType, List<String>> policyTargetTypeListMap = updatePolicyTargetsFromDTO(requestJson);
330 PolicyDefinition policyDefinition = updatePolicyTargetsFromMap(policyTargetTypeListMap, containerComponentType, containerComponentId, policyId, userId);
331 return buildOkResponse(policyDefinition);
336 @Path("/{componentType}/{componentId}/create/policies")
337 @Operation(description = "Create policies on service", method = "POST", summary = "Return policies list",
338 responses = {@ApiResponse(
339 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
340 @ApiResponse(responseCode = "200", description = "Component found"),
341 @ApiResponse(responseCode = "403", description = "Restricted operation"),
342 @ApiResponse(responseCode = "404", description = "Component not found")})
343 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
344 public Response declareProperties(@PathParam("componentType") final String componentType,
345 @PathParam("componentId") final String componentId, @Context final HttpServletRequest request,
346 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
347 @Parameter(description = "ComponentIns policies Object to be created",
348 required = true) String componentInstPoliciesMapObj) {
350 return super.declareProperties(userId, componentId, componentType, componentInstPoliciesMapObj,
351 DeclarationTypeEnum.POLICY, request);
355 private PolicyDefinition updatePolicyTargetsFromMap(Map<PolicyTargetType, List<String>> policyTarget, String containerComponentType, String containerComponentId, String policyId, String userId) {
356 ComponentTypeEnum componentTypeEnum = convertToComponentType(containerComponentType);
357 return policyBusinessLogic.updatePolicyTargets(componentTypeEnum, containerComponentId, policyId, policyTarget, userId);
360 private Map<PolicyTargetType, List<String>> updatePolicyTargetsFromDTO(List<PolicyTargetDTO> targetDTOList) {
361 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.STARTED,"Starting to update Policy target");
362 Map<PolicyTargetType, List<String>> policyTarget = new HashMap<>();
363 for (PolicyTargetDTO currentTarget : targetDTOList) {
364 if(!addTargetsByType(policyTarget, currentTarget.getType(), currentTarget.getUniqueIds())){
365 throw new ByActionStatusComponentException(ActionStatus.POLICY_TARGET_TYPE_DOES_NOT_EXIST, currentTarget.getType());
368 loggerSupportability.log(LoggerSupportabilityActions.UPDATE_POLICY_TARGET, StatusCode.COMPLETE,"Ended update Policy target");
373 public boolean addTargetsByType(Map<PolicyTargetType, List<String>> policyTarget, String type, List<String> uniqueIds) {
374 PolicyTargetType targetTypeEnum = PolicyTargetType.getByNameIgnoreCase(type);
375 if(targetTypeEnum != null){
376 policyTarget.put(targetTypeEnum, validateUniquenessOfIds(uniqueIds));
384 private List<String> validateUniquenessOfIds(List<String> uniqueIds) {
385 return uniqueIds.stream().distinct().collect(Collectors.toList());