86a8a2d35270325fc76f2b36c8dc6c2581d35736
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ConfigServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.servers.Server;
29 import io.swagger.v3.oas.annotations.servers.Servers;
30 import io.swagger.v3.oas.annotations.tags.Tag;
31 import io.swagger.v3.oas.annotations.tags.Tags;
32 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
33 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
34 import org.openecomp.sdc.be.config.Configuration;
35 import org.openecomp.sdc.common.api.ConfigurationSource;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.common.servlets.BasicServlet;
39
40 import javax.servlet.ServletContext;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.GET;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.Produces;
45 import javax.ws.rs.core.Context;
46 import javax.ws.rs.core.MediaType;
47
48 /**
49  * Root resource (exposed at "/" path)
50  */
51 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
52 @Path("/config")
53 @Tags({@Tag(name = "SDC Internal APIs")})
54 @Servers({@Server(url = "/sdc2/rest")})
55 public class ConfigServlet extends BasicServlet {
56
57     private static final Logger log = Logger.getLogger(ConfigServlet.class);
58
59     @GET
60     @Path("/get")
61     @Produces(MediaType.APPLICATION_JSON)
62     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
63     @Operation(description = "Retrieve configuration", method = "GET", responses = {
64             @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = String.class)))
65     })
66     public String getConfig(@Context final HttpServletRequest request) {
67
68         String result = null;
69
70         ServletContext context = request.getSession().getServletContext();
71
72         ConfigurationSource configurationSource = (ConfigurationSource) context.getAttribute(Constants.CONFIGURATION_SOURCE_ATTR);
73         if (configurationSource != null) {
74             Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null);
75
76             if (configuration == null) {
77                 log.warn("Configuration of type {} was not found", Configuration.class);
78             }
79             log.debug("{}", configuration);
80             log.info("Info level ENABLED...");
81             log.info("The value returned from getConfig is {}", configuration);
82
83             result = gson.toJson(configuration);
84
85         } else {
86             log.warn("Source Configuration object was not initialized in the context.");
87         }
88
89         return result;
90
91     }
92
93 }