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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.context.schema.avro;
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;
34 * This static final class contains utility methods for Avro schemas.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
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_";
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_]
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
56 public static String translateIllegalKeys(final String jsonString, final boolean revert) {
57 if (jsonString == null) {
61 // Create a JSON element for the incoming JSON string
62 final var jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
65 final var translatedJsonElement = translateIllegalKeys(jsonElement, revert);
67 return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
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_]
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
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);
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_]
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
97 public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
98 final var newJsonObject = new JsonObject();
100 for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
101 newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
102 translateIllegalKeys(jsonObjectEntry.getValue(), revert));
105 return newJsonObject;
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_]
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
116 public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
117 final var newJsonArray = new JsonArray();
119 for (var i = 0; i < jsonArray.size(); i++) {
120 newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
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_]
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
134 private static String translateIllegalKey(final String key, final boolean revert) {
136 return key.replace(DOT_STRING_REPLACEMENT, DOT_STRING)
137 .replace(DASH_STRING_REPLACEMENT, DASH_STRING)
138 .replace(COLON_STRING_REPLACEMENT, COLON_STRING);
140 return key.replace(DOT_STRING, DOT_STRING_REPLACEMENT)
141 .replace(DASH_STRING, DASH_STRING_REPLACEMENT)
142 .replace(COLON_STRING, COLON_STRING_REPLACEMENT);