903528b9535298234a6d526d8104fe098698ac9c
[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.netty.handler.codec.http.HttpHeaders;
24 import io.netty.handler.ssl.SslContext;
25 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
26 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.get.AaiHttpGetClient;
28 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.patch.AaiHttpPatchClient;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
30 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Mono;
34 import reactor.netty.Connection;
35 import reactor.netty.http.client.HttpClient;
36 import reactor.netty.http.client.HttpClientRequest;
37 import reactor.netty.http.client.HttpClientResponse;
38
39 import javax.net.ssl.SSLException;
40 import java.util.Base64;
41 import java.util.Map;
42 import java.util.function.BiConsumer;
43
44 public class AaiHttpClientFactory {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
47
48     private final AaiClientConfiguration configuration;
49     private final SslFactory sslFactory;
50
51
52     public AaiHttpClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
53         this.configuration = configuration;
54         this.sslFactory = sslFactory;
55     }
56
57     public AaiHttpClient<String> get() throws SSLException {
58         return new AaiHttpGetClient(configuration).createAaiHttpClient(build());
59     }
60
61     public AaiHttpClient<Integer> patch(JsonBodyBuilder jsonBodyBuilder) throws SSLException {
62         return new AaiHttpPatchClient(configuration, jsonBodyBuilder).createAaiHttpClient(build());
63     }
64
65     private HttpClient build() throws SSLException {
66         LOGGER.debug("Setting ssl context");
67
68         SslContext sslContext = createSslContext();
69
70         return HttpClient.create()
71                 .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
72                 .headers(this::settingHeaders)
73                 .doOnRequest(logRequest())
74                 .doOnResponse(logResponse());
75     }
76
77     private SslContext createSslContext() throws SSLException {
78         if (configuration.enableAaiCertAuth()) {
79             return sslFactory.createSecureContext(
80                     configuration.keyStorePath(),
81                     configuration.keyStorePasswordPath(),
82                     configuration.trustStorePath(),
83                     configuration.trustStorePasswordPath()
84             );
85         }
86         return sslFactory.createInsecureContext();
87     }
88
89     private HttpHeaders settingHeaders(HttpHeaders httpHeaders) {
90         httpHeaders.add("Authorization", "Basic " + performBasicAuthentication());
91         for(Map.Entry<String,String> header : configuration.aaiHeaders().entrySet())
92             httpHeaders.add(header.getKey(), header.getValue());
93         return httpHeaders;
94     }
95
96     private String performBasicAuthentication() {
97         return Base64.getEncoder().encodeToString(
98                 (configuration.aaiUserName() + ":" + configuration.aaiUserPassword()).getBytes()
99         );
100     }
101
102     private static BiConsumer<HttpClientRequest, Connection> logRequest() {
103         return (httpClientRequest, connection) -> {
104             LOGGER.info("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
105             httpClientRequest.requestHeaders().forEach(stringStringEntry ->
106                     LOGGER.info("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue())
107             );
108         };
109     }
110
111     private static BiConsumer<? super HttpClientResponse, ? super Connection>  logResponse() {
112         return (httpClientResponse, connection) ->
113                 LOGGER.info("ResponseStatus {}", httpClientResponse.status().code());
114     }
115 }