aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java
blob: d4c264dabf22d8e9eef7b440d3521f71493a41f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.ccsdk.oran.a1policymanagementservice.clients;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Common json functionality used by the CCSDK A1 Adapter clients
 */
@SuppressWarnings("java:S1192") // Same text in several traces
class A1AdapterJsonHelper {
    private static Gson gson = new GsonBuilder() //
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
            .create();
    private static final String OUTPUT = "output";
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private A1AdapterJsonHelper() {}

    public static Flux<String> parseJsonArrayOfString(String inputString) {
        try {
            List<String> arrayList = new ArrayList<>();
            if (!inputString.isEmpty()) {
                JSONArray jsonArray = new JSONArray(inputString);
                for (int i = 0; i < jsonArray.length(); i++) {
                    Object value = jsonArray.get(i);
                    arrayList.add(value.toString());
                }
            }
            return Flux.fromIterable(arrayList);
        } catch (JSONException ex) { // invalid json
            logger.debug("Invalid json {}", ex.getMessage());
            return Flux.error(ex);
        }
    }

    public static <T> String createInputJsonString(T params) {
        JsonElement paramsJson = gson.toJsonTree(params);
        JsonObject jsonObj = new JsonObject();
        jsonObj.add("input", paramsJson);
        return gson.toJson(jsonObj);
    }

    public static <T> String createOutputJsonString(T params) {
        JsonElement paramsJson = gson.toJsonTree(params);
        JsonObject jsonObj = new JsonObject();
        jsonObj.add(OUTPUT, paramsJson);
        return gson.toJson(jsonObj);
    }

    public static Mono<JSONObject> getOutput(String response) {
        try {
            JSONObject outputJson = new JSONObject(response);
            JSONObject responseParams = outputJson.getJSONObject(OUTPUT);
            return Mono.just(responseParams);
        } catch (JSONException ex) { // invalid json
            logger.debug("Invalid json {}", ex.getMessage());
            return Mono.error(ex);
        }
    }

    public static Mono<String> getValueFromResponse(String response, String key) {
        return getOutput(response) //
                .map(responseParams -> {
                    if (!responseParams.has(key)) {
                        return "";
                    }
                    return responseParams.get(key).toString();
                });
    }

    public static Mono<String> extractPolicySchema(String inputString) {
        try {
            JSONObject jsonObject = new JSONObject(inputString);
            JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
            String schemaString = schemaObject.toString();
            return Mono.just(schemaString);
        } catch (JSONException ex) { // invalid json
            logger.debug("Invalid json {}", ex.getMessage());
            return Mono.error(ex);
        }
    }
}