Merge "Fix incorrect naming on context plugins"
[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 java.util.Map.Entry;
24
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
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
47     /**
48      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
49      * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
50      *
51      * @param jsonString The JSON string to translate
52      * @param revert True if we want to revert the field names to their original values
53      * @return the translated JSON string
54      */
55     public static String translateIllegalKeys(final String jsonString, final boolean revert) {
56         if (jsonString == null) {
57             return jsonString;
58         }
59
60         // Create a JSON element for the incoming JSON string
61         final JsonElement jsonElement =
62                 new GsonBuilder().serializeNulls().create().fromJson(jsonString, JsonElement.class);
63
64         final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
65
66         return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
67     }
68
69     /**
70      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
71      * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
72      *
73      * @param jsonElement The JSON element to translate
74      * @param revert True if we want to revert the field names to their original values
75      * @return the translated JSON element
76      */
77     public static JsonElement translateIllegalKeys(final JsonElement jsonElement, final boolean revert) {
78         // We only act on JSON objects and arrays
79         if (jsonElement.isJsonObject()) {
80             return translateIllegalKeys(jsonElement.getAsJsonObject(), revert);
81         } else if (jsonElement.isJsonArray()) {
82             return translateIllegalKeys(jsonElement.getAsJsonArray(), revert);
83         } else {
84             return jsonElement;
85         }
86     }
87
88     /**
89      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
90      * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
91      *
92      * @param jsonObject The JSON object to translate
93      * @param revert True if we want to revert the field names to their original values
94      * @return the translated JSON element
95      */
96     public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
97         final JsonObject newJsonObject = new JsonObject();
98
99         for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
100             newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
101                     translateIllegalKeys(jsonObjectEntry.getValue(), revert));
102         }
103
104         return newJsonObject;
105     }
106
107     /**
108      * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
109      * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
110      *
111      * @param jsonArray The JSON array to translate
112      * @param revert True if we want to revert the field names to their original values
113      * @return the translated JSON element
114      */
115     public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
116         final JsonArray newJsonArray = new JsonArray();
117
118         for (int i = 0; i < jsonArray.size(); i++) {
119             newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
120         }
121
122         return newJsonArray;
123     }
124
125     /**
126      * Translate characters in a single JSON key to values that are legal in Avro. Avro names must
127      * start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
128      *
129      * @param key The key to translate
130      * @param revert True if we want to revert the field names to their original values
131      * @return the translated key
132      */
133     private static String translateIllegalKey(final String key, final boolean revert) {
134         if (revert) {
135             return key.replaceAll(DOT_STRING_REPLACEMENT, DOT_STRING).replaceAll(DASH_STRING_REPLACEMENT, DASH_STRING);
136         } else {
137             return key.replaceAll(DOT_STRING, DOT_STRING_REPLACEMENT).replaceAll(DASH_STRING, DASH_STRING_REPLACEMENT);
138         }
139     }
140 }