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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
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;
36 * Factory for Config Binding Service client.
41 public class CbsClientFactory {
42 private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientFactory.class);
45 * <p>Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.</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.
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.
56 * @param configuration required CBS configuration as viewed by client application
57 * @return non-null {@link Mono} of {@link CbsClient} instance
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()))
64 .flatMap(httpClient -> createCbsClientMono(httpClient, configuration));
67 private static RxHttpClient buildHttpClient(TrustStoreKeys trustStoreKeys) {
68 return trustStoreKeys != null
69 ? RxHttpClientFactory.create(trustStoreKeys)
70 : RxHttpClientFactory.create();
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);