2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * * Copyright (C) 2017 AT&T Intellectual Property.
6 * ================================================================================
7 * Copyright (C) 2018 Samsung Electronics. All rights
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.plugins.sshapicall.model;
26 import static com.google.common.base.Preconditions.checkNotNull;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
33 import org.codehaus.jettison.json.JSONArray;
34 import org.codehaus.jettison.json.JSONException;
35 import org.codehaus.jettison.json.JSONObject;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class JsonParser {
42 private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
44 private JsonParser() {
45 // Preventing instantiation of the same.
48 @SuppressWarnings("unchecked")
49 public static Map<String, String> convertToProperties(String s)
50 throws SvcLogicException {
52 checkNotNull(s, "Input should not be null.");
55 JSONObject json = new JSONObject(s);
56 Map<String, Object> wm = new HashMap<>();
57 Iterator<String> ii = json.keys();
58 while (ii.hasNext()) {
59 String key1 = ii.next();
60 wm.put(key1, json.get(key1));
63 Map<String, String> mm = new HashMap<>();
66 for (String key : new ArrayList<>(wm.keySet())) {
67 Object o = wm.get(key);
70 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
71 mm.put(key, o.toString());
73 log.info("Added property: {} : {}", key, o.toString());
74 } else if (o instanceof JSONObject) {
75 JSONObject jo = (JSONObject) o;
76 Iterator<String> i = jo.keys();
78 String key1 = i.next();
79 wm.put(key + "." + key1, jo.get(key1));
81 } else if (o instanceof JSONArray) {
82 JSONArray ja = (JSONArray) o;
83 mm.put(key + "_length", String.valueOf(ja.length()));
85 log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
87 for (int i = 0; i < ja.length(); i++)
88 wm.put(key + '[' + i + ']', ja.get(i));
92 } catch (JSONException e) {
93 throw new SvcLogicException("Unable to convert JSON to properties" + e.getLocalizedMessage(), e);