4c3ba7f8eca6f60f1dc99277015cccf081e2be46
[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.codehaus.jettison.json.JSONArray;
29 import org.codehaus.jettison.json.JSONException;
30 import org.codehaus.jettison.json.JSONObject;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import static com.google.common.base.Preconditions.checkNotNull;
36
37 public final class JsonParser {
38
39     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
40
41     private JsonParser() {
42         // Preventing instantiation of the same.
43     }
44
45     @SuppressWarnings("unchecked")
46     public static Map<String, String> convertToProperties(String s)
47             throws SvcLogicException {
48
49         checkNotNull(s, "Input should not be null.");
50
51         try {
52             Map<String, Object> wm = new HashMap<>();
53             String topLvlArrLength = null;
54             JSONObject json;
55             JSONArray jsonArr;
56             //support top level list in json response
57             if (s.startsWith("[")) {
58                 jsonArr = new JSONArray(s);
59                 topLvlArrLength = String.valueOf(jsonArr.length());
60                 for (int i = 0, length = jsonArr.length(); i < length; i++) {
61                     json = jsonArr.getJSONObject(i);
62                     Iterator<String> ii = json.keys();
63                     while (ii.hasNext()) {
64                         String key = ii.next();
65                         String key1 = "[" + i + "]." + key;
66                         String[] subKey = key1.split(":");
67                         if (subKey.length == 2) {
68                             wm.put(subKey[1], json.get(key));
69                         } else {
70                             wm.put(key1, json.get(key));
71                         }
72                     }
73                 }
74             } else {
75                 json = new JSONObject(s);
76                 Iterator<String> ii = json.keys();
77                 while (ii.hasNext()) {
78                     String key1 = ii.next();
79                     String[] subKey = key1.split(":");
80                     if (subKey.length == 2) {
81                         wm.put(subKey[1], json.get(key1));
82                     } else {
83                         wm.put(key1, json.get(key1));
84                     }
85                 }
86             }
87
88             Map<String, String> mm = new HashMap<>();
89             if (topLvlArrLength != null) {
90                 mm.put("_length", topLvlArrLength);
91             }
92             while (!wm.isEmpty()) {
93                 for (String key : new ArrayList<>(wm.keySet())) {
94                     Object o = wm.get(key);
95                     wm.remove(key);
96
97                     if (o instanceof Boolean || o instanceof Number || o instanceof String) {
98                         mm.put(key, o.toString());
99
100                         log.info("Added property: {} : {}", key, o.toString());
101                     } else if (o instanceof JSONObject) {
102                         JSONObject jo = (JSONObject) o;
103                         Iterator<String> i = jo.keys();
104                         while (i.hasNext()) {
105                             String key1 = i.next();
106                             String[] subKey = key1.split(":");
107                             if (subKey.length == 2) {
108                                 wm.put(key + "." + subKey[1], jo.get(key1));
109                             } else {
110                                 wm.put(key + "." + key1, jo.get(key1));
111                             }
112                         }
113                     } else if (o instanceof JSONArray) {
114                         JSONArray ja = (JSONArray) o;
115                         mm.put(key + "_length", String.valueOf(ja.length()));
116
117                         log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
118
119                         for (int i = 0; i < ja.length(); i++) {
120                             wm.put(key + '[' + i + ']', ja.get(i));
121                         }
122                     }
123                 }
124             }
125             return mm;
126         } catch (JSONException e) {
127             throw new SvcLogicException("Unable to convert JSON to properties" + e.getLocalizedMessage(), e);
128         }
129     }
130
131 }