c2d0e4002c0752a6ef5d4da4767b3be3625080b2
[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)
51             throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
52         if (clients.containsKey(busTopicParams.getClientName())) {
53             return clients.get(busTopicParams.getClientName());
54         }
55
56         JerseyClient client =
57                 new JerseyClient(busTopicParams);
58
59         if (busTopicParams.isManaged()) {
60             clients.put(busTopicParams.getClientName(), client);
61         }
62
63         return client;
64     }
65
66     @Override
67     public synchronized List<HttpClient> build(Properties properties)
68             throws KeyManagementException, NoSuchAlgorithmException {
69         ArrayList<HttpClient> clientList = new ArrayList<>();
70
71         String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
72         if (StringUtils.isBlank(clientNames)) {
73             return clientList;
74         }
75
76         for (String clientName : clientNames.split("\\s*,\\s*")) {
77             addClient(clientList, clientName, properties);
78         }
79
80         return clientList;
81     }
82
83     private void addClient(ArrayList<HttpClient> clientList, String clientName, Properties properties) {
84         String clientPrefix = PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName;
85
86         PropertyUtils props = new PropertyUtils(properties, clientPrefix,
87             (name, value, ex) ->
88                 logger.warn("{}: {} {} is in invalid format for http client {} ", this, name, value, clientName));
89
90         int port = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
91         if (port < 0) {
92             logger.warn("No HTTP port for client in {}", clientName);
93             return;
94         }
95
96         boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
97
98         try {
99             HttpClient client = this.build(BusTopicParams.builder()
100                 .clientName(clientName)
101                 .useHttps(https)
102                 .allowSelfSignedCerts(https)
103                 .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null))
104                 .port(port)
105                 .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null))
106                 .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
107                                 null))
108                 .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
109                                 null))
110                 .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true))
111                 .serializationProvider(props.getString(
112                                 PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null))
113                 .build());
114             clientList.add(client);
115         } catch (Exception e) {
116             logger.error("http-client-factory: cannot build client {}", clientName, e);
117         }
118     }
119
120     @Override
121     public synchronized HttpClient get(String name) {
122         if (clients.containsKey(name)) {
123             return clients.get(name);
124         }
125
126         throw new IllegalArgumentException("Http Client " + name + " not found");
127     }
128
129     @Override
130     public synchronized List<HttpClient> inventory() {
131         return new ArrayList<>(this.clients.values());
132     }
133
134     @Override
135     public synchronized void destroy(String name) {
136         if (!clients.containsKey(name)) {
137             return;
138         }
139
140         HttpClient client = clients.remove(name);
141         try {
142             client.shutdown();
143         } catch (IllegalStateException e) {
144             logger.error("http-client-factory: cannot shutdown client {}", client, e);
145         }
146     }
147
148     @Override
149     public void destroy() {
150         List<HttpClient> clientsInventory = this.inventory();
151         for (HttpClient client : clientsInventory) {
152             client.shutdown();
153         }
154
155         synchronized (this) {
156             this.clients.clear();
157         }
158     }
159
160 }