2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2021 Nokia. All rights reserved.
6 * Copyright (C) 2021 Wipro Limited.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonObject;
28 import java.util.LinkedHashMap;
30 import org.jetbrains.annotations.NotNull;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
33 import org.onap.dcaegen2.services.sdk.services.common.FileReader;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.yaml.snakeyaml.Yaml;
37 import reactor.core.publisher.Mono;
39 public class CbsClientConfigMap implements CbsClient {
41 private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class);
42 private final String configMapFilePath;
43 private String policySyncFilePath = "";
44 private String appName = "";
46 public CbsClientConfigMap(String configMapFilePath) {
47 this.configMapFilePath = configMapFilePath;
50 public CbsClientConfigMap(String configMapFilePath, String policySyncFilePath, String appName) {
51 this.configMapFilePath = configMapFilePath;
52 this.policySyncFilePath = policySyncFilePath;
53 this.appName = appName;
57 public @NotNull Mono<JsonObject> get(CbsRequest request) {
58 Mono<JsonObject> configJsonMono =
59 Mono.just(this.loadConfigMapFile()).map(CbsClientEnvironmentParsing::processEnvironmentVariables);
60 if (this.shouldReadPolicySyncFile(request)) {
62 return configJsonMono.map(this::loadPolicySyncFile).doOnNext(this::logConfigMapOutput);
64 return configJsonMono.doOnNext(this::logConfigMapOutput);
67 public boolean verifyConfigMapFile() {
69 LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
70 this.loadConfigMapFile().isJsonObject();
72 } catch (Exception ex) {
73 this.logConfigMapError(ex);
78 private JsonObject loadConfigMapFile() {
79 Gson gson = new GsonBuilder().create();
80 return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
83 private Object loadYamlConfigMapFile() {
84 return new Yaml().load(new FileReader(configMapFilePath).getContent());
87 private void logConfigMapOutput(JsonObject jsonObject) {
88 LOGGER.info("Got successful output from ConfigMap file");
89 LOGGER.debug("ConfigMap output: {}", jsonObject);
92 private void logConfigMapError(Exception ex) {
93 LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());
96 private boolean shouldReadPolicySyncFile(CbsRequest request) {
98 return request.requestPath()
99 .getForService(appName)
100 .contains("service_component_all");
101 } catch (Exception ex) {
102 LOGGER.error("Error finding requestPath", ex.getMessage());
107 private JsonObject loadPolicySyncFile(JsonObject configJsonObject) {
111 if (new FileReader(policySyncFilePath).doesFileExists()) {
112 LOGGER.info("PolicySync file is present");
113 Gson gson = new GsonBuilder().create();
114 JsonObject policyJsonObject = gson.fromJson(this.loadJsonStringPolicySyncFile(), JsonObject.class);
115 policyJsonObject.add("config", configJsonObject);
116 return policyJsonObject;
118 LOGGER.info("PolicySync file does not exist");
119 JsonObject policyJsonObject = new JsonObject();
120 policyJsonObject.add("config", configJsonObject);
121 return policyJsonObject;
123 } catch (Exception ex) {
124 LOGGER.error("PolicySync file does not contain a valid json");
125 JsonObject policyJsonObject = new JsonObject();
126 policyJsonObject.add("config", configJsonObject);
127 return policyJsonObject;
132 private String loadJsonStringPolicySyncFile() {
133 return new FileReader(policySyncFilePath).getContent();