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