adffdaf9d7a005d197e9d5ac11781e5d70c7b47e
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / DirectiveServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import org.apache.commons.collections.CollectionUtils;
41 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
42 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
43 import org.openecomp.sdc.be.config.BeEcompErrorManager;
44 import org.openecomp.sdc.be.config.ConfigurationManager;
45 import org.openecomp.sdc.be.dao.api.ActionStatus;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.model.User;
48 import org.openecomp.sdc.be.user.UserBusinessLogic;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 @Path("/v1/catalog/directives")
54 @Singleton
55 public class DirectiveServlet extends BeGenericServlet {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(DirectiveServlet.class);
58
59     @Inject
60     public DirectiveServlet(final UserBusinessLogic userAdminManager,
61                             final ComponentsUtils componentsUtils) {
62         super(userAdminManager, componentsUtils);
63     }
64
65     @GET
66     @Path("/")
67     @Produces(MediaType.APPLICATION_JSON)
68     @Operation(description = "Retrieve all Directives values from configuration file", method = "GET",
69         summary = "Retrieve all Directives", responses = {
70         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))),
71         @ApiResponse(responseCode = "200",
72             description = "Returns Directive values from configuration file Ok"),
73         @ApiResponse(responseCode = "404",
74             description = "Directive not found"),
75         @ApiResponse(responseCode = "500", description = "Internal Server Error")})
76     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
77     public Response getConfCategoriesAndVersion(@Context final HttpServletRequest request,
78                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
79
80         final String url = request.getMethod() + " " + request.getRequestURI();
81         LOGGER.debug("Start handle request of {}", url);
82
83         final Map<String, Object> directivesMap = new HashMap<>();
84         try {
85             final List<String> directives = getDirectiveValues();
86             if (CollectionUtils.isEmpty(directives)) {
87                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
88             }
89             directivesMap.put("directives", directives);
90
91         } catch (final Exception e) {
92             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("SDC Failed to retrieve all Directives");
93             LOGGER.debug("Method getDirectiveValues failed with unexpected exception", e);
94             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
95         }
96
97         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), directivesMap);
98     }
99
100     private List<String> getDirectiveValues() {
101         return ConfigurationManager.getConfigurationManager().getConfiguration().getDirectives();
102     }
103
104 }