f43e24791b124563844c106a5546905c3a72648a
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / DefaultCustomToscaFunctionServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.servlets;
21
22 import io.swagger.v3.oas.annotations.Operation;
23 import io.swagger.v3.oas.annotations.media.ArraySchema;
24 import io.swagger.v3.oas.annotations.media.Content;
25 import io.swagger.v3.oas.annotations.media.Schema;
26 import io.swagger.v3.oas.annotations.responses.ApiResponse;
27 import io.swagger.v3.oas.annotations.tags.Tag;
28 import io.swagger.v3.oas.annotations.tags.Tags;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34 import javax.inject.Inject;
35 import javax.inject.Singleton;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.HeaderParam;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
47 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.config.Configuration;
50 import org.openecomp.sdc.be.config.ConfigurationManager;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.model.User;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 @Path("/v1/catalog/customToscaFunctions")
59 @Tags({@Tag(name = "SDCE-2 APIs")})
60 @Singleton
61 public class DefaultCustomToscaFunctionServlet extends BeGenericServlet {
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCustomToscaFunctionServlet.class);
64
65     @Inject
66     public DefaultCustomToscaFunctionServlet(final ComponentsUtils componentsUtils) {
67         super(componentsUtils);
68     }
69
70     @GET
71     @Path("/{type}")
72     @Produces(MediaType.APPLICATION_JSON)
73     @Operation(description = "Retrieve default custom tosca functions values from the configuration file based on type", method = "GET",
74         summary = "Retrieve all custom tosca functions",
75         responses = {
76             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))),
77             @ApiResponse(responseCode = "200", description = "Returns default custom tosca functions values from configuration file Ok"),
78             @ApiResponse(responseCode = "404", description = "Default custom tosca functions not found"),
79             @ApiResponse(responseCode = "500", description = "Internal Server Error")})
80     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
81     public Response getDefaultCustomToscaFunctionValues(@Context final HttpServletRequest request,
82                                                         @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
83                                                         @PathParam("type") Type type) {
84         final String url = request.getMethod() + " " + request.getRequestURI();
85         LOGGER.debug("Start handle request of {}", url);
86         final Map<String, Object> defaultCustomToscaFunctionssMap = new HashMap<>();
87         try {
88             List<Configuration.CustomToscaFunction> defaultCustomToscaFunction = getDefaultCustomToscaFunctionValues();
89             if (!type.equals(Type.ALL)) {
90                 defaultCustomToscaFunction = defaultCustomToscaFunction.stream()
91                     .filter(func -> type.name().toLowerCase().equals(func.getType())).collect(
92                         Collectors.toList());
93             }
94             if (CollectionUtils.isEmpty(defaultCustomToscaFunction)) {
95                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
96             }
97             defaultCustomToscaFunctionssMap.put("defaultCustomToscaFunction", defaultCustomToscaFunction);
98         } catch (final Exception e) {
99             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("SDC Failed to retrieve all default custom tosca functions");
100             LOGGER.debug("Method getDefaultCustomToscaFunctionValues failed with unexpected exception", e);
101             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
102         }
103         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), defaultCustomToscaFunctionssMap);
104     }
105
106     private List<Configuration.CustomToscaFunction> getDefaultCustomToscaFunctionValues() {
107         List<Configuration.CustomToscaFunction> customFunctions =
108             ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
109         return customFunctions == null ? Collections.emptyList() : customFunctions;
110     }
111
112     public enum Type {ALL, CUSTOM, GET_INPUT}
113 }