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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.aai.client.service;
23 import io.netty.handler.ssl.SslContext;
24 import io.vavr.control.Try;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.UUID;
28 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
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.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class AaiHttpClientFactory {
42 private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
44 private final AaiClientConfiguration configuration;
45 private final SslFactory sslFactory;
47 public AaiHttpClientFactory(AaiClientConfiguration configuration) {
48 this(configuration, new SslFactory());
51 public AaiHttpClientFactory(AaiClientConfiguration configuration, SslFactory sslFactory) {
52 this.configuration = configuration;
53 this.sslFactory = sslFactory;
56 public CloudHttpClient build() {
57 LOGGER.debug("Setting ssl context");
58 return new CloudHttpClient(createSslContext());
61 private SslContext createSslContext() {
62 if (configuration.enableAaiCertAuth()) {
63 final SecurityKeys collectorSecurityKeys = ImmutableSecurityKeys.builder()
64 .keyStore(ImmutableSecurityKeysStore.of(resource(configuration.keyStorePath()).get()))
65 .keyStorePassword(Passwords.fromResource(configuration.keyStorePasswordPath()))
66 .trustStore(ImmutableSecurityKeysStore.of(resource(configuration.trustStorePath()).get()))
67 .trustStorePassword(Passwords.fromResource(configuration.trustStorePasswordPath()))
69 return sslFactory.createSecureClientContext(collectorSecurityKeys);
71 return sslFactory.createInsecureClientContext();
74 private Try<Path> resource(String resource) {
75 return Try.of(() -> Paths.get(Passwords.class.getResource(resource).toURI()));
78 public static RequestDiagnosticContext createRequestDiagnosticContext() {
79 return ImmutableRequestDiagnosticContext.builder()
80 .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();