7776a1ef1a063b428f1ce002ee73f34536bf9076
[dcaegen2/services/sdk.git] /
1 /*
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
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  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson;
22
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;
28 import io.vavr.Lazy;
29 import io.vavr.control.Option;
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;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParsingException;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.dmaap.mr.GsonAdaptersMessageRouterDmaapInfo;
37 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.kafka.GsonAdaptersKafkaInfo;
38 import org.onap.dcaegen2.services.sdk.model.streams.GsonAdaptersAafCredentials;
39 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.GsonAdaptersDataRouterSink;
40 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.GsonAdaptersDataRouterSource;
41
42 /**
43  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
44  * @since March 2019
45  */
46 public final class GsonUtils {
47
48     private static final Lazy<Gson> GSON = Lazy.of(() -> {
49         GsonBuilder gsonBuilder = new GsonBuilder();
50         gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersKafkaInfo());
51         gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersAafCredentials());
52         gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersMessageRouterDmaapInfo());
53         gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersDataRouterSink());
54         gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersDataRouterSource());
55         return gsonBuilder.create();
56     });
57
58     private GsonUtils() {
59     }
60
61     public  static Gson gsonInstance() {
62         return GSON.get();
63     }
64
65     public  static String requiredString(JsonObject parent, String childName) {
66         return requiredChild(parent, childName).getAsString();
67     }
68
69     public  static Option<String> optionalString(JsonObject parent, String childName) {
70             return Option.of(parent.get(childName).getAsString());
71     }
72
73     public static JsonElement requiredChild(JsonObject parent, String childName) {
74         return optionalChild(parent, childName)
75                 .getOrElseThrow(() -> new StreamParsingException(
76                         "Could not find sub-node '" + childName + "'. Actual sub-nodes: "
77                                 + stringifyChildrenNames(parent)));
78
79     }
80
81     public  static Option<JsonElement> optionalChild(JsonObject parent, String childName) {
82         return Option.of(parent.get(childName));
83     }
84
85     public  static JsonObject readObjectFromResource(String resource) throws IOException {
86         return readFromResource(resource).getAsJsonObject();
87     }
88
89     public static JsonElement readFromResource(String resource) throws IOException {
90         try (Reader reader = new InputStreamReader(GsonUtils.class.getResourceAsStream(resource))) {
91             return new JsonParser().parse(reader);
92         }
93     }
94
95     private static String stringifyChildrenNames(JsonObject parent) {
96         return parent.entrySet().stream().map(Entry::getKey).collect(Collectors.joining(", "));
97     }
98 }