2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.util;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.common.collect.Lists;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonObject;
33 import com.google.gson.JsonPrimitive;
35 import java.time.Instant;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Optional;
39 import java.util.Spliterator;
40 import java.util.Spliterators;
41 import java.util.stream.Collectors;
42 import java.util.stream.StreamSupport;
44 import org.onap.clamp.clds.model.properties.AbstractModelElement;
45 import org.onap.clamp.clds.service.SecureServicePermission;
46 import org.onap.clamp.clds.service.SecureServicePermissionDeserializer;
47 import org.onap.clamp.dao.model.gson.converter.InstantDeserializer;
48 import org.onap.clamp.dao.model.gson.converter.InstantSerializer;
51 * This class is used to access the GSON with restricted type access.
53 public class JsonUtils {
55 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractModelElement.class);
56 private static final String LOG_ELEMENT_NOT_FOUND = "Value '{}' for key 'name' not found in JSON";
57 private static final String LOG_ELEMENT_NOT_FOUND_IN_JSON = "Value '{}' for key 'name' not found in JSON {}";
59 public static final Gson GSON = new GsonBuilder()
60 .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()).create();
62 public static final Gson GSON_JPA_MODEL = new GsonBuilder()
63 .registerTypeAdapter(Instant.class, new InstantSerializer())
64 .registerTypeAdapter(Instant.class, new InstantDeserializer()).setPrettyPrinting()
65 .excludeFieldsWithoutExposeAnnotation().create();
71 * typeAdapter Return the value field of the json node element that has a name
72 * field equals to the given name.
74 public static String getStringValueByName(JsonElement jsonElement, String name) {
75 String value = extractJsonValueFromElement(jsonElement, name).map(JsonUtils::extractStringValueFromElement)
78 if (logger.isDebugEnabled()) {
79 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, jsonElement.toString());
81 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
88 * Return an array of values for the field of the json node element that has a
89 * name field equals to the given name.
91 public static List<String> getStringValuesByName(JsonElement jsonElement, String name) {
92 List<String> values = extractJsonValueFromElement(jsonElement, name)
93 .map(JsonUtils::extractStringValuesFromElement).orElse(new ArrayList<>());
94 if (values.isEmpty()) {
95 if (logger.isDebugEnabled()) {
96 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, jsonElement.toString());
98 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
105 * Return the int value field of the json node element that has a name field
106 * equals to the given name.
108 public static Integer getIntValueByName(JsonElement element, String name) {
109 String value = getStringValueByName(element, name);
110 return Integer.valueOf(value);
114 * Return the Json value field of the json node element that has a name field
115 * equals to the given name.
117 public static JsonObject getJsonObjectByName(JsonElement jsonElement, String name) {
118 JsonObject jsonObject = extractJsonValueFromElement(jsonElement, name).map(JsonElement::getAsJsonObject)
120 if (jsonObject == null) {
121 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
123 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, jsonElement.toString());
128 private static Optional<JsonElement> extractJsonValueFromElement(JsonElement jsonElement, String name) {
129 if (jsonElement != null) {
130 if (jsonElement.isJsonArray()) {
131 return extractValueJsonFromArray(jsonElement, name);
132 } else if (hasMatchingParameterName(name, jsonElement)) {
133 return Optional.of(jsonElement);
136 return Optional.empty();
139 private static Optional<JsonElement> extractValueJsonFromArray(JsonElement jsonElement, String name) {
140 for (JsonElement element : jsonElement.getAsJsonArray()) {
141 if (hasMatchingParameterName(name, element)) {
142 return Optional.of(element.getAsJsonObject().get("value"));
145 return Optional.empty();
148 private static boolean hasMatchingParameterName(String name, JsonElement element) {
149 return element.isJsonObject() && element.getAsJsonObject().has("name")
150 && name.equals(element.getAsJsonObject().get("name").getAsString());
153 private static String extractStringValueFromElement(JsonElement element) {
154 if (element.isJsonArray()) {
155 return element.getAsJsonArray().get(0).getAsString();
156 } else if (element.isJsonPrimitive()) {
157 return element.getAsJsonPrimitive().getAsString();
159 return GSON.toJson(element);
163 private static List<String> extractStringValuesFromElement(JsonElement element) {
164 if (element.isJsonArray()) {
166 .stream(Spliterators.spliteratorUnknownSize(element.getAsJsonArray().iterator(), Spliterator.ORDERED),
168 .filter(JsonElement::isJsonPrimitive).map(JsonElement::getAsJsonPrimitive)
169 .filter(JsonPrimitive::isString).map(JsonPrimitive::getAsString).collect(Collectors.toList());
171 String value = extractStringValueFromElement(element);
172 return Lists.newArrayList(value);