Catalog alignment
[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.annotations.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
29 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
30 import org.openecomp.sdc.be.config.Configuration;
31 import org.openecomp.sdc.common.api.ConfigurationSource;
32 import org.openecomp.sdc.common.api.Constants;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.servlets.BasicServlet;
35
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.MediaType;
43
44 /**
45  * Root resource (exposed at "/" path)
46  */
47 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
48 @Path("/config")
49 @Api(value = "Get configuration", description = "Get configuration")
50 public class ConfigServlet extends BasicServlet {
51
52     private static final Logger log = Logger.getLogger(ConfigServlet.class);
53
54     @GET
55     @Path("/get")
56     @Produces(MediaType.APPLICATION_JSON)
57     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
58     @ApiOperation(value = "Retrieve configuration", httpMethod = "GET", notes = "Returns configuration", response = String.class)
59     @ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
60     public String getConfig(@Context final HttpServletRequest request) {
61
62         String result = null;
63
64         ServletContext context = request.getSession().getServletContext();
65
66         ConfigurationSource configurationSource = (ConfigurationSource) context.getAttribute(Constants.CONFIGURATION_SOURCE_ATTR);
67         if (configurationSource != null) {
68             Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null);
69
70             if (configuration == null) {
71                 log.warn("Configuration of type {} was not found", Configuration.class);
72             }
73             log.debug("{}", configuration);
74             log.info("Info level ENABLED...");
75             log.info("The value returned from getConfig is {}", configuration);
76
77             result = gson.toJson(configuration);
78
79         } else {
80             log.warn("Source Configuration object was not initialized in the context.");
81         }
82
83         return result;
84
85     }
86
87 }