From cb9bd650a1ba6c1c2f5c7dd410f640b9b6f876c7 Mon Sep 17 00:00:00 2001 From: Ravi Pendurty Date: Fri, 20 Oct 2023 13:23:33 +0530 Subject: Extend user settings API Extend user settings API Issue-ID: CCSDK-3947 Change-Id: Ic0e7783abb2b084142c5bc2d36dc31810f6ca9cf Signed-off-by: Ravi Pendurty --- .../database/sqldb/data/HtUserdataManagerBase.java | 101 ++++++++++++++++++++- sdnr/wt/data-provider/dblib/test2.properties | 7 -- sdnr/wt/data-provider/installer/pom.xml | 13 ++- .../wt/dataprovider/http/UserdataHttpServlet.java | 2 +- .../sdnr/wt/dataprovider/test/TestUserdata.java | 58 +++++++++++- .../provider/src/test/resources/userdata/full.json | 7 ++ .../wt/dataprovider/setup/data/DatabaseInfo7.java | 13 +++ .../setup/istanbul/IstanbulReleaseInformation.java | 2 +- 8 files changed, 186 insertions(+), 17 deletions(-) delete mode 100644 sdnr/wt/data-provider/dblib/test2.properties (limited to 'sdnr') 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 + 4.0.0 @@ -43,7 +44,17 @@ sdnr-wt-data-provider false - + + + + org.onap.ccsdk.parent + installed-odl-bom + 2.6.0 + pom + import + + + ${project.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 createDBMap() { Map 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\": " -- cgit 1.2.3-korg