ad8ef99cbbe505e40e5dcc4e98ae1d5f67dd3425
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.onap.policy.common.endpoints.http.server;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
29 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Indexed factory implementation.
35  */
36 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
37
38     private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
39
40     /**
41      * logger.
42      */
43     protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
44
45     /**
46      * servers index.
47      */
48     protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
49
50     @Override
51     public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
52         boolean swagger, boolean managed) {
53
54         if (servers.containsKey(port)) {
55             return servers.get(port);
56         }
57
58         JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
59         if (managed) {
60             servers.put(port, server);
61         }
62
63         return server;
64     }
65
66     @Override
67     public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
68         boolean managed) {
69         return build(name, false, host, port, contextPath, swagger, managed);
70     }
71
72     @Override
73     public synchronized List<HttpServletServer> build(Properties properties) {
74
75         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
76
77         String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
78         if (serviceNames == null || serviceNames.isEmpty()) {
79             logger.warn("No topic for HTTP Service: {}", properties);
80             return serviceList;
81         }
82
83         List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
84
85         for (String serviceName : serviceNameList) {
86             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
87                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
88
89             int servicePort;
90             try {
91                 if (servicePortString == null || servicePortString.isEmpty()) {
92                     if (logger.isWarnEnabled()) {
93                         logger.warn("No HTTP port for service in {}", serviceName);
94                     }
95                     continue;
96                 }
97                 servicePort = Integer.parseInt(servicePortString);
98             } catch (NumberFormatException nfe) {
99                 if (logger.isWarnEnabled()) {
100                     logger.warn("No HTTP port for service in {}", serviceName);
101                 }
102                 continue;
103             }
104
105             final String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
106                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
107
108             final String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
109                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
110
111             final String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
112                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
113
114             final String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
115                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
116
117             final String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
118                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
119
120             final String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
121                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
122
123             final String filterClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
124                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX);
125
126             final String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
127                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
128
129             final String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
130                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
131
132             final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
133                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
134             boolean managed = true;
135             if (managedString != null && !managedString.isEmpty()) {
136                 managed = Boolean.parseBoolean(managedString);
137             }
138
139             String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
140                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
141             boolean swagger = false;
142             if (swaggerString != null && !swaggerString.isEmpty()) {
143                 swagger = Boolean.parseBoolean(swaggerString);
144             }
145
146             String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
147                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
148             boolean https = false;
149             if (httpsString != null && !httpsString.isEmpty()) {
150                 https = Boolean.parseBoolean(httpsString);
151             }
152
153             String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
154                 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
155             boolean aaf = false;
156             if (aafString != null && !aafString.isEmpty()) {
157                 aaf = Boolean.parseBoolean(aafString);
158             }
159
160             HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger,
161                 managed);
162
163             /* authentication method either AAF or HTTP Basic Auth */
164
165             if (aaf) {
166                 service.setAafAuthentication(contextUriPath);
167             } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
168                 service.setBasicAuthentication(userName, password, authUriPath);
169             }
170
171             if (filterClasses != null && !filterClasses.isEmpty()) {
172                 List<String> filterClassesList = Arrays.asList(filterClasses.split(SPACES_COMMA_SPACES));
173                 for (String filterClass : filterClassesList) {
174                     service.addFilterClass(restUriPath, filterClass);
175                 }
176             }
177
178             if (restClasses != null && !restClasses.isEmpty()) {
179                 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
180                 for (String restClass : restClassesList) {
181                     service.addServletClass(restUriPath, restClass);
182                 }
183             }
184
185             if (restPackages != null && !restPackages.isEmpty()) {
186                 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
187                 for (String restPackage : restPackageList) {
188                     service.addServletPackage(restUriPath, restPackage);
189                 }
190             }
191
192             serviceList.add(service);
193         }
194
195         return serviceList;
196     }
197
198     @Override
199     public synchronized HttpServletServer get(int port) {
200
201         if (servers.containsKey(port)) {
202             return servers.get(port);
203         }
204
205         throw new IllegalArgumentException("Http Server for " + port + " not found");
206     }
207
208     @Override
209     public synchronized List<HttpServletServer> inventory() {
210         return new ArrayList<>(this.servers.values());
211     }
212
213     @Override
214     public synchronized void destroy(int port) {
215
216         if (!servers.containsKey(port)) {
217             return;
218         }
219
220         HttpServletServer server = servers.remove(port);
221         server.shutdown();
222     }
223
224     @Override
225     public synchronized void destroy() {
226         List<HttpServletServer> httpServletServers = this.inventory();
227         for (HttpServletServer server : httpServletServers) {
228             server.shutdown();
229         }
230
231         synchronized (this) {
232             this.servers.clear();
233         }
234     }
235
236 }