f6bf1acdb73806c0c8c50994a4f501046ba38f6f
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.common.util;
18
19 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import net.sf.json.JSONArray;
24 import net.sf.json.JSONException;
25 import net.sf.json.JSONObject;
26
27 /**
28  *
29  * Json Utility Class.<br/>
30  * <p>
31  * </p>
32  *
33  * @author
34  * @version     VFC 1.0  Sep 10, 2016
35  */
36 public final class JsonUtil {
37
38     private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
39
40     private static final int TYPE_STRING = 0;
41
42     private static final int TYPE_INT = 1;
43
44     private static final int TYPE_JSONA = 2;
45
46     private static final int TYPE_JSONO = 3;
47
48     private static final int TYPE_LONG = 4;
49
50     private JsonUtil() {
51     }
52
53     /**
54      *
55      * Get Json Field String.<br>
56      *
57      * @param jsonObj
58      * @param fieldName
59      * @return
60      * @since  VFC 1.0
61      */
62     public static String getJsonFieldStr(JSONObject jsonObj, String fieldName) {
63         return (String)getJsonFieldObject(jsonObj, fieldName, TYPE_STRING);
64     }
65
66     /**
67      *
68      * Get Json Field Integer.<br>
69      *
70      * @param jsonObj
71      * @param fieldName
72      * @return
73      * @since  VFC 1.0
74      */
75     public static Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
76         return (Integer)getJsonFieldObject(jsonObj, fieldName, TYPE_INT);
77     }
78
79     /**
80      *
81      * Get Json Field array.<br>
82      *
83      * @param jsonObj
84      * @param fieldName
85      * @return
86      * @since  VFC 1.0
87      */
88     public static JSONArray getJsonFieldArr(JSONObject jsonObj, String fieldName) {
89         return (JSONArray)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONA);
90     }
91
92     /**
93      *
94      * Get Json Field Json.<br>
95      *
96      * @param jsonObj
97      * @param fieldName
98      * @return
99      * @since  VFC 1.0
100      */
101     public static JSONObject getJsonFieldJson(JSONObject jsonObj, String fieldName) {
102         return (JSONObject)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONO);
103     }
104
105     /**
106      *
107      * Get Json Field Long.<br>
108      *
109      * @param jsonObj
110      * @param fieldName
111      * @return
112      * @since  VFC 1.0
113      */
114     public static Long getJsonFieldLong(JSONObject jsonObj, String fieldName) {
115         return (Long)getJsonFieldObject(jsonObj, fieldName, TYPE_LONG);
116     }
117
118     /**
119      *
120      * Get Json Field Object.<br>
121      *
122      * @param jsonObj
123      * @param fieldName
124      * @param classType
125      * @return
126      * @since  VFC 1.0
127      */
128     private static Object getJsonFieldObject(JSONObject jsonObj, String fieldName, int classType) {
129         try {
130             if(null != jsonObj && jsonObj.has(fieldName)) {
131                 Object result = new Object();
132                 switch(classType) {
133                     case TYPE_STRING:
134                         result = "null".equals(jsonObj.getString(fieldName)) ? "" : jsonObj.getString(fieldName);
135                         break;
136                     case TYPE_INT:
137                         result = jsonObj.getInt(fieldName);
138                         break;
139                     case TYPE_JSONA:
140                         result = jsonObj.getJSONArray(fieldName);
141                         break;
142                     case TYPE_JSONO:
143                         result = jsonObj.getJSONObject(fieldName);
144                         break;
145                     case TYPE_LONG:
146                         result = jsonObj.getLong(fieldName);
147                         break;
148                     default:
149                         result = null;
150                         break;
151                 }
152                 return result;
153             }
154         } catch(JSONException e) {
155             LOG.error("function=getJsonFieldLong, exception: {} ", e);
156             return null;
157         }
158         return null;
159     }
160
161     /**
162      *
163      * Check whether the Json Object is empty.<br>
164      *
165      * @param jsonObject
166      * @return
167      * @since  VFC 1.0
168      */
169     public static boolean isNullJson(JSONObject jsonObject) {
170         if(null == jsonObject || jsonObject.isEmpty()) {
171             return true;
172         }
173         return false;
174     }
175
176     /**
177      *
178      * Get String value by Json.<br>
179      *
180      * @param json
181      * @param key
182      * @return
183      * @since  VFC 1.0
184      */
185     public static String getStrValueByjson(JSONObject json, String key) {
186         JSONArray names = json.names();
187         String result = null;
188         for(int i = 0; i < names.size(); i++) {
189             String nodeName = names.getString(i);
190             if(json.optJSONObject(nodeName) != null) {
191                 result = getStrValueByjson(json.getJSONObject(nodeName), key);
192             }
193             if(json.optJSONArray(nodeName) != null) {
194                 result = getStrValueByJArray(json.getJSONArray(nodeName), key);
195             }
196             if(nodeName.equals(key)) {
197                 result = json.getString(nodeName);
198                 break;
199             }
200         }
201         return result;
202     }
203
204     private static String getStrValueByJArray(JSONArray json, String key) {
205         String result = null;
206         for(int i = 0; i < json.size(); i++) {
207             if(json.optJSONObject(i) != null) {
208                 result = getStrValueByjson(json.getJSONObject(i), key);
209             }
210             if(json.optJSONArray(i) != null) {
211                 result = getStrValueByJArray(json.getJSONArray(i), key);
212             }
213         }
214         return result;
215     }
216
217     /**
218      *
219      * Get Json Value by Json object.<br>
220      *
221      * @param json
222      * @param key
223      * @return
224      * @since  VFC 1.0
225      */
226     public static JSONObject getJsonValueByjson(JSONObject json, String key) {
227         JSONObject resultJson = new JSONObject();
228         String result = getStrValueByjson(json, key);
229         if(null == result) {
230             return null;
231         }
232         resultJson.element(key, result);
233         return resultJson;
234
235     }
236
237     /**
238      *
239      * Get String Value by Json object.<br>
240      *
241      * @param json
242      * @param parentKey
243      * @param key
244      * @return
245      * @since  VFC 1.0
246      */
247     public static String getStrValueByJson(JSONObject json, String parentKey, String key) {
248         if(parentKey.isEmpty()) {
249             return getStrValueByjson(json, key);
250         }
251         JSONObject parentJson = getJsonValueByjson(json, parentKey);
252         if(isNullJson(parentJson)) {
253             return null;
254         }
255         return getStrValueByjson(parentJson, key);
256
257     }
258
259     /**
260      *
261      * Get response Data.<br>
262      *
263      * @param obj
264      * @return
265      * @since  VFC 1.0
266      */
267     public static JSONObject getResponseData(JSONObject obj) {
268         JSONObject result = new JSONObject();
269
270         Integer retCode = getJsonFieldInt(obj, ParamConstant.PARAM_RETCODE);
271         if(null == retCode || retCode == -1) {
272             return null;
273         }
274
275         if(obj.optJSONObject(ParamConstant.PARAM_DATA) != null) {
276             result = obj.getJSONObject(ParamConstant.PARAM_DATA);
277         } else if(obj.optJSONArray(ParamConstant.PARAM_DATA) != null) {
278             result = obj.getJSONArray(ParamConstant.PARAM_DATA).getJSONObject(0);
279         }
280         if(result.isEmpty()) {
281             return null;
282         }
283
284         if(result.containsKey(ParamConstant.PARAM_RETCODE)) {
285             result = getResponseData(result);
286         }
287         return result;
288     }
289
290 }