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;
40 public CbsClientConfigMap (String configMapFilePath) {
41 this.configMapFilePath = configMapFilePath;
45 public @NotNull Mono<JsonObject> get(CbsRequest request) {
46 return Mono.just(this.loadConfigMapFile())
47 .doOnNext(this::logConfigMapOutput);
50 public boolean verifyConfigMapFile() {
52 LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
53 this.loadConfigMapFile().isJsonObject();
55 } catch(Exception ex) {
56 this.logConfigMapError(ex);
61 private JsonObject loadConfigMapFile() {
62 Gson gson = new GsonBuilder().create();
63 return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
66 private Object loadYamlConfigMapFile() {
67 return new Yaml().load(new FileReader(configMapFilePath).getContent());
70 private void logConfigMapOutput(JsonObject jsonObject) {
71 LOGGER.info("Got successful output from ConfigMap file");
72 LOGGER.debug("ConfigMap output: {}", jsonObject);
75 private void logConfigMapError(Exception ex) {
76 LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());