002d878d3619889e54223f420180142f80d618eb
[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 package org.openecomp.sdc.be.servlets;
20
21 import io.swagger.v3.oas.annotations.Operation;
22 import io.swagger.v3.oas.annotations.media.ArraySchema;
23 import io.swagger.v3.oas.annotations.media.Content;
24 import io.swagger.v3.oas.annotations.media.Schema;
25 import io.swagger.v3.oas.annotations.responses.ApiResponse;
26 import io.swagger.v3.oas.annotations.tags.Tag;
27 import io.swagger.v3.oas.annotations.tags.Tags;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import javax.inject.Inject;
32 import javax.inject.Singleton;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import org.apache.commons.collections.CollectionUtils;
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.config.BeEcompErrorManager;
45 import org.openecomp.sdc.be.config.ConfigurationManager;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.impl.ComponentsUtils;
48 import org.openecomp.sdc.be.model.User;
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 @Tags({@Tag(name = "SDCE-2 APIs")})
55 @Singleton
56 public class DirectiveServlet extends BeGenericServlet {
57
58     private static final Logger LOGGER = LoggerFactory.getLogger(DirectiveServlet.class);
59
60     @Inject
61     public DirectiveServlet(final ComponentsUtils componentsUtils) {
62         super(componentsUtils);
63     }
64
65     @GET
66     @Path("/")
67     @Produces(MediaType.APPLICATION_JSON)
68     @Operation(description = "Retrieve all Directives values from configuration file", method = "GET", summary = "Retrieve all Directives", responses = {
69         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))),
70         @ApiResponse(responseCode = "200", description = "Returns Directive values from configuration file Ok"),
71         @ApiResponse(responseCode = "404", description = "Directive not found"),
72         @ApiResponse(responseCode = "500", description = "Internal Server Error")})
73     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
74     public Response getConfCategoriesAndVersion(@Context final HttpServletRequest request,
75                                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
76         final String url = request.getMethod() + " " + request.getRequestURI();
77         LOGGER.debug("Start handle request of {}", url);
78         final Map<String, Object> directivesMap = new HashMap<>();
79         try {
80             final List<String> directives = getDirectiveValues();
81             if (CollectionUtils.isEmpty(directives)) {
82                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
83             }
84             directivesMap.put("directives", directives);
85         } catch (final Exception e) {
86             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("SDC Failed to retrieve all Directives");
87             LOGGER.debug("Method getDirectiveValues failed with unexpected exception", e);
88             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
89         }
90         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), directivesMap);
91     }
92
93     private List<String> getDirectiveValues() {
94         return ConfigurationManager.getConfigurationManager().getConfiguration().getDirectives();
95     }
96 }