2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data;
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;
33 public abstract class HtUserdataManagerBase implements HtUserdataManager {
35 private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerBase.class);
37 private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json";
38 private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults();
40 protected static JSONObject loadDefaults() {
41 File f = new File(USERDATA_DEFAULTS_FILENAME);
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);
57 protected abstract String readUserdata(String username, String defaultValue);
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();
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() : "{}";
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());
83 public boolean removeUserdata(String username, String key) {
84 JSONObject o = new JSONObject(this.getUserdata(username));
87 return this.setUserdata(username, o.toString());
92 protected static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) {
94 o.put(key, subObject);
96 JSONObject tmp = new JSONObject();
97 tmp.put(key, subObject);
98 o = mergeData(tmp, o);
103 protected static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException {
104 String[] keys = JSONObject.getNames(source);
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);
114 // existing value for "key" - recursively deep merge:
115 if (value instanceof JSONObject) {
116 JSONObject valueJson = (JSONObject) value;
117 mergeData(valueJson, target.getJSONObject(key));
119 target.put(key, value);