saltstack reqExecSls implemented as adaptor
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / model / JsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.saltstack.model;
23
24 import org.codehaus.jettison.json.JSONArray;
25 import org.codehaus.jettison.json.JSONException;
26 import org.codehaus.jettison.json.JSONObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Map;
34
35 import static com.google.common.base.Preconditions.checkNotNull;
36
37 public final class JsonParser {
38
39     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
40
41     private JsonParser() {
42         // Preventing instantiation of the same.
43     }
44
45     @SuppressWarnings("unchecked")
46     public static Map<String, String> convertToProperties(String s)
47             throws JSONException {
48
49         checkNotNull(s, "Input should not be null.");
50
51         JSONObject json = new JSONObject(s);
52         Map<String, Object> wm = new HashMap<>();
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         Map<String, String> mm = new HashMap<>();
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                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
67                     mm.put(key, o.toString());
68
69                     log.info("Added property: {} : {}", key, o.toString());
70                 } else if (o instanceof JSONObject) {
71                     JSONObject jo = (JSONObject) o;
72                     Iterator<String> i = jo.keys();
73                     while (i.hasNext()) {
74                         String key1 = i.next();
75                         wm.put(key + "." + key1, jo.get(key1));
76                     }
77                 } else if (o instanceof JSONArray) {
78                     JSONArray ja = (JSONArray) o;
79                     mm.put(key + "_length", String.valueOf(ja.length()));
80
81                     log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
82
83                     for (int i = 0; i < ja.length(); i++)
84                         wm.put(key + '[' + i + ']', ja.get(i));
85                 }
86             }
87         return mm;
88     }
89 }