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