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.elasticsearch.impl;
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.common.database.HtDatabaseClient;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class HtUserdataManagerImpl implements HtUserdataManager {
40 private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerImpl.class);
42 private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json";
43 private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults();
45 private static JSONObject loadDefaults() {
46 File f = new File(USERDATA_DEFAULTS_FILENAME);
51 content = Files.readString(f.toPath());
52 o = new JSONObject(content);
53 } catch (IOException e) {
54 LOG.warn("problem loading defaults: ", e);
55 } catch (JSONException e) {
56 LOG.warn("problem parsing defaults: ", e);
63 private final HtDatabaseClient dbClient;
65 public HtUserdataManagerImpl(HtDatabaseClient rawClient) {
66 this.dbClient = rawClient;
70 public String getUserdata(String username) {
71 SearchResult<SearchHit> result = this.dbClient.doReadByQueryJsonData(Entity.Userdata.getName(),
72 QueryBuilders.matchQuery("_id", username));
73 String json = result.getHits().size() > 0 ? result.getHits().get(0).getSourceAsString() : "{}";
74 if (USERDATA_DEFAULTS_CONTENT != null) {
75 JSONObject merge = mergeData(new JSONObject(json), USERDATA_DEFAULTS_CONTENT);
76 json = merge.toString();
82 public String getUserdata(String username, String key) {
83 JSONObject o = new JSONObject(this.getUserdata(username));
84 return o.has(key) ? o.get(key).toString() : "{}";
88 public boolean setUserdata(String username, String data) {
89 JSONObject o = new JSONObject(this.getUserdata(username));
90 JSONObject merge = mergeData(o, new JSONObject(data));
91 return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, merge.toString()) != null;
95 public boolean setUserdata(String username, String key, String data) {
96 JSONObject o = new JSONObject(this.getUserdata(username));
97 o = mergeData(o, key, new JSONObject(data));
98 return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, o.toString()) != null;
102 public boolean removeUserdata(String username) {
103 return this.dbClient.doRemove(Entity.Userdata.getName(), username);
107 public boolean removeUserdata(String username, String key) {
108 JSONObject o = new JSONObject(this.getUserdata(username));
111 return this.setUserdata(username, o.toString());
116 private static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) {
118 o.put(key, subObject);
120 JSONObject tmp = new JSONObject();
121 tmp.put(key, subObject);
122 o = mergeData(tmp, o);
127 private static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException {
128 String[] keys = JSONObject.getNames(source);
132 for (String key : keys) {
133 Object value = source.get(key);
134 if (!target.has(key)) {
135 // new value for "key":
136 target.put(key, value);
138 // existing value for "key" - recursively deep merge:
139 if (value instanceof JSONObject) {
140 JSONObject valueJson = (JSONObject) value;
141 mergeData(valueJson, target.getJSONObject(key));
143 target.put(key, value);