875e20ed1e1c22745e020c584d2afc0a33b94200
[dcaegen2/services/sdk.git] /
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     public CbsClientConfigMap (String configMapFilePath) {
41         this.configMapFilePath = configMapFilePath;
42     }
43
44     @Override
45     public @NotNull Mono<JsonObject> get(CbsRequest request) {
46         return Mono.just(this.loadConfigMapFile())
47                 .doOnNext(this::logConfigMapOutput);
48     }
49
50     public boolean verifyConfigMapFile() {
51         try {
52             LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
53             this.loadConfigMapFile().isJsonObject();
54             return true;
55         } catch(Exception ex) {
56             this.logConfigMapError(ex);
57             return false;
58         }
59     }
60
61     private JsonObject loadConfigMapFile() {
62         Gson gson = new GsonBuilder().create();
63         return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
64     }
65
66     private Object loadYamlConfigMapFile() {
67         return new Yaml().load(new FileReader(configMapFilePath).getContent());
68     }
69
70     private void logConfigMapOutput(JsonObject jsonObject) {
71         LOGGER.info("Got successful output from ConfigMap file");
72         LOGGER.debug("ConfigMap output: {}", jsonObject);
73     }
74
75     private void logConfigMapError(Exception ex) {
76         LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());
77     }
78 }