X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=plugins%2Fplugins-context%2Fplugins-context-schema%2Fplugins-context-schema-avro%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fapex%2Fplugins%2Fcontext%2Fschema%2Favro%2FAvroSchemaKeyTranslationUtilities.java;h=9654572065918dbbd3cdf49dd14bce699f255735;hb=HEAD;hp=b4c8737dd3d61c8f747dce3b1f8c026defb05a64;hpb=4cfa2e2d98f6877d54da304ef17f096284430908;p=policy%2Fapex-pdp.git diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java index b4c8737dd..babca5cb2 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java @@ -1,19 +1,21 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -24,29 +26,28 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; - import java.util.Map.Entry; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; /** * This static final class contains utility methods for Avro schemas. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class AvroSchemaKeyTranslationUtilities { // Constants for key replacements - private static final String DOT_STRING = "\\."; + private static final String DOT_STRING = "."; private static final String DOT_STRING_REPLACEMENT = "_DoT_"; private static final String DASH_STRING = "-"; private static final String DASH_STRING_REPLACEMENT = "_DasH_"; + private static final String COLON_STRING = ":"; + private static final String COLON_STRING_REPLACEMENT = "_ColoN_"; /** - * Default constructor to avoid subclassing. - */ - private AvroSchemaKeyTranslationUtilities() {} - - /** - * Translate characters in JSON keys to values that are legal in Avro. Avro names must start - * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] + * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and + * subsequently contain only [A-Za-z0-9_] * * @param jsonString The JSON string to translate * @param revert True if we want to revert the field names to their original values @@ -58,17 +59,17 @@ public final class AvroSchemaKeyTranslationUtilities { } // Create a JSON element for the incoming JSON string - final JsonElement jsonElement = - new GsonBuilder().serializeNulls().create().fromJson(jsonString, JsonElement.class); + final var jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString, + JsonElement.class); - final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert); + final var translatedJsonElement = translateIllegalKeys(jsonElement, revert); return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement); } /** - * Translate characters in JSON keys to values that are legal in Avro. Avro names must start - * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] + * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and + * subsequently contain only [A-Za-z0-9_] * * @param jsonElement The JSON element to translate * @param revert True if we want to revert the field names to their original values @@ -86,15 +87,15 @@ public final class AvroSchemaKeyTranslationUtilities { } /** - * Translate characters in JSON keys to values that are legal in Avro. Avro names must start - * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] + * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and + * subsequently contain only [A-Za-z0-9_] * * @param jsonObject The JSON object to translate * @param revert True if we want to revert the field names to their original values * @return the translated JSON element */ public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) { - final JsonObject newJsonObject = new JsonObject(); + final var newJsonObject = new JsonObject(); for (final Entry jsonObjectEntry : jsonObject.entrySet()) { newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert), @@ -105,17 +106,17 @@ public final class AvroSchemaKeyTranslationUtilities { } /** - * Translate characters in JSON keys to values that are legal in Avro. Avro names must start - * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] + * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and + * subsequently contain only [A-Za-z0-9_] * * @param jsonArray The JSON array to translate * @param revert True if we want to revert the field names to their original values * @return the translated JSON element */ public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) { - final JsonArray newJsonArray = new JsonArray(); + final var newJsonArray = new JsonArray(); - for (int i = 0; i < jsonArray.size(); i++) { + for (var i = 0; i < jsonArray.size(); i++) { newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert)); } @@ -123,8 +124,8 @@ public final class AvroSchemaKeyTranslationUtilities { } /** - * Translate characters in a single JSON key to values that are legal in Avro. Avro names must - * start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] + * Translate characters in a single JSON key to values that are legal in Avro. Avro names must start with [A-Za-z_] + * and subsequently contain only [A-Za-z0-9_] * * @param key The key to translate * @param revert True if we want to revert the field names to their original values @@ -132,9 +133,13 @@ public final class AvroSchemaKeyTranslationUtilities { */ private static String translateIllegalKey(final String key, final boolean revert) { if (revert) { - return key.replaceAll(DOT_STRING_REPLACEMENT, DOT_STRING).replaceAll(DASH_STRING_REPLACEMENT, DASH_STRING); + return key.replace(DOT_STRING_REPLACEMENT, DOT_STRING) + .replace(DASH_STRING_REPLACEMENT, DASH_STRING) + .replace(COLON_STRING_REPLACEMENT, COLON_STRING); } else { - return key.replaceAll(DOT_STRING, DOT_STRING_REPLACEMENT).replaceAll(DASH_STRING, DASH_STRING_REPLACEMENT); + return key.replace(DOT_STRING, DOT_STRING_REPLACEMENT) + .replace(DASH_STRING, DASH_STRING_REPLACEMENT) + .replace(COLON_STRING, COLON_STRING_REPLACEMENT); } } }