diff options
author | Ravi Pendurty <ravi.pendurty@highstreet-technologies.com> | 2023-10-20 13:23:33 +0530 |
---|---|---|
committer | Ravi Pendurty <ravi.pendurty@highstreet-technologies.com> | 2023-10-20 13:24:11 +0530 |
commit | cb9bd650a1ba6c1c2f5c7dd410f640b9b6f876c7 (patch) | |
tree | 18ae49eb64806a3687d388d24a35276361759d3e /sdnr | |
parent | 70cc4c4295376e4af9cb9009a6ee5ddc389d61d9 (diff) |
Extend user settings API
Extend user settings API
Issue-ID: CCSDK-3947
Change-Id: Ic0e7783abb2b084142c5bc2d36dc31810f6ca9cf
Signed-off-by: Ravi Pendurty <ravi.pendurty@highstreet-technologies.com>
Diffstat (limited to 'sdnr')
8 files changed, 186 insertions, 17 deletions
diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java index e015dd29f..d6ad35488 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java @@ -24,6 +24,7 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; @@ -68,25 +69,115 @@ public abstract class HtUserdataManagerBase implements HtUserdataManager { @Override public String getUserdata(String username, String key) { + final String EMPTY_JSON_OBJECT = "{}"; JSONObject o = new JSONObject(this.getUserdata(username)); - return o.has(key) ? o.get(key).toString() : "{}"; + if(key==null || !key.contains(".")) + return o.has(key) ? o.get(key).toString() : EMPTY_JSON_OBJECT; + final String[] keys = key.split("\\."); + Object tmp; + for (int i=0;i<keys.length-1;i++){ + if(!o.has(keys[i])){ + return EMPTY_JSON_OBJECT; + } + tmp = o.get(keys[i]); + if(!(tmp instanceof JSONObject)) { + return EMPTY_JSON_OBJECT; + } + o = (JSONObject)tmp; + } + tmp= o.has(keys[keys.length-1])? o.get(keys[keys.length-1]):EMPTY_JSON_OBJECT; + if(tmp instanceof JSONObject){ + return ((JSONObject)tmp).toString(); + } + if(tmp instanceof JSONArray){ + return ((JSONArray)tmp).toString(); + } + if( tmp instanceof String){ + return String.format("\"%s\"",tmp); + } + return String.valueOf(tmp); } @Override public boolean setUserdata(String username, String key, String data) { + if(key == null || data == null){ + return false; + } JSONObject o = new JSONObject(this.getUserdata(username)); - o.put(key, new JSONObject(data)); + if(!key.contains(".")) { + o.put(key, new JSONObject(data)); + } + else{ + final String[] keys = key.split("\\."); + JSONObject tmp = o; + Object tmpObject; + for(int i=0;i<keys.length-1;i++){ + if(!tmp.has(keys[i])){ + tmp.put(keys[i], new JSONObject()); + } + tmpObject = tmp.get(keys[i]); + if(!(tmpObject instanceof JSONObject)){ + return false; + } + tmp = (JSONObject) tmpObject; + } + if(data.startsWith("{")){ + tmp.put(keys[keys.length-1], new JSONObject(data)); + } + else if (data.startsWith("[")){ + tmp.put(keys[keys.length-1], new JSONArray(data)); + } + else if(data.startsWith("\"") && data.endsWith("\"")){ + tmp.put(keys[keys.length-1],data.substring(1,data.length()-1)); + } + else if("true".equals(data) || "false".equals(data)){ + tmp.put(keys[keys.length-1],"true".equals(data)); + } + else { + try { + tmp.put(keys[keys.length - 1], Double.parseDouble(data)); + } catch (NumberFormatException e) { + return false; + } + } + } return this.setUserdata(username, o.toString()); } @Override public boolean removeUserdata(String username, String key) { + if(key == null){ + return false; + } JSONObject o = new JSONObject(this.getUserdata(username)); - if (o.has(key)) { - o.remove(key); + if(!key.contains(".")) { + if (o.has(key)) { + o.remove(key); + return this.setUserdata(username, o.toString()); + } + } + else { + final String[] keys = key.split("\\."); + JSONObject tmp = o; + Object tmpObject; + for(int i=0;i<keys.length-1;i++){ + if(!tmp.has(keys[i])){ + return false; + } + tmpObject = tmp.get(keys[i]); + if(!(tmpObject instanceof JSONObject)){ + return false; + } + tmp = (JSONObject) tmpObject; + } + if(!tmp.has(keys[keys.length-1])){ + return false; + } + tmp.remove(keys[keys.length-1]); return this.setUserdata(username, o.toString()); + } - return true; + return false; } protected static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) { diff --git a/sdnr/wt/data-provider/dblib/test2.properties b/sdnr/wt/data-provider/dblib/test2.properties deleted file mode 100644 index e3485ea95..000000000 --- a/sdnr/wt/data-provider/dblib/test2.properties +++ /dev/null @@ -1,7 +0,0 @@ -[mariadb] -url=${SDNRDBURL} -username=${SDNRDBUSERNAME} -password=${SDNRDBPASSWORD} -controllerId=null -suffix= - diff --git a/sdnr/wt/data-provider/installer/pom.xml b/sdnr/wt/data-provider/installer/pom.xml index 2de56eff3..596c38afc 100755 --- a/sdnr/wt/data-provider/installer/pom.xml +++ b/sdnr/wt/data-provider/installer/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -43,7 +44,17 @@ <application.name>sdnr-wt-data-provider</application.name> <include.transitive.dependencies>false</include.transitive.dependencies> </properties> - + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.onap.ccsdk.parent</groupId> + <artifactId>installed-odl-bom</artifactId> + <version>2.6.0</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> <dependencies> <dependency> <groupId>${project.groupId}</groupId> diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/UserdataHttpServlet.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/UserdataHttpServlet.java index 92bccce6f..1346e7e44 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/UserdataHttpServlet.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/UserdataHttpServlet.java @@ -47,7 +47,7 @@ public class UserdataHttpServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(UserdataHttpServlet.class); - private static final String REGEX = "^\\/userdata[\\/]?([a-zA-Z0-9]+)?$"; + private static final String REGEX = "^\\/userdata[\\/]?([a-zA-Z0-9\\.]+)?$"; private static final Pattern PATTERN = Pattern.compile(REGEX); private static final String JWT_PAYLOAD_USERNAME_PROPERTYKEY = "sub"; private static HtUserdataManager dbUserManager; diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestUserdata.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestUserdata.java index ce4044780..79c4dd185 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestUserdata.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestUserdata.java @@ -21,10 +21,12 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.test; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.IOException; import org.apache.sshd.common.util.io.IoUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; @@ -36,6 +38,8 @@ import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; public class TestUserdata { private static final String USERNAME = "admin132"; + private static final String USERNAME2 = "admin133"; + private static HtDatabaseClient dbRawProvider; private static HtUserdataManagerImpl userDbProvider; @@ -45,6 +49,7 @@ public class TestUserdata { HostInfo[] hosts = HostInfoForTest.get(); dbRawProvider = HtDatabaseClient.getClient(hosts); userDbProvider = new HtUserdataManagerImpl(dbRawProvider); + } public static void trySleep(long ms) { @@ -89,6 +94,55 @@ public class TestUserdata { JSONAssert.assertEquals(mergedContent, userdata, false); } + @Test + public void test2() { + String fullContent = ""; + boolean success = false; + try { + fullContent = getFileContent("/userdata/full.json"); + success = userDbProvider.setUserdata(USERNAME2, fullContent); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + assertTrue("problem writing data into db",success); + + trySleep(2000); + // read with complex key + String userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.styling"); + JSONAssert.assertEquals("{\"theme\": \"dark\"}", userdata, false); + userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.startupPosition.longitude"); + assertEquals("\"13.35\"", userdata); + userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.tileOpacity"); + assertEquals("\"26\"", userdata); + + // write with complex key => forbidden + success = userDbProvider.setUserdata(USERNAME2,"networkMap.styling.theme",null); + assertFalse(success); + success = userDbProvider.setUserdata(USERNAME2,"networkMap.themes.key","\"abc\""); + assertFalse(success); + // write with complex key => allowed + success = userDbProvider.setUserdata(USERNAME2,"networkMap.styling.theme","\"test\""); + assertTrue(success); + userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.styling.theme"); + assertEquals("\"test\"",userdata); + success = userDbProvider.setUserdata(USERNAME2,"networkMap.styling.theme","{\"test\":\"abc\"}"); + assertTrue(success); + userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.styling.theme"); + assertEquals("{\"test\":\"abc\"}",userdata); + + // delete with complex key => forbidden + success = userDbProvider.removeUserdata(USERNAME2,"networkMap.styling.theme2"); + assertFalse(success); + // delete with complex key => allowed + success = userDbProvider.removeUserdata(USERNAME2,"networkMap.styling.theme"); + assertTrue(success); + userdata = userDbProvider.getUserdata(USERNAME2, "networkMap.styling"); + assertEquals("{}",userdata); + + + } + private static String getFileContent(String filename) throws IOException { return String.join("\n", IoUtils.readAllLines(TestUserdata.class.getResourceAsStream(filename))); } diff --git a/sdnr/wt/data-provider/provider/src/test/resources/userdata/full.json b/sdnr/wt/data-provider/provider/src/test/resources/userdata/full.json index c5b41ec21..4206ddc31 100644 --- a/sdnr/wt/data-provider/provider/src/test/resources/userdata/full.json +++ b/sdnr/wt/data-provider/provider/src/test/resources/userdata/full.json @@ -8,6 +8,13 @@ "zoom": "10", "longitude": "13.35" }, + "themes": [ + { + "key": "light", + "padding": 10, + "mainColor": "#ff0000" + } + ], "tileOpacity": "26" } }
\ No newline at end of file diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/DatabaseInfo7.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/DatabaseInfo7.java index 87fc07ef9..8a7f03040 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/DatabaseInfo7.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/DatabaseInfo7.java @@ -27,20 +27,33 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data; */ public class DatabaseInfo7 extends DatabaseInfo { + private boolean disableMapping; + public DatabaseInfo7(String alias, String doctype, String mapping) { super(alias, alias, doctype, mapping); + this.disableMapping = false; } public DatabaseInfo7(String alias, String doctype, String mapping, String settingsformat) { super(alias, alias, doctype, mapping, settingsformat); + this.disableMapping = false; } public DatabaseInfo7(String index, String alias, String doctype, String mapping, String settingsformat) { super(index, alias, doctype, mapping, settingsformat); + this.disableMapping = false; } @Override public String getMapping(boolean useStrict) { + if(this.disableMapping){ + return "{\"enabled\": false}"; + } return this.mapping == null ? null : String.format("{%s\"properties\":%s}", useStrict ? "\"dynamic\": false," : "\"dynamic\": true,", this.mapping); } + + public DatabaseInfo disableMapping() { + this.disableMapping = true; + return this; + } } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/istanbul/IstanbulReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/istanbul/IstanbulReleaseInformation.java index 6148e21d5..ba39a81b3 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/istanbul/IstanbulReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/istanbul/IstanbulReleaseInformation.java @@ -170,7 +170,7 @@ public class IstanbulReleaseInformation extends ReleaseInformation { public static Map<ComponentName, DatabaseInfo> createDBMap() { Map<ComponentName, DatabaseInfo> map = HonoluluReleaseInformation.createDBMap(); - map.put(ComponentName.USERDATA, new DatabaseInfo7("userdata", "userdata", "{}")); + map.put(ComponentName.USERDATA, new DatabaseInfo7("userdata", "userdata", "").disableMapping()); map.put(ComponentName.REQUIRED_NETWORKELEMENT, new DatabaseInfo7("networkelement-connection", "networkelement-connection", "{\"node-id\": {\"type\": \"keyword\"},\"host\": {\"type\": \"keyword\"},\"port\": " |