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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.servlets;
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;
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.config.BeEcompErrorManager;
47 import org.openecomp.sdc.be.config.Configuration;
48 import org.openecomp.sdc.be.config.ConfigurationManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.User;
52 import org.openecomp.sdc.common.api.Constants;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @Path("/v1/catalog/customToscaFunctions")
57 @Tags({@Tag(name = "SDCE-2 APIs")})
59 public class DefaultCustomToscaFunctionServlet extends BeGenericServlet {
61 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCustomToscaFunctionServlet.class);
64 public DefaultCustomToscaFunctionServlet(final ComponentsUtils componentsUtils) {
65 super(componentsUtils);
70 @Produces(MediaType.APPLICATION_JSON)
71 @Operation(description = "Retrieve default custom tosca functions values from the configuration file based on type", method = "GET",
72 summary = "Retrieve all custom tosca functions",
74 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))),
75 @ApiResponse(responseCode = "200", description = "Returns default custom tosca functions values from configuration file Ok"),
76 @ApiResponse(responseCode = "404", description = "Default custom tosca functions not found"),
77 @ApiResponse(responseCode = "500", description = "Internal Server Error")})
78 public Response getDefaultCustomToscaFunctionValues(@Context final HttpServletRequest request,
79 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
80 @PathParam("type") Type type) {
81 final String url = request.getMethod() + " " + request.getRequestURI();
82 LOGGER.debug("Start handle request of {}", url);
83 final Map<String, Object> defaultCustomToscaFunctionssMap = new HashMap<>();
85 List<Configuration.CustomToscaFunction> defaultCustomToscaFunction = getDefaultCustomToscaFunctionValues();
86 if (!type.equals(Type.ALL)) {
87 defaultCustomToscaFunction = defaultCustomToscaFunction.stream()
88 .filter(func -> type.name().toLowerCase().equals(func.getType())).collect(
91 if (CollectionUtils.isEmpty(defaultCustomToscaFunction)) {
92 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
94 defaultCustomToscaFunctionssMap.put("defaultCustomToscaFunction", defaultCustomToscaFunction);
95 } catch (final Exception e) {
96 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("SDC Failed to retrieve all default custom tosca functions");
97 LOGGER.debug("Method getDefaultCustomToscaFunctionValues failed with unexpected exception", e);
98 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
100 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), defaultCustomToscaFunctionssMap);
103 private List<Configuration.CustomToscaFunction> getDefaultCustomToscaFunctionValues() {
104 List<Configuration.CustomToscaFunction> customFunctions =
105 ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
106 return customFunctions == null ? Collections.emptyList() : customFunctions;
109 public enum Type {ALL, CUSTOM, GET_INPUT}