108424d4b6790b8065baabc57517752af35907c0
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.clients;
22
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28
29 import java.lang.invoke.MethodHandles;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * Common json functionality used by the CCSDK A1 Adapter clients
43  */
44 @SuppressWarnings("java:S1192") // Same text in several traces
45 class A1AdapterJsonHelper {
46     private static Gson gson = new GsonBuilder() //
47             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
48             .create();
49     private static final String OUTPUT = "output";
50     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52     private A1AdapterJsonHelper() {}
53
54     public static Flux<String> parseJsonArrayOfString(String inputString) {
55         try {
56             List<String> arrayList = new ArrayList<>();
57             if (!inputString.isEmpty()) {
58                 JSONArray jsonArray = new JSONArray(inputString);
59                 for (int i = 0; i < jsonArray.length(); i++) {
60                     Object value = jsonArray.get(i);
61                     arrayList.add(value.toString());
62                 }
63             }
64             return Flux.fromIterable(arrayList);
65         } catch (Exception ex) { // invalid json
66             logger.debug("Invalid json {}", ex.getMessage());
67             return Flux.error(ex);
68         }
69     }
70
71     public static <T> String createInputJsonString(T params) {
72         JsonElement paramsJson = gson.toJsonTree(params);
73         JsonObject jsonObj = new JsonObject();
74         jsonObj.add("input", paramsJson);
75         return gson.toJson(jsonObj);
76     }
77
78     public static <T> String createOutputJsonString(T params) {
79         JsonElement paramsJson = gson.toJsonTree(params);
80         JsonObject jsonObj = new JsonObject();
81         jsonObj.add(OUTPUT, paramsJson);
82         return gson.toJson(jsonObj);
83     }
84
85     public static Mono<JSONObject> getOutput(String response) {
86         try {
87             JSONObject outputJson = new JSONObject(response);
88             JSONObject responseParams = outputJson.getJSONObject(OUTPUT);
89             return Mono.just(responseParams);
90         } catch (Exception ex) { // invalid json
91             logger.debug("Invalid json {}", ex.getMessage());
92             return Mono.error(ex);
93         }
94     }
95
96     public static Mono<String> getValueFromResponse(String response, String key) {
97         return getOutput(response) //
98                 .map(responseParams -> {
99                     if (!responseParams.has(key)) {
100                         return "";
101                     }
102                     return responseParams.get(key).toString();
103                 });
104     }
105
106     public static Mono<String> extractPolicySchema(String inputString) {
107         try {
108             JSONObject jsonObject = new JSONObject(inputString);
109             JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
110             String schemaString = schemaObject.toString();
111             return Mono.just(schemaString);
112         } catch (Exception ex) { // invalid json
113             logger.debug("Invalid json {}", ex.getMessage());
114             return Mono.error(ex);
115         }
116     }
117 }