e4f330e018a6385bd0ef603ade843e40963e76af
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 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.prh.service;
22
23 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME;
25 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
26
27 import io.netty.handler.ssl.SslContext;
28 import java.util.Map;
29 import javax.net.ssl.SSLException;
30 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
31 import org.onap.dcaegen2.services.prh.ssl.SslFactory;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.slf4j.MDC;
35 import org.springframework.http.client.reactive.ClientHttpConnector;
36 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
37 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import reactor.core.publisher.Mono;
40 import reactor.netty.http.client.HttpClient;
41
42
43 public class AaiReactiveWebClientFactory {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(AaiReactiveWebClientFactory.class);
46
47     private final String aaiUserName;
48     private final String aaiUserPassword;
49     private final Map<String, String> aaiHeaders;
50     private final Boolean enableAaiCertAuth;
51     private final String trustStorePath;
52     private final String trustStorePasswordPath;
53     private final String keyStorePath;
54     private final String keyStorePasswordPath;
55     private final SslFactory sslFactory;
56
57     /**
58      * Creating AaiReactiveWebClientFactory.
59      *
60      * @param configuration - configuration object
61      * @param sslFactory - factory for ssl setup
62      */
63     public AaiReactiveWebClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
64         this.aaiUserName = configuration.aaiUserName();
65         this.aaiUserPassword = configuration.aaiUserPassword();
66         this.aaiHeaders = configuration.aaiHeaders();
67         this.trustStorePath = configuration.trustStorePath();
68         this.trustStorePasswordPath = configuration.trustStorePasswordPath();
69         this.keyStorePath = configuration.keyStorePath();
70         this.keyStorePasswordPath = configuration.keyStorePasswordPath();
71         this.enableAaiCertAuth = configuration.enableAaiCertAuth();
72         this.sslFactory = sslFactory;
73     }
74
75     /**
76      * Construct Reactive WebClient with appropriate settings.
77      *
78      * @return WebClient
79      */
80     public WebClient build() throws SSLException {
81         LOGGER.debug("Setting ssl context");
82         
83         SslContext sslContext = createSslContext();
84         
85         ClientHttpConnector reactorClientHttpConnector = new ReactorClientHttpConnector(
86             HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)));
87
88         return WebClient.builder()
89             .clientConnector(reactorClientHttpConnector)
90             .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
91             .filter(basicAuthentication(aaiUserName, aaiUserPassword))
92             .filter(logRequest())
93             .filter(logResponse())
94             .build();
95     }
96
97     private SslContext createSslContext() throws SSLException {
98         if (enableAaiCertAuth) {
99             return sslFactory.createSecureContext(
100                 keyStorePath,
101                 keyStorePasswordPath,
102                 trustStorePath,
103                 trustStorePasswordPath
104             );
105         }
106         return sslFactory.createInsecureContext();
107     }
108     
109     private ExchangeFilterFunction logRequest() {
110         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
111             MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
112             LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url());
113             clientRequest.headers()
114                 .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
115             MDC.remove(SERVICE_NAME);
116             return Mono.just(clientRequest);
117         });
118     }
119
120     private ExchangeFilterFunction logResponse() {
121         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
122             MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
123             LOGGER.info("Response Status {}", clientResponse.statusCode());
124             MDC.remove(RESPONSE_CODE);
125             return Mono.just(clientResponse);
126         });
127     }
128 }