Allow only types from selected model in service creation
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / PolicyTypesEndpoint.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 io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.media.ArraySchema;
26 import io.swagger.v3.oas.annotations.media.Content;
27 import io.swagger.v3.oas.annotations.media.Schema;
28 import io.swagger.v3.oas.annotations.responses.ApiResponse;
29 import io.swagger.v3.oas.annotations.servers.Server;
30 import io.swagger.v3.oas.annotations.servers.Servers;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import io.swagger.v3.oas.annotations.tags.Tags;
33 import java.util.List;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.QueryParam;
40 import javax.ws.rs.core.MediaType;
41 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
42 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
43 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.mixin.PolicyTypeMixin;
46 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
47 import org.openecomp.sdc.be.user.UserBusinessLogic;
48 import org.openecomp.sdc.be.view.ResponseView;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.openecomp.sdc.common.util.ValidationUtils;
52 import org.springframework.stereotype.Controller;
53
54 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
55 @Path("/v1/catalog")
56 @Tags({@Tag(name = "SDCE-2 APIs")})
57 @Servers({@Server(url = "/sdc2/rest")})
58 @Controller
59 @Consumes(MediaType.APPLICATION_JSON)
60 @Produces(MediaType.APPLICATION_JSON)
61 public class PolicyTypesEndpoint extends BeGenericServlet {
62
63     private static final Logger log = Logger.getLogger(PolicyTypesEndpoint.class);
64     private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
65
66     public PolicyTypesEndpoint(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils,
67                                PolicyTypeBusinessLogic policyTypeBusinessLogic) {
68         super(userBusinessLogic, componentsUtils);
69         this.policyTypeBusinessLogic = policyTypeBusinessLogic;
70     }
71
72     @GET
73     @Path("/policyTypes")
74     @Operation(description = "Get policy types ", method = "GET", summary = "Returns policy types", responses = {
75         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = PolicyTypeDefinition.class)))),
76         @ApiResponse(responseCode = "200", description = "policy types found"),
77         @ApiResponse(responseCode = "403", description = "Restricted operation"),
78         @ApiResponse(responseCode = "500", description = "The GET request failed due to internal SDC problem.")})
79     @ResponseView(mixin = {PolicyTypeMixin.class})
80     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
81     public List<PolicyTypeDefinition> getPolicyTypes(
82         @Parameter(description = "An optional parameter to indicate the type of the container from where this call is executed")
83         @QueryParam("internalComponentType") String internalComponentType,
84         @QueryParam("componentModel") String internalComponentModel,
85     @Parameter(description = "The user id", required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
86         log.debug("(get) Start handle request of GET policyTypes");
87         if (internalComponentModel != null) {
88             internalComponentModel = ValidationUtils.sanitizeInputString(internalComponentModel.trim());
89         }
90         return policyTypeBusinessLogic
91             .getAllPolicyTypes(userId, internalComponentType, internalComponentModel);
92     }
93 }