Dependency management update
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-schema / plugins-context-schema-avro / src / main / java / org / onap / policy / apex / plugins / context / schema / avro / AvroSchemaKeyTranslationUtilities.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.util.Map.Entry;
29
30 /**
31  * This static final class contains utility methods for Avro schemas.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public final class AvroSchemaKeyTranslationUtilities {
36     // Constants for key replacements
37     private static final String DOT_STRING = ".";
38     private static final String DOT_STRING_REPLACEMENT = "_DoT_";
39     private static final String DASH_STRING = "-";
40     private static final String DASH_STRING_REPLACEMENT = "_DasH_";
41
42     /**
43      * Default constructor to avoid subclassing.
44      */
45     private AvroSchemaKeyTranslationUtilities() {
46         // Private constructor to prevent subclassing
47     }
48
49     /**
50      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
51      * subsequently contain only [A-Za-z0-9_]
52      *
53      * @param jsonString The JSON string to translate
54      * @param revert True if we want to revert the field names to their original values
55      * @return the translated JSON string
56      */
57     public static String translateIllegalKeys(final String jsonString, final boolean revert) {
58         if (jsonString == null) {
59             return jsonString;
60         }
61
62         // Create a JSON element for the incoming JSON string
63         final JsonElement jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
64                 JsonElement.class);
65
66         final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
67
68         return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
69     }
70
71     /**
72      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
73      * subsequently contain only [A-Za-z0-9_]
74      *
75      * @param jsonElement The JSON element to translate
76      * @param revert True if we want to revert the field names to their original values
77      * @return the translated JSON element
78      */
79     public static JsonElement translateIllegalKeys(final JsonElement jsonElement, final boolean revert) {
80         // We only act on JSON objects and arrays
81         if (jsonElement.isJsonObject()) {
82             return translateIllegalKeys(jsonElement.getAsJsonObject(), revert);
83         } else if (jsonElement.isJsonArray()) {
84             return translateIllegalKeys(jsonElement.getAsJsonArray(), revert);
85         } else {
86             return jsonElement;
87         }
88     }
89
90     /**
91      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
92      * subsequently contain only [A-Za-z0-9_]
93      *
94      * @param jsonObject The JSON object to translate
95      * @param revert True if we want to revert the field names to their original values
96      * @return the translated JSON element
97      */
98     public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
99         final JsonObject newJsonObject = new JsonObject();
100
101         for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
102             newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
103                     translateIllegalKeys(jsonObjectEntry.getValue(), revert));
104         }
105
106         return newJsonObject;
107     }
108
109     /**
110      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
111      * subsequently contain only [A-Za-z0-9_]
112      *
113      * @param jsonArray The JSON array to translate
114      * @param revert True if we want to revert the field names to their original values
115      * @return the translated JSON element
116      */
117     public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
118         final JsonArray newJsonArray = new JsonArray();
119
120         for (int i = 0; i < jsonArray.size(); i++) {
121             newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
122         }
123
124         return newJsonArray;
125     }
126
127     /**
128      * Translate characters in a single JSON key to values that are legal in Avro. Avro names must start with [A-Za-z_]
129      * and subsequently contain only [A-Za-z0-9_]
130      *
131      * @param key The key to translate
132      * @param revert True if we want to revert the field names to their original values
133      * @return the translated key
134      */
135     private static String translateIllegalKey(final String key, final boolean revert) {
136         if (revert) {
137             return key.replace(DOT_STRING_REPLACEMENT, DOT_STRING).replace(DASH_STRING_REPLACEMENT, DASH_STRING);
138         } else {
139             return key.replace(DOT_STRING, DOT_STRING_REPLACEMENT).replace(DASH_STRING, DASH_STRING_REPLACEMENT);
140         }
141     }
142 }