63fd7006ecb32f8b6e500dd648c6a4bc49dba6ce
[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 : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.tool;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.codehaus.jettison.json.JSONArray;
30 import org.codehaus.jettison.json.JSONException;
31 import org.codehaus.jettison.json.JSONObject;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
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) throws JSONException {
49         JSONObject json = new JSONObject(s);
50         Map<String, String> mm = new HashMap<String, String>();
51
52         Map<String, Object> wm = new HashMap<String, Object>();
53         Iterator<String> ii = json.keys();
54         while (ii.hasNext()) {
55             String key1 = ii.next();
56             wm.put(key1, json.get(key1));
57
58
59         }
60
61         while (!wm.isEmpty())
62             for (String key : new ArrayList<>(wm.keySet())) {
63                 Object o = wm.get(key);
64                 wm.remove(key);
65
66
67                 if(blockKeys != null && blockKeys.contains(key) && o != null){
68                     //log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
69                     mm.put("block_" +key,o.toString());
70                 }
71
72                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
73                     mm.put(key, o.toString());
74                     //log.info("Added property: " + key + ": " + o.toString());
75                 }
76
77                 else if (o instanceof JSONObject) {
78                     JSONObject jo = (JSONObject) o;
79                     Iterator<String> i = jo.keys();
80                     while (i.hasNext()) {
81                         String key1 = i.next();
82                         wm.put(key + "." + key1, jo.get(key1));
83                     }
84                 }
85
86                 else if (o instanceof JSONArray) {
87                     JSONArray ja = (JSONArray) o;
88                     mm.put("size_"+key, String.valueOf(ja.length()));
89
90                     //log.info("Added property: " + key + "_length" + ": " + String.valueOf(ja.length()));
91
92                     for (int i = 0; i < ja.length(); i++)
93                         wm.put(key + '[' + i + ']', ja.get(i));
94                 }
95             }
96
97         return mm;
98     }
99     
100     /*
101     public static Map<String, String> convertToProperties1(String s,List<String> blockKeys) throws Exception {
102         ObjectMapper objectMapper = new ObjectMapper();
103         
104         JsonNode rootNode = objectMapper.readTree(s);
105         
106         Map<String, String> mm = new HashMap<String, String>();
107
108         Map<String, Object> wm = new HashMap<String, Object>();
109         Iterator<String> ii = rootNode.fieldNames();
110         while (ii.hasNext()) {
111             String key1 = ii.next();
112             wm.put(key1, rootNode.get(key1));
113
114
115         }
116
117         while (!wm.isEmpty())
118             for (String key : new ArrayList<>(wm.keySet())) {
119                 Object o = wm.get(key);
120                 wm.remove(key);
121
122
123                 if(blockKeys != null && blockKeys.contains(key) && o != null){
124                     //log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
125                     mm.put("block_" +key,o.toString());
126                 }
127
128                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
129                     mm.put(key, o.toString());
130                     //log.info("Added property: " + key + ": " + o.toString());
131                 }
132
133                 else if (o instanceof JSONObject) {
134                     JSONObject jo = (JSONObject) o;
135                     Iterator<String> i = jo.keys();
136                     while (i.hasNext()) {
137                         String key1 = i.next();
138                         wm.put(key + "." + key1, jo.get(key1));
139                     }
140                 }
141
142                 else if (o instanceof JSONArray) {
143                     JSONArray ja = (JSONArray) o;
144                     mm.put("size_"+key, String.valueOf(ja.length()));
145
146                     //log.info("Added property: " + key + "_length" + ": " + String.valueOf(ja.length()));
147
148                     for (int i = 0; i < ja.length(); i++)
149                         wm.put(key + '[' + i + ']', ja.get(i));
150                 }
151             }
152
153         return mm;
154     }
155 */
156
157 }