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