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