f0293564b5c3056f7080f95c3d790cc95abb2d18
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * Copyright (C) 2021 Wipro Limited.
7  * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
23
24 import org.jetbrains.annotations.NotNull;
25 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientConfigMap;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
29 import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import reactor.core.publisher.Mono;
33
34 /**
35  * <p>
36  * Factory for Config Binding Service client.
37  * </p>
38  *
39  * @since 1.1.2
40  */
41 public class CbsClientFactory {
42     private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientFactory.class);
43
44     /**
45      * <p>Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.</p>
46      *
47      * <p>
48      * This method will do a lookup of Config Binding Service and create client configured with found address.
49      * Created client will be published in returned Mono instance.
50      * </p>
51      * <p>
52      * In case of failure during CBS resolution, returned Mono will emit error signal with possible cause.
53      * User is expected to handle this signal and possibly retry subscription to returned Mono.
54      * </p>
55      *
56      * @param configuration required CBS configuration as viewed by client application
57      * @return non-null {@link Mono} of {@link CbsClient} instance
58      * @since 1.1.2
59      */
60     public static @NotNull Mono<CbsClient> createCbsClient(CbsClientConfiguration configuration) {
61         LOGGER.info("Configuration used for CBS Client: {}", configuration);
62         return Mono.fromCallable(() -> buildHttpClient(configuration.trustStoreKeys()))
63             .cache()
64             .flatMap(httpClient -> createCbsClientMono(httpClient, configuration));
65     }
66
67     private static RxHttpClient buildHttpClient(TrustStoreKeys trustStoreKeys) {
68         return trustStoreKeys != null
69                 ? RxHttpClientFactory.create(trustStoreKeys)
70                 : RxHttpClientFactory.create();
71     }
72
73     private static Mono<CbsClient> createCbsClientMono(RxHttpClient httpClient,
74         CbsClientConfiguration configuration) {
75             CbsClientConfigMap cbsClientConfigMap = new CbsClientConfigMap(configuration.configMapFilePath(),
76                     configuration.policySyncFilePath(), configuration.appName());
77         return Mono.just(cbsClientConfigMap);
78     }
79 }