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