SSHApiCallNode sshExec API Impl
[ccsdk/sli/plugins.git] / sshapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / sshapicall / model / JsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * * Copyright (C) 2017 AT&T Intellectual Property.
6  * ================================================================================
7  * Copyright (C) 2018 Samsung Electronics. All rights
8  *                      reserved.
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
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
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=========================================================
22  */
23
24 package org.onap.ccsdk.sli.plugins.sshapicall.model;
25
26 import static com.google.common.base.Preconditions.checkNotNull;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32
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;
39
40 public final class JsonParser {
41
42     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
43
44     private JsonParser() {
45         // Preventing instantiation of the same.
46     }
47
48     @SuppressWarnings("unchecked")
49     public static Map<String, String> convertToProperties(String s)
50         throws JSONException {
51
52         checkNotNull(s, "Input should not be null.");
53
54             JSONObject json = new JSONObject(s);
55             Map<String, Object> wm = new HashMap<>();
56             Iterator<String> ii = json.keys();
57             while (ii.hasNext()) {
58                 String key1 = ii.next();
59                 wm.put(key1, json.get(key1));
60             }
61
62             Map<String, String> mm = new HashMap<>();
63
64             while (!wm.isEmpty())
65                 for (String key : new ArrayList<>(wm.keySet())) {
66                     Object o = wm.get(key);
67                     wm.remove(key);
68
69                     if (o instanceof Boolean || o instanceof Number || o instanceof String) {
70                         mm.put(key, o.toString());
71
72                         log.info("Added property: {} : {}", key, o.toString());
73                     } else if (o instanceof JSONObject) {
74                         JSONObject jo = (JSONObject) o;
75                         Iterator<String> i = jo.keys();
76                         while (i.hasNext()) {
77                             String key1 = i.next();
78                             wm.put(key + "." + key1, jo.get(key1));
79                         }
80                     } else if (o instanceof JSONArray) {
81                         JSONArray ja = (JSONArray) o;
82                         mm.put(key + "_length", String.valueOf(ja.length()));
83
84                         log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
85
86                         for (int i = 0; i < ja.length(); i++)
87                             wm.put(key + '[' + i + ']', ja.get(i));
88                     }
89                 }
90             return mm;
91     }
92 }