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 org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
28 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
29 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys;
30 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore;
31 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
32 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
33 import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Base64;
40 import java.util.UUID;
42 public class AaiHttpClientFactory {
44 private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
46 private final AaiClientConfiguration configuration;
47 private final SslFactory sslFactory = new SslFactory();
50 public AaiHttpClientFactory(AaiClientConfiguration configuration) {
51 this.configuration = configuration;
54 public CloudHttpClient build() {
55 LOGGER.debug("Setting ssl context");
56 return new CloudHttpClient(createSslContext());
59 private SslContext createSslContext() {
60 if (configuration.enableAaiCertAuth()) {
61 final SecurityKeys collectorSecurityKeys = ImmutableSecurityKeys.builder()
62 .keyStore(ImmutableSecurityKeysStore.of(resource(configuration.keyStorePath()).get()))
63 .keyStorePassword(Passwords.fromResource(configuration.keyStorePasswordPath()))
64 .trustStore(ImmutableSecurityKeysStore.of(resource(configuration.trustStorePath()).get()))
65 .trustStorePassword(Passwords.fromResource(configuration.trustStorePasswordPath()))
67 return sslFactory.createSecureClientContext(collectorSecurityKeys);
69 return sslFactory.createInsecureClientContext();
72 private Try<Path> resource(String resource) {
73 return Try.of(() -> Paths.get(Passwords.class.getResource(resource).toURI()));
76 public static String performBasicAuthentication(String userName, String password) {
77 return Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
80 public static RequestDiagnosticContext createRequestDiagnosticContext() {
81 return ImmutableRequestDiagnosticContext.builder()
82 .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();