0245a1537ab5b20b2793f523c8b3968a8a2fb360
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / SdncJsonHelper.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.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.util.ArrayList;
30 import java.util.List;
31
32 import org.json.JSONArray;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38
39 /**
40  * Common json functionality used by the SDNC clients
41  */
42 class SdncJsonHelper {
43     private static Gson gson = new GsonBuilder() //
44         .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
45         .create();
46
47     private SdncJsonHelper() {
48     }
49
50     public static Flux<String> parseJsonArrayOfString(String inputString) {
51         try {
52             List<String> arrayList = new ArrayList<>();
53             if (!inputString.isEmpty()) {
54                 JSONArray jsonArray = new JSONArray(inputString);
55                 for (int i = 0; i < jsonArray.length(); i++) {
56                     arrayList.add(jsonArray.getString(i));
57                 }
58             }
59             return Flux.fromIterable(arrayList);
60         } catch (JSONException ex) { // invalid json
61             return Flux.error(ex);
62         }
63     }
64
65     public static <T> String createInputJsonString(T params) {
66         JsonElement paramsJson = gson.toJsonTree(params);
67         JsonObject jsonObj = new JsonObject();
68         jsonObj.add("input", paramsJson);
69         return gson.toJson(jsonObj);
70     }
71
72     public static Mono<String> getValueFromResponse(String response, String key) {
73         try {
74             JSONObject outputJson = new JSONObject(response);
75             JSONObject responseParams = outputJson.getJSONObject("output");
76             if (!responseParams.has(key)) {
77                 return Mono.just("");
78             }
79             String value = responseParams.get(key).toString();
80             return Mono.just(value);
81         } catch (JSONException ex) { // invalid json
82             return Mono.error(ex);
83         }
84     }
85
86     public static Mono<String> extractPolicySchema(String inputString) {
87         try {
88             JSONObject jsonObject = new JSONObject(inputString);
89             JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
90             String schemaString = schemaObject.toString();
91             return Mono.just(schemaString);
92         } catch (JSONException ex) { // invalid json
93             return Mono.error(ex);
94         }
95     }
96 }