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