Fixes for Chef Adapter bundle
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / ChefApiClientFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7  * =============================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.adapter.chef.chefclient;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.security.KeyManagementException;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.cert.CertificateException;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
31 import org.apache.http.impl.client.HttpClients;
32 import org.apache.http.ssl.SSLContexts;
33 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
34 import org.onap.appc.adapter.chef.chefclient.impl.ChefApiClientImpl;
35 import org.onap.appc.adapter.chef.chefclient.impl.ChefApiHeaderFactory;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.google.common.collect.ImmutableMap;
39
40 public class ChefApiClientFactory {
41
42     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientFactory.class);
43
44     private HttpClient createChefHttpClient() {
45         String trustStoreFileName = "/opt/app/bvc/chef/chefServerSSL.jks";
46         char[] trustStoreCreds = "adminadmin".toCharArray();
47         try {
48             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
49                     SSLContexts.custom().loadTrustMaterial(new File(trustStoreFileName), trustStoreCreds).build(),
50                     SSLConnectionSocketFactory.getDefaultHostnameVerifier());
51             return HttpClients.custom().setSSLSocketFactory(sslsf).build();
52         } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
53                 | IOException e) {
54             logger.error(e.getMessage(), e);
55         }
56         return null;
57     }
58
59     private HttpClient httpClient = createChefHttpClient();
60     private ChefApiHeaderFactory chefApiHeaderFactory = new ChefApiHeaderFactory();
61
62     public ChefApiClient create(String endPoint, String organizations, String userId, String pemPath) {
63         return new ChefApiClientImpl(httpClient, endPoint, organizations, (methodName, requestPath, body) -> chefApiHeaderFactory
64                 .create(methodName, requestPath, body, userId, organizations, pemPath));
65     }
66
67     public ChefApiClient create(String endPoint, String organizations)  {
68         return new ChefApiClientImpl(httpClient, endPoint, organizations, (methodName, requestPath, body) -> ImmutableMap.of());
69     }
70 }