2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. 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=========================================================
20 package org.onap.dcae.configuration.cbs;
22 import com.google.gson.JsonObject;
23 import io.vavr.control.Option;
24 import java.util.function.Consumer;
25 import org.json.JSONObject;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 public class CbsConfigResolver {
38 private static final Logger log = LoggerFactory.getLogger(CbsConfigResolver.class);
40 private final CbsClientConfiguration cbsClientConfiguration;
41 private final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
42 private final CbsRequest cbsConfigurationRequest = CbsRequests.getConfiguration(diagnosticContext);
44 CbsConfigResolver(CbsClientConfiguration cbsClientConfiguration) {
45 this.cbsClientConfiguration = cbsClientConfiguration;
48 public Option<JSONObject> getAppConfig() {
49 JsonObject emptyJson = new JsonObject();
50 JsonObject jsonObject = CbsClientFactory.createCbsClient(cbsClientConfiguration)
51 .flatMap(cbsClient -> cbsClient.get(cbsConfigurationRequest))
52 .doOnError(error -> log.warn("Failed to fetch configuration from CBS " + error.getMessage()))
53 .onErrorReturn(emptyJson)
56 return emptyJson.equals(jsonObject) ? Option.none() : Option.of(new JSONObject(jsonObject.toString()));