diff options
author | Michael DÜrre <michael.duerre@highstreet-technologies.com> | 2022-03-10 10:39:16 +0100 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2022-03-23 15:08:48 -0400 |
commit | eb2a7c97c0007b013bd1784ac17d57be02b63d03 (patch) | |
tree | b0861438532727219216cfd426e21ae053abb31a /sdnr/wt/data-provider/dblib/src/main | |
parent | 3c74361514bb920ed441d105f66ca978bd074ced (diff) |
migrate sdnr features to phosphorus
fix mapper and switch to dom api
Updated to use phosphorus version of parent poms
Issue-ID: CCSDK-3566
Signed-off-by: Michael DÜrre <michael.duerre@highstreet-technologies.com>
Change-Id: I98c5bef9286622e0d66b53db687557d798cd53f5
Signed-off-by: Michael DÜrre <michael.duerre@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/data-provider/dblib/src/main')
19 files changed, 248 insertions, 147 deletions
diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java index ce9d4dcd1..1ed0b5f17 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java @@ -56,7 +56,6 @@ public class SqlDBClient { private static final String DBVERSION_REGEX = "^([\\d]+\\.[\\d]+\\.[\\d]+)"; private static final Pattern DBVERSION_PATTERN = Pattern.compile(DBVERSION_REGEX); private static final String SELECT_VERSION_QUERY = "SELECT @@version as version"; - private static final String LOG_PROBLEM_CLOSING_CONNECTION = "problem closing connection: "; private static final String DBNAME_DEFAULT = "sdnrdb"; private final String dbConnectionString; @@ -100,6 +99,7 @@ public class SqlDBClient { } catch (SQLException e) { LOG.warn("problem reading views: ", e); } + try { data.close(); } catch (SQLException ignore) { } return list; } @@ -114,6 +114,7 @@ public class SqlDBClient { } catch (SQLException e) { LOG.warn("problem reading tables: ", e); } + try { data.close(); } catch (SQLException ignore) { } return list; } @@ -155,22 +156,18 @@ public class SqlDBClient { public boolean createTable(String query) { boolean result = false; PreparedStatement stmt = null; + Connection connection = null; try { - Connection connection = this.getConnection(); + connection = this.getConnection(); stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt.execute(); - connection.close(); + result = true; } catch (SQLException e) { LOG.warn("problem creating table:", e); } finally { - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { - LOG.warn("problem closing stmt:", e); - } - } + if (stmt != null) try { stmt.close(); } catch (SQLException logOrIgnore) {} + if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} } return result; } @@ -199,20 +196,17 @@ public class SqlDBClient { boolean result = false; SQLException innerE = null; Statement stmt = null; - try (Connection connection = this.getConnection()) { + Connection connection = null; + try { + connection= this.getConnection(); stmt = connection.createStatement(); result = stmt.execute(query); result = stmt.getUpdateCount() > 0 ? stmt.getUpdateCount() > 0 : result; } catch (SQLException e) { innerE = e; } finally { - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - LOG.warn(LOG_PROBLEM_CLOSING_CONNECTION, e); - } + if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {} + if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } if (innerE != null) { throw innerE; @@ -224,20 +218,17 @@ public class SqlDBClient { boolean result = false; SQLException innerE = null; PreparedStatement stmt = null; - try (Connection connection = this.getConnection()) { + Connection connection = null; + try { + connection = this.getConnection(); stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); result = stmt.execute(); result = stmt.getUpdateCount() > 0 ? stmt.getUpdateCount() > 0 : result; } catch (SQLException e) { innerE = e; } finally { - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - LOG.warn(LOG_PROBLEM_CLOSING_CONNECTION, e); - } + if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {} + if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } if (innerE != null) { throw innerE; @@ -249,24 +240,22 @@ public class SqlDBClient { String result = null; SQLException innerE = null; PreparedStatement stmt = null; - try (Connection connection = this.getConnection()) { + ResultSet generatedKeys = null; + Connection connection = null; + try { + connection = this.getConnection(); stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt.execute(); - ResultSet generatedKeys = stmt.getGeneratedKeys(); + generatedKeys = stmt.getGeneratedKeys(); if (generatedKeys.next()) { result = String.valueOf(generatedKeys.getLong(1)); } } catch (SQLException e) { innerE = e; } finally { - try { - if (stmt != null) { - stmt.close(); - } - - } catch (SQLException e) { - LOG.warn(LOG_PROBLEM_CLOSING_CONNECTION, e); - } + if (generatedKeys != null) try { generatedKeys.close(); } catch (SQLException ignore) {} + if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {} + if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } if (innerE != null) { throw innerE; @@ -291,19 +280,16 @@ public class SqlDBClient { public ResultSet read(String query) { ResultSet data = null; Statement stmt = null; - try (Connection connection = this.getConnection()) { + Connection connection = null; + try{ + connection = this.getConnection(); stmt = connection.createStatement(); data = stmt.executeQuery(query); } catch (SQLException e) { LOG.warn("problem reading db for query '{}': ", query, e); } finally { - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - LOG.warn(LOG_PROBLEM_CLOSING_CONNECTION, e); - } + if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {} + if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } return data; } 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 60f28d95c..e015dd29f 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 @@ -74,7 +74,7 @@ public abstract class HtUserdataManagerBase implements HtUserdataManager { @Override public boolean setUserdata(String username, String key, String data) { - JSONObject o = new JSONObject(); + JSONObject o = new JSONObject(this.getUserdata(username)); o.put(key, new JSONObject(data)); return this.setUserdata(username, o.toString()); } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java index 79d963751..832d473c2 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java @@ -21,16 +21,7 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data; -import java.util.Arrays; -import java.util.List; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult; import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriterUserdata; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultcurrentListInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.PaginationBuilder; -import org.opendaylight.yangtools.yang.common.Uint32; -import org.opendaylight.yangtools.yang.common.Uint64; public class HtUserdataManagerImpl extends HtUserdataManagerBase { @@ -42,7 +33,9 @@ public class HtUserdataManagerImpl extends HtUserdataManagerBase { @Override public boolean setUserdata(String username, String data) { - return this.rw.write(new UserdataBuilder().setId(username).setValue(data).build(), username) != null; + Userdata o = new UserdataBuilder().setId(username).setValue(data).build(); + String x = this.rw.updateOrInsert(o, username); + return x!=null; } @Override @@ -52,17 +45,8 @@ public class HtUserdataManagerImpl extends HtUserdataManagerBase { @Override protected String readUserdata(String username, String defaultValue) { - EntityInput input = new ReadFaultcurrentListInputBuilder() - .setFilter(Arrays.asList(new FilterBuilder().setProperty("id").setFiltervalue(username).build())) - .setPagination(new PaginationBuilder().setPage(Uint64.valueOf(1)).setSize(Uint32.valueOf(1)).build()) - .build(); - QueryResult<Userdata> result = this.rw.getData(input); - if (result != null) { - List<Userdata> data = result.getResult(); - Userdata user = (data != null && !data.isEmpty()) ? data.get(0) : null; - return user == null ? defaultValue : user.getValue(); - } - return defaultValue; + Userdata user = this.rw.read(username); + return user!=null? user.getValue():defaultValue; } } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java index b6df73b89..1bb30dc69 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java @@ -120,11 +120,11 @@ public class SqlDBDataProvider extends HtDatabaseEventsService implements Databa this.mediatorserverRW = new SqlDBReaderWriter<>(this.dbClient, Entity.MediatorServer, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId).setWriteInterface(MediatorServerEntity.class); + this.controllerId).setWriteInterface(MediatorServerEntity.class); this.maintenanceRW = new SqlDBReaderWriter<>(this.dbClient, Entity.Maintenancemode, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId).setWriteInterface(MaintenanceEntity.class); + this.controllerId).setWriteInterface(MaintenanceEntity.class); this.readStatus = new SqlDBStatusReader(this.dbClient, this.controllerId); @@ -137,7 +137,7 @@ public class SqlDBDataProvider extends HtDatabaseEventsService implements Databa }; this.dbMaintenanceService = new HtDatabaseMaintenanceService(this); this.usermanager = new HtUserdataManagerImpl(new SqlDBReaderWriterUserdata(this.dbClient, - Entity.Userdata, config.getDbSuffix(), this.dbClient.getDatabaseName(), this.controllerId)); + Entity.Userdata, config.getDbSuffix())); if (initControllerId) { try { this.setControllerId(); @@ -512,6 +512,7 @@ public class SqlDBDataProvider extends HtDatabaseEventsService implements Databa query = String.format("INSERT INTO `%s` (`id`,`desc`) VALUES ('%s','%s')", this.controllerTableName, this.controllerId, ""); LOG.trace(query); + try { data.close(); } catch (SQLException ignore) { } return this.dbClient.write(query); } else { LOG.trace("controllerId already set"); diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java index 6244894e5..9d09a11c8 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java @@ -74,7 +74,6 @@ public class DatabaseIdGenerator { public static String getFaultcurrentId(String nodeId, String objectId, String problemName) { String uuId = extractUuid(objectId); - return String.format(FORMAT_FAULTDATA_ID, nodeId, uuId, problemName); } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java index 88aaa583d..776c91fe5 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java @@ -40,6 +40,7 @@ import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CmlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionLogStatus; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionlogEntity; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity; @@ -50,6 +51,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionEntity; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterBuilder; import org.slf4j.Logger; @@ -89,68 +91,68 @@ public class HtDatabaseEventsService implements DataProvider { this.dbClient = new SqlDBClient(config.getUrl(), config.getUsername(), config.getPassword()); this.connectionlogRW = new SqlDBReaderWriter<>(dbClient, Entity.Connectionlog, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.eventlogRW = new SqlDBReaderWriter<>(dbClient, Entity.Eventlog, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.eventRWFaultLog = new SqlDBReaderWriter<>(dbClient, Entity.Faultlog, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.eventRWFaultCurrent = new SqlDBReaderWriterFault<>(dbClient, Entity.Faultcurrent, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.equipmentRW = new SqlDBReaderWriter<>(dbClient, Entity.Inventoryequipment, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.guicutthroughRW = new SqlDBReaderWriter<>(dbClient, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity.Guicutthrough, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId).setWriteInterface(Guicutthrough.class); + this.controllerId).setWriteInterface(Guicutthrough.class); this.networkelementConnectionRW = new SqlDBReaderWriter<>(dbClient, Entity.NetworkelementConnection, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.networkelementConnectionRW.setWriteInterface(NetworkElementConnectionEntity.class); this.pm15mRW = new SqlDBReaderWriterPm<>(dbClient, Entity.Historicalperformance15min, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.pm24hRW = new SqlDBReaderWriterPm<>(dbClient, Entity.Historicalperformance24h, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); this.eventRWCMLog = new SqlDBReaderWriter<>(dbClient, Entity.Cmlog, config.getDbSuffix(), org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.cmlog.list.output.Data.class, - this.dbClient.getDatabaseName(), this.controllerId); + this.controllerId); } @Override public void writeConnectionLog(ConnectionlogEntity event) { - this.connectionlogRW.write(event, event.getId()); + this.connectionlogRW.write(event, null); } @Override public void writeEventLog(EventlogEntity event) { - this.eventlogRW.write(event, event.getId()); + this.eventlogRW.write(event, null); } @Override public void writeFaultLog(FaultlogEntity fault) { - this.eventRWFaultLog.write(fault, fault.getId()); + this.eventRWFaultLog.write(fault, null); } @Override public void writeCMLog(CmlogEntity cm) { - this.eventRWCMLog.write(cm, cm.getId()); + this.eventRWCMLog.write(cm, null); } @Override public void updateFaultCurrent(FaultcurrentEntity fault) { - final String id = fault.getId() != null ? fault.getId() : DatabaseIdGenerator.getFaultcurrentId(fault); + final String id = DatabaseIdGenerator.getFaultcurrentId(fault); if (FaultEntityManager.isManagedAsCurrentProblem(fault)) { if (FaultEntityManager.isNoAlarmIndication(fault)) { LOG.debug("Remove from currentFaults: {}", fault.toString()); @@ -198,8 +200,7 @@ public class HtDatabaseEventsService implements DataProvider { @Override public int clearGuiCutThroughEntriesOfNode(String nodeName) { - this.guicutthroughRW.remove(nodeName); - return 0; + return this.guicutthroughRW.remove(nodeName); } @Override @@ -213,9 +214,28 @@ public class HtDatabaseEventsService implements DataProvider { return this.networkelementConnectionRW.updateOrInsert(ne, nodeId) != null; } + /** + * Remove network element connection if not required + * This function is called onDisconnect event for netconf node + */ @Override public void removeNetworkConnection(String nodeId) { - this.networkelementConnectionRW.remove(nodeId); + NetworkElementConnectionEntity e = this.networkelementConnectionRW.read(nodeId); + Boolean isRequired = e!=null? e.getIsRequired():null; + if (e != null && isRequired != null) { + if (isRequired) { + LOG.debug("updating connection status for {} of required ne to disconnected", nodeId); + this.networkelementConnectionRW.update(new UpdateNetworkElementConnectionInputBuilder() + .setStatus(ConnectionLogStatus.Disconnected).build(), nodeId); + } else { + LOG.debug("remove networkelement-connection for {} entry because of non-required", nodeId); + this.networkelementConnectionRW.remove(nodeId); + } + } else { + LOG.warn("Unable to update connection-status. dbentry for {} not found in networkelement-connection", + nodeId); + } + } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java index a17c6ae48..8c549ded8 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java @@ -33,8 +33,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.MaintenanceEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter; @@ -109,12 +107,7 @@ public class HtDatabaseMaintenanceService implements HtDatabaseMaintenance { @Override public MaintenanceEntity getMaintenance(@Nullable String nodeId) { - ReadMaintenanceListOutput result = this.dbProvider - .readMaintenanceList( - new ReadMaintenanceListInputBuilder().setFilter(getFilterInput("node-id", nodeId)).build()) - .build(); - final List<Data> data = result.getData(); - return data != null ? !data.isEmpty() ? data.get(0) : null : null; + return this.maintenanceRw.read(DatabaseIdGenerator.getMaintenanceId(nodeId)); } @Override diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java index c63d2f16b..19323b9f7 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java @@ -60,7 +60,7 @@ public class SqlDBMapper { private static final String ENUM_DBTYPE = "VARCHAR(100)"; private static final String BIGINT_DBTYPE = "BIGINT"; public static final String ODLID_DBCOL = "controller-id"; - private static final String ID_DBCOL = "id"; + public static final String ID_DBCOL = "id"; private static List<Class<?>> numericClasses = Arrays.asList(Byte.class, Integer.class, Long.class, BigInteger.class, Uint8.class, Uint16.class, Uint32.class, Uint64.class); private static final YangToolsMapper mapper = new YangToolsMapper(); @@ -78,18 +78,19 @@ public class SqlDBMapper { } public static <T> String createTable(Class<T> clazz, Entity e) throws UnableToMapClassException { - return createTable(clazz, e, "", false); + return createTable(clazz, e, "", false, true); } public static <T> String createTable(Class<T> clazz, Entity e, String suffix) throws UnableToMapClassException { - return createTable(clazz, e, suffix, false); + return createTable(clazz, e, suffix, false, true); } public static <T> String createTable(Class<T> clazz, Entity e, boolean autoIndex) throws UnableToMapClassException { - return createTable(clazz, e, "", false); + return createTable(clazz, e, "", false, true); } - public static <T> String createTable(Class<T> clazz, Entity e, String suffix, boolean autoIndex) + public static <T> String createTable(Class<T> clazz, Entity e, String suffix, boolean autoIndex, + boolean withControllerId) throws UnableToMapClassException { StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE IF NOT EXISTS `" + e.getName() + suffix + "` (\n"); @@ -99,7 +100,9 @@ public class SqlDBMapper { } else { sb.append("`" + ID_DBCOL + "` " + STRING_DBTYPE + " " + getColumnOptions(ID_DBCOL, STRING_DBTYPE) + ",\n"); } - sb.append("`" + ODLID_DBCOL + "` " + ODLID_DBTYPE + " " + getColumnOptions(ODLID_DBCOL, ODLID_DBTYPE) + ",\n"); + if(withControllerId) { + sb.append("`" + ODLID_DBCOL + "` " + ODLID_DBTYPE + " " + getColumnOptions(ODLID_DBCOL, ODLID_DBTYPE) + ",\n"); + } for (Method method : getFilteredMethods(clazz, true)) { Class<?> valueType = method.getReturnType(); String colName = getColumnName(method); @@ -110,8 +113,10 @@ public class SqlDBMapper { String options = getColumnOptions(colName, dbType); sb.append("`" + colName + "` " + dbType + " " + options + ",\n"); } - sb.append("primary key(" + ID_DBCOL + "),"); - sb.append("foreign key(`" + ODLID_DBCOL + "`) references " + TABLENAME_CONTROLLER + "(" + ID_DBCOL + ")"); + sb.append("primary key(" + ID_DBCOL + ")"); + if(withControllerId) { + sb.append(",foreign key(`" + ODLID_DBCOL + "`) references " + TABLENAME_CONTROLLER + "(" + ID_DBCOL + ")"); + } sb.append(");"); return sb.toString(); @@ -350,11 +355,14 @@ public class SqlDBMapper { public static <T> List<T> read(ResultSet data, Class<T> clazz, String column) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException, SecurityException, NoSuchMethodException, JsonProcessingException { - + if(data==null) { + return Arrays.asList(); + } Builder<T> builder = findPOJOBuilder(clazz); if(builder==null && column==null) { throw new InstantiationException("unable to find builder for class "+clazz.getName()); } + List<T> list = new ArrayList<>(); while (data.next()) { if (column == null) { diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java index b5322d6ff..56e965c9f 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java @@ -57,34 +57,47 @@ public class SqlDBReaderWriter<T extends DataObject> { protected final Entity entity; private final Class<T> clazz; protected final SqlDBClient dbService; - private final String dbName; protected final String controllerId; protected final String tableName; + private final boolean ignoreControllerId; - public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String dbName, + public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String controllerId) { + this(dbService, e, dbSuffix, clazz, controllerId, false); + } + + public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, + String controllerId, boolean ignoreControllerId) { this.dbService = dbService; this.entity = e; this.clazz = clazz; - this.dbName = dbName; this.tableName = this.entity.getName() + dbSuffix; this.controllerId = controllerId; + this.ignoreControllerId = ignoreControllerId; } public long count(List<Filter> filter) throws SQLException { String query; if (filter == null || filter.isEmpty()) { - query = String.format("SELECT table_rows FROM `information_schema`.`tables` " - + "WHERE `table_schema` = '%s' AND `table_name` = '%s'", this.dbName, this.tableName); + // query = String.format("SELECT table_rows FROM `information_schema`.`tables` " + // + "WHERE `table_schema` = '%s' AND `table_name` = '%s'", this.dbName, this.tableName); + query = String.format("SELECT COUNT(`id`) FROM `%s`", this.tableName); } else { query = String.format("SELECT COUNT(`id`) FROM `%s` %s", this.tableName, SqlQuery.getWhereExpression(filter)); } ResultSet data = this.dbService.read(query); + if(data==null) { + return 0; + } long cnt = 0; if (data.next()) { cnt = data.getLong(1); } + try { + data.close(); + } catch (SQLException ignore) { + } return cnt; } @@ -106,11 +119,19 @@ public class SqlDBReaderWriter<T extends DataObject> { public QueryResult<T> getData(EntityInput input) { SelectQuery query = new SelectQuery(this.tableName, input, this.controllerId); - LOG.trace("query={}", query.toSql()); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } try { ResultSet data = this.dbService.read(query.toSql()); List<T> mappedData = SqlDBMapper.read(data, clazz); final Map<FilterKey, Filter> filter = input.getFilter(); + try { + if(data!=null) { + data.close(); + } + } catch (SQLException ignore) { + } long total = this.count(filter != null ? new ArrayList<>(filter.values()) : null, this.controllerId); return new QueryResult<T>(mappedData, query.getPage(), query.getPageSize(), total); } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException @@ -126,8 +147,11 @@ public class SqlDBReaderWriter<T extends DataObject> { if (id == null) { return this.writeWithoutId(object); } - InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId); + InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId); query.setId(id); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } boolean success = false; try { success = this.dbService.write(query.toSql()); @@ -140,7 +164,11 @@ public class SqlDBReaderWriter<T extends DataObject> { private <S extends DataObject> String writeWithoutId(S object) { - InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId); + InsertQuery<S> query = + new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } try { return this.dbService.writeAndReturnId(query.toSql()); } catch (SQLException e) { @@ -150,8 +178,11 @@ public class SqlDBReaderWriter<T extends DataObject> { } public <S extends DataObject> String update(S object, String id) { - UpdateQuery<S> query = new UpdateQuery<S>(this.entity, object, this.controllerId); + UpdateQuery<S> query = new UpdateQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true); query.setId(id); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } String insertedId = null; PreparedStatement stmt = null; try { @@ -164,6 +195,9 @@ public class SqlDBReaderWriter<T extends DataObject> { if (affectedRows > 0) { insertedId = id; } + if (LOG.isTraceEnabled()) { + LOG.trace("insertedid={}", insertedId); + } } catch (SQLException e) { LOG.warn("problem writing data into db: ", e); } finally { @@ -180,10 +214,12 @@ public class SqlDBReaderWriter<T extends DataObject> { } public <S extends DataObject> String updateOrInsert(S object, String id) { - UpsertQuery<S> query = new UpsertQuery<S>(this.entity, object, this.controllerId); + UpsertQuery<S> query = new UpsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true); query.setId(id); String insertedId = null; - LOG.trace("query={}", query.toSql()); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } PreparedStatement stmt = null; try { Connection connection = this.dbService.getConnection(); @@ -221,6 +257,9 @@ public class SqlDBReaderWriter<T extends DataObject> { public int remove(List<Filter> filters) { DeleteQuery query = new DeleteQuery(this.entity, filters); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } int affectedRows = 0; PreparedStatement stmt = null; try { @@ -249,9 +288,16 @@ public class SqlDBReaderWriter<T extends DataObject> { public <S extends DataObject> List<S> readAll(Class<S> clazz) { SelectQuery query = new SelectQuery(this.tableName); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } try { ResultSet data = this.dbService.read(query.toSql()); List<S> mappedData = SqlDBMapper.read(data, clazz); + try { + data.close(); + } catch (SQLException ignore) { + } return mappedData; } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { @@ -262,9 +308,16 @@ public class SqlDBReaderWriter<T extends DataObject> { public List<String> readAll(String key) { SelectQuery query = new SelectQuery(this.tableName, key, this.controllerId).groupBy(key); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } try { ResultSet data = this.dbService.read(query.toSql()); List<String> mappedData = SqlDBMapper.read(data, String.class, key); + try { + data.close(); + } catch (SQLException ignore) { + } return mappedData; } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { @@ -272,4 +325,22 @@ public class SqlDBReaderWriter<T extends DataObject> { } return null; } + + public T read(String id) { + SelectQuery query = + new SelectQuery(this.tableName, this.controllerId).addFilter(SqlDBMapper.ID_DBCOL, id); + if (LOG.isTraceEnabled()) { + LOG.trace("query={}", query.toSql()); + } + T item = null; + try { + ResultSet data = this.dbService.read(query.toSql()); + List<T> mappedData = SqlDBMapper.read(data, clazz); + item = mappedData.size()>0? mappedData.get(0): null; + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading data {}: ", this.entity, e); + } + return item; + } } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java index 2d4e26eea..86d888a16 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java @@ -30,9 +30,9 @@ public class SqlDBReaderWriterFault<T extends DataObject> extends SqlDBReaderWri private static final String NODE_KEY = "node-id"; - public SqlDBReaderWriterFault(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String dbName, + public SqlDBReaderWriterFault(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String controllerId) { - super(dbService, e, dbSuffix, clazz, dbName, controllerId); + super(dbService, e, dbSuffix, clazz, controllerId); } public List<String> getAllNodes() { diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java index 021741bd8..22aabc429 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java @@ -53,9 +53,9 @@ public class SqlDBReaderWriterPm<T extends DataObject> extends SqlDBReaderWriter private static final FilterKey FILTERKEY = new FilterKey(KEY); - public SqlDBReaderWriterPm(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String dbName, + public SqlDBReaderWriterPm(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String controllerId) { - super(dbService, e, dbSuffix, clazz, dbName, controllerId); + super(dbService, e, dbSuffix, clazz, controllerId); } /** @@ -83,6 +83,7 @@ public class SqlDBReaderWriterPm<T extends DataObject> extends SqlDBReaderWriter try { ResultSet data = this.dbService.read(query.toSql()); List<String> mappedData = SqlDBMapper.read(data, String.class, UUID_KEY); + try { data.close(); } catch (SQLException ignore) { } Map<FilterKey, Filter> inpFilter = input.getFilter(); long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId); return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total); @@ -114,6 +115,7 @@ public class SqlDBReaderWriterPm<T extends DataObject> extends SqlDBReaderWriter try { ResultSet data = this.dbService.read(query.toSql()); List<String> mappedData = SqlDBMapper.read(data, String.class, NODE_KEY); + try { data.close(); } catch (SQLException ignore) { } Map<FilterKey, Filter> inpFilter = input.getFilter(); long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId); return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total); diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterUserdata.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterUserdata.java index 2c849ac67..258cd160f 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterUserdata.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterUserdata.java @@ -27,11 +27,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro public class SqlDBReaderWriterUserdata extends SqlDBReaderWriter<Userdata> { - public SqlDBReaderWriterUserdata(SqlDBClient dbService, Entity e, String dbSuffix, String dbName, - String controllerId) { - super(dbService, e, dbSuffix, Userdata.class, dbName, controllerId); + public SqlDBReaderWriterUserdata(SqlDBClient dbService, Entity e, String dbSuffix) { + super(dbService, e, dbSuffix, Userdata.class, null, true); } + } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java index c98ba388d..b0b169d18 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java @@ -74,6 +74,7 @@ public class SqlDBStatusReader { } } + try { data.close(); } catch (SQLException ignore) { } } catch (SQLException e) { LOG.warn("problem reading status:", e); } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java index c499fefb8..1c1ec622b 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java @@ -59,7 +59,7 @@ public class CountQuery implements SqlQuery { this(e); Map<FilterKey, Filter> filter = input != null ? input.getFilter() : null; if (filter != null && filter.size() > 0) { - this.filters.addAll(filters); + this.filters.addAll(filter.values()); } if (controllerId != null) { this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId); diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java index 108888ad0..f52fbd55a 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java @@ -39,17 +39,28 @@ public class InsertQuery<T extends DataObject> implements SqlQuery { protected final Entity entity; private final String controllerId; + private final boolean ignoreControllerId; private final T object; private final boolean ignoreNull; private String id; + private final boolean ignoreIdField; public InsertQuery(Entity e, T object, String controllerId) { + this(e, object, controllerId, SqlQuery.DEFAULT_IGNORE_CONTROLLERID); + } + + public InsertQuery(Entity e, T object, String controllerId, boolean ignoreControllerId) { + this(e, object, controllerId, ignoreControllerId, SqlQuery.DEFAULT_IGNORE_ID_FIELD); + } + + public InsertQuery(Entity e, T object, String controllerId, boolean ignoreControllerId, boolean ignoreIdField) { this.entity = e; this.controllerId = controllerId; this.object = object; this.ignoreNull = true; this.id = null; - + this.ignoreControllerId = ignoreControllerId; + this.ignoreIdField = ignoreIdField; } @Override @@ -63,8 +74,8 @@ public class InsertQuery<T extends DataObject> implements SqlQuery { return null; } - protected String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, - JsonProcessingException { + protected String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, + InvocationTargetException, JsonProcessingException { Class<?> cls = this.object.getClass(); List<DBKeyValuePair<String>> kvps = new ArrayList<>(); List<String> cols = new ArrayList<>(); @@ -78,26 +89,37 @@ public class InsertQuery<T extends DataObject> implements SqlQuery { m.setAccessible(true); value = m.invoke(this.object); col = SqlDBMapper.getColumnName(m); - if (col.equals("id") && this.id != null) { - value = this.id; + if (col.equals("id")) { + if (this.ignoreIdField) { + continue; + } + if (this.id != null) { + value = this.id; + } } if (ignoreNull && value == null) { continue; } - DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m,col, value); + DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m, col, value); cols.add(kvp.getKey()); args.add(kvp.getValue()); kvps.add(kvp); } if (this.id != null && !cols.contains("`id`")) { cols.add("`id`"); - args.add("'"+this.id+"'"); + args.add("'" + this.id + "'"); + } + if (!this.ignoreControllerId) { + args.add("'" + this.controllerId + "'"); + } + sb.append(String.join(",", cols)); + if (!this.ignoreControllerId) { + sb.append(",`" + SqlDBMapper.ODLID_DBCOL + "`) VALUES ("); + } else { + sb.append(") VALUES ("); } - args.add("'"+this.controllerId+"'"); - sb.append( String.join(",", cols)); - sb.append(",`" + SqlDBMapper.ODLID_DBCOL + "`) VALUES ("); - sb.append( String.join(",", args)+" )"); - this.appendAdditionalToQuery(sb,kvps); + sb.append(String.join(",", args) + " )"); + this.appendAdditionalToQuery(sb, kvps); return sb.toString(); } diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java index 2b3f9f3fa..6403a5c1d 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java @@ -54,9 +54,11 @@ public class SelectQuery implements SqlQuery { private final List<String> groups; public SelectQuery(String tableName) { - this(tableName, Arrays.asList("*"), null); + this(tableName, (String)null); + } + public SelectQuery(String tableName, String controllerId) { + this(tableName, Arrays.asList("*"), controllerId); } - public SelectQuery(String tableName, List<String> fields, String controllerId) { this.tableName = tableName; this.fields = fields; @@ -105,18 +107,21 @@ public class SelectQuery implements SqlQuery { } - public void addFilter(String property, String filtervalue) { + public SelectQuery addFilter(String property, String filtervalue) { this.addFilter(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build()); + return this; } private static Filter cleanFilter(Filter filter) { - if (filter.getFiltervalue() != null - && (filter.getFiltervalues() == null || filter.getFiltervalues().isEmpty())) { + final String sFilter = filter.getFiltervalue(); + final List<String> sFilters = filter.getFiltervalues(); + //if only single filter value is set + if (sFilter != null && (sFilters == null || sFilter.isEmpty())) { return "*".equals(filter.getFiltervalue()) ? null : filter; } else { List<String> list = new ArrayList<>(filter.getFiltervalues()); - if (filter.getFiltervalue() != null && !filter.getFiltervalue().isEmpty()) { - list.add(filter.getFiltervalue()); + if (sFilter != null && !sFilter.isEmpty()) { + list.add(sFilter); } if (list.size() == 1 && "*".equals(list.get(0))) { return null; diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java index 82f10b1e5..3e26d0935 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java @@ -27,6 +27,7 @@ import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.TimeZone; +import java.util.stream.Collectors; import org.eclipse.jdt.annotation.Nullable; import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBFilterKeyValuePair; @@ -46,12 +47,15 @@ public interface SqlQuery { static final String MARIADB_TIMESTAMP_REPLACER_MIN = "0000-00-00 00:00:00"; static final int MARIADB_TIMESTAMP_REPLACER_MIN_LENGTH = MARIADB_TIMESTAMP_REPLACER_MIN.length(); static final int MARIADB_TIMESTAMP_REPLACER_MAX_LENGTH = MARIADB_TIMESTAMP_REPLACER.length(); + static final boolean DEFAULT_IGNORE_CONTROLLERID = false; + static final boolean DEFAULT_IGNORE_ID_FIELD = false; public static String getWhereExpression(List<Filter> filters) { if (filters == null) { return ""; } StringBuilder sb = new StringBuilder(); + filters = filters.stream().filter(e -> !"*".equals(e.getFiltervalue())).collect(Collectors.toList()); if (!filters.isEmpty()) { sb.append(" WHERE (" + getFilterExpression(filters.get(0)) + ")"); @@ -64,7 +68,8 @@ public interface SqlQuery { public static String getFilterExpression(Filter filter) { String property = filter.getProperty(); - List<String> values = collectValues(filter.getFiltervalue(), filter.getFiltervalues()); + List<String> values = collectValues(filter.getFiltervalue(), filter.getFiltervalues()).stream() + .filter(e -> !"*".equals(e)).collect(Collectors.toList()); if (values.size() == 1) { return getFilterExpression(property, values.get(0)); } else if (values.size() > 1) { @@ -225,7 +230,7 @@ public interface SqlQuery { } private static String netconfToMariaDBTimestamp(String ts) { - if(ts==null) { + if (ts == null) { return null; } String v = ts.replace("T", " ").replace("Z", ""); diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java index b47b03042..dc81161ee 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java @@ -44,10 +44,10 @@ public class UpdateQuery<T extends DataObject> implements SqlQuery { private String id; public UpdateQuery(Entity e, T object) { - this(e, object, null); + this(e, object, null, SqlQuery.DEFAULT_IGNORE_CONTROLLERID, SqlQuery.DEFAULT_IGNORE_ID_FIELD); } - public UpdateQuery(Entity e, T object, String controllerId) { + public UpdateQuery(Entity e, T object, String controllerId, boolean ignoreControllerId, boolean ignoreIdField) { this.entity = e; this.controllerId = controllerId; this.object = object; diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java index e6f1e13cb..39c505454 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java @@ -32,6 +32,10 @@ public class UpsertQuery<T extends DataObject> extends InsertQuery<T> { super(e, object, controllerId); } + public UpsertQuery(Entity e, T object, String controllerId, boolean ignoreControllerId, boolean ignoreIdField) { + super(e, object, controllerId, ignoreControllerId, ignoreIdField); + + } @Override protected void appendAdditionalToQuery(StringBuilder sb, List<DBKeyValuePair<String>> keyValues) { |