42f536163f8ac2c82bf89f48d980fb7ae6180151
[dcaegen2/services/sdk.git] / rest-services / cbs-client / src / main / java / org / onap / dcaegen2 / services / sdk / rest / services / cbs / client / impl / CbsClientConfigMap.java
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
21
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;
34
35 public class CbsClientConfigMap implements CbsClient {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class);
38     private final String configMapFilePath;
39
40
41     public CbsClientConfigMap (String configMapFilePath) {
42         this.configMapFilePath = configMapFilePath;
43     }
44
45     @Override
46     public @NotNull Mono<JsonObject> get(CbsRequest request) {
47         return Mono.just(this.loadConfigMapFile())
48                 .map(CbsClientEnvironmentParsing::processEnvironmentVariables)
49                 .doOnNext(this::logConfigMapOutput);
50     }
51
52     public boolean verifyConfigMapFile() {
53         try {
54             LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
55             this.loadConfigMapFile().isJsonObject();
56             return true;
57         } catch(Exception ex) {
58             this.logConfigMapError(ex);
59             return false;
60         }
61     }
62
63     private JsonObject loadConfigMapFile() {
64         Gson gson = new GsonBuilder().create();
65         return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
66     }
67
68     private Object loadYamlConfigMapFile() {
69         return new Yaml().load(new FileReader(configMapFilePath).getContent());
70     }
71
72
73
74     private void logConfigMapOutput(JsonObject jsonObject) {
75         LOGGER.info("Got successful output from ConfigMap file");
76         LOGGER.debug("ConfigMap output: {}", jsonObject);
77     }
78
79     private void logConfigMapError(Exception ex) {
80         LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());
81     }
82 }