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