Release version 1.1.0 of sli/plugins
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / restapicall / JsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.restapicall;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import org.apache.commons.lang3.StringUtils;
29 import org.codehaus.jettison.json.JSONArray;
30 import org.codehaus.jettison.json.JSONException;
31 import org.codehaus.jettison.json.JSONObject;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import static com.google.common.base.Preconditions.checkNotNull;
37
38 public final class JsonParser {
39
40     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
41
42     private JsonParser() {
43         // Preventing instantiation of the same.
44     }
45
46
47     private static void handleJsonArray(String key, Map<String, Object> jArrayMap, JSONArray jsonArr) throws JSONException {
48         JSONObject jsonObj;
49         JSONArray subJsonArr;
50         boolean stripKey = false;
51
52         for (int i = 0, length = jsonArr.length(); i < length; i++) {
53             if (stripKey)
54                 key = key.substring(0, key.length()-3);
55
56             subJsonArr = jsonArr.optJSONArray(i);
57             if (subJsonArr != null) {
58                 key = StringUtils.trimToEmpty(key) + "[" + i + "]";
59                 jArrayMap.putIfAbsent(key + "_length", String.valueOf(subJsonArr.length()));
60                 handleJsonArray(key, jArrayMap, subJsonArr);
61                 stripKey = true;
62                 continue;
63             }
64
65             jsonObj = jsonArr.optJSONObject(i);
66             if (jsonObj != null) {
67                 Iterator<String> ii = jsonObj.keys();
68                 while (ii.hasNext()) {
69                     String nodeKey = ii.next();
70                     String key1 = "[" + i + "]." + nodeKey;
71                     String[] subKey = key1.split(":");
72                     if (subKey.length == 2) {
73                         jArrayMap.putIfAbsent(subKey[1], jsonObj.get(nodeKey));
74                     } else {
75                         jArrayMap.putIfAbsent(key1, jsonObj.get(nodeKey));
76                     }
77                 }
78             }
79             else {
80                 jArrayMap.putIfAbsent(StringUtils.trimToEmpty(key), jsonArr);
81                 break;
82             }
83         }
84     }
85
86     @SuppressWarnings("unchecked")
87     public static Map<String, String> convertToProperties(String s)
88             throws SvcLogicException {
89
90         checkNotNull(s, "Input should not be null.");
91
92         try {
93             Map<String, Object> wm = new HashMap<>();
94             JSONObject json;
95             JSONArray jsonArr;
96             //support top level list in json response
97             if (s.startsWith("[")) {
98                 jsonArr = new JSONArray(s);
99                 wm.put("_length", String.valueOf(jsonArr.length()));
100                 handleJsonArray(null, wm, jsonArr);
101             } else {
102                 json = new JSONObject(s);
103                 Iterator<String> ii = json.keys();
104                 while (ii.hasNext()) {
105                     String key1 = ii.next();
106                     String[] subKey = key1.split(":");
107                     if (subKey.length == 2) {
108                         wm.put(subKey[1], json.get(key1));
109                     } else {
110                         wm.put(key1, json.get(key1));
111                     }
112                 }
113             }
114
115             Map<String, String> mm = new HashMap<>();
116             while (!wm.isEmpty()) {
117                 for (String key : new ArrayList<>(wm.keySet())) {
118                     Object o = wm.get(key);
119                     wm.remove(key);
120
121                     if (o instanceof Boolean || o instanceof Number || o instanceof String) {
122                         mm.put(key, o.toString());
123                         log.info("Added property: {} : {}", key, o.toString());
124                     } else if (o instanceof JSONObject) {
125                         JSONObject jo = (JSONObject) o;
126                         Iterator<String> i = jo.keys();
127                         while (i.hasNext()) {
128                             String key1 = i.next();
129                             String[] subKey = key1.split(":");
130                             if (subKey.length == 2) {
131                                 wm.put(key + "." + subKey[1], jo.get(key1));
132                             } else {
133                                 wm.put(key + "." + key1, jo.get(key1));
134                             }
135                         }
136                     } else if (o instanceof JSONArray) {
137                         JSONArray ja = (JSONArray) o;
138                         mm.put(key + "_length", String.valueOf(ja.length()));
139                         log.info("Added property: {}_length: {}", key, ja.length());
140
141                         for (int i = 0; i < ja.length(); i++) {
142                             wm.put(key + '[' + i + ']', ja.get(i));
143                         }
144                     }
145                 }
146             }
147             return mm;
148         } catch (JSONException e) {
149             throw new SvcLogicException("Unable to convert JSON to properties" + e.getLocalizedMessage(), e);
150         }
151     }
152
153 }