5e51b832ef57b71e9ac89be73bef468cb979c686
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA 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.dcaegen2.services.sdk.rest.services.aai.client.service;
22
23 import io.vavr.control.Try;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.UUID;
27 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
28 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
29 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
31 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
32 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys;
33 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore;
34 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
35 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class AaiHttpClientFactory {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
42
43     private final AaiClientConfiguration configuration;
44
45     public AaiHttpClientFactory(AaiClientConfiguration configuration) {
46         this.configuration = configuration;
47     }
48
49     public RxHttpClient build() {
50         LOGGER.debug("Setting ssl context");
51
52         if (configuration.enableAaiCertAuth()) {
53             return RxHttpClientFactory.create(createSslKeys());
54         } else {
55             return RxHttpClientFactory.createInsecure();
56         }
57     }
58
59     private SecurityKeys createSslKeys() {
60         return ImmutableSecurityKeys.builder()
61                 .keyStore(ImmutableSecurityKeysStore.of(Paths.get(configuration.keyStorePath())))
62                 .keyStorePassword(Passwords.fromPath(Paths.get(configuration.keyStorePasswordPath())))
63                 .trustStore(ImmutableSecurityKeysStore.of(Paths.get(configuration.trustStorePath())))
64                 .trustStorePassword(Passwords.fromPath(Paths.get(configuration.trustStorePasswordPath())))
65                 .build();
66     }
67
68     public static RequestDiagnosticContext createRequestDiagnosticContext() {
69         return ImmutableRequestDiagnosticContext.builder()
70                 .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();
71     }
72
73 }