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.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonPrimitive;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.EnvironmentParsingException;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
32 * <p>Config Binding Service client environment variables parsing.</p>
36 public class CbsClientEnvironmentParsing {
38 private static final Pattern shellEnvPattern = Pattern.compile("\\$\\{(.+?)}");
40 private CbsClientEnvironmentParsing() {}
44 * This method will do a lookup of shell variables in provided jsonObject and replace it with found environment variables.
47 * In case of failure during resolving environment variables, EnvironmentParsingException is thrown.
54 public static JsonObject processEnvironmentVariables(JsonObject jsonObject) {
55 JsonObject jsonObjectCopy = jsonObject.deepCopy();
56 processJsonObject(jsonObjectCopy);
57 return jsonObjectCopy;
59 private static void processJsonObject(JsonObject jsonObject) {
60 for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
61 processJsonObjectEntry(jsonObject, entry);
64 private static void processJsonObjectEntry(JsonObject jsonObject, Map.Entry<String, JsonElement> entry) {
65 if (entry.getValue().isJsonArray()) {
66 processJsonArray(entry.getValue().getAsJsonArray());
67 } else if (entry.getValue().isJsonObject()) {
68 processJsonObject(entry.getValue().getAsJsonObject());
70 Matcher matcher = getMatcher(entry.getValue().getAsString());
72 jsonObject.addProperty(entry.getKey(), getValueFromSystemEnv(matcher.group(1)));
77 private static void processJsonArray(JsonArray jsonArray) {
78 for (int i = 0; i < jsonArray.size(); i++) {
79 if (jsonArray.get(i).isJsonObject()) {
80 processJsonObject(jsonArray.get(i).getAsJsonObject());
81 } else if (jsonArray.get(i).isJsonArray()) {
82 processJsonArray(jsonArray.get(i).getAsJsonArray());
84 Matcher matcher = getMatcher(jsonArray.get(i).getAsString());
86 jsonArray.set(i, new JsonPrimitive(getValueFromSystemEnv(matcher.group(1))));
92 private static Matcher getMatcher(String value) {
93 return shellEnvPattern.matcher(value);
96 private static String getValueFromSystemEnv( String envName) {
97 String envValue = System.getenv(envName);
98 if (envValue == null || "".equals(envValue)) {
99 throw new EnvironmentParsingException("Cannot read " + envName + " from environment.");