2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2021 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.dcaegen2.services.sdk.rest.services.cbs.client.impl;
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonObject;
25 import org.jetbrains.annotations.NotNull;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
28 import org.onap.dcaegen2.services.sdk.services.common.FileReader;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.yaml.snakeyaml.Yaml;
32 import reactor.core.publisher.Mono;
33 import java.util.LinkedHashMap;
35 public class CbsClientConfigMap implements CbsClient {
37 private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class);
38 private final String configMapFilePath;
41 public CbsClientConfigMap (String configMapFilePath) {
42 this.configMapFilePath = configMapFilePath;
46 public @NotNull Mono<JsonObject> get(CbsRequest request) {
47 return Mono.just(this.loadConfigMapFile())
48 .map(CbsClientEnvironmentParsing::processEnvironmentVariables)
49 .doOnNext(this::logConfigMapOutput);
52 public boolean verifyConfigMapFile() {
54 LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
55 this.loadConfigMapFile().isJsonObject();
57 } catch(Exception ex) {
58 this.logConfigMapError(ex);
63 private JsonObject loadConfigMapFile() {
64 Gson gson = new GsonBuilder().create();
65 return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
68 private Object loadYamlConfigMapFile() {
69 return new Yaml().load(new FileReader(configMapFilePath).getContent());
74 private void logConfigMapOutput(JsonObject jsonObject) {
75 LOGGER.info("Got successful output from ConfigMap file");
76 LOGGER.debug("ConfigMap output: {}", jsonObject);
79 private void logConfigMapError(Exception ex) {
80 LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());