Update snapshot and/or references of policy/apex-pdp to latest snapshots
[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-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.context.schema.avro;
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 import java.util.Map.Entry;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32
33 /**
34  * This static final class contains utility methods for Avro schemas.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class AvroSchemaKeyTranslationUtilities {
40     // Constants for key replacements
41     private static final String DOT_STRING = ".";
42     private static final String DOT_STRING_REPLACEMENT = "_DoT_";
43     private static final String DASH_STRING = "-";
44     private static final String DASH_STRING_REPLACEMENT = "_DasH_";
45     private static final String COLON_STRING = ":";
46     private static final String COLON_STRING_REPLACEMENT = "_ColoN_";
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 var jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
63                 JsonElement.class);
64
65         final var 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 var 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 var newJsonArray = new JsonArray();
118
119         for (var 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.replace(DOT_STRING_REPLACEMENT, DOT_STRING)
137                     .replace(DASH_STRING_REPLACEMENT, DASH_STRING)
138                     .replace(COLON_STRING_REPLACEMENT, COLON_STRING);
139         } else {
140             return key.replace(DOT_STRING, DOT_STRING_REPLACEMENT)
141                     .replace(DASH_STRING, DASH_STRING_REPLACEMENT)
142                     .replace(COLON_STRING, COLON_STRING_REPLACEMENT);
143         }
144     }
145 }