JSONTool sonar fixes
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / tool / JSONTool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.tool;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import org.codehaus.jettison.json.JSONArray;
35 import org.codehaus.jettison.json.JSONException;
36 import org.codehaus.jettison.json.JSONObject;
37
38
39 public class JSONTool {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
42
43     private JSONTool() {
44     }
45
46     public static Map<String, String> convertToProperties(String s) throws JSONException {
47         return convertToProperties(s, null);
48     }
49
50     public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
51         throws JSONException {
52         JSONObject json = new JSONObject(s);
53         Map<String, String> mm = new HashMap<>();
54         Map<String, Object> wm = new HashMap<>();
55
56         Iterator<String> ii = json.keys();
57         while (ii.hasNext()) {
58             String key1 = ii.next();
59             wm.put(key1, json.get(key1));
60         }
61         while (!wm.isEmpty()) {
62             for (String key : new ArrayList<>(wm.keySet())) {
63                 Object o = wm.get(key);
64                 wm.remove(key);
65                 tryAddBlockKeys(blockKeys, mm, key, o);
66                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
67                     mm.put(key, o.toString());
68                     log.info("Added property: " + key + ": " + o.toString());
69                 } else if (o instanceof JSONObject) {
70                     fill(wm, key, (JSONObject) o);
71                 } else if (o instanceof JSONArray) {
72                     fill(mm, wm, key, (JSONArray) o);
73                 }
74             }
75         }
76         return mm;
77     }
78
79     private static void tryAddBlockKeys(List<String> blockKeys, Map<String, String> mm, String key, Object o) {
80         if (blockKeys != null && blockKeys.contains(key) && o != null) {
81             mm.put("block_" + key, o.toString());
82             log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
83         }
84     }
85
86     private static void fill(Map<String, String> mm, Map<String, Object> wm, String key, JSONArray array)
87         throws JSONException {
88         mm.put("size_" + key, String.valueOf(array.length()));
89         log.info("Added property: " + key + "_length" + ": " + array.length());
90
91         for (int i = 0; i < array.length(); i++) {
92             wm.put(key + '[' + i + ']', array.get(i));
93         }
94     }
95
96     private static void fill(Map<String, Object> wm, String key, JSONObject object) throws JSONException {
97
98         Iterator<String> i = object.keys();
99         while (i.hasNext()) {
100             String key1 = i.next();
101             wm.put(key + "." + key1, object.get(key1));
102         }
103     }
104 }