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