Merge "Sonar fix: TemplateNode.java"
[ccsdk/sli/plugins.git] / sshapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / sshapicall / model / JsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Samsung Electronics. All rights
8  *                      reserved.
9  * ================================================================================
10  * Modifications Copyright © 2018 IBM
11  * ================================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  * 
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  * 
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.ccsdk.sli.plugins.sshapicall.model;
27
28 import static com.google.common.base.Preconditions.checkNotNull;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Map;
34
35 import org.codehaus.jettison.json.JSONArray;
36 import org.codehaus.jettison.json.JSONException;
37 import org.codehaus.jettison.json.JSONObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public final class JsonParser {
42
43     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
44
45     private JsonParser() {
46         // Preventing instantiation of the same.
47     }
48
49     @SuppressWarnings("unchecked")
50     public static Map<String, String> convertToProperties(String s)
51         throws JSONException {
52
53         checkNotNull(s, "Input should not be null.");
54
55             JSONObject json = new JSONObject(s);
56             Map<String, Object> wm = new HashMap<>();
57             Iterator<String> ii = json.keys();
58             while (ii.hasNext()) {
59                 String key1 = ii.next();
60                 wm.put(key1, json.get(key1));
61             }
62
63             Map<String, String> mm = new HashMap<>();
64
65             while (!wm.isEmpty())
66                 for (String key : new ArrayList<>(wm.keySet())) {
67                     Object o = wm.get(key);
68                     wm.remove(key);
69
70                     if (o instanceof Boolean || o instanceof Number || o instanceof String) {
71                         mm.put(key, o.toString());
72
73                         log.info("Added property: {} : {}", key, o.toString());
74                     } else if (o instanceof JSONObject) {
75                         JSONObject jo = (JSONObject) o;
76                         Iterator<String> i = jo.keys();
77                         while (i.hasNext()) {
78                             String key1 = i.next();
79                             wm.put(key + "." + key1, jo.get(key1));
80                         }
81                     } else if (o instanceof JSONArray) {
82                         JSONArray ja = (JSONArray) o;
83                         mm.put(key + "_length", String.valueOf(ja.length()));
84
85                         log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
86
87                         for (int i = 0; i < ja.length(); i++)
88                             wm.put(key + '[' + i + ']', ja.get(i));
89                     }
90                 }
91             return mm;
92     }
93 }