edf8ff6f7427b7fa446783200539fca2300c6bdf
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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.client;
22
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.http.client.internal.JerseyClient;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.onap.policy.common.endpoints.utils.PropertyUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * HTTP client factory implementation indexed by name.
39  */
40 class IndexedHttpClientFactory implements HttpClientFactory {
41
42     /**
43      * Logger.
44      */
45     private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
46
47     protected HashMap<String, HttpClient> clients = new HashMap<>();
48
49     @Override
50     public synchronized HttpClient build(BusTopicParams busTopicParams) throws HttpClientConfigException {
51         if (clients.containsKey(busTopicParams.getClientName())) {
52             return clients.get(busTopicParams.getClientName());
53         }
54
55         JerseyClient client;
56         try {
57             client = new JerseyClient(busTopicParams);
58         } catch (KeyManagementException | NoSuchAlgorithmException | ClassNotFoundException e) {
59             throw new HttpClientConfigException(e);
60         }
61
62         if (busTopicParams.isManaged()) {
63             clients.put(busTopicParams.getClientName(), client);
64         }
65
66         return client;
67     }
68
69     @Override
70     public synchronized List<HttpClient> build(Properties properties) throws HttpClientConfigException {
71         ArrayList<HttpClient> clientList = new ArrayList<>();
72
73         String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
74         if (StringUtils.isBlank(clientNames)) {
75             return clientList;
76         }
77
78         for (String clientName : clientNames.split("\\s*,\\s*")) {
79             addClient(clientList, clientName, properties);
80         }
81
82         return clientList;
83     }
84
85     private void addClient(ArrayList<HttpClient> clientList, String clientName, Properties properties) {
86         String clientPrefix = PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName;
87
88         PropertyUtils props = new PropertyUtils(properties, clientPrefix,
89             (name, value, ex) ->
90                 logger.warn("{}: {} {} is in invalid format for http client {} ", this, name, value, clientName));
91
92         int port = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
93         if (port < 0) {
94             logger.warn("No HTTP port for client in {}", clientName);
95             return;
96         }
97
98         boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
99
100         try {
101             HttpClient client = this.build(BusTopicParams.builder()
102                 .clientName(clientName)
103                 .useHttps(https)
104                 .allowSelfSignedCerts(https)
105                 .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null))
106                 .port(port)
107                 .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null))
108                 .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
109                                 null))
110                 .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
111                                 null))
112                 .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true))
113                 .serializationProvider(props.getString(
114                                 PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null))
115                 .build());
116             clientList.add(client);
117         } catch (Exception e) {
118             logger.error("http-client-factory: cannot build client {}", clientName, e);
119         }
120     }
121
122     @Override
123     public synchronized HttpClient get(String name) {
124         if (clients.containsKey(name)) {
125             return clients.get(name);
126         }
127
128         throw new IllegalArgumentException("Http Client " + name + " not found");
129     }
130
131     @Override
132     public synchronized List<HttpClient> inventory() {
133         return new ArrayList<>(this.clients.values());
134     }
135
136     @Override
137     public synchronized void destroy(String name) {
138         if (!clients.containsKey(name)) {
139             return;
140         }
141
142         HttpClient client = clients.remove(name);
143         try {
144             client.shutdown();
145         } catch (IllegalStateException e) {
146             logger.error("http-client-factory: cannot shutdown client {}", client, e);
147         }
148     }
149
150     @Override
151     public void destroy() {
152         List<HttpClient> clientsInventory = this.inventory();
153         for (HttpClient client : clientsInventory) {
154             client.shutdown();
155         }
156
157         synchronized (this) {
158             this.clients.clear();
159         }
160     }
161
162 }