9dbdb136a287cbb83ea2286c0b86220eda765bf9
[ccsdk/features.git] /
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.elasticsearch.impl;
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.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;
37
38 public class HtUserdataManagerImpl implements HtUserdataManager {
39
40     private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerImpl.class);
41
42     private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json";
43     private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults();
44
45     private static JSONObject loadDefaults() {
46         File f = new File(USERDATA_DEFAULTS_FILENAME);
47         String content;
48         JSONObject o = null;
49         if (f.exists()) {
50             try {
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);
57             }
58         }
59         return o;
60     }
61
62
63     private final HtDatabaseClient dbClient;
64
65     public HtUserdataManagerImpl(HtDatabaseClient rawClient) {
66         this.dbClient = rawClient;
67     }
68
69     @Override
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();
77         }
78         return json;
79     }
80
81     @Override
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() : "{}";
85     }
86
87     @Override
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;
92     }
93
94     @Override
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;
99     }
100
101     @Override
102     public boolean removeUserdata(String username) {
103         return this.dbClient.doRemove(Entity.Userdata.getName(), username);
104     }
105
106     @Override
107     public boolean removeUserdata(String username, String key) {
108         JSONObject o = new JSONObject(this.getUserdata(username));
109         if (o.has(key)) {
110             o.remove(key);
111             return this.setUserdata(username, o.toString());
112         }
113         return true;
114     }
115
116     private static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) {
117         if (!o.has(key)) {
118             o.put(key, subObject);
119         } else {
120             JSONObject tmp = new JSONObject();
121             tmp.put(key, subObject);
122             o = mergeData(tmp, o);
123         }
124         return o;
125     }
126
127     private static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException {
128         String[] keys = JSONObject.getNames(source);
129         if (keys == null) {
130             return target;
131         }
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);
137             } else {
138                 // existing value for "key" - recursively deep merge:
139                 if (value instanceof JSONObject) {
140                     JSONObject valueJson = (JSONObject) value;
141                     mergeData(valueJson, target.getJSONObject(key));
142                 } else {
143                     target.put(key, value);
144                 }
145             }
146         }
147         return target;
148     }
149
150 }