60f28d95c1eeaf54a6cd2e577d4ebac23d049279
[ccsdk/features.git] / sdnr / wt / data-provider / dblib / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / database / sqldb / data / HtUserdataManagerBase.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights 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.features.sdnr.wt.dataprovider.database.sqldb.data;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public abstract class HtUserdataManagerBase implements HtUserdataManager {
34
35     private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerBase.class);
36
37     private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json";
38     private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults();
39
40     protected static JSONObject loadDefaults() {
41         File f = new File(USERDATA_DEFAULTS_FILENAME);
42         String content;
43         JSONObject o = null;
44         if (f.exists()) {
45             try {
46                 content = Files.readString(f.toPath());
47                 o = new JSONObject(content);
48             } catch (IOException e) {
49                 LOG.warn("problem loading defaults: ", e);
50             } catch (JSONException e) {
51                 LOG.warn("problem parsing defaults: ", e);
52             }
53         }
54         return o;
55     }
56
57     protected abstract String readUserdata(String username, String defaultValue);
58
59     @Override
60     public String getUserdata(String username) {
61         String json = this.readUserdata(username, "{}");
62         if (USERDATA_DEFAULTS_CONTENT != null) {
63             JSONObject merge = mergeData(new JSONObject(json), USERDATA_DEFAULTS_CONTENT);
64             json = merge.toString();
65         }
66         return json;
67     }
68
69     @Override
70     public String getUserdata(String username, String key) {
71         JSONObject o = new JSONObject(this.getUserdata(username));
72         return o.has(key) ? o.get(key).toString() : "{}";
73     }
74
75     @Override
76     public boolean setUserdata(String username, String key, String data) {
77         JSONObject o = new JSONObject();
78         o.put(key, new JSONObject(data));
79         return this.setUserdata(username, o.toString());
80     }
81
82     @Override
83     public boolean removeUserdata(String username, String key) {
84         JSONObject o = new JSONObject(this.getUserdata(username));
85         if (o.has(key)) {
86             o.remove(key);
87             return this.setUserdata(username, o.toString());
88         }
89         return true;
90     }
91
92     protected static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) {
93         if (!o.has(key)) {
94             o.put(key, subObject);
95         } else {
96             JSONObject tmp = new JSONObject();
97             tmp.put(key, subObject);
98             o = mergeData(tmp, o);
99         }
100         return o;
101     }
102
103     protected static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException {
104         String[] keys = JSONObject.getNames(source);
105         if (keys == null) {
106             return target;
107         }
108         for (String key : keys) {
109             Object value = source.get(key);
110             if (!target.has(key)) {
111                 // new value for "key":
112                 target.put(key, value);
113             } else {
114                 // existing value for "key" - recursively deep merge:
115                 if (value instanceof JSONObject) {
116                     JSONObject valueJson = (JSONObject) value;
117                     mergeData(valueJson, target.getJSONObject(key));
118                 } else {
119                     target.put(key, value);
120                 }
121             }
122         }
123         return target;
124     }
125
126 }