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