Print the short description of exception in OscA1Client
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / JsonHelper.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 java.util.ArrayList;
27 import java.util.List;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
33
34 public class JsonHelper {
35     private static Gson gson = new GsonBuilder() //
36         .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
37         .create();
38
39     private JsonHelper() {
40
41     }
42
43     public static Flux<String> parseJsonArrayOfString(String inputString) {
44         try {
45             List<String> arrayList = new ArrayList<>();
46             if (!inputString.isEmpty()) {
47                 JSONArray jsonArray = new JSONArray(inputString);
48                 for (int i = 0; i < jsonArray.length(); i++) {
49                     arrayList.add(jsonArray.getString(i));
50                 }
51             }
52             return Flux.fromIterable(arrayList);
53         } catch (JSONException ex) { // invalid json
54             return Flux.error(ex);
55         }
56     }
57
58     public static <T> String createInputJsonString(T params) {
59         JSONObject inputJson = new JSONObject();
60         inputJson.put("input", gson.toJson(params));
61         return inputJson.toString();
62     }
63
64     public static Mono<String> getValueFromResponse(String response, String key) {
65         try {
66             JSONObject outputJson = new JSONObject(response);
67             JSONObject responseParams = outputJson.getJSONObject("output");
68             if (!responseParams.has(key)) {
69                 return Mono.just("");
70             }
71             String value = responseParams.get(key).toString();
72             return Mono.just(value);
73         } catch (JSONException ex) { // invalid json
74             return Mono.error(ex);
75         }
76     }
77
78     public static Mono<String> extractPolicySchema(String inputString) {
79         try {
80             JSONObject jsonObject = new JSONObject(inputString);
81             JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
82             String schemaString = schemaObject.toString();
83             return Mono.just(schemaString);
84         } catch (JSONException ex) { // invalid json
85             return Mono.error(ex);
86         }
87     }
88
89     public static Mono<String> validateJson(String inputString) {
90         try {
91             new JSONObject(inputString);
92             return Mono.just(inputString);
93         } catch (JSONException ex) { // invalid json
94             return Mono.error(ex);
95         }
96     }
97 }