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