Add API to retrieve UI configuration
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / 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.fe.servlets;
22
23 import org.openecomp.sdc.common.api.Constants;
24 import org.openecomp.sdc.fe.config.FeEcompErrorManager;
25 import org.openecomp.sdc.fe.impl.PluginStatusBL;
26 import org.openecomp.sdc.fe.config.ConfigurationManager;
27 import org.openecomp.sdc.fe.config.WorkspaceConfiguration;
28 import org.openecomp.sdc.exception.NotFoundException;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
43
44 /**
45  * Root resource (exposed at "/" path)
46  */
47 @Path("/config")
48 public class ConfigServlet extends LoggingServlet {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigServlet.class.getName());
51
52     @GET
53     @Path("/ui/workspace")
54     @Produces(MediaType.APPLICATION_JSON)
55     public Response getUIWorkspaceConfiguration(@Context final HttpServletRequest request) {
56
57         try {
58             logFeRequest(request);
59
60             ServletContext context = request.getSession().getServletContext();
61
62             ConfigurationManager configurationManager = (ConfigurationManager) context
63                     .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR);
64
65             WorkspaceConfiguration configuration = configurationManager.getWorkspaceConfiguration();
66             if (configuration == null) {
67                 throw new NotFoundException(WorkspaceConfiguration.class.getSimpleName());
68             }
69             LOGGER.info("The value returned from getConfig is {}", configuration);
70             String result = gson.toJson(configuration);
71             Response response = Response.status(Status.OK).entity(result).build();
72             logFeResponse(request, response);
73
74             return response;
75         } catch (Exception e) {
76             FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response");
77             LOGGER.error("Unexpected FE response logging error :", e);
78             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
79         }
80
81     }
82
83
84     @GET
85     @Path("/ui/plugins")
86     @Produces(MediaType.APPLICATION_JSON)
87     public Response getPluginsConfiguration(@Context final HttpServletRequest request) {
88
89         try {
90             logFeRequest(request);
91
92             ServletContext context = request.getSession().getServletContext();
93
94             PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT);
95
96             String result = pluginStatusBL.getPluginsList();
97
98             Response response = Response.status(Status.OK).entity(result).build();
99
100             logFeResponse(request, response);
101
102             return response;
103         } catch (Exception e) {
104             FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response");
105             LOGGER.error("Unexpected FE response logging error :", e);
106             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
107         }
108
109     }
110
111     @GET
112     @Path("/ui/plugins/{pluginId}/online")
113     @Produces(MediaType.APPLICATION_JSON)
114     public Response getPluginOnlineState(@PathParam("pluginId") final String pluginId, @Context final HttpServletRequest request) {
115
116         try {
117             logFeRequest(request);
118
119             ServletContext context = request.getSession().getServletContext();
120
121             PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT);
122
123             String result = pluginStatusBL.getPluginAvailability(pluginId);
124
125             if (result == null) {
126                 LOGGER.debug("Plugin with pluginId: {} was not found in the configuration", pluginId);
127                 return Response.status(Status.NOT_FOUND).entity("Plugin with pluginId:\"" + pluginId + "\" was not found in the configuration").build();
128             }
129
130             Response response = Response.status(Status.OK).entity(result).build();
131
132             logFeResponse(request, response);
133
134             return response;
135         } catch (Exception e) {
136             FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response");
137             LOGGER.error("Unexpected FE response logging error :", e);
138             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
139         }
140     }
141
142     protected void inHttpRequest(HttpServletRequest httpRequest) {
143         LOGGER.info("{} {} {}", httpRequest.getMethod(), httpRequest.getRequestURI(), httpRequest.getProtocol());
144     }
145
146     /**
147      * Extracted for purpose of clear method name, for logback %M parameter
148      *
149      * @param response http response
150      */
151     protected void outHttpResponse(Response response) {
152         LOGGER.info("SC=\"{}\"", response.getStatus());
153     }
154 }