17144f1afc56d577ab58a19aa165d5c2143eb177
[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.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;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 /**
32  * <p>Config Binding Service client environment variables parsing.</p>
33  *
34  * @since 1.8.4
35  */
36 public class CbsClientEnvironmentParsing {
37
38     private static final Pattern shellEnvPattern = Pattern.compile("\\$\\{(.+?)}");
39
40     private CbsClientEnvironmentParsing() {}
41
42     /**
43      * <p>
44      * This method will do a lookup of shell variables in provided jsonObject and replace it with found environment variables.
45      * </p>
46      * <p>
47      * In case of failure during resolving environment variables, EnvironmentParsingException is thrown.
48      * </p>
49      *
50      * @param jsonObject
51      * @return JsonObject
52      * @since 1.8.4
53      */
54     public static JsonObject processEnvironmentVariables(JsonObject jsonObject) {
55         JsonObject jsonObjectCopy = jsonObject.deepCopy();
56         processJsonObject(jsonObjectCopy);
57         return jsonObjectCopy;
58     }
59     private static void processJsonObject(JsonObject jsonObject) {
60         for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
61             processJsonObjectEntry(jsonObject, entry);
62         }
63     }
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());
69         } else {
70             Matcher matcher = getMatcher(entry.getValue().getAsString());
71             if (matcher.find()) {
72                 jsonObject.addProperty(entry.getKey(), getValueFromSystemEnv(matcher.group(1)));
73             }
74         }
75     }
76
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());
83             } else {
84                 Matcher matcher = getMatcher(jsonArray.get(i).getAsString());
85                 if (matcher.find()) {
86                     jsonArray.set(i, new JsonPrimitive(getValueFromSystemEnv(matcher.group(1))));
87                 }
88             }
89         }
90     }
91
92     private static Matcher getMatcher(String value) {
93         return shellEnvPattern.matcher(value);
94     }
95
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.");
100         }
101         return envValue;
102     }
103 }