Change designer to plugin in code
[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 java.util.concurrent.TimeUnit;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.container.AsyncResponse;
31 import javax.ws.rs.container.Suspended;
32 import javax.ws.rs.container.TimeoutHandler;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37
38 import org.openecomp.sdc.common.api.ConfigurationSource;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.common.servlets.BasicServlet;
41 import org.openecomp.sdc.fe.config.Configuration;
42 import org.openecomp.sdc.fe.impl.PluginStatusBL;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Root resource (exposed at "/" path)
48  */
49 @Path("/config")
50 public class ConfigServlet extends BasicServlet {
51
52         private static final long serialVersionUID = 1L;
53         private static Logger log = LoggerFactory.getLogger(ConfigServlet.class.getName());
54
55         //@GET
56         //@Path("/get")
57         //@Produces(MediaType.APPLICATION_JSON)
58         public String getConfig(@Context final HttpServletRequest request) {
59
60                 String result = null;
61
62                 ServletContext context = request.getSession().getServletContext();
63
64                 ConfigurationSource configurationSource = (ConfigurationSource) context
65                                 .getAttribute(Constants.CONFIGURATION_SOURCE_ATTR);
66                 if (configurationSource != null) {
67                         Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null);
68
69                         if (configuration == null) {
70                                 log.warn("Configuration of type {} was not found", Configuration.class);
71                         }
72                         log.debug("{}", configuration);
73                         if (log.isInfoEnabled()) {
74                                 log.info("Info level ENABLED...");
75                         }
76                         log.info("The value returned from getConfig is {}", configuration);
77
78                         result = gson.toJson(configuration);
79
80                 } else {
81                         log.warn("Source Configuration object was not initialized in the context.");
82                 }
83
84                 return result;
85
86         }
87
88         //@GET
89         //@Path("/asyncget")
90         public void asyncGet(@Suspended final AsyncResponse asyncResponse) {
91
92                 asyncResponse.setTimeoutHandler(new TimeoutHandler() {
93
94                         @Override
95                         public void handleTimeout(AsyncResponse asyncResponse) {
96                                 asyncResponse.resume(
97                                                 Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Operation time out.").build());
98                         }
99                 });
100                 asyncResponse.setTimeout(3, TimeUnit.SECONDS);
101
102                 new Thread(new Runnable() {
103                         @Override
104                         public void run() {
105                                 String result = veryExpensiveOperation();
106                                 asyncResponse.resume(result);
107                         }
108
109                         private String veryExpensiveOperation() {
110
111                                 return "veryExpensiveOperation SUCCESS";
112
113                         }
114                 }).start();
115         }
116
117         @GET
118         @Path("/ui/plugins")
119         @Produces(MediaType.APPLICATION_JSON)
120         public Response getPluginsConfiguration(@Context final HttpServletRequest request) {
121                 String result = null;
122
123                 ServletContext context = request.getSession().getServletContext();
124
125                 PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT);
126
127                 result = pluginStatusBL.checkPluginsListAvailability();
128
129                 return Response.status(Status.OK).entity(result).build();
130
131         }       
132 }