Update CBS-Client to read policy configuration from a file exposed by policy-sidecar...
[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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
23
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonObject;
27
28 import java.util.LinkedHashMap;
29
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;
38
39 public class CbsClientConfigMap implements CbsClient {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class);
42     private final String configMapFilePath;
43     private String policySyncFilePath = "";
44     private String appName = "";
45
46     public CbsClientConfigMap(String configMapFilePath) {
47         this.configMapFilePath = configMapFilePath;
48     }
49
50     public CbsClientConfigMap(String configMapFilePath, String policySyncFilePath, String appName) {
51         this.configMapFilePath = configMapFilePath;
52         this.policySyncFilePath = policySyncFilePath;
53         this.appName = appName;
54     }
55
56     @Override
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)) {
61
62             return configJsonMono.map(this::loadPolicySyncFile).doOnNext(this::logConfigMapOutput);
63         }
64         return configJsonMono.doOnNext(this::logConfigMapOutput);
65     }
66
67     public boolean verifyConfigMapFile() {
68         try {
69             LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
70             this.loadConfigMapFile().isJsonObject();
71             return true;
72         } catch (Exception ex) {
73             this.logConfigMapError(ex);
74             return false;
75         }
76     }
77
78     private JsonObject loadConfigMapFile() {
79         Gson gson = new GsonBuilder().create();
80         return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
81     }
82
83     private Object loadYamlConfigMapFile() {
84         return new Yaml().load(new FileReader(configMapFilePath).getContent());
85     }
86
87     private void logConfigMapOutput(JsonObject jsonObject) {
88         LOGGER.info("Got successful output from ConfigMap file");
89         LOGGER.debug("ConfigMap output: {}", jsonObject);
90     }
91
92     private void logConfigMapError(Exception ex) {
93         LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());
94     }
95
96     private boolean shouldReadPolicySyncFile(CbsRequest request) {
97         try {
98             return request.requestPath()
99                           .getForService(appName)
100                           .contains("service_component_all");
101         } catch (Exception ex) {
102             LOGGER.error("Error finding requestPath", ex.getMessage());
103             return false;
104         }
105     }
106
107     private JsonObject loadPolicySyncFile(JsonObject configJsonObject) {
108
109         try {
110
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;
117             }
118             LOGGER.info("PolicySync file does not exist");
119             JsonObject policyJsonObject = new JsonObject();
120             policyJsonObject.add("config", configJsonObject);
121             return policyJsonObject;
122
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;
128         }
129
130     }
131
132     private String loadJsonStringPolicySyncFile() {
133         return new FileReader(policySyncFilePath).getContent();
134     }
135 }