40f5c9ad31136c76c55a61732e201b7897297510
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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.policy.drools.http.server;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Properties;
27
28 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
29 import org.openecomp.policy.common.logging.flexlogger.Logger;
30 import org.openecomp.policy.drools.http.server.internal.JettyJerseyServer;
31 import org.openecomp.policy.drools.properties.PolicyProperties;
32
33 /**
34  * Factory of HTTP Servlet-Enabled Servlets
35  */
36 public interface HttpServletServerFactory {
37
38         /**
39          * builds an http server with support for servlets
40          * 
41          * @param name name
42          * @param host binding host
43          * @param port port
44          * @param contextPath server base path
45          * @param swagger enable swagger documentation
46          * @param managed is it managed by infrastructure
47          * @return http server
48          * @throws IllegalArgumentException when invalid parameters are provided
49          */
50         public HttpServletServer build(String name, String host, int port, String contextPath, 
51                                                boolean swagger, boolean managed)
52                    throws IllegalArgumentException;
53         
54         /**
55          * list of http servers per properties
56          * 
57          * @param properties properties based configuration
58          * @return list of http servers
59          * @throws IllegalArgumentException when invalid parameters are provided
60          */
61         public ArrayList<HttpServletServer> build(Properties properties) throws IllegalArgumentException;
62         
63         /**
64          * gets a server based on the port
65          * 
66          * @param port port
67          * @return http server
68          */
69         public HttpServletServer get(int port);
70         
71         /**
72          * provides an inventory of servers
73          * 
74          * @return inventory of servers
75          */
76         public List<HttpServletServer> inventory();
77         
78         /**
79          * destroys server bound to a port
80          * @param port
81          */
82         public void destroy(int port);
83         
84         /**
85          * destroys the factory and therefore all servers
86          */
87         public void destroy();
88 }
89
90 /**
91  * Indexed factory implementation
92  */
93 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
94         
95         /**
96          * logger
97          */
98         protected static Logger logger = FlexLogger.getLogger(IndexedHttpServletServerFactory.class);   
99         
100         /**
101          * servers index
102          */
103         protected HashMap<Integer, HttpServletServer> servers = new HashMap<Integer, HttpServletServer>();
104
105         @Override
106         public synchronized HttpServletServer build(String name, String host, int port, 
107                                                             String contextPath, boolean swagger,
108                                                             boolean managed) 
109                 throws IllegalArgumentException {       
110                 
111                 if (servers.containsKey(port))
112                         return servers.get(port);
113                 
114                 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger);
115                 if (managed)
116                         servers.put(port, server);
117                 
118                 return server;
119         }
120         
121         @Override
122         public synchronized ArrayList<HttpServletServer> build(Properties properties) 
123                 throws IllegalArgumentException {       
124                 
125                 ArrayList<HttpServletServer> serviceList = new ArrayList<HttpServletServer>();
126                 
127                 String serviceNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES);
128                 if (serviceNames == null || serviceNames.isEmpty()) {
129                         logger.warn("No topic for HTTP Service " + properties);
130                         return serviceList;
131                 }
132                 
133                 List<String> serviceNameList = 
134                                 new ArrayList<String>(Arrays.asList(serviceNames.split("\\s*,\\s*")));
135                 
136                 for (String serviceName : serviceNameList) {
137                         String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + 
138                                                                       serviceName + 
139                                                                       PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
140                         
141                         int servicePort;
142                         try {
143                                 if (servicePortString == null || servicePortString.isEmpty()) {
144                                         if (logger.isWarnEnabled())
145                                                 logger.warn("No HTTP port for service in " + serviceName);
146                                         continue;
147                                 }
148                                 servicePort = Integer.parseInt(servicePortString);
149                         } catch (NumberFormatException nfe) {
150                                 if (logger.isWarnEnabled())
151                                         logger.warn("No HTTP port for service in " + serviceName);
152                                 continue;
153                         }
154                         
155                         String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
156                                                      serviceName + 
157                                                      PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
158                         
159                         String contextUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
160                                                         serviceName + 
161                                                         PolicyProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
162                         
163                         String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
164                                                                          serviceName + 
165                                                      PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
166
167                         String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
168                                                      serviceName + 
169                                                      PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
170                         
171                         String authUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
172                                                         serviceName + 
173                                                         PolicyProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
174                         
175                         String restClasses = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
176                                                       serviceName + 
177                                                       PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
178                         
179                         String restPackages = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
180                                                         serviceName + 
181                                                         PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
182                         String restUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
183                                                          serviceName + 
184                                                          PolicyProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
185                         
186                         String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
187                                                           serviceName + 
188                                                           PolicyProperties.PROPERTY_MANAGED_SUFFIX);            
189                         boolean managed = true;
190                         if (managedString != null && !managedString.isEmpty()) {
191                                 managed = Boolean.parseBoolean(managedString);
192                         }
193                         
194                         String swaggerString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
195                                                                                               serviceName + 
196                                                                                               PolicyProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);           
197                         boolean swagger = false;
198                         if (swaggerString != null && !swaggerString.isEmpty()) {
199                                 swagger = Boolean.parseBoolean(swaggerString);
200                         }
201                         
202                         HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, swagger, managed);
203                         if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
204                                 service.setBasicAuthentication(userName, password, authUriPath);
205                         }
206                         
207                         if (restClasses != null && !restClasses.isEmpty()) {
208                                 List<String> restClassesList = 
209                                                 new ArrayList<String>(Arrays.asList(restClasses.split("\\s*,\\s*")));
210                                 for (String restClass : restClassesList)
211                                         service.addServletClass(restUriPath, restClass);
212                         }
213                         
214                         if (restPackages != null && !restPackages.isEmpty()) {
215                                 List<String> restPackageList = 
216                                                 new ArrayList<String>(Arrays.asList(restPackages.split("\\s*,\\s*")));
217                                 for (String restPackage : restPackageList)
218                                         service.addServletPackage(restUriPath, restPackage);
219                         }
220                         
221                         serviceList.add(service);
222                 }
223                 
224                 return serviceList;
225         }
226
227         @Override
228         public synchronized HttpServletServer get(int port) throws IllegalArgumentException {
229                 
230                 if (servers.containsKey(port)) {
231                         return servers.get(port);
232                 } 
233                 
234                 throw new IllegalArgumentException("Http Server for " + port + " not found");
235         }
236
237         @Override
238         public synchronized List<HttpServletServer> inventory() {
239                  return new ArrayList<HttpServletServer>(this.servers.values());
240         }
241         
242         @Override
243         public synchronized void destroy(int port) throws IllegalArgumentException, IllegalStateException {
244                 
245                 if (!servers.containsKey(port)) {
246                         return;
247                 }
248                 
249                 HttpServletServer server = servers.remove(port);
250                 server.shutdown();
251         }
252
253         @Override
254         public synchronized void destroy() throws IllegalArgumentException, IllegalStateException {
255                 List<HttpServletServer> servers = this.inventory();
256                 for (HttpServletServer server: servers) {
257                         server.shutdown();
258                 }
259                 
260                 synchronized(this) {
261                         this.servers.clear();
262                 }
263         }
264         
265 }