b2c49eaef8d8a8bce1ff5155524ac6a272d83540
[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 classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
133                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER);
134             
135             final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
136                 + "." + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
137             boolean managed = true;
138             if (managedString != null && !managedString.isEmpty()) {
139                 managed = Boolean.parseBoolean(managedString);
140             }
141
142             String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
143                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
144             boolean swagger = false;
145             if (swaggerString != null && !swaggerString.isEmpty()) {
146                 swagger = Boolean.parseBoolean(swaggerString);
147             }
148
149             String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
150                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
151             boolean https = false;
152             if (httpsString != null && !httpsString.isEmpty()) {
153                 https = Boolean.parseBoolean(httpsString);
154             }
155
156             String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
157                 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
158             boolean aaf = false;
159             if (aafString != null && !aafString.isEmpty()) {
160                 aaf = Boolean.parseBoolean(aafString);
161             }
162
163             
164             HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger,
165                 managed);
166
167             if (classProv != null && !classProv.isEmpty()) {
168                 service.setSerializationProvider(classProv);
169             }
170
171             /* authentication method either AAF or HTTP Basic Auth */
172
173             if (aaf) {
174                 service.setAafAuthentication(contextUriPath);
175             } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
176                 service.setBasicAuthentication(userName, password, authUriPath);
177             }
178
179             if (filterClasses != null && !filterClasses.isEmpty()) {
180                 List<String> filterClassesList = Arrays.asList(filterClasses.split(SPACES_COMMA_SPACES));
181                 for (String filterClass : filterClassesList) {
182                     service.addFilterClass(restUriPath, filterClass);
183                 }
184             }
185
186             if (restClasses != null && !restClasses.isEmpty()) {
187                 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
188                 for (String restClass : restClassesList) {
189                     service.addServletClass(restUriPath, restClass);
190                 }
191             }
192
193             if (restPackages != null && !restPackages.isEmpty()) {
194                 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
195                 for (String restPackage : restPackageList) {
196                     service.addServletPackage(restUriPath, restPackage);
197                 }
198             }
199
200             serviceList.add(service);
201         }
202
203         return serviceList;
204     }
205
206     @Override
207     public synchronized HttpServletServer get(int port) {
208
209         if (servers.containsKey(port)) {
210             return servers.get(port);
211         }
212
213         throw new IllegalArgumentException("Http Server for " + port + " not found");
214     }
215
216     @Override
217     public synchronized List<HttpServletServer> inventory() {
218         return new ArrayList<>(this.servers.values());
219     }
220
221     @Override
222     public synchronized void destroy(int port) {
223
224         if (!servers.containsKey(port)) {
225             return;
226         }
227
228         HttpServletServer server = servers.remove(port);
229         server.shutdown();
230     }
231
232     @Override
233     public synchronized void destroy() {
234         List<HttpServletServer> httpServletServers = this.inventory();
235         for (HttpServletServer server : httpServletServers) {
236             server.shutdown();
237         }
238
239         synchronized (this) {
240             this.servers.clear();
241         }
242     }
243
244 }