5c3f52d3cee2439b9f268f62e25b662e3039e136
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2019, 2021 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         try {
99             HttpClient client = this.build(BusTopicParams.builder()
100                 .clientName(clientName)
101                 .useHttps(props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false))
102                 .allowSelfSignedCerts(
103                     props.getBoolean(PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, false))
104                 .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null))
105                 .port(port)
106                 .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null))
107                 .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
108                                 null))
109                 .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
110                                 null))
111                 .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true))
112                 .serializationProvider(props.getString(
113                                 PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null))
114                 .build());
115             clientList.add(client);
116         } catch (Exception e) {
117             logger.error("http-client-factory: cannot build client {}", clientName, e);
118         }
119     }
120
121     @Override
122     public synchronized HttpClient get(String name) {
123         if (clients.containsKey(name)) {
124             return clients.get(name);
125         }
126
127         throw new IllegalArgumentException("Http Client " + name + " not found");
128     }
129
130     @Override
131     public synchronized List<HttpClient> inventory() {
132         return new ArrayList<>(this.clients.values());
133     }
134
135     @Override
136     public synchronized void destroy(String name) {
137         if (!clients.containsKey(name)) {
138             return;
139         }
140
141         HttpClient client = clients.remove(name);
142         try {
143             client.shutdown();
144         } catch (IllegalStateException e) {
145             logger.error("http-client-factory: cannot shutdown client {}", client, e);
146         }
147     }
148
149     @Override
150     public void destroy() {
151         List<HttpClient> clientsInventory = this.inventory();
152         for (HttpClient client : clientsInventory) {
153             client.shutdown();
154         }
155
156         synchronized (this) {
157             this.clients.clear();
158         }
159     }
160
161 }