Update appc-config-generator to use onap packaging
[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 java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import org.codehaus.jettison.json.JSONArray;
33 import org.codehaus.jettison.json.JSONException;
34 import org.codehaus.jettison.json.JSONObject;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38
39
40 public class JSONTool {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
43
44     public static Map<String, String> convertToProperties(String s) throws JSONException {
45         return convertToProperties(s, null);
46     }
47
48     public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
49             throws JSONException {
50         JSONObject json = new JSONObject(s);
51         Map<String, String> mm = new HashMap<String, String>();
52
53         Map<String, Object> wm = new HashMap<String, Object>();
54         Iterator<String> ii = json.keys();
55         while (ii.hasNext()) {
56             String key1 = ii.next();
57             wm.put(key1, json.get(key1));
58
59
60         }
61
62         while (!wm.isEmpty())
63             for (String key : new ArrayList<>(wm.keySet())) {
64                 Object o = wm.get(key);
65                 wm.remove(key);
66
67
68                 if (blockKeys != null && blockKeys.contains(key) && o != null) {
69                     // log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
70                     mm.put("block_" + key, o.toString());
71                 }
72
73                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
74                     mm.put(key, o.toString());
75                     // log.info("Added property: " + key + ": " + o.toString());
76                 }
77
78                 else if (o instanceof JSONObject) {
79                     JSONObject jo = (JSONObject) o;
80                     Iterator<String> i = jo.keys();
81                     while (i.hasNext()) {
82                         String key1 = i.next();
83                         wm.put(key + "." + key1, jo.get(key1));
84                     }
85                 }
86
87                 else if (o instanceof JSONArray) {
88                     JSONArray ja = (JSONArray) o;
89                     mm.put("size_" + key, String.valueOf(ja.length()));
90
91                     // log.info("Added property: " + key + "_length" + ": " +
92                     // String.valueOf(ja.length()));
93
94                     for (int i = 0; i < ja.length(); i++)
95                         wm.put(key + '[' + i + ']', ja.get(i));
96                 }
97             }
98
99         return mm;
100     }
101
102 }