Fix vm-id URL parsing
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.generator.tool;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import org.codehaus.jettison.json.JSONArray;
34 import org.codehaus.jettison.json.JSONException;
35 import org.codehaus.jettison.json.JSONObject;
36
37
38 public class JSONTool {
39
40     private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
41
42     private JSONTool() {
43     }
44
45     public static Map<String, String> convertToProperties(String s) throws JSONException {
46         return convertToProperties(s, null);
47     }
48
49     public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
50         throws JSONException {
51         JSONObject json = new JSONObject(s);
52         Map<String, String> mm = new HashMap<>();
53         Map<String, Object> wm = new HashMap<>();
54
55         Iterator<String> ii = json.keys();
56         while (ii.hasNext()) {
57             String key1 = ii.next();
58             wm.put(key1, json.get(key1));
59         }
60         while (!wm.isEmpty()) {
61             for (String key : new ArrayList<>(wm.keySet())) {
62                 Object o = wm.get(key);
63                 wm.remove(key);
64                 tryAddBlockKeys(blockKeys, mm, key, o);
65                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
66                     String oString = o.toString();
67                     //Add escape characters to the string in case it is a string representation
68                     //of a json object.
69                     oString = JSONObject.quote(oString, false);
70                     //Remove the surrouding quotes added by the JSONObject.quote() method.
71                     //JSONObject.quote() will always return, at minimum, a string with two quotes,
72                     //even if a null string is passed to it. So this substring method does not
73                     //need any checks.
74                     oString = oString.substring(1, oString.length() - 1);
75                     mm.put(key, oString);
76                     log.info("Added property: " + key + ": " + oString);
77                 } else if (o instanceof JSONObject) {
78                     fill(wm, key, (JSONObject) o);
79                 } else if (o instanceof JSONArray) {
80                     fill(mm, wm, key, (JSONArray) o);
81                 }
82             }
83         }
84         return mm;
85     }
86
87     //Finds json values which themselves contain String representations of json
88     //data and adds escape characters to the quotes. Example:
89     //Input: { "key1": "{"internalKey1": "internalValue1"}" }
90     //Output: { "key1": "{\"internalKey1\": \"internalValue1\"}" }
91     public static String escapeInternalJson(String jsonString) throws JSONException
92     {
93         StringBuilder sb = new StringBuilder();
94         char lastChar = 0;
95         boolean inJson = false;
96         for(char c : jsonString.toCharArray()) {
97             //If we see a { directly after a quote, it would mean that this string will contain json data
98             //as a string.
99             if(c == '{' && lastChar == '"') {
100                 inJson = true;
101             }
102             //Checks if we are currently in a json block and if the character we are looking at is
103             //a quote. The quote is what needs to be escaped.
104             if(inJson && c == '"') {
105                 //If a } precedes a quote, then this would signal the end of a string containing json
106                 if(lastChar == '}') {
107                     inJson = false;
108                     //since this quote we are looking at is outside the json string block, we should not escape it
109                     sb.append("\"");
110                 } else {
111                     //Else block for the case where the quote was preceded by anything other than a }
112                     //We know we are still in the json string block
113                     //If the last character was not a backslash, we know that the quote we are looking at has
114                     //not been escaped.
115                     if(lastChar != '\\') {
116                         //un-escaped quote should be escaped
117                         sb.append("\\\"");
118                     } else {
119                         //quote is already escaped, we can add it as it is
120                         sb.append(c);
121                     }
122                 }
123             } else {
124                 //If we are not in a json block, or if the character is not a quote, it can be added as is
125                 sb.append(c);
126             }
127             if(!Character.isWhitespace(c)) {
128                 lastChar = c;
129             }
130         }
131         if(inJson == true) {
132             //We reached the end of the string, but the internal string containing
133             //the json data to escape never ended.
134             throw new JSONException("End of json data reached, but end of internal"
135                     + "json string never reached.");
136         }
137         return sb.toString();
138     }
139
140     private static void tryAddBlockKeys(List<String> blockKeys, Map<String, String> mm, String key, Object o) {
141         if (blockKeys != null && blockKeys.contains(key) && o != null) {
142             mm.put("block_" + key, o.toString());
143             log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
144         }
145     }
146
147     private static void fill(Map<String, String> mm, Map<String, Object> wm, String key, JSONArray array)
148         throws JSONException {
149         mm.put("size_" + key, String.valueOf(array.length()));
150         log.info("Added property: " + key + "_length" + ": " + array.length());
151
152         for (int i = 0; i < array.length(); i++) {
153             wm.put(key + '[' + i + ']', array.get(i));
154         }
155     }
156
157     private static void fill(Map<String, Object> wm, String key, JSONObject object) throws JSONException {
158
159         Iterator<String> i = object.keys();
160         while (i.hasNext()) {
161             String key1 = i.next();
162             wm.put(key + "." + key1, object.get(key1));
163         }
164     }
165 }