2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.clients;
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;
29 import java.lang.invoke.MethodHandles;
30 import java.util.ArrayList;
31 import java.util.List;
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;
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
43 * Common json functionality used by the SDNC clients
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) //
50 private static final String OUTPUT = "output";
51 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 private SdncJsonHelper() {
56 public static Flux<String> parseJsonArrayOfString(String inputString) {
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 arrayList.add(jsonArray.getString(i));
65 return Flux.fromIterable(arrayList);
66 } catch (JSONException ex) { // invalid json
67 logger.debug("Invalid json {}", ex.getMessage());
68 return Flux.error(ex);
72 public static Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
74 JSONObject obj = new JSONObject(policyTypeResponse);
75 JSONObject schemaObj = obj.getJSONObject("create_schema");
76 schemaObj.put("title", policyTypeId);
77 return Mono.just(schemaObj.toString());
78 } catch (Exception e) {
79 String exceptionString = e.toString();
80 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
85 public static <T> String createInputJsonString(T params) {
86 JsonElement paramsJson = gson.toJsonTree(params);
87 JsonObject jsonObj = new JsonObject();
88 jsonObj.add("input", paramsJson);
89 return gson.toJson(jsonObj);
92 public static <T> String createOutputJsonString(T params) {
93 JsonElement paramsJson = gson.toJsonTree(params);
94 JsonObject jsonObj = new JsonObject();
95 jsonObj.add(OUTPUT, paramsJson);
96 return gson.toJson(jsonObj);
99 public static Mono<JSONObject> getOutput(String response) {
101 JSONObject outputJson = new JSONObject(response);
102 JSONObject responseParams = outputJson.getJSONObject(OUTPUT);
103 return Mono.just(responseParams);
104 } catch (JSONException ex) { // invalid json
105 logger.debug("Invalid json {}", ex.getMessage());
106 return Mono.error(ex);
110 public static Mono<String> getValueFromResponse(String response, String key) {
111 return getOutput(response) //
112 .flatMap(responseParams -> {
113 if (!responseParams.has(key)) {
114 return Mono.just("");
116 String value = responseParams.get(key).toString();
117 return Mono.just(value);
121 public static Mono<String> extractPolicySchema(String inputString) {
123 JSONObject jsonObject = new JSONObject(inputString);
124 JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
125 String schemaString = schemaObject.toString();
126 return Mono.just(schemaString);
127 } catch (JSONException ex) { // invalid json
128 logger.debug("Invalid json {}", ex.getMessage());
129 return Mono.error(ex);