2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. 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.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.util.Map.Entry;
34 import java.util.stream.Collectors;
36 import io.vavr.control.Option;
37 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.GsonAdaptersAafCredentials;
38 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.GsonAdaptersDataRouterSink;
39 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.GsonAdaptersDataRouterSource;
42 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
45 final class GsonUtils {
46 private static final Lazy<Gson> GSON = Lazy.of(() -> {
47 GsonBuilder gsonBuilder = new GsonBuilder();
48 gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersKafkaInfo());
49 gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersAafCredentials());
50 gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersMessageRouterDmaapInfo());
51 gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersDataRouterSink());
52 gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersDataRouterSource());
53 return gsonBuilder.create();
59 static Gson gsonInstance() {
63 static void assertStreamType(JsonObject json, String expectedType) {
64 final String actualType = requiredString(json, "type");
65 if (!actualType.equals(expectedType)) {
66 throw new IllegalArgumentException("Invalid stream type. Expected '" + expectedType + "', but was '" + actualType + "'");
70 static String requiredString(JsonObject parent, String childName) {
71 return requiredChild(parent, childName).getAsString();
74 static Option<String> optionalString(JsonObject parent, String childName) {
75 return Option.of(parent.get(childName).getAsString());
78 static JsonElement requiredChild(JsonObject parent, String childName) {
79 if (parent.has(childName)) {
80 return parent.get(childName);
82 throw new IllegalArgumentException(
83 "Could not find sub-node '" + childName + "'. Actual sub-nodes: " + stringifyChildrenNames(parent));
87 static JsonObject readObjectFromResource(String resource) throws IOException {
88 return readFromResource(resource).getAsJsonObject();
91 static JsonElement readFromResource(String resource) throws IOException {
92 try (Reader reader = new InputStreamReader(GsonUtils.class.getResourceAsStream(resource))) {
93 return new JsonParser().parse(reader);
97 private static String stringifyChildrenNames(JsonObject parent) {
98 return parent.entrySet().stream().map(Entry::getKey).collect(Collectors.joining(", "));