From 016536791b95df23811dabd063010f0561526163 Mon Sep 17 00:00:00 2001 From: Ravi Pendurty Date: Mon, 26 Jul 2021 12:42:00 +0530 Subject: Support for MariaDB Support for MariaDB Issue-ID: CCSDK-3384 Signed-off-by: Ravi Pendurty Change-Id: Ic93ad33257a091f2cd208cdf02d1d7400c256bf0 Signed-off-by: Ravi Pendurty --- sdnr/wt/common-yang/openroadm-pm-types/pom.xml | 12 +- sdnr/wt/common-yang/utils/pom.xml | 5 + .../sdnr/wt/yang/mapper/YangToolsMapperHelper.java | 17 + .../YangToolsBuilderAnnotationIntrospector.java | 12 +- .../YangToolsDeserializerModifier.java | 26 +- .../ConfigurationFileRepresentation.java | 2 +- .../sdnr/wt/common/database/ExtRestClient.java | 8 +- .../sdnr/wt/common/database/HtDatabaseClient.java | 6 +- .../sdnr/wt/common/database/data/AliasesEntry.java | 4 + .../wt/common/database/data/DatabaseVersion.java | 149 +++++ .../sdnr/wt/common/database/data/EsVersion.java | 149 ----- .../sdnr/wt/common/database/data/IndicesEntry.java | 16 +- .../wt/common/database/queries/QueryBuilder.java | 13 + .../common/database/responses/GetInfoResponse.java | 8 +- .../features/sdnr/wt/common/file/PomFile.java | 6 +- .../sdnr/wt/common/http/BaseHTTPClient.java | 17 +- .../features/sdnr/wt/common/test/TestEsData.java | 10 +- sdnr/wt/data-provider/dblib/pom.xml | 133 +++++ .../database/DatabaseDataProvider.java | 132 +++++ .../dataprovider/database/sqldb/SqlDBClient.java | 264 +++++++++ .../dataprovider/database/sqldb/SqlDBConfig.java | 112 ++++ .../database/sqldb/data/HtUserdataManagerImpl.java | 150 +++++ .../database/sqldb/data/SqlDBDataProvider.java | 509 ++++++++++++++++ .../database/sqldb/data/SqlDBIndicesEntry.java | 33 ++ .../sqldb/data/entity/DatabaseIdGenerator.java | 104 ++++ .../sqldb/data/entity/FaultEntityManager.java | 65 +++ .../sqldb/data/entity/HtDatabaseEventsService.java | 269 +++++++++ .../data/entity/HtDatabaseMaintenanceService.java | 126 ++++ .../sqldb/data/rpctypehelper/QueryResult.java | 60 ++ .../data/rpctypehelper/SqlDBQueryByFilter.java | 373 ++++++++++++ .../database/sqldb/database/SqlDBMapper.java | 536 +++++++++++++++++ .../database/sqldb/database/SqlDBReaderWriter.java | 246 ++++++++ .../sqldb/database/SqlDBReaderWriterFault.java | 43 ++ .../sqldb/database/SqlDBReaderWriterPm.java | 133 +++++ .../database/sqldb/database/SqlDBStatusReader.java | 132 +++++ .../database/sqldb/query/CountQuery.java | 82 +++ .../database/sqldb/query/DeleteQuery.java | 44 ++ .../database/sqldb/query/InsertQuery.java | 111 ++++ .../database/sqldb/query/SelectQuery.java | 166 ++++++ .../database/sqldb/query/SqlQuery.java | 351 ++++++++++++ .../database/sqldb/query/UpdateQuery.java | 111 ++++ .../database/sqldb/query/UpsertQuery.java | 56 ++ .../sqldb/query/filters/DBFilterKeyValuePair.java | 31 + .../sqldb/query/filters/DBKeyValuePair.java | 64 +++ .../sqldb/query/filters/RangeSqlDBFilter.java | 51 ++ .../sqldb/query/filters/RegexSqlDBFilter.java | 35 ++ .../database/sqldb/query/filters/SqlDBFilter.java | 28 + .../dataprovider/dblib/test/TestCRUDMariaDB.java | 236 ++++++++ .../dblib/test/TestMariaDataProvider.java | 563 ++++++++++++++++++ .../dataprovider/dblib/test/TestQuerySyntax.java | 157 +++++ .../dblib/test/util/MariaDBTestBase.java | 192 +++++++ .../dblib/src/test/resources/inventory.json | 381 ++++++++++++ .../dblib/src/test/resources/pmdata15m.json | 532 +++++++++++++++++ sdnr/wt/data-provider/feature/pom.xml | 4 + sdnr/wt/data-provider/installer/pom.xml | 4 + .../dataprovider/model/ArchiveCleanProvider.java | 6 +- .../sdnr/wt/dataprovider/model/DataProvider.java | 14 +- .../model/HtDatabaseMediatorserver.java | 30 + .../wt/dataprovider/model/IEntityDataProvider.java | 3 - .../sdnr/wt/dataprovider/model/IEsConfig.java | 3 +- .../sdnr/wt/dataprovider/model/SdnrDbType.java | 26 + .../src/main/yang/data-provider@2020-11-10.yang | 44 ++ sdnr/wt/data-provider/pom.xml | 1 + sdnr/wt/data-provider/provider/pom.xml | 29 +- .../data/ElasticSearchDataProvider.java | 455 --------------- .../dataprovider/data/HtUserdataManagerImpl.java | 150 ----- .../data/MediatorServerDataProvider.java | 103 ---- .../data/acessor/DataObjectAcessor.java | 60 -- .../data/acessor/DataObjectAcessorPm.java | 132 ----- .../data/acessor/DataObjectAcessorStatus.java | 76 --- .../data/acessor/DataObjectAcessorWithId.java | 41 -- .../data/entity/FaultEntityManager.java | 98 ---- .../data/entity/HtDatabaseEventsService.java | 628 -------------------- .../data/entity/HtDatabaseMaintenanceService.java | 176 ------ .../data/rpctypehelper/QueryByFilter.java | 433 -------------- .../data/rpctypehelper/QueryByFilterStatic.java | 337 ----------- .../data/rpctypehelper/QueryResult.java | 71 --- .../database/EsDataObjectReaderWriter2.java | 405 ------------- .../database/elasticsearch/EsConfig.java | 224 ++++++++ .../elasticsearch/EsDataObjectReaderWriter2.java | 413 +++++++++++++ .../data/acessor/DataObjectAcessor.java | 60 ++ .../data/acessor/DataObjectAcessorPm.java | 131 +++++ .../data/acessor/DataObjectAcessorStatus.java | 103 ++++ .../data/acessor/DataObjectAcessorWithId.java | 40 ++ .../data/entity/FaultEntityManager.java | 98 ++++ .../data/entity/HtDatabaseEventsService.java | 638 +++++++++++++++++++++ .../data/entity/HtDatabaseMaintenanceService.java | 176 ++++++ .../data/rpctypehelper/QueryByFilter.java | 436 ++++++++++++++ .../data/rpctypehelper/QueryByFilterStatic.java | 337 +++++++++++ .../data/rpctypehelper/QueryResult.java | 71 +++ .../impl/ElasticSearchDataProvider.java | 520 +++++++++++++++++ .../elasticsearch/impl/HtUserdataManagerImpl.java | 150 +++++ .../wt/dataprovider/http/DataTreeChildObject.java | 13 +- .../sdnr/wt/dataprovider/http/MsServlet.java | 15 +- .../wt/dataprovider/http/ReadyHttpServlet.java | 140 ----- .../dataprovider/http/about/AboutHttpServlet.java | 120 ++-- .../wt/dataprovider/http/about/MarkdownTable.java | 22 +- .../wt/dataprovider/http/about/ODLVersionLUT.java | 7 + .../http/yangschema/YangFileProvider.java | 25 +- .../wt/dataprovider/impl/DataProviderConfig.java | 74 +++ .../wt/dataprovider/impl/DataProviderImpl.java | 13 +- .../dataprovider/impl/DataProviderServiceImpl.java | 58 +- .../wt/dataprovider/impl/DataTreeProviderImpl.java | 34 +- .../sdnr/wt/dataprovider/impl/EsConfig.java | 215 ------- .../impl/MediatorServerDataProvider.java | 92 +++ .../provider/src/main/resources/about/README.json | 16 + .../org/opendaylight/blueprint/impl-blueprint.xml | 18 +- .../sdnr/wt/dataprovider/test/TestAbout.java | 2 +- .../wt/dataprovider/test/TestCRUDforDatabase.java | 7 +- .../sdnr/wt/dataprovider/test/TestConfig.java | 2 +- .../wt/dataprovider/test/TestConfiguration.java | 2 +- .../wt/dataprovider/test/TestEventService.java | 21 +- .../wt/dataprovider/test/TestFilterConversion.java | 4 +- .../wt/dataprovider/test/TestImplementation.java | 1 - .../test/TestInventoryConsistency.java | 9 +- .../test/TestMaintenanceServiceData.java | 5 +- .../test/TestMediatorServerService.java | 9 +- .../wt/dataprovider/test/TestTimestampFilter.java | 2 +- .../sdnr/wt/dataprovider/test/TestTree.java | 5 +- .../sdnr/wt/dataprovider/test/TestUserdata.java | 2 +- .../dataprovider/test/TestYangGenSalMapping.java | 2 +- .../wt/dataprovider/test/util/MariaDBTestBase.java | 169 ++++++ .../provider/src/test/resources/tlskeys/keys1.json | 40 ++ sdnr/wt/data-provider/setup/pom.xml | 21 +- .../setup/DataMigrationProviderImpl.java | 408 +------------ .../setup/DataMigrationProviderService.java | 13 +- .../sdnr/wt/dataprovider/setup/Program.java | 31 +- .../wt/dataprovider/setup/ReleaseInformation.java | 39 +- .../dataprovider/setup/data/MariaDBTableInfo.java | 42 ++ .../sdnr/wt/dataprovider/setup/data/Release.java | 65 ++- .../wt/dataprovider/setup/data/ReleaseGroup.java | 8 +- .../ElasticsearchDataMigrationProvider.java | 465 +++++++++++++++ .../database/MariaDbDataMigrationProvider.java | 433 ++++++++++++++ .../setup/elalto/ElAltoReleaseInformation.java | 18 +- .../frankfurt/FrankfurtReleaseInformation.java | 22 +- .../frankfurt/FrankfurtReleaseInformationR2.java | 19 +- .../setup/guilin/GuilinReleaseInformation.java | 15 +- .../setup/honolulu/HonoluluReleaseInformation.java | 15 +- .../setup/istanbul/IstanbulReleaseInformation.java | 161 +++++- .../wt/dataprovider/setup/MariaDBTestBase.java | 187 ++++++ .../wt/dataprovider/setup/TestMariaDBIstanbul.java | 90 +++ .../wt/dataprovider/setup/TestMariaDBMapper.java | 228 ++++++++ .../dataprovider/setup/TestMigrationProvider.java | 6 +- .../wt/devicemanager/impl/DeviceManagerImpl.java | 4 - .../wt/devicemanager/test/TestDevicemanager.java | 1 - 145 files changed, 12851 insertions(+), 4381 deletions(-) create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DatabaseVersion.java delete mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java create mode 100644 sdnr/wt/data-provider/dblib/pom.xml create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/DatabaseDataProvider.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBConfig.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBIndicesEntry.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/FaultEntityManager.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/QueryResult.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/SqlDBQueryByFilter.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/DeleteQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBFilterKeyValuePair.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBKeyValuePair.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RangeSqlDBFilter.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RegexSqlDBFilter.java create mode 100644 sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/SqlDBFilter.java create mode 100644 sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestCRUDMariaDB.java create mode 100644 sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestMariaDataProvider.java create mode 100644 sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestQuerySyntax.java create mode 100644 sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/util/MariaDBTestBase.java create mode 100644 sdnr/wt/data-provider/dblib/src/test/resources/inventory.json create mode 100644 sdnr/wt/data-provider/dblib/src/test/resources/pmdata15m.json create mode 100644 sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/HtDatabaseMediatorserver.java create mode 100644 sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/SdnrDbType.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/ElasticSearchDataProvider.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/HtUserdataManagerImpl.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/MediatorServerDataProvider.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessor.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorPm.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorStatus.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorWithId.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/FaultEntityManager.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseEventsService.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseMaintenanceService.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilter.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilterStatic.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryResult.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter2.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsConfig.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsDataObjectReaderWriter2.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessor.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorPm.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorStatus.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorWithId.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/FaultEntityManager.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseEventsService.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseMaintenanceService.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilter.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilterStatic.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryResult.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/ElasticSearchDataProvider.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/HtUserdataManagerImpl.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/ReadyHttpServlet.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderConfig.java delete mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/EsConfig.java create mode 100644 sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/MediatorServerDataProvider.java create mode 100644 sdnr/wt/data-provider/provider/src/main/resources/about/README.json create mode 100644 sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/util/MariaDBTestBase.java create mode 100644 sdnr/wt/data-provider/provider/src/test/resources/tlskeys/keys1.json create mode 100644 sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/MariaDBTableInfo.java create mode 100644 sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/ElasticsearchDataMigrationProvider.java create mode 100644 sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/MariaDbDataMigrationProvider.java create mode 100644 sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/MariaDBTestBase.java create mode 100644 sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBIstanbul.java create mode 100644 sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBMapper.java (limited to 'sdnr') diff --git a/sdnr/wt/common-yang/openroadm-pm-types/pom.xml b/sdnr/wt/common-yang/openroadm-pm-types/pom.xml index aa8ebd306..046703945 100755 --- a/sdnr/wt/common-yang/openroadm-pm-types/pom.xml +++ b/sdnr/wt/common-yang/openroadm-pm-types/pom.xml @@ -39,10 +39,10 @@ bundle ccsdk-features :: ${project.artifactId} - - - com.fasterxml.jackson.core - jackson-databind - - + + + com.fasterxml.jackson.core + jackson-databind + + diff --git a/sdnr/wt/common-yang/utils/pom.xml b/sdnr/wt/common-yang/utils/pom.xml index ef3c22b4c..2fa1021db 100644 --- a/sdnr/wt/common-yang/utils/pom.xml +++ b/sdnr/wt/common-yang/utils/pom.xml @@ -69,6 +69,11 @@ rfc6991-ietf-yang-types provided + + org.opendaylight.mdsal + mdsal-dom-api + provided + diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java index 3086167bf..c50e3210e 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java @@ -35,6 +35,8 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; +import org.opendaylight.mdsal.dom.api.DOMEvent; +import org.opendaylight.mdsal.dom.api.DOMNotification; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.EventInstantAware; @@ -264,6 +266,9 @@ public class YangToolsMapperHelper { public static boolean hasTime(Notification notification) { return notification instanceof EventInstantAware; } + public static boolean hasTime(DOMNotification notification) { + return notification instanceof DOMEvent; + } public static DateAndTime getTime(Notification notification, Instant defaultValue) { Instant time; if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware @@ -275,4 +280,16 @@ public class YangToolsMapperHelper { } return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); } + + public static DateAndTime getTime(DOMNotification notification, Instant defaultValue) { + Instant time; + if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware + time = ((DOMEvent) notification).getEventInstant(); + LOG.debug("Event time {}", time); + } else { + time = defaultValue; + LOG.debug("Defaulting to actual time of processing the notification - {}", time); + } + return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); + } } diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsBuilderAnnotationIntrospector.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsBuilderAnnotationIntrospector.java index 46c966076..7dde2ef7f 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsBuilderAnnotationIntrospector.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsBuilderAnnotationIntrospector.java @@ -99,15 +99,15 @@ public class YangToolsBuilderAnnotationIntrospector extends JacksonAnnotationInt Class p2 = setter2.getRawParameterType(0); AnnotatedMethod res = null; - if (this.isAssignable(p1, p2, Map.class, List.class)) { + if (isAssignable(p1, p2, Map.class, List.class)) { res = p1.isAssignableFrom(List.class) ? setter1 : setter2; //prefer List setter - } else if (this.isAssignable(p1, p2, Uint64.class, BigInteger.class)) { + } else if (isAssignable(p1, p2, Uint64.class, BigInteger.class)) { res = setter1; - } else if (this.isAssignable(p1, p2, Uint32.class, Long.class)) { + } else if (isAssignable(p1, p2, Uint32.class, Long.class)) { res = setter1; - } else if (this.isAssignable(p1, p2, Uint16.class, Integer.class)) { + } else if (isAssignable(p1, p2, Uint16.class, Integer.class)) { res = setter1; - } else if (this.isAssignable(p1, p2, Uint8.class, Short.class)) { + } else if (isAssignable(p1, p2, Uint8.class, Short.class)) { res = setter1; } if (res == null) { @@ -123,7 +123,7 @@ public class YangToolsBuilderAnnotationIntrospector extends JacksonAnnotationInt return res; } - private boolean isAssignable(Class p1, Class p2, Class c1, Class c2) { + public static boolean isAssignable(Class p1, Class p2, Class c1, Class c2) { return ((p1.isAssignableFrom(c1) && p2.isAssignableFrom(c2)) || (p2.isAssignableFrom(c1) && p1.isAssignableFrom(c2))); diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java index c12d17e74..58e75bde5 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.NoSuchElementException; +import java.util.Optional; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer; @@ -48,7 +49,25 @@ import org.slf4j.LoggerFactory; public class YangToolsDeserializerModifier extends BeanDeserializerModifier { private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class); - private static final String getEnumMethodName="valueOf"; + private static final String getEnumMethodName = "valueOf"; + private static final String getEnumMethodName2 = "forName"; + + @SuppressWarnings("unchecked") + public static Enum parseEnum(String value, Class clazz) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + try { + Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class); + Enum result = (Enum) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | NoSuchElementException | SecurityException e) { + Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class); + Optional> result = (Optional>) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result.orElseThrow(); + } + } @Override public JsonDeserializer> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, @@ -60,10 +79,7 @@ public class YangToolsDeserializerModifier extends BeanDeserializerModifier { Class clazz = type.getRawClass(); try { - Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class); - Enum result = (Enum) method.invoke(null, jp.getValueAsString()); - LOG.debug("Deserialize '{}' with class '{}' to '{}'", jp.getValueAsString(), clazz.getName(), result); - return result; + return parseEnum(jp.getValueAsString(), clazz); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | NoSuchElementException | SecurityException e) { LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(), diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java index 96bfa070b..54a5172a3 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java @@ -46,7 +46,7 @@ public class ConfigurationFileRepresentation implements IConfigChangedListener { private static final Logger LOG = LoggerFactory.getLogger(ConfigurationFileRepresentation.class); private static final long FILE_POLL_INTERVAL_MS = 1000; - private static final String SECTIONNAME_ROOT = ""; + public static final String SECTIONNAME_ROOT = ""; private static final String LR = "\n"; private static final String EMPTY = ""; // end of constants diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java index 1f67e5ef8..8b2dbb004 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java @@ -44,7 +44,7 @@ import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; import org.json.JSONException; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterHealthRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateAliasRequest; @@ -150,9 +150,9 @@ public class ExtRestClient { this.client = RestClient.builder(get(hosts)) .setHttpClientConfigCallback(new BasicAuthHttpClientConfigCallback(username, password, trustAll)) .build(); - EsVersion tmp = autoDetectVersion(); + DatabaseVersion tmp = autoDetectVersion(); LOG.info("working with sdnrdb version {}", tmp.toString()); - this.isES7 = tmp.isNewerOrEqualThan(new EsVersion(7, 0, 0)); + this.isES7 = tmp.isNewerOrEqualThan(new DatabaseVersion(7, 0, 0)); } /** @@ -160,7 +160,7 @@ public class ExtRestClient { * @throws IOException * @throws Exception */ - private EsVersion autoDetectVersion() throws IOException, Exception { + private DatabaseVersion autoDetectVersion() throws IOException, Exception { GetInfoResponse infoResponse = this.getInfo(); return infoResponse.getVersion(); diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java index 232b68c73..e4b8893c5 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java @@ -60,6 +60,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A private static final long TIMOUT_MS_DEFAULT = 30000; private static final long READ_MAX_SIZE = 9999; private final static long SLEEPTIMEMS = 5000; + private static final boolean FULLSIZEREQUEST_DEFAULT = false; private final Logger LOG = LoggerFactory.getLogger(HtDatabaseClient.class); @@ -90,7 +91,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A } static public HtDatabaseClient getClient(HostInfo[] hosts, String username, String password, boolean trustAll, - long timeoutms) throws HtDatabaseClientException { + boolean doFullsizeRequests, long timeoutms) throws HtDatabaseClientException { return getClient(hosts, REFRESH_AFTER_REWRITE_DEFAULT, username, password, trustAll, TIMOUT_MS_DEFAULT); } @@ -261,7 +262,8 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A total = response.getTotal(); } catch (IOException e) { - LOG.warn("Possible Database connection failure. If this error persists, please check Database connectivity"); + LOG.warn( + "Possible Database connection failure. If this error persists, please check Database connectivity"); LOG.warn("error do search {}: {}", queryBuilder, e); } return new SearchResult(response != null ? response.getHits() : new SearchHit[] {}, total); diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/AliasesEntry.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/AliasesEntry.java index d55e66d2e..118cf0282 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/AliasesEntry.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/AliasesEntry.java @@ -45,6 +45,10 @@ public class AliasesEntry { private final String alias; private final String index; + public AliasesEntry(String index, String alias) { + this.alias = alias; + this.index = index; + } public AliasesEntry(String line) throws ParseException { final Matcher matcher = pattern.matcher(line); if (!matcher.find() || matcher.groupCount() < 2) { diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DatabaseVersion.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DatabaseVersion.java new file mode 100644 index 000000000..e37b9d924 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DatabaseVersion.java @@ -0,0 +1,149 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.common.database.data; + +import java.text.ParseException; +import org.eclipse.jdt.annotation.NonNull; + + +/** + * @author Michael Dürre + * + */ +public class DatabaseVersion { + + private final String raw; + private final int major; + private final int minor; + private final int revision; + + public DatabaseVersion(@NonNull String version) throws ParseException { + String[] hlp = version.split("\\."); + if (hlp.length < 3) { + throw new ParseException("unable to parse version string: " + version, 0); + } + this.raw = version; + this.major = Integer.parseInt(hlp[0]); + this.minor = Integer.parseInt(hlp[1]); + this.revision = Integer.parseInt(hlp[2]); + } + + @Override + public String toString() { + return String.format("%d.%d.%d", this.major, this.minor, this.revision); + } + + /** + * @param major + * @param minor + * @param revision + */ + public DatabaseVersion(int major, int minor, int revision) { + this.raw = String.format("%d.%d.%d", major, minor, revision); + this.major = major; + this.minor = minor; + this.revision = revision; + } + + /** + * @return the revision + */ + public int getRevision() { + return revision; + } + + /** + * @return the minor + */ + public int getMinor() { + return minor; + } + + /** + * @return the major + */ + public int getMajor() { + return major; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof DatabaseVersion)) { + return false; + } + DatabaseVersion esobj = (DatabaseVersion) obj; + return this.major == esobj.major && this.minor == esobj.minor && this.revision == esobj.revision; + } + + @Override + public int hashCode() { + return this.raw.hashCode(); + } + + public boolean isNewerOrEqualThan(DatabaseVersion v) { + if (this.equals(v)) { + return true; + } + return this.isNewerThan(v); + } + public boolean isNewerThan(DatabaseVersion v) { + if (this.major > v.major) { + return true; + } else if (this.major < v.major) { + return false; + } + if (this.minor > v.minor) { + return true; + } else if (this.minor < v.minor) { + return false; + } + if (this.revision > v.revision) { + return true; + } + return false; + } + + public boolean isOlderOrEqualThan(DatabaseVersion v) { + if (this.equals(v)) { + return true; + } + return this.isOlderThan(v); + } + + public boolean isOlderThan(DatabaseVersion v) { + if (this.major < v.major) { + return true; + } else if (this.major > v.major) { + return false; + } + if (this.minor < v.minor) { + return true; + } else if (this.minor > v.minor) { + return false; + } + if (this.revision < v.revision) { + return true; + } + return false; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java deleted file mode 100644 index e1fd02de0..000000000 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.common.database.data; - -import java.text.ParseException; -import org.eclipse.jdt.annotation.NonNull; - - -/** - * @author Michael Dürre - * - */ -public class EsVersion { - - private final String raw; - private final int major; - private final int minor; - private final int revision; - - public EsVersion(@NonNull String version) throws ParseException { - String[] hlp = version.split("\\."); - if (hlp.length < 3) { - throw new ParseException("unable to parse version string: " + version, 0); - } - this.raw = version; - this.major = Integer.parseInt(hlp[0]); - this.minor = Integer.parseInt(hlp[1]); - this.revision = Integer.parseInt(hlp[2]); - } - - @Override - public String toString() { - return String.format("%d.%d.%d", this.major, this.minor, this.revision); - } - - /** - * @param major - * @param minor - * @param revision - */ - public EsVersion(int major, int minor, int revision) { - this.raw = String.format("%d.%d.%d", major, minor, revision); - this.major = major; - this.minor = minor; - this.revision = revision; - } - - /** - * @return the revision - */ - public int getRevision() { - return revision; - } - - /** - * @return the minor - */ - public int getMinor() { - return minor; - } - - /** - * @return the major - */ - public int getMajor() { - return major; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof EsVersion)) { - return false; - } - EsVersion esobj = (EsVersion) obj; - return this.major == esobj.major && this.minor == esobj.minor && this.revision == esobj.revision; - } - - @Override - public int hashCode() { - return this.raw.hashCode(); - } - - public boolean isNewerOrEqualThan(EsVersion v) { - if (this.equals(v)) { - return true; - } - return this.isNewerThan(v); - } - public boolean isNewerThan(EsVersion v) { - if (this.major > v.major) { - return true; - } else if (this.major < v.major) { - return false; - } - if (this.minor > v.minor) { - return true; - } else if (this.minor < v.minor) { - return false; - } - if (this.revision > v.revision) { - return true; - } - return false; - } - - public boolean isOlderOrEqualThan(EsVersion v) { - if (this.equals(v)) { - return true; - } - return this.isOlderThan(v); - } - - public boolean isOlderThan(EsVersion v) { - if (this.major < v.major) { - return true; - } else if (this.major > v.major) { - return false; - } - if (this.minor < v.minor) { - return true; - } else if (this.minor > v.minor) { - return false; - } - if (this.revision < v.revision) { - return true; - } - return false; - } - -} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/IndicesEntry.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/IndicesEntry.java index b31ff4389..cc7fafb76 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/IndicesEntry.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/IndicesEntry.java @@ -29,7 +29,7 @@ import java.util.regex.Pattern; * @author Michael Dürre * * Entry of list indices http request (/_cat/indices) - * + * * yellow open inventoryequipment-v1 5nNPRbJ3T9arMxqxBbJKyQ 5 1 0 0 1.2kb 1.2kb */ public class IndicesEntry { @@ -93,6 +93,20 @@ public class IndicesEntry { return size2; } + protected IndicesEntry(String name, String status, String status2, String hash, int shards, int replicas, int c1, + int c2, String size1, String size2) { + this.name = name; + this.status = status; + this.status2 = status2; + this.hash = hash; + this.shards = shards; + this.replicas = replicas; + this.c1 = c1; + this.c2 = c2; + this.size1 = size1; + this.size2 = size2; + } + public IndicesEntry(String line) throws ParseException { Matcher matcher = pattern.matcher(line.trim()); if (!matcher.find() || matcher.groupCount() < 10) { diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java index 519502efd..265c6f529 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java @@ -100,4 +100,17 @@ public class QueryBuilder { public QueryBuilder aggregations(String key) { return this.aggregations(key, null); } + + public void doFullsizeRequest() { + this.setFullsizeRequest(true); + } + + public QueryBuilder setFullsizeRequest(boolean doFullsizeRequest) { + if (doFullsizeRequest) { + this.outerQuery.put("track_total_hits", doFullsizeRequest); + } else { + this.outerQuery.remove("track_total_hits"); + } + return this; + } } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetInfoResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetInfoResponse.java index 946a8fb4a..79172e01c 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetInfoResponse.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetInfoResponse.java @@ -23,7 +23,7 @@ package org.onap.ccsdk.features.sdnr.wt.common.database.responses; import org.elasticsearch.client.Response; import org.json.JSONObject; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; public class GetInfoResponse extends BaseResponse { @@ -37,7 +37,7 @@ public class GetInfoResponse extends BaseResponse { private final String clusterName; private final String name; - private final EsVersion version; + private final DatabaseVersion version; public GetInfoResponse(Response response) throws Exception { super(response); @@ -47,7 +47,7 @@ public class GetInfoResponse extends BaseResponse { } this.name = o.getString("name"); this.clusterName = o.getString("cluster_name"); - this.version = new EsVersion(o.getJSONObject("version").getString("number")); + this.version = new DatabaseVersion(o.getJSONObject("version").getString("number")); } public String getClusterName() { @@ -58,7 +58,7 @@ public class GetInfoResponse extends BaseResponse { return name; } - public EsVersion getVersion() { + public DatabaseVersion getVersion() { return version; } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java index c19cea08d..2e0701257 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java @@ -41,8 +41,10 @@ public class PomFile { public PomFile(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - // Remediate XML external entity vulnerabilty - prohibit the use of all protocols by external entities: - documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + // documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + // documentBuilderFactory.setFeature(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); + // documentBuilderFactory.setFeature(XMLInputFactory.SUPPORT_DTD, false); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); this.xmlDoc = documentBuilder.parse(is); } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java index ffb31c676..de878a5d5 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java @@ -113,7 +113,12 @@ public class BaseHTTPClient { @Nonnull public BaseHTTPResponse sendRequest(String uri, String method, String body, Map headers) throws IOException { - return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers); + return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers, 0); + } + @Nonnull + public BaseHTTPResponse sendRequest(String uri, String method, String body, Map headers, int timeoutMs) + throws IOException { + return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers, timeoutMs); } public BaseHTTPResponse sendRequest(HttpServletRequest req) throws IOException { final String method = req.getMethod(); @@ -125,7 +130,7 @@ public class BaseHTTPClient { is.close(); } - return this.sendRequest(req.getRequestURI(), method, buffer,mapHeaders(req)); + return this.sendRequest(req.getRequestURI(), method, buffer,mapHeaders(req), 0); } private Map mapHeaders(HttpServletRequest req) { @@ -138,8 +143,10 @@ public class BaseHTTPClient { } return headers; } - - protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, byte[] body, Map headers) + protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, byte[] body, Map headers) throws IOException { + return this.sendRequest(uri, method, body, headers, 0); + } + protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, byte[] body, Map headers, int timeoutMs) throws IOException { if (uri == null) { uri = ""; @@ -156,7 +163,7 @@ public class BaseHTTPClient { LOG.trace("body:" + (body == null ? "null" : new String(body, CHARSET))); URL url = new URL(surl); URLConnection http = url.openConnection(); - http.setConnectTimeout(this.timeout); + http.setConnectTimeout(timeoutMs>0?timeoutMs:this.timeout); if (surl.toString().startsWith("https")) { if (sc != null) { ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory()); diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestEsData.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestEsData.java index 883d48ac1..c2471f58e 100644 --- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestEsData.java +++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestEsData.java @@ -32,7 +32,7 @@ import java.text.ParseException; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry; import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; @@ -45,9 +45,9 @@ public class TestEsData { @Test public void testVersion() { - EsVersion version = null; + DatabaseVersion version = null; try { - version = new EsVersion("2.3.4"); + version = new DatabaseVersion("2.3.4"); } catch (ParseException e) { fail(e.getMessage()); } @@ -56,8 +56,8 @@ public class TestEsData { assertEquals(3, version.getMinor()); assertEquals(4, version.getRevision()); - EsVersion versionNewer = new EsVersion(5, 0, 0); - EsVersion versionOlder = new EsVersion(2, 2, 0); + DatabaseVersion versionNewer = new DatabaseVersion(5, 0, 0); + DatabaseVersion versionOlder = new DatabaseVersion(2, 2, 0); assertTrue(version.isOlderOrEqualThan(versionNewer)); assertTrue(version.isNewerOrEqualThan(versionOlder)); diff --git a/sdnr/wt/data-provider/dblib/pom.xml b/sdnr/wt/data-provider/dblib/pom.xml new file mode 100644 index 000000000..c4c3d8695 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/pom.xml @@ -0,0 +1,133 @@ + + + + + 4.0.0 + + + org.onap.ccsdk.parent + binding-parent + 2.2.0-SNAPSHOT + + + + org.onap.ccsdk.features.sdnr.wt + sdnr-wt-data-provider-dblib + 1.2.0-SNAPSHOT + bundle + + ccsdk-features :: ${project.artifactId} + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0 + + + + + + ${project.groupId} + sdnr-wt-common + ${project.version} + + + ${project.groupId} + sdnr-wt-yang-utils + ${project.version} + + + ${project.groupId} + sdnr-wt-data-provider-model + ${project.version} + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-databind + + + org.opendaylight.netconf + sal-netconf-connector + provided + + + org.opendaylight.mdsal.binding.model.ietf + rfc6991-ietf-yang-types + provided + + + org.opendaylight.mdsal + yang-binding + provided + + + org.opendaylight.mdsal + mdsal-singleton-common-api + provided + + + org.json + json + provided + + + org.osgi + org.osgi.core + provided + + + + org.apache.tomcat + tomcat-jdbc + + + org.mariadb.jdbc + mariadb-java-client + + + com.google.guava + guava + + + org.apache.commons + commons-lang3 + + + ch.vorburger.mariaDB4j + mariaDB4j + test + + + org.mockito + mockito-core + test + + + + + diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/DatabaseDataProvider.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/DatabaseDataProvider.java new file mode 100644 index 000000000..9d4beb71b --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/DatabaseDataProvider.java @@ -0,0 +1,132 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMediatorserver; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInput; +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.CreateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateNetworkElementConnectionOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionOutputBuilder; +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.NetworkElementConnectionEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadConnectionlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultcurrentListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadGuiCutThroughEntryOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadInventoryListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMediatorServerListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadStatusOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionOutputBuilder; + + +public interface DatabaseDataProvider { + + HtDatabaseClient getRawClient(); + + ReadFaultcurrentListOutputBuilder readFaultCurrentList(EntityInput input); + + ReadFaultlogListOutputBuilder readFaultLogList(EntityInput input); + + ReadMaintenanceListOutputBuilder readMaintenanceList(EntityInput input); + + ReadMediatorServerListOutputBuilder readMediatorServerList(EntityInput input); + + ReadNetworkElementConnectionListOutputBuilder readNetworkElementConnectionList(EntityInput input); + + ReadInventoryListOutputBuilder readInventoryList(EntityInput input); + + ReadConnectionlogListOutputBuilder readConnectionlogList(EntityInput input); + + ReadEventlogListOutputBuilder readEventlogList(EntityInput input) throws IOException; + + ReadPmdata15mListOutputBuilder readPmdata15mList(EntityInput input); + + ReadPmdata24hListOutputBuilder readPmdata24hList(EntityInput input); + + ReadPmdata15mLtpListOutputBuilder readPmdata15mLtpList(EntityInput input) throws IOException; + + ReadPmdata15mDeviceListOutputBuilder readPmdata15mDeviceList(EntityInput input) throws IOException; + + ReadPmdata24hLtpListOutputBuilder readPmdata24hLtpList(EntityInput input) throws IOException; + + ReadPmdata24hDeviceListOutputBuilder readPmdata24hDeviceList(EntityInput input) throws IOException; + + ReadStatusOutputBuilder readStatus() throws IOException; + + boolean waitForYellowDatabaseStatus(long timeout, TimeUnit unit); + + CreateNetworkElementConnectionOutputBuilder createNetworkElementConnection(NetworkElementConnectionEntity input) + throws IOException; + + UpdateNetworkElementConnectionOutputBuilder updateNetworkElementConnection( + UpdateNetworkElementConnectionInput input) throws IOException; + + DeleteNetworkElementConnectionOutputBuilder deleteNetworkElementConnection( + DeleteNetworkElementConnectionInput input) throws IOException; + + DeleteMediatorServerOutputBuilder deleteMediatorServer(DeleteMediatorServerInput input) throws IOException; + + DeleteMaintenanceOutputBuilder deleteMaintenance(DeleteMaintenanceInput input) throws IOException; + + UpdateMaintenanceOutputBuilder updateMaintenance(UpdateMaintenanceInput input) throws IOException; + + UpdateMediatorServerOutputBuilder updateMediatorServer(UpdateMediatorServerInput input) throws IOException; + + CreateMaintenanceOutputBuilder createMaintenance(CreateMaintenanceInput input) throws IOException; + + CreateMediatorServerOutputBuilder createMediatorServer(CreateMediatorServerInput input) throws IOException; + + ReadGuiCutThroughEntryOutputBuilder readGuiCutThroughEntry(EntityInput input); + + DataProvider getDataProvider(); + + HtDatabaseMaintenance getHtDatabaseMaintenance(); + + HtDatabaseMediatorserver getHtDatabaseMediatorServer(); + +} 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 new file mode 100644 index 000000000..f16eadd01 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBClient.java @@ -0,0 +1,264 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.ParseException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.onap.ccsdk.features.sdnr.wt.common.database.Portstatus; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBIndicesEntry; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper.UnableToMapClassException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBClient { + + private static final Logger LOG = LoggerFactory.getLogger(SqlDBClient.class); + + // matches: + // 1=>type, e.g. mariadb, mysql, ... + // 2=>host + // 3=>port + // 4=>dbname + private static final String DBURL_REGEX = "^jdbc:([^:]+):\\/\\/([^:]+):([0-9]+)\\/(.+)$"; + private static final Pattern DBURL_PATTERN = Pattern.compile(DBURL_REGEX); + 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 DBNAME_DEFAULT = "sdnrdb"; + private final String dbConnectionString; + private final String dbName; + private final String dbHost; + private final int dbPort; + + /** + * + * @param dbUrl e.g. jdbc:mysql://sdnrdb:3306/sdnrdb + * @param username + * @param password + */ + public SqlDBClient(String dbUrl, String username, String password) throws IllegalArgumentException { + this.dbConnectionString = String.format("%s?user=%s&password=%s", dbUrl, username, password); + final Matcher matcher = DBURL_PATTERN.matcher(dbUrl); + if(!matcher.find()) { + throw new IllegalArgumentException("unable to parse databaseUrl "+dbUrl); + } + this.dbHost = matcher.group(2); + this.dbPort = Integer.parseInt(matcher.group(3)); + this.dbName = matcher.group(4); + } + + public AliasesEntryList readViews() { + return this.readViews(DBNAME_DEFAULT); + } + + public AliasesEntryList readViews(String dbName) { + AliasesEntryList list = new AliasesEntryList(); + final String query = "SELECT v.`TABLE_NAME` AS vn, t.`TABLE_NAME` AS tn\n" + + "FROM `information_schema`.`TABLES` AS v\n" + + "LEFT JOIN `information_schema`.`TABLES` AS t ON t.`TABLE_NAME` LIKE CONCAT(v.`TABLE_NAME`,'%')" + + " AND t.`TABLE_TYPE`='BASE TABLE'\n" + "WHERE v.`TABLE_SCHEMA`='" + dbName + + "' AND v.`TABLE_TYPE`='VIEW'"; + ResultSet data = this.read(query); + try { + while (data.next()) { + list.add(new AliasesEntry(data.getString(2), data.getString(1))); + } + } catch (SQLException e) { + LOG.warn("problem reading views: ", e); + } + return list; + } + + public IndicesEntryList readTables() { + final String query = "SHOW FULL TABLES WHERE `Table_type` = 'BASE TABLE'"; + IndicesEntryList list = new IndicesEntryList(); + ResultSet data = this.read(query); + try { + while (data.next()) { + list.add(new SqlDBIndicesEntry(data.getString(1))); + } + } catch (SQLException e) { + LOG.warn("problem reading tables: ", e); + } + return list; + } + + public void waitForYellowStatus(long timeoutms) { + Portstatus.waitSecondsTillAvailable(timeoutms/1000, this.dbHost, this.dbPort); + } + + public DatabaseVersion readActualVersion() throws SQLException, ParseException { + ResultSet data; + try { + data = this.read(SELECT_VERSION_QUERY); + if (data.next()) { + final String s = data.getString(1); + final Matcher matcher = DBVERSION_PATTERN.matcher(s); + data.afterLast(); + data.close(); + if (matcher.find()) { + return new DatabaseVersion(matcher.group(1)); + } else { + throw new ParseException(String.format("unable to extract version out of string '%s'", s), 0); + } + } + } catch (SQLException e) { + LOG.warn("problem reading tables: ", e); + } + throw new SQLException("unable to read version from database"); + } + + public boolean createTable(Entity entity, Class clazz, String suffix) throws UnableToMapClassException { + String createStatement = SqlDBMapper.createTable(clazz, entity, suffix); + return this.createTable(createStatement); + } + + public boolean createTable(String tableName, String tableMappings) { + final String createStatement = String.format("CREATE TABLE IF NOT EXISTS `%s` (%s)", tableName, tableMappings); + return this.createTable(createStatement); + } + + public boolean createTable(String query) { + try { + Connection connection = this.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + stmt.execute(); + connection.close(); + return true; + } catch (SQLException e) { + LOG.warn("problem creating table:", e); + } + return false; + } + + public boolean createView(String tableName, String viewName) throws SQLException { + try { + this.write(String.format("CREATE VIEW IF NOT EXISTS `%s` AS SELECT * FROM `%s`", viewName, tableName)); + return true; + } catch (SQLException e) { + LOG.warn("problem deleting table:", e); + } + return false; + } + + public boolean deleteView(String viewName) throws SQLException { + try { + this.write(String.format("DROP VIEW IF EXISTS `%s`", viewName)); + return true; + } catch (SQLException e) { + LOG.warn("problem deleting view:", e); + } + return false; + } + + public boolean update(String query) throws SQLException { + boolean result = false; + Connection connection = null; + connection = DriverManager.getConnection(this.dbConnectionString); + Statement stmt = connection.createStatement(); + result = stmt.execute(query); + return stmt.getUpdateCount() > 0 ? stmt.getUpdateCount() > 0 : result; + } + + public boolean write(String query) throws SQLException { + Connection connection = this.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + boolean result = stmt.execute(); + connection.close(); + return stmt.getUpdateCount() > 0 ? stmt.getUpdateCount() > 0 : result; + } + + public String writeAndReturnId(String query) throws SQLException { + Connection connection = this.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + stmt.execute(); + ResultSet generatedKeys = stmt.getGeneratedKeys(); + connection.close(); + if (generatedKeys.next()) { + return String.valueOf(generatedKeys.getLong(1)); + } + return null; + } + + public boolean deleteTable(String tableName) throws SQLException { + try { + this.write(String.format("DROP TABLE IF EXISTS `%s`", tableName)); + return true; + } catch (SQLException e) { + LOG.warn("problem deleting table:", e); + } + return false; + } + + public String getDatabaseName() { + return this.dbName; + } + + public ResultSet read(String query) { + ResultSet data = null; + Connection connection = null; + Statement stmt = null; + try { + connection = DriverManager.getConnection(this.dbConnectionString); + stmt = connection.createStatement(); + data = stmt.executeQuery(query); + } catch (SQLException e) { + LOG.warn("problem reading tables: ", e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + LOG.warn("problem closing connection: ", e); + } + } + + return data; + } + + public Connection getConnection() throws SQLException { + return DriverManager.getConnection(this.dbConnectionString); + } + + public boolean delete(String query) throws SQLException { + this.write(query); + return true; + } + + + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBConfig.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBConfig.java new file mode 100644 index 000000000..8c7e7b1cb --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/SqlDBConfig.java @@ -0,0 +1,112 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb; + +import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity.DatabaseIdGenerator; + +public class SqlDBConfig implements Configuration { + + private static final String SECTION_MARKER_MARIADB = "mariadb"; + + private static final String PROPERTY_KEY_DBURL = "url"; + private static final String PROPERTY_KEY_USERNAME = "username"; + private static final String PROPERTY_KEY_PASSWORD = "password"; + private static final String PROPERTY_KEY_CONTROLLERID = "controllerId"; + private static final String PROPERTY_KEY_DBSUFFIX = "suffix"; + + + private static final String DEFAULT_VALUE_DBURL = "${SDNRDBURL}"; + private static final String DEFAULT_VALUE_DBUSERNAME = "${SDNRDBUSERNAME}"; + private static final String DEFAULT_VALUE_DBPASSWORD = "${SDNRDBPASSWORD}"; + private static final String DEFAULT_VALUE_CONTROLLERID = DatabaseIdGenerator.getControllerId(); + private static final String DEFAULT_VALUE_DBSUFFIX = "-v6"; + + private final ConfigurationFileRepresentation configuration; + + public SqlDBConfig(ConfigurationFileRepresentation configuration) { + + this.configuration = configuration; + this.configuration.addSection(SECTION_MARKER_MARIADB); + defaults(); + } + + + /* + * Getter + */ + + public String getUrl() { + return configuration.getProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBURL); + } + + public void setUrl(String url) { + configuration.setProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBURL, url); + + } + + public String getUsername() { + return this.configuration.getProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_USERNAME); + } + + public String getPassword() { + return this.configuration.getProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_PASSWORD); + } + + public String getControllerId() { + String v = this.configuration.getProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_CONTROLLERID); + return (v == null || v.equals("null") || v.isEmpty()) ? null : v; + } + + public String getDbSuffix() { + return this.configuration.getProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBSUFFIX); + } + + @Override + public String getSectionName() { + return SECTION_MARKER_MARIADB; + } + + @Override + public synchronized void defaults() { + // Add default if not available + configuration.setPropertyIfNotAvailable(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBURL, DEFAULT_VALUE_DBURL); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_MARIADB, PROPERTY_KEY_USERNAME, + DEFAULT_VALUE_DBUSERNAME); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_MARIADB, PROPERTY_KEY_PASSWORD, + DEFAULT_VALUE_DBPASSWORD); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_MARIADB, PROPERTY_KEY_CONTROLLERID, + DEFAULT_VALUE_CONTROLLERID); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBSUFFIX, DEFAULT_VALUE_DBSUFFIX); + + } + + + public void setControllerId(String id) { + configuration.setProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_CONTROLLERID, id); + } + + public void setDbSuffix(String suffix) { + configuration.setProperty(SECTION_MARKER_MARIADB, PROPERTY_KEY_DBSUFFIX, suffix); + } +} 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 new file mode 100644 index 000000000..9dd1adb50 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerImpl.java @@ -0,0 +1,150 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +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.JSONException; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HtUserdataManagerImpl implements HtUserdataManager { + + private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerImpl.class); + + private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json"; + private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults(); + + private static JSONObject loadDefaults() { + File f = new File(USERDATA_DEFAULTS_FILENAME); + String content; + JSONObject o = null; + if (f.exists()) { + try { + content = Files.readString(f.toPath()); + o = new JSONObject(content); + } catch (IOException e) { + LOG.warn("problem loading defaults: ", e); + } catch (JSONException e) { + LOG.warn("problem parsing defaults: ", e); + } + } + return o; + } + + + private final HtDatabaseClient dbClient; + + public HtUserdataManagerImpl(HtDatabaseClient rawClient) { + this.dbClient = rawClient; + } + + @Override + public String getUserdata(String username) { + SearchResult result = this.dbClient.doReadByQueryJsonData(Entity.Userdata.getName(), + QueryBuilders.matchQuery("_id", username)); + String json = result.getHits().size() > 0 ? result.getHits().get(0).getSourceAsString() : "{}"; + if (USERDATA_DEFAULTS_CONTENT != null) { + JSONObject merge = mergeData(new JSONObject(json), USERDATA_DEFAULTS_CONTENT); + json = merge.toString(); + } + return json; + } + + @Override + public String getUserdata(String username, String key) { + JSONObject o = new JSONObject(this.getUserdata(username)); + return o.has(key) ? o.get(key).toString() : "{}"; + } + + @Override + public boolean setUserdata(String username, String data) { + JSONObject o = new JSONObject(this.getUserdata(username)); + JSONObject merge = mergeData(o, new JSONObject(data)); + return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, merge.toString()) != null; + } + + @Override + public boolean setUserdata(String username, String key, String data) { + JSONObject o = new JSONObject(this.getUserdata(username)); + o = mergeData(o, key, new JSONObject(data)); + return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, o.toString()) != null; + } + + @Override + public boolean removeUserdata(String username) { + return this.dbClient.doRemove(Entity.Userdata.getName(), username); + } + + @Override + public boolean removeUserdata(String username, String key) { + JSONObject o = new JSONObject(this.getUserdata(username)); + if (o.has(key)) { + o.remove(key); + return this.setUserdata(username, o.toString()); + } + return true; + } + + private static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) { + if (!o.has(key)) { + o.put(key, subObject); + } else { + JSONObject tmp = new JSONObject(); + tmp.put(key, subObject); + o = mergeData(tmp, o); + } + return o; + } + + private static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException { + String[] keys = JSONObject.getNames(source); + if (keys == null) { + return target; + } + for (String key : keys) { + Object value = source.get(key); + if (!target.has(key)) { + // new value for "key": + target.put(key, value); + } else { + // existing value for "key" - recursively deep merge: + if (value instanceof JSONObject) { + JSONObject valueJson = (JSONObject) value; + mergeData(valueJson, target.getJSONObject(key)); + } else { + target.put(key, value); + } + } + } + return target; + } + +} 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 new file mode 100644 index 000000000..94c7dcfc6 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBDataProvider.java @@ -0,0 +1,509 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity.HtDatabaseEventsService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity.HtDatabaseMaintenanceService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBStatusReader; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.DeleteQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMediatorserver; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInput; +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.CreateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateNetworkElementConnectionOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionOutputBuilder; +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.EntityInput; +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.MediatorServerEntity; +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.ReadConnectionlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultcurrentListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadGuiCutThroughEntryOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadInventoryListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMediatorServerListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadStatusOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionOutputBuilder; +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.read.mediator.server.list.output.Data; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SqlDBDataProvider extends HtDatabaseEventsService implements DatabaseDataProvider { + + private static final Logger LOG = LoggerFactory.getLogger(SqlDBDataProvider.class); + + private static final String EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE = "unable to write data to database"; + private static final String EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE = "unable to update data in database"; + private static final String EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE = "unable to remove data from database"; + + private final HtDatabaseMediatorserver dbMediatorServerService; + private final HtDatabaseMaintenance dbMaintenanceService; + private final SqlDBReaderWriter mediatorserverRW; + private final SqlDBReaderWriter maintenanceRW; + private final SqlDBStatusReader readStatus; + + public SqlDBReaderWriter getMaintenanceReaderWriter() { + return this.maintenanceRW; + } + + public SqlDBReaderWriter getMediatorServerReaderWriter() { + return this.mediatorserverRW; + } + + public SqlDBDataProvider(SqlDBConfig config) { + this(config, true); + } + public SqlDBDataProvider(SqlDBConfig config, boolean initControllerId) { + super(config); + + 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.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.readStatus = new SqlDBStatusReader(this.dbClient, this.controllerId); + + this.dbMediatorServerService = new HtDatabaseMediatorserver() { + + @Override + public List getAll() { + return SqlDBDataProvider.this.mediatorserverRW.readAll(MediatorServerEntity.class); + } + }; + this.dbMaintenanceService = new HtDatabaseMaintenanceService(this); + if(initControllerId) { + try { + this.setControllerId(); + } catch (SQLException e) { + LOG.warn("problem setting controllerId: ", e); + } + } + + } + + /*------------------------- + * Provide access to model API + */ + + @Override + public ReadFaultcurrentListOutputBuilder readFaultCurrentList(EntityInput input) { + + ReadFaultcurrentListOutputBuilder outputBuilder = new ReadFaultcurrentListOutputBuilder(); + + QueryResult result = + this.eventRWFaultCurrent.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadFaultlogListOutputBuilder readFaultLogList(EntityInput input) { + ReadFaultlogListOutputBuilder outputBuilder = new ReadFaultlogListOutputBuilder(); + QueryResult result = + this.eventRWFaultLog.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadMaintenanceListOutputBuilder readMaintenanceList(EntityInput input) { + ReadMaintenanceListOutputBuilder outputBuilder = new ReadMaintenanceListOutputBuilder(); + QueryResult result = + this.maintenanceRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadMediatorServerListOutputBuilder readMediatorServerList(EntityInput input) { + + ReadMediatorServerListOutputBuilder outputBuilder = new ReadMediatorServerListOutputBuilder(); + QueryResult result = + this.mediatorserverRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadNetworkElementConnectionListOutputBuilder readNetworkElementConnectionList(EntityInput input) { + ReadNetworkElementConnectionListOutputBuilder outputBuilder = + new ReadNetworkElementConnectionListOutputBuilder(); + QueryResult result = + this.networkelementConnectionRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadInventoryListOutputBuilder readInventoryList(EntityInput input) { + ReadInventoryListOutputBuilder outputBuilder = new ReadInventoryListOutputBuilder(); + QueryResult result = + this.equipmentRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadConnectionlogListOutputBuilder readConnectionlogList(EntityInput input) { + ReadConnectionlogListOutputBuilder outputBuilder = new ReadConnectionlogListOutputBuilder(); + QueryResult result = + this.connectionlogRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadEventlogListOutputBuilder readEventlogList(EntityInput input) throws IOException { + ReadEventlogListOutputBuilder outputBuilder = new ReadEventlogListOutputBuilder(); + QueryResult result = + this.eventlogRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata15mListOutputBuilder readPmdata15mList(EntityInput input) { + ReadPmdata15mListOutputBuilder outputBuilder = new ReadPmdata15mListOutputBuilder(); + QueryResult result = + this.pm15mRW.getData(input); + LOG.debug("Read data: readPmdata15mList: {}", result); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata24hListOutputBuilder readPmdata24hList(EntityInput input) { + ReadPmdata24hListOutputBuilder outputBuilder = new ReadPmdata24hListOutputBuilder(); + QueryResult result = + this.pm24hRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata15mLtpListOutputBuilder readPmdata15mLtpList(EntityInput input) throws IOException { + ReadPmdata15mLtpListOutputBuilder outputBuilder = new ReadPmdata15mLtpListOutputBuilder(); + QueryResult result = this.pm15mRW.getDataLtpList(input); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.ltp.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult()); + return outputBuilder; + } + + @Override + public ReadPmdata15mDeviceListOutputBuilder readPmdata15mDeviceList(EntityInput input) throws IOException { + ReadPmdata15mDeviceListOutputBuilder outputBuilder = new ReadPmdata15mDeviceListOutputBuilder(); + QueryResult result = this.pm15mRW.getDataDeviceList(input); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.device.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult()); + return outputBuilder; + } + + @Override + public ReadPmdata24hLtpListOutputBuilder readPmdata24hLtpList(EntityInput input) throws IOException { + + QueryResult result = this.pm24hRW.getDataLtpList(input); + + ReadPmdata24hLtpListOutputBuilder outputBuilder = new ReadPmdata24hLtpListOutputBuilder(); + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder(); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult()); + return outputBuilder; + } + + @Override + public ReadPmdata24hDeviceListOutputBuilder readPmdata24hDeviceList(EntityInput input) throws IOException { + + QueryResult result = pm24hRW.getDataDeviceList(input); + + ReadPmdata24hDeviceListOutputBuilder outputBuilder = new ReadPmdata24hDeviceListOutputBuilder(); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.device.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult()); + return outputBuilder; + } + + @Override + public ReadStatusOutputBuilder readStatus() throws IOException { + QueryResult result = + readStatus.getDataStatus(); + + ReadStatusOutputBuilder outputBuilder = new ReadStatusOutputBuilder(); + outputBuilder.setData(result.getResult()); + return outputBuilder; + } + + @Override + public CreateNetworkElementConnectionOutputBuilder createNetworkElementConnection( + NetworkElementConnectionEntity input) throws IOException { + String id = this.networkelementConnectionRW.write(input, input.getNodeId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateNetworkElementConnectionOutputBuilder builder = new CreateNetworkElementConnectionOutputBuilder(); + builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) + .setUsername(input.getUsername()).setPassword(input.getPassword()).setIsRequired(input.isIsRequired()) + .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); + return builder; + } + + @Override + public UpdateNetworkElementConnectionOutputBuilder updateNetworkElementConnection( + UpdateNetworkElementConnectionInput input) throws IOException { + String id = this.networkelementConnectionRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateNetworkElementConnectionOutputBuilder builder = new UpdateNetworkElementConnectionOutputBuilder(); + builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) + .setUsername(input.getUsername()).setPassword(input.getPassword()) + .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); + return builder; + } + + @Override + public DeleteNetworkElementConnectionOutputBuilder deleteNetworkElementConnection( + DeleteNetworkElementConnectionInput input) throws IOException { + boolean removed = this.networkelementConnectionRW.remove(input.getId()) > 0; + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteNetworkElementConnectionOutputBuilder(); + } + + @Override + public DeleteMediatorServerOutputBuilder deleteMediatorServer(DeleteMediatorServerInput input) throws IOException { + boolean removed = this.mediatorserverRW.remove(input.getId()) > 0; + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteMediatorServerOutputBuilder(); + } + + @Override + public DeleteMaintenanceOutputBuilder deleteMaintenance(DeleteMaintenanceInput input) throws IOException { + boolean removed = this.maintenanceRW.remove(input.getId()) > 0; + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteMaintenanceOutputBuilder(); + } + + @Override + public UpdateMaintenanceOutputBuilder updateMaintenance(UpdateMaintenanceInput input) throws IOException { + if (input.getId() == null) { + throw new IOException("please give the id for updating entry"); + } + String id = this.maintenanceRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateMaintenanceOutputBuilder builder = new UpdateMaintenanceOutputBuilder(input).setId(id); + return builder; + } + + @Override + public UpdateMediatorServerOutputBuilder updateMediatorServer(UpdateMediatorServerInput input) throws IOException { + if (input.getId() == null) { + throw new IOException("please give the id for updating entry"); + } + String id = this.mediatorserverRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateMediatorServerOutputBuilder builder = new UpdateMediatorServerOutputBuilder(); + builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); + return builder; + } + + @Override + public CreateMaintenanceOutputBuilder createMaintenance(CreateMaintenanceInput input) throws IOException { + String id = this.maintenanceRW.write(input, input.getNodeId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateMaintenanceOutputBuilder builder = new CreateMaintenanceOutputBuilder(input).setId(id); + return builder; + } + + @Override + public CreateMediatorServerOutputBuilder createMediatorServer(CreateMediatorServerInput input) throws IOException { + String id = this.mediatorserverRW.write(input, null); + + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateMediatorServerOutputBuilder builder = new CreateMediatorServerOutputBuilder(); + builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); + return builder; + } + + @Override + public ReadGuiCutThroughEntryOutputBuilder readGuiCutThroughEntry(EntityInput input) { + ReadGuiCutThroughEntryOutputBuilder outputBuilder = new ReadGuiCutThroughEntryOutputBuilder(); + QueryResult result = + this.guicutthroughRW.getData(input); + outputBuilder.setData(result.getResult()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.PaginationBuilder() + .build()); + return outputBuilder; + } + + + + @Override + public boolean waitForYellowDatabaseStatus(long timeout, TimeUnit unit) { + return true; + } + + @Override + public DataProvider getDataProvider() { + return this; + } + + @Override + public HtDatabaseMaintenance getHtDatabaseMaintenance() { + return this.dbMaintenanceService; + } + + @Override + public HtDatabaseMediatorserver getHtDatabaseMediatorServer() { + return this.dbMediatorServerService; + } + + public boolean delete(Entity e, List filters) throws SQLException { + DeleteQuery query = new DeleteQuery(e, filters); + return this.dbClient.write(query.toSql()); + + } + + public SqlDBClient getDBService() { + return this.dbClient; + } + + public boolean setControllerId() throws SQLException { + if (this.controllerId == null) { + return true; + } + LOG.info("set controllerId {}", this.controllerId); + String query = String.format("SELECT * FROM `%s` WHERE `id`='%s';", this.controllerTableName, + this.controllerId); + LOG.info(query); + ResultSet data = this.dbClient.read(query); + + if (!data.next()) { + query = String.format("INSERT INTO `%s` (`id`,`desc`) VALUES ('%s','%s')", + this.controllerTableName, this.controllerId, ""); + LOG.info(query); + return this.dbClient.write(query); + } + else { + LOG.info("controllerId already set"); + } + return true; + } + + public void waitForDatabaseReady(int i, TimeUnit unit) { + this.dbClient.waitForYellowStatus(unit.convert(i, TimeUnit.MILLISECONDS)); + } + + public String getControllerId() { + return this.controllerId; + } + + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBIndicesEntry.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBIndicesEntry.java new file mode 100644 index 000000000..fdea87c8b --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/SqlDBIndicesEntry.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data; + +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry; + +public class SqlDBIndicesEntry extends IndicesEntry{ + + public SqlDBIndicesEntry(String name) { + super(name,"","","",0,0,0,0,"",""); + } + + +} 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 new file mode 100644 index 000000000..5a9e58cef --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/DatabaseIdGenerator.java @@ -0,0 +1,104 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity; + +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +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.NetworkElementConnectionEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata15mEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata24hEntity; + +/** + * Database id generator for those classes which need manual id generation by object + * implementation for this spec: https://wiki.onap.org/display/DW/SDN-R+Database+for+Instanbul + * @author jack + * + */ +public class DatabaseIdGenerator { + + private static final Pattern FAULTPATTERN = Pattern.compile(".*\\[layerProtocol=(.*)\\]"); + + public static String getMaintenanceId(String nodeId) { + return nodeId; + } + + public static String getMaintenanceId(MaintenanceEntity object) { + return object == null ? null : object.getNodeId(); + } + + public static String getControllerId() { + return UUID.randomUUID().toString(); + } + + public static String getFaultcurrentId(String nodeId, String objectId, String problemName) { + String uuId; + + Matcher matcher = FAULTPATTERN.matcher(objectId); + if (matcher.matches() && matcher.groupCount() == 1) { + uuId = matcher.group(1); + } else { + uuId = objectId; + } + return String.format("%s/%s/%s", nodeId, uuId, problemName); + } + + public static String getFaultcurrentId(FaultcurrentEntity object) { + return object == null ? null : getFaultcurrentId(object.getNodeId(), object.getObjectId(), object.getProblem()); + } + + public static String getNetworkelementConnectionId(String nodeId) { + return nodeId; + } + + public static String getNetworkelementConnectionId(NetworkElementConnectionEntity object) { + return object == null ? null : object.getNodeId(); + } + + public static String getPmData15mId(String nodeId, String uuidInterface, String timestamp) { + return String.format("%s/%s/%s", nodeId, uuidInterface, timestamp); + } + + public static String getPmData15mId(Pmdata15mEntity object) { + return object == null ? null + : getPmData15mId(object.getNodeName(), object.getUuidInterface(), object.getTimeStamp().getValue()); + } + public static String getPmData24hId(String nodeId, String uuidInterface, String timestamp) { + return String.format("%s/%s/%s", nodeId, uuidInterface, timestamp); + } + + public static String getPmData24hId(Pmdata24hEntity object) { + return object == null ? null + : getPmData24hId(object.getNodeName(), object.getUuidInterface(), object.getTimeStamp().getValue()); + } + + public static String getInventoryId(InventoryEntity object) { + return getInventoryId(object.getNodeId(),object.getUuid()); + } + + private static String getInventoryId(String nodeId, String uuid) { + return String.format("%s/%s", nodeId, uuid); + } +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/FaultEntityManager.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/FaultEntityManager.java new file mode 100644 index 000000000..8371b9c2b --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/FaultEntityManager.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity; + +import java.util.Date; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Fault; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; +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; + +public class FaultEntityManager { + + /** + * The leading indication for notification or events that are not in the currentProblem data of the ONF Coremodel + */ + private static final String NOCURRENTPROBLEMINDICATION = "#"; + + /** + * Specific problems are not moving into current problem list + * + * @param problemName to be verified + * @return true if problem is current + */ + public static boolean isManagedAsCurrentProblem(String problemName) { + return !problemName.startsWith(NOCURRENTPROBLEMINDICATION); + } + + public static boolean isManagedAsCurrentProblem(Fault problem) { + return isManagedAsCurrentProblem(problem.getProblem()); + } + + /** + * Specific problems are not moving into current problem list + * + * @param fault to be verified + * @return true if cleared indication + */ + public static boolean isNoAlarmIndication(@Nonnull Fault fault) { + return SeverityType.NonAlarmed.equals(fault.getSeverity()); + } + + public static Filter getOlderOrEqualFilter(Date olderAreOutdated) { + return new FilterBuilder().setProperty("timestamp").setFiltervalue("<").build(); + } +} 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 new file mode 100644 index 000000000..84786b0cd --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseEventsService.java @@ -0,0 +1,269 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import org.mariadb.jdbc.Driver; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriterFault; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriterPm; +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.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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GranularityPeriodType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; +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.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; +import org.slf4j.LoggerFactory; + +/** + * Event service, writing all events into the database into the appropriate index. + * + * @author herbert + */ +public class HtDatabaseEventsService implements DataProvider { + private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseEventsService.class); + + private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();; + + protected final SqlDBClient dbClient; + protected final String controllerId; + protected final SqlDBReaderWriter connectionlogRW; + protected final SqlDBReaderWriter eventlogRW; + protected final SqlDBReaderWriter eventRWFaultLog; + protected final SqlDBReaderWriterFault eventRWFaultCurrent; + protected final SqlDBReaderWriter equipmentRW; + protected final SqlDBReaderWriter guicutthroughRW; + protected final SqlDBReaderWriter networkelementConnectionRW; + protected final SqlDBReaderWriterPm pm15mRW; + protected final SqlDBReaderWriterPm pm24hRW; + protected final String controllerTableName; + + public HtDatabaseEventsService(SqlDBConfig config) { + LOG.debug("Creating dummy instance of org.mariadb.jdbc.Driver"); + @SuppressWarnings("unused") + Driver dvr = new org.mariadb.jdbc.Driver(); + dvr = null; + this.controllerId = config.getControllerId(); + this.controllerTableName = SqlDBMapper.TABLENAME_CONTROLLER + config.getDbSuffix(); + 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.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.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.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.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.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.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.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.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); + + } + + @Override + public void writeConnectionLog(ConnectionlogEntity event) { + this.connectionlogRW.write(event, event.getId()); + } + + @Override + public void writeEventLog(EventlogEntity event) { + this.eventlogRW.write(event, event.getId()); + + } + + @Override + public void writeFaultLog(FaultlogEntity fault) { + this.eventRWFaultLog.write(fault, fault.getId()); + } + + @Override + public void updateFaultCurrent(FaultcurrentEntity fault) { + final String id = fault.getId() != null ? fault.getId() : DatabaseIdGenerator.getFaultcurrentId(fault); + if (FaultEntityManager.isManagedAsCurrentProblem(fault)) { + if (FaultEntityManager.isNoAlarmIndication(fault)) { + LOG.debug("Remove from currentFaults: {}", fault.toString()); + this.eventRWFaultCurrent.remove(id); + } else { + LOG.debug("Write to currentFaults: {}", fault.toString()); + this.eventRWFaultCurrent.updateOrInsert(fault, id); + } + } else { + LOG.debug("Ingnore for currentFaults: {}", fault.toString()); + } + } + + @Override + public int clearFaultsCurrentOfNode(String nodeName) { + return this.eventRWFaultCurrent + .remove(Arrays.asList(new FilterBuilder().setProperty("node-id").setFiltervalue(nodeName).build())); + } + + @Override + public int clearFaultsCurrentOfNodeWithObjectId(String nodeName, String objectId) { + return this.eventRWFaultCurrent + .remove(Arrays.asList(new FilterBuilder().setProperty("node-id").setFiltervalue(nodeName).build(), + new FilterBuilder().setProperty("object-id").setFiltervalue(objectId).build())); + } + + @Override + public List getAllNodesWithCurrentAlarms() { + return this.eventRWFaultCurrent.getAllNodes(); + } + + @Override + public void writeInventory(String nodeId, List list) { + for (Inventory internalEquipment : list) { + this.equipmentRW.updateOrInsert(internalEquipment, + internalEquipment.getId() != null ? internalEquipment.getId() + : DatabaseIdGenerator.getInventoryId(internalEquipment)); + } + } + + @Override + public void writeGuiCutThroughData(Guicutthrough gcData, String nodeId) { + this.guicutthroughRW.write(gcData, nodeId); + } + + @Override + public int clearGuiCutThroughEntriesOfNode(String nodeName) { + this.guicutthroughRW.remove(nodeName); + return 0; + } + + @Override + public boolean updateNetworkConnectionDeviceType(NetworkElementConnectionEntity networkElementConnectionEntitiy, + String nodeId) { + return this.networkelementConnectionRW.updateOrInsert(networkElementConnectionEntitiy, nodeId) != null; + } + + @Override + public boolean updateNetworkConnection22(NetworkElementConnectionEntity ne, String nodeId) { + return this.networkelementConnectionRW.updateOrInsert(ne, nodeId) != null; + } + + @Override + public void removeNetworkConnection(String nodeId) { + this.networkelementConnectionRW.remove(nodeId); + + } + + @Override + public int doIndexClean(Date olderAreOutdated) { + String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated); + List filter = new ArrayList<>(); + filter.add(new FilterBuilder().setProperty("timestamp").setFiltervalue(String.format("<%s", netconfTimeStamp)) + .build()); + if (this.controllerId != null) { + filter.add( + new FilterBuilder().setProperty(SqlDBMapper.ODLID_DBCOL).setFiltervalue(this.controllerId).build()); + } + int removed = 0; + + removed += this.eventlogRW.remove(filter); + removed += this.eventRWFaultLog.remove(filter); + return removed; + } + + @Override + public long getNumberOfOldObjects(Date olderAreOutdated) { + List filter = Arrays.asList(FaultEntityManager.getOlderOrEqualFilter(olderAreOutdated)); + try { + return this.eventRWFaultLog.count(filter, this.controllerId); + } catch (SQLException e) { + LOG.warn("problem counting faults older than {}: ", olderAreOutdated, e); + } + return 0; + } + + @Override + public List getNetworkElementConnections() { + return this.networkelementConnectionRW.readAll(NetworkElementConnectionEntity.class); + } + + @Override + public void doWritePerformanceData(List list) { + list.stream().forEach((pmData) -> { + GranularityPeriodType granularityPeriod = + pmData.getGranularityPeriod() != null ? pmData.getGranularityPeriod() + : GranularityPeriodType.Unknown; + switch (granularityPeriod) { + case Period15Min: + this.pm15mRW.write(pmData); + break; + case Period24Hours: + this.pm24hRW.write(pmData); + break; + case Unknown: + default: + LOG.debug("Unknown granularity {}", granularityPeriod); + break; + } + }); + + } + + @Override + public HtDatabaseClient getRawClient() { + return null; + } + +} 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 new file mode 100644 index 000000000..dbbb89c47 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/entity/HtDatabaseMaintenanceService.java @@ -0,0 +1,126 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBReaderWriter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInputBuilder; +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; +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.FilterKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.Data; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HtDatabaseMaintenanceService implements HtDatabaseMaintenance { + + private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseMaintenanceService.class); + private final SqlDBReaderWriter maintenanceRw; + private final SqlDBDataProvider dbProvider; + + public HtDatabaseMaintenanceService(SqlDBDataProvider dbProvider) { + this.dbProvider = dbProvider; + this.maintenanceRw = dbProvider.getMaintenanceReaderWriter(); + } + + + @Override + public MaintenanceEntity createIfNotExists(String nodeId) { + + MaintenanceEntity e = this.getMaintenance(nodeId); + if (e == null) { + try { + CreateMaintenanceOutputBuilder createResult = this.dbProvider + .createMaintenance(new CreateMaintenanceInputBuilder().setNodeId(nodeId).build()); + e = createResult.build(); + } catch (IOException e1) { + LOG.warn("problem writing initial maintenance entry for {} : ", nodeId, e); + } + } + return e; + } + + private static Map getFilterInput(String key, String value) { + Map filterMap = new HashMap<>(); + Filter f = new FilterBuilder().setProperty(key).setFiltervalue(value).build(); + filterMap.put(f.key(), f); + return filterMap; + } + + @Override + public void deleteIfNotRequired(String nodeId) { + ReadNetworkElementConnectionListOutput result = this.dbProvider.readNetworkElementConnectionList( + new ReadNetworkElementConnectionListInputBuilder().setFilter(getFilterInput("node-id", nodeId)).build()) + .build(); + if (result.getData() != null && result.getData().size() > 0) { + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.Data entry = + result.getData().get(0); + if (entry.isIsRequired()) { + return; + + } + } + try { + this.dbProvider.deleteMaintenance( + new DeleteMaintenanceInputBuilder().setId(DatabaseIdGenerator.getMaintenanceId(nodeId)).build()); + } catch (IOException e) { + LOG.warn("problem deleting maintenance entry for node {}: ", nodeId, e); + } + } + + @Override + public List getAll() { + return this.maintenanceRw.readAll(MaintenanceEntity.class); + } + + @Override + public MaintenanceEntity getMaintenance(@Nullable String nodeId) { + ReadMaintenanceListOutput result = this.dbProvider + .readMaintenanceList( + new ReadMaintenanceListInputBuilder().setFilter(getFilterInput("node-id", nodeId)).build()) + .build(); + + return result.getData() != null ? result.getData().size() > 0 ? result.getData().get(0) : null : null; + } + + @Override + public MaintenanceEntity setMaintenance(MaintenanceEntity m) { + + this.maintenanceRw.updateOrInsert(m, DatabaseIdGenerator.getMaintenanceId(m)); + return this.getMaintenance(m.getNodeId()); + } + + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/QueryResult.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/QueryResult.java new file mode 100644 index 000000000..ca90e84fb --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/QueryResult.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper; + +import java.util.List; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PaginationOutputG; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; + +public class QueryResult { + + private List result; + private PaginationOutputG pagination; + + public QueryResult( List result,long page, long pageSize,long total) { + this.result = result; + + PaginationBuilder x = new PaginationBuilder(); + x.setPage(Uint64.valueOf(page)); + x.setSize(Uint32.valueOf(pageSize)); + x.setTotal(Uint64.valueOf(total)); + pagination = x.build(); + } + + public List getResult() { + return result; + } + + public PaginationOutputG getPagination() { + return pagination; + } + + @Override + public String toString() { + return "QueryResult [result=" + result + ", pagination=" + pagination + "]"; + } + + + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/SqlDBQueryByFilter.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/SqlDBQueryByFilter.java new file mode 100644 index 000000000..4354b1647 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/rpctypehelper/SqlDBQueryByFilter.java @@ -0,0 +1,373 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.common.YangHelper; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; +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.SortOrder; +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.Pagination; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Sortorder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBQueryByFilter { + + private static final Logger LOG = LoggerFactory.getLogger(SqlDBQueryByFilter.class); + private static final List timestampValueNames = Arrays.asList("timestamp", "time-stamp", "start", "end"); + + private static List emptySortOrderList = new ArrayList<>(); + private static List emptyFilterList = new ArrayList<>(); + + // Derived from input + private long page; + private long pageSize; + private long fromPage; + private List filterList; + private List sortOrder; + + /** + * Process input from RPC into Queries to database + * + * @param input Input from RPC, for test it could be null + */ + public SqlDBQueryByFilter(EntityInput input) { + page = -1; + pageSize = -1; + if (input != null) { + @Nullable + Pagination pagination = input.getPagination(); + if (pagination != null) { + @Nullable Uint64 pageOrNull = YangHelper2.getUint64(pagination.getPage()); + if (pageOrNull != null) { + page = pageOrNull.longValue(); + } + @Nullable Uint32 pageSizeOrNull = YangHelper2.getUint32(pagination.getSize()); + if (pageSizeOrNull != null) { + pageSize = pageSizeOrNull.longValue(); + } + } + } + if (page < 0) + page = 1; + if (pageSize < 0) + pageSize = 1; + + fromPage = (page - 1) * pageSize; + if (fromPage < 0 || pageSize > 10000) + throw new IllegalArgumentException("mismatching input parameters. From:" + fromPage + " size:" + pageSize); + + filterList = YangHelper.getList(input.getFilter()); + if (filterList == null) + filterList = emptyFilterList; + sortOrder = YangHelper.getList(input.getSortorder()); + if (sortOrder == null) + sortOrder = emptySortOrderList; + + } + + public QueryBuilder getQueryBuilderByFilter() { + return getQueryBuilderByFilter(""); + } + + public QueryBuilder getQueryBuilderByFilter(String prefix) { + QueryBuilder queryBuilder = fromFilter(filterList, prefix).from(fromPage).size(pageSize); + setSortOrder(queryBuilder, sortOrder, prefix); + return queryBuilder; + } + + public long getPage() { + return page; + } + + public long getPageSize() { + return pageSize; + } + + public long getPageStartIndex() { + return fromPage; + } + + @Override + public String toString() { + return "QueryByFilter [page=" + page + ", pageSize=" + pageSize + ", fromPage=" + fromPage + ", filterList=" + + filterList + ", sortOrder=" + sortOrder + "]"; + } + + /* + * Private and static implementations + */ + private static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder, String prefix) { + if (sortorder != null && sortorder.size() > 0) { + for (Sortorder so : sortorder) { + query.sort(handlePrefix(prefix, so.getProperty()), convert(so.getSortorder())); + } + } + return query; + } + + private static org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder convert(SortOrder sortOrder) { + return sortOrder == SortOrder.Ascending + ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING + : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING; + }; + + private static String fillTimeStamp(String value) { + int idx = value.lastIndexOf("*"); + final String REPLACE = "0000-00-00T00:00:00.0Z"; + String s = value.substring(0, idx) + REPLACE.substring(idx); + if (Integer.parseInt(s.substring(5, 7)) == 0) { + s = s.substring(0, 5) + "01-" + s.substring(8); + } + if (Integer.parseInt(s.substring(8, 10)) == 0) { + s = s.substring(0, 8) + "01" + s.substring(10); + } + + return s; + } + + /** + * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: + * 2017-01-01T00:00:00Z, lt:2018-01-01T00:00:00Z + * + * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 + * + */ + private static @Nullable QueryBuilder fromTimestampSearchFilter(String property, String value) { + if (!value.endsWith("*")) { + return null; + } + int idx = value.lastIndexOf("*"); + String lowerEnd = fillTimeStamp(value); + String upperEnd = null; + NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); + Date dt = null; + try { + dt = converter.getDateFromNetconf(lowerEnd); + } catch (Exception e) { + + } + if (dt == null) { + return null; + } + // property.substring(0,idx)+REPLACE.substring(idx+1); + Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + c.setTime(dt); + int tmpvalue; + switch (idx) { + case 1: // (2*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 2: // (20*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 3: // (200*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 4: // (2000*) + case 5: // (2000-*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 6: // switch 10 months (2000-0* or 2000-1*) + tmpvalue = c.get(Calendar.MONTH); + if (tmpvalue < 9) { + c.set(Calendar.MONTH, 9); + } else { + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + c.set(Calendar.MONTH, 0); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + + break; + case 7: // switch one month (2018-01* or 2018-01-*) + case 8: + c.add(Calendar.MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 9: // (2018-01-0*) + tmpvalue = c.get(Calendar.DAY_OF_MONTH); + if (tmpvalue == 1) { + c.set(Calendar.DAY_OF_MONTH, 10); + } else if (tmpvalue == 10) { + c.set(Calendar.DAY_OF_MONTH, 20); + } else if (tmpvalue == 20) { + if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + c.set(Calendar.DAY_OF_MONTH, 30); + } + } else if (tmpvalue == 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + break; + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 10: // (2018-01-01*) + case 11: // (2018-01-01T*) + c.add(Calendar.DAY_OF_MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 12: // (2018-01-01T1*) + tmpvalue = c.get(Calendar.HOUR_OF_DAY); + if (tmpvalue == 20) { + c.set(Calendar.HOUR_OF_DAY, 0); + c.add(Calendar.DAY_OF_MONTH, 1); + } else { + c.add(Calendar.HOUR_OF_DAY, 10); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 13: // (2018-01-01T11*) + case 14: // (2018-01-01T11-*) + c.add(Calendar.HOUR_OF_DAY, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 15: // (2018-01-01T11-3*) + c.add(Calendar.MINUTE, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 16: // (2018-01-01T11-32*) + case 17: // (2018-01-01T11-32-*) + c.add(Calendar.MINUTE, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 18: // (2018-01-01T11-32-1*) + c.add(Calendar.SECOND, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 19: // (2018-01-01T11-32-11*) + case 20: // (2018-01-01T11-32-11.*) + c.add(Calendar.SECOND, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + + default: + break; + } + + if (upperEnd == null) { + return null; + } + return QueryBuilders.rangeQuery(property).gte(lowerEnd).lt(upperEnd); + + } + + private static QueryBuilder fromFilter(@Nullable List filters, String prefix) { + if (filters == null || filters.size() == 0) { + return QueryBuilders.matchAllQuery(); + + } else if (filters.size() == 1) { + QueryBuilder query; + String p = filters.get(0).getProperty(); + String v = filters.get(0).getFiltervalue(); + if ("id".equals(p)) { + p = "_id"; + } else { + // v=v.toLowerCase(); + } + if (DbFilter.hasSearchParams(v)) { + if (p != null && timestampValueNames.contains(p.toLowerCase())) { + query = fromTimestampSearchFilter(p, v); + if (query != null) { + return query; + } + } + return QueryBuilders.regex(p, DbFilter.createDatabaseRegex(v)); + + + } else if (DbFilter.isComparisonValid(v)) { + RangeQueryBuilder q = DbFilter.getRangeQuery(handlePrefix(prefix, p), v); + if (q != null) { + return q; + } else { + return QueryBuilders.matchQuery(handlePrefix(prefix, p), v); + } + } else { + return QueryBuilders.matchQuery(handlePrefix(prefix, p), v); + } + } else { + BoolQueryBuilder query = new BoolQueryBuilder(); + QueryBuilder tmpQuery; + for (Filter fi : filters) { + String p = fi.getProperty(); + String v = fi.getFiltervalue(); + if ("id".equals(p)) { + p = "_id"; + } else { + // v=v.toLowerCase(); + } + if (DbFilter.hasSearchParams(v)) { + if (p != null && timestampValueNames.contains(p.toLowerCase())) { + tmpQuery = fromTimestampSearchFilter(p, v); + if (tmpQuery != null) { + query.must(tmpQuery); + } else { + query.must(QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v))); + } + } else { + query.must(QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v))); + } + } else if (DbFilter.isComparisonValid(v)) { + RangeQueryBuilder q = DbFilter.getRangeQuery(handlePrefix(prefix, p), v); + if (q != null) { + query.must(q); + } else { + query.must(QueryBuilders.matchQuery(handlePrefix(prefix, p), v)); + } + } else { + query.must(QueryBuilders.matchQuery(handlePrefix(prefix, p), v)); + } + } + LOG.trace("Query result. {}", query.toJSON()); + return query; + } + } + + private static String handlePrefix(String prefix, String p) { + return (prefix != null ? prefix : "") + p; + } + +} 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 new file mode 100644 index 000000000..85d3cfe4b --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java @@ -0,0 +1,536 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.math.BigInteger; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.sql.rowset.CachedRowSet; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsBuilderAnnotationIntrospector; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsDeserializerModifier; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Enumeration; +import org.opendaylight.yangtools.yang.common.Uint16; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; +import org.opendaylight.yangtools.yang.common.Uint8; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBMapper { + + private static final Logger LOG = LoggerFactory.getLogger(SqlDBMapper.class); + + private static final Map, String> mariaDBTypeMap = initTypeMap(); + private static final String ODLID_DBTYPE = "VARCHAR(40)"; + private static final String STRING_DBTYPE = "VARCHAR(255)"; + private static final String ENUM_DBTYPE = "VARCHAR(100)"; + public static final String ODLID_DBCOL = "controller-id"; + private static List> 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(); + public static final String TABLENAME_CONTROLLER = "controller"; + private static final String DEFAULTID_DBTYPE = "int(11)"; + + public static String createTableOdl() { + return "CREATE TABLE IF NOT EXISTS " + TABLENAME_CONTROLLER + " (" + "`id` " + ODLID_DBTYPE + " " + + getColumnOptions("id", ODLID_DBTYPE) + "," + "`desc` " + STRING_DBTYPE + " " + + getColumnOptions("description", STRING_DBTYPE) + "," + "primary key(id))"; + } + + public static String createTable(Class clazz, Entity e) throws UnableToMapClassException { + return createTable(clazz, e, "", false); + } + + public static String createTable(Class clazz, Entity e, String suffix) throws UnableToMapClassException { + return createTable(clazz, e, suffix, false); + } + + public static String createTable(Class clazz, Entity e, boolean autoIndex) throws UnableToMapClassException { + return createTable(clazz, e, "", false); + } + + public static String createTable(Class clazz, Entity e, String suffix, boolean autoIndex) + throws UnableToMapClassException { + StringBuilder sb = new StringBuilder(); + sb.append("CREATE TABLE IF NOT EXISTS `" + e.getName() + suffix + "` (\n"); + if (autoIndex) { + sb.append("`id` " + DEFAULTID_DBTYPE + " " + getColumnOptions("id", DEFAULTID_DBTYPE) + ",\n"); + } else { + sb.append("`id` " + STRING_DBTYPE + " " + getColumnOptions("id", STRING_DBTYPE) + ",\n"); + } + 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); + if (colName.equals("id")) { + continue; + } + String dbType = getDBType(valueType); + String options = getColumnOptions(colName, dbType); + sb.append("`" + colName + "` " + dbType + " " + options + ",\n"); + } + sb.append("primary key(id),"); + sb.append("foreign key(`" + ODLID_DBCOL + "`) references " + TABLENAME_CONTROLLER + "(id)"); + + sb.append(");"); + return sb.toString(); + } + + private static String getColumnOptions(String colName, String dbType) { + StringBuilder options = new StringBuilder(); + if (dbType.contains("VARCHAR")) { + options.append("CHARACTER SET utf8 "); + } + if (colName.equals("id") || colName.equals(ODLID_DBCOL)) { + if (dbType.equals(DEFAULTID_DBTYPE)) { + options.append("NOT NULL AUTO_INCREMENT"); + } else { + options.append("NOT NULL"); + } + } + return options.toString(); + } + + public static List getFilteredMethods(Class clazz, boolean getterOrSetter) { + Method[] methods = clazz.getMethods(); + List list = new ArrayList<>(); + for (Method method : methods) { + if (getterOrSetter) { + if (!isGetter(method)) { + continue; + } + } else { + if (!isSetter(method)) { + continue; + } + } + if (ignoreMethod(method, methods, getterOrSetter)) { + continue; + } + list.add(method); + } + return list; + } + + + private static Map, String> initTypeMap() { + Map, String> map = new HashMap<>(); + map.put(String.class, STRING_DBTYPE); + map.put(Boolean.class, "BOOLEAN"); + map.put(Byte.class, "TINYINT"); + map.put(Integer.class, "INTEGER"); + map.put(Long.class, "BIGINT"); + map.put(BigInteger.class, "BIGINT"); + map.put(Uint8.class, "SMALLINT"); + map.put(Uint16.class, "INTEGER"); + map.put(Uint32.class, "BIGINT"); + map.put(Uint64.class, "BIGINT"); //???? + map.put(DateAndTime.class, "DATETIME(3)"); + return map; + } + + private static boolean ignoreMethod(Method method, Method[] classMehtods, boolean getterOrSetter) { + final String name = method.getName(); + if (name.equals("getAugmentations") || name.equals("getImplementedInterface") + || name.equals("implementedInterface") || name.equals("getClass")) { + return true; + } + for (Method cm : classMehtods) { + if (!cm.equals(method) && cm.getName().equals(name)) { + //resolve conflict + return !resolveConflict(method, cm, getterOrSetter); + } + //silicon fix + if (method.getReturnType().equals(Boolean.class) && getterOrSetter) { + if (name.startsWith("get") && cm.getName().startsWith("is") + && cm.getName().endsWith(name.substring(3))) { + return true; + } + } + } + return false; + } + + private static boolean resolveConflict(Method m1, Method m2, boolean getterOrSetter) { + Class p1 = getterOrSetter ? m1.getReturnType() : m1.getParameterTypes()[0]; + Class p2 = getterOrSetter ? m2.getReturnType() : m2.getParameterTypes()[0]; + if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Map.class, List.class)) { + return p1.isAssignableFrom(List.class); //prefer List setter + } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint64.class, BigInteger.class)) { + return p1.isAssignableFrom(Uint64.class); + } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint32.class, Long.class)) { + return p1.isAssignableFrom(Uint32.class); + } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint16.class, Integer.class)) { + return p1.isAssignableFrom(Uint16.class); + } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint8.class, Short.class)) { + return p1.isAssignableFrom(Uint8.class); + } + return false; + } + + public static String getColumnName(Method method) { + String camelName = (method.getName().startsWith("get") || method.getName().startsWith("set")) + ? method.getName().substring(3) + : method.getName().substring(2); + return convertCamelToKebabCase(camelName); + } + + private static String getDBType(Class valueType) throws UnableToMapClassException { + String type = mariaDBTypeMap.getOrDefault(valueType, null); + if (type == null) { + if (implementsInterface(valueType, DataObject.class) || implementsInterface(valueType, List.class) + || implementsInterface(valueType, Map.class)) { + return "JSON"; + } + if (implementsInterface(valueType, Enumeration.class)) { + return ENUM_DBTYPE; + } + throw new UnableToMapClassException("no mapping for " + valueType.getName() + " found"); + } + return type; + } + + private static boolean implementsInterface(Class valueType, Class iftoImpl) { + return iftoImpl.isAssignableFrom(valueType); + } + + private static boolean isGetter(Method method) { + return method.getName().startsWith("get") || method.getName().startsWith("is") + || method.getName().startsWith("do"); + } + + private static boolean isSetter(Method method) { + return method.getName().startsWith("set"); + } + + /** + * @param input string in Camel Case + * @return String in Kebab case Inspiration from KebabCaseStrategy class of com.fasterxml.jackson.databind with an + * additional condition to handle numbers as well Using QNAME would have been a more fool proof solution, + * however it can lead to performance problems due to usage of Java reflection + */ + private static String convertCamelToKebabCase(String input) { + if (input == null) + return input; // garbage in, garbage out + int length = input.length(); + if (length == 0) { + return input; + } + + StringBuilder result = new StringBuilder(length + (length >> 1)); + + int upperCount = 0; + + for (int i = 0; i < length; ++i) { + char ch = input.charAt(i); + char lc = Character.toLowerCase(ch); + + if (lc == ch) { // lower-case letter means we can get new word + // but need to check for multi-letter upper-case (acronym), where assumption + // is that the last upper-case char is start of a new word + if ((upperCount > 1)) { + // so insert hyphen before the last character now + result.insert(result.length() - 1, '-'); + } else if ((upperCount == 1) && Character.isDigit(ch) && i != length - 1) { + result.append('-'); + } + upperCount = 0; + } else { + // Otherwise starts new word, unless beginning of string + if ((upperCount == 0) && (i > 0)) { + result.append('-'); + } + ++upperCount; + } + result.append(lc); + } + return result.toString(); + } + + public static class UnableToMapClassException extends Exception { + + private static final long serialVersionUID = 1L; + + public UnableToMapClassException(String message) { + super(message); + } + + } + + public static String escape(Object o) { + return escape(o.toString()); + } + + public static String escape(String o) { + return o.replace("'", "\'"); + } + + public static boolean isComplex(Class valueType) { + return DataObject.class.isAssignableFrom(valueType) || List.class.isAssignableFrom(valueType); + } + + public static Object getNumericValue(Object value, Class valueType) { + if (valueType.equals(Byte.class) || valueType.equals(Integer.class) || valueType.equals(Long.class)) { + return value; + } + if (valueType.equals(Uint8.class) || valueType.equals(Uint16.class) || valueType.equals(Uint32.class) + || valueType.equals(Uint64.class)) { + return ((Number) value).longValue(); + } + return value; + } + + public static Object bool2int(Object invoke) { + return Boolean.TRUE.equals(invoke) ? 1 : 0; + } + + public static boolean isBoolean(Class valueType) { + return valueType.equals(Boolean.class); + } + + public static boolean isNumeric(Class valueType) { + return numericClasses.contains(valueType); + + } + + private static boolean isDateTime(Class valueType) { + return valueType.equals(DateAndTime.class); + } + + private static boolean isYangEnum(Class valueType) { + return YangToolsMapperHelper.implementsInterface(valueType, Enumeration.class); + } + + public static List read(CachedRowSet data, Class clazz) + throws JsonMappingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, + InstantiationException, SecurityException, NoSuchMethodException, JsonProcessingException, SQLException { + return read(data, clazz, null); + } + + public static List read(ResultSet data, Class clazz) + throws JsonMappingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, + InstantiationException, SecurityException, NoSuchMethodException, JsonProcessingException, SQLException { + return read(data, clazz, null); + } + + @SuppressWarnings("unchecked") + public static List read(CachedRowSet data, Class clazz, String column) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException, + SecurityException, NoSuchMethodException, JsonMappingException, JsonProcessingException { + + List list = new ArrayList<>(); + while (data.next()) { + if (column == null) { + Builder builder = findPOJOBuilder(clazz); + Class argType; + String col; + for (Method m : getFilteredMethods(builder.getClass(), false)) { + argType = m.getParameterTypes()[0]; + col = getColumnName(m); + m.setAccessible(true); + m.invoke(builder, getValueOrDefault(data, col, argType, null)); + } + list.add(builder.build()); + } else { + Object value = getValueOrDefault(data, column, clazz, null); + if (value != null) { + list.add((T) value); + } + } + } + return list; + } + + @SuppressWarnings("unchecked") + public static List read(ResultSet data, Class clazz, String column) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException, + SecurityException, NoSuchMethodException, JsonMappingException, JsonProcessingException { + + List list = new ArrayList<>(); + while (data.next()) { + if (column == null) { + Builder builder = findPOJOBuilder(clazz); + Class argType; + String col; + for (Method m : getFilteredMethods(builder.getClass(), false)) { + argType = m.getParameterTypes()[0]; + col = getColumnName(m); + m.setAccessible(true); + m.invoke(builder, getValueOrDefault(data, col, argType, null)); + } + list.add(builder.build()); + } else { + Object value = getValueOrDefault(data, column, clazz, null); + if (value != null) { + list.add((T) value); + } + } + } + return list; + } + + @SuppressWarnings("unchecked") + private static Builder findPOJOBuilder(Class ac) throws InstantiationException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException { + try { + String builder = null; + + if (ac.isInterface()) { + String clsName = ac.getName(); + if (clsName.endsWith("Entity")) { + clsName = clsName.substring(0, clsName.length() - 6); + } + builder = clsName + "Builder"; + } + if (builder != null) { + Class innerBuilder = YangToolsMapperHelper.findClass(builder); + Class> builderClass = (Class>) innerBuilder; + return builderClass.getDeclaredConstructor().newInstance(); + } + } catch (ClassNotFoundException e) { + + } + return null; + } + + private static Object getValueOrDefault(CachedRowSet data, String col, Class dstType, Object defaultValue) + throws SQLException, JsonMappingException, JsonProcessingException { + if (isBoolean(dstType)) { + return data.getBoolean(col); + } else if (isNumeric(dstType)) { + return getNumeric(dstType, data.getLong(col)); + } else if (String.class.equals(dstType)) { + return data.getString(col); + } else if (isYangEnum(dstType)) { + return getYangEnum(data.getString(col), dstType); + } else if (isDateTime(dstType)) { + String v = data.getString(col); + return v == null || v.equals("null") ? null : DateAndTime.getDefaultInstance(v.replace(" ", "T") + "Z"); + } else if (isComplex(dstType)) { + String v = data.getString(col); + + return (v == null || v.toLowerCase().equals("null")) ? null : mapper.readValue(v, dstType); + } + return defaultValue; + } + + private static Object getValueOrDefault(ResultSet data, String col, Class dstType, Object defaultValue) + throws SQLException, JsonMappingException, JsonProcessingException { + if (isBoolean(dstType)) { + return data.getBoolean(col); + } else if (isNumeric(dstType)) { + return getNumeric(dstType, data.getLong(col)); + } else if (String.class.equals(dstType)) { + return data.getString(col); + } else if (isYangEnum(dstType)) { + return getYangEnum(data.getString(col), dstType); + } else if (isDateTime(dstType)) { + String v = data.getString(col); + return v == null || v.equals("null") ? null : DateAndTime.getDefaultInstance(v.replace(" ", "T") + "Z"); + } else if (isComplex(dstType)) { + String v = data.getString(col); + + return (v == null || v.toLowerCase().equals("null")) ? null : mapper.readValue(v, dstType); + } + return defaultValue; + } + + + + private static Object getYangEnum(String value, Class dstType) { + if (value == null || value.equals("null")) { + return null; + } + try { + return YangToolsDeserializerModifier.parseEnum(value, dstType); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException e) { + LOG.warn("unable to parse enum value '{}' to class {}: ", value, dstType, e); + } + return null; + } + + private static Object getNumeric(Class dstType, long value) { + if (dstType.equals(Uint64.class)) { + return Uint64.valueOf(value); + } else if (dstType.equals(Uint32.class)) { + return Uint32.valueOf(value); + } else if (dstType.equals(Uint16.class)) { + return Uint16.valueOf(value); + } else if (dstType.equals(Uint16.class)) { + return Uint8.valueOf(value); + } else if (dstType.equals(Long.class)) { + return Long.valueOf(value); + } else if (dstType.equals(Integer.class)) { + return Long.valueOf(value).intValue(); + } else if (dstType.equals(Byte.class)) { + return Long.valueOf(value).byteValue(); + } + return null; + } + + public static DBKeyValuePair getEscapedKeyValue(Method m, String col, Object value) + throws JsonProcessingException { + Class valueType = m.getReturnType(); + String svalue = null; + if (isBoolean(valueType)) { + svalue = String.valueOf(bool2int(value)); + } else if (isNumeric(valueType)) { + svalue = String.valueOf(getNumericValue(value, valueType)); + } else if (isDateTime(valueType)) { + svalue = "'" + getDateTimeValue((DateAndTime) value) + "'"; + } else if (isComplex(valueType)) { + svalue = "'" + escape(mapper.writeValueAsString(value)) + "'"; + } else { + svalue = "'" + escape(value) + "'"; + } + return new DBKeyValuePair("`" + col + "`", svalue); + } + + private static String getDateTimeValue(DateAndTime value) { + String s = value.getValue(); + if (s.endsWith("Z")) { + s = s.substring(0, s.length() - 1).replace("T", " "); + } else if (s.contains("+")) { + s = s.substring(0, s.indexOf("+")).replace("T", " "); + } + return s; + } + + +} 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 new file mode 100644 index 000000000..d033e45f7 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriter.java @@ -0,0 +1,246 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.lang.reflect.InvocationTargetException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.DeleteQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.InsertQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SqlQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.UpdateQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.UpsertQuery; +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.EntityInput; +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.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBReaderWriter { + + private static final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriter.class); + + protected final Entity entity; + private final Class clazz; + protected final SqlDBClient dbService; + private final String dbName; + protected final String controllerId; + protected final String tableName; + + public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class clazz, String dbName, + String controllerId) { + this.dbService = dbService; + this.entity = e; + this.clazz = clazz; + this.dbName = dbName; + this.tableName = this.entity.getName() + dbSuffix; + this.controllerId = controllerId; + } + + public long count(List 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); + } else { + query = String.format("SELECT COUNT(`id`) FROM `%s` %s", this.tableName, + SqlQuery.getWhereExpression(filter)); + } + ResultSet data = this.dbService.read(query); + long cnt = 0; + if (data.next()) { + cnt = data.getLong(1); + } + return cnt; + } + + public long count(List list, String controllerId) throws SQLException { + if (list == null) { + list = new ArrayList<>(); + } + Optional cFilter = + list.stream().filter(e -> SqlDBMapper.ODLID_DBCOL.equals(e.getProperty())).findFirst(); + if (!cFilter.isEmpty()) { + list.remove(cFilter.get()); + } + if (controllerId != null) { + list.add(new FilterBuilder().setProperty(SqlDBMapper.ODLID_DBCOL).setFiltervalue(this.controllerId) + .build()); + } + return this.count(list); + } + + public QueryResult getData(EntityInput input) { + SelectQuery query = new SelectQuery(this.tableName, input, this.controllerId); + LOG.info("query={}", query.toSql()); + try { + ResultSet data = this.dbService.read(query.toSql()); + List mappedData = SqlDBMapper.read(data, clazz); + long total = this.count(input.getFilter() != null ? new ArrayList<>(input.getFilter().values()) : null, + this.controllerId); + return new QueryResult(mappedData, query.getPage(), query.getPageSize(), total); + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading data {}: ", this.entity, e); + } + return null; + } + + + + public String write(S object, String id) { + if (id == null) { + return this.writeWithoutId(object); + } + InsertQuery query = new InsertQuery(this.entity, object, this.controllerId); + query.setId(id); + boolean success = false; + try { + success = this.dbService.write(query.toSql()); + } catch (SQLException e) { + LOG.warn("problem writing data into db: ", e); + } + + return success ? id : null; + } + + private String writeWithoutId(S object) { + + InsertQuery query = new InsertQuery(this.entity, object, this.controllerId); + try { + return this.dbService.writeAndReturnId(query.toSql()); + } catch (SQLException e) { + LOG.warn("problem writing data into db: ", e); + } + return null; + } + + public String update(S object, String id) { + UpdateQuery query = new UpdateQuery(this.entity, object, this.controllerId); + query.setId(id); + String insertedId = null; + try { + Connection connection = this.dbService.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query.toSql()); + stmt.execute(); + + int affectedRows = stmt.getUpdateCount(); + connection.close(); + if (affectedRows > 0) { + insertedId = id; + } + } catch (SQLException e) { + LOG.warn("problem writing data into db: ", e); + } + + return insertedId; + } + + public String updateOrInsert(S object, String id) { + UpsertQuery query = new UpsertQuery(this.entity, object, this.controllerId); + query.setId(id); + String insertedId = null; + LOG.info("query={}", query.toSql()); + try { + Connection connection = this.dbService.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query.toSql()); + stmt.execute(); + + int affectedRows = stmt.getUpdateCount(); + connection.close(); + if (affectedRows > 0) { + insertedId = id; + } + } catch (SQLException e) { + LOG.warn("problem writing data into db: ", e); + } + return insertedId; + } + + public SqlDBReaderWriter setWriteInterface(Class writeInterfaceClazz) { + LOG.debug("Set write interface to {}", writeInterfaceClazz); + if (writeInterfaceClazz == null) { + throw new IllegalArgumentException("Null not allowed here."); + } + + // this.writeInterfaceClazz = writeInterfaceClazz; + return this; + } + + public int remove(List filters) { + DeleteQuery query = new DeleteQuery(this.entity, filters); + int affectedRows = 0; + try { + Connection connection = this.dbService.getConnection(); + PreparedStatement stmt = connection.prepareStatement(query.toSql()); + stmt.execute(); + affectedRows = stmt.getUpdateCount(); + connection.close(); + } catch (SQLException e) { + LOG.warn("problem execute delete query: ", e); + } + return affectedRows; + } + + public int remove(@Nullable String id) { + return this.remove(Arrays.asList(new FilterBuilder().setProperty("id").setFiltervalue(id).build())); + } + + public List readAll(Class clazz) { + SelectQuery query = new SelectQuery(this.tableName); + try { + ResultSet data = this.dbService.read(query.toSql()); + List mappedData = SqlDBMapper.read(data, clazz); + return mappedData; + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading data {}: ", this.entity, e); + } + return null; + } + + public List readAll(String key) { + SelectQuery query = new SelectQuery(this.tableName, key, this.controllerId).groupBy(key); + try { + ResultSet data = this.dbService.read(query.toSql()); + List mappedData = SqlDBMapper.read(data, String.class, key); + return mappedData; + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading data {}: ", this.entity, e); + } + return null; + } +} 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 new file mode 100644 index 000000000..2d4e26eea --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterFault.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database; + +import java.util.List; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.yang.binding.DataObject; + +public class SqlDBReaderWriterFault extends SqlDBReaderWriter { + + private static final String NODE_KEY = "node-id"; + + public SqlDBReaderWriterFault(SqlDBClient dbService, Entity e, String dbSuffix, Class clazz, String dbName, + String controllerId) { + super(dbService, e, dbSuffix, clazz, dbName, controllerId); + } + + public List getAllNodes() { + + return this.readAll(NODE_KEY); + } + +} 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 new file mode 100644 index 000000000..f0ce268db --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBReaderWriterPm.java @@ -0,0 +1,133 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; +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.EntityInput; +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.entity.input.Filter; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBReaderWriterPm extends SqlDBReaderWriter { + + private final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriterPm.class); + + private static final String UUID_KEY = "uuid-interface"; + private static final String NODE_KEY = "node-name"; + private static final String KEY = "node-name"; + + private static final FilterKey FILTERKEY = new FilterKey(KEY); + + public SqlDBReaderWriterPm(SqlDBClient dbService, Entity e, String dbSuffix, Class clazz, String dbName, + String controllerId) { + super(dbService, e, dbSuffix, clazz, dbName, controllerId); + } + + /** + * get aggregated list of ltps for filter NODE_KEY + * + * @param input + * @return + * @throws IOException + */ + public QueryResult getDataLtpList(EntityInput input) throws IOException { + + SelectQuery query = new SelectQuery(this.tableName, UUID_KEY, this.controllerId).groupBy(UUID_KEY); + query.setPagination(input.getPagination()); + Map filter = input.nonnullFilter(); + if (!filter.containsKey(FILTERKEY)) { + String msg = "no node-name in filter found "; + LOG.debug(msg); + throw new IllegalArgumentException(msg); + } + for (Filter f : filter.values()) { + query.addFilter(f.getProperty(), f.getFiltervalue()); + } + + + try { + ResultSet data = this.dbService.read(query.toSql()); + List mappedData = SqlDBMapper.read(data, String.class, UUID_KEY); + long total = this.count(input.getFilter() != null ? new ArrayList<>(input.getFilter().values()) : null, + this.controllerId); + return new QueryResult(mappedData, query.getPage(), query.getPageSize(), total); + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading ltp list: ", e); + } + return null; + } + + /** + * get aggregated devices list + * + * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name') + * @return + * @throws IOException + */ + public QueryResult getDataDeviceList(EntityInput input) throws IOException { + + SelectQuery query = new SelectQuery(this.tableName, NODE_KEY, this.controllerId).groupBy(NODE_KEY); + query.setPagination(input.getPagination()); + Map filter = input.getFilter(); + if (filter != null) { + for (Filter f : filter.values()) { + query.addFilter(f.getProperty(), f.getFiltervalue()); + } + } + + try { + ResultSet data = this.dbService.read(query.toSql()); + List mappedData = SqlDBMapper.read(data, String.class, NODE_KEY); + long total = this.count(input.getFilter() != null ? new ArrayList<>(input.getFilter().values()) : null, + this.controllerId); + return new QueryResult(mappedData, query.getPage(), query.getPageSize(), total); + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + LOG.warn("problem reading device list: ", e); + } + return null; + } + + public void write(PmdataEntity pmData) { + DateAndTime date = pmData.getTimeStamp(); + final String id = String.format("%s/%s/%s", pmData.getNodeName(), pmData.getUuidInterface(), + date != null ? date.getValue() : "null"); + this.updateOrInsert(pmData, id); + } + +} 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 new file mode 100644 index 000000000..c98ba388d --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBStatusReader.java @@ -0,0 +1,132 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult; +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.Entity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.Data; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.DataBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.FaultsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.NetworkElementConnectionsBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SqlDBStatusReader { + + private final Logger LOG = LoggerFactory.getLogger(SqlDBStatusReader.class); + + private final SqlDBClient dbClient; + private final String controllerId; + + public SqlDBStatusReader(SqlDBClient dbClient, String controllerId) { + this.dbClient = dbClient; + this.controllerId = controllerId; + } + + public QueryResult getDataStatus() { + String selectQuery = createCountQuery("severity", Entity.Faultcurrent, this.controllerId); + long criticalCount = 0; + long majorCount = 0; + long minorCount = 0; + long warningCount = 0; + ResultSet data; + try { + data = this.dbClient.read(selectQuery); + String severity; + while (data.next()) { + severity = data.getString(1); + if (severity != null) { + if (severity.equals(SeverityType.Critical.getName())) { + criticalCount = data.getLong(2); + } else if (severity.equals(SeverityType.Major.getName())) { + majorCount = data.getLong(2); + } else if (severity.equals(SeverityType.Minor.getName())) { + minorCount = data.getLong(2); + } else if (severity.equals(SeverityType.Warning.getName())) { + warningCount = data.getLong(2); + } + + } + } + } catch (SQLException e) { + LOG.warn("problem reading status:", e); + } + DataBuilder builder = new DataBuilder().setFaults( + new FaultsBuilder().setCriticals(Uint32.valueOf(criticalCount)).setMajors(Uint32.valueOf(majorCount)) + .setMinors(Uint32.valueOf(minorCount)).setWarnings(Uint32.valueOf(warningCount)).build()); + selectQuery = createCountQuery("status", Entity.NetworkelementConnection, this.controllerId); + NetworkElementConnectionsBuilder neBuilder = new NetworkElementConnectionsBuilder(); + String state; + long connectedCount = 0, connectingCount = 0, disconnectedCount = 0, mountedCount = 0, unableToConnectCount = 0, + undefinedCount = 0, unmountedCount = 0; + long cnt; + try { + data = this.dbClient.read(selectQuery); + while (data.next()) { + state = data.getString(1); + cnt = data.getLong(2); + if (state != null) { + if (state.equals(ConnectionLogStatus.Connected.getName())) { + connectedCount = cnt; + } else if (state.equals(ConnectionLogStatus.Connecting.getName())) { + connectingCount = cnt; + } else if (state.equals(ConnectionLogStatus.Disconnected.getName())) { + disconnectedCount = cnt; + } else if (state.equals(ConnectionLogStatus.Mounted.getName())) { + mountedCount = cnt; + } else if (state.equals(ConnectionLogStatus.UnableToConnect.getName())) { + unableToConnectCount = cnt; + } else if (state.equals(ConnectionLogStatus.Undefined.getName())) { + undefinedCount = cnt; + } else if (state.equals(ConnectionLogStatus.Unmounted.getName())) { + unmountedCount = cnt; + } + } + } + } catch (SQLException e) { + LOG.warn("problem reading status:", e); + } + neBuilder.setConnected(Uint32.valueOf(connectedCount)).setConnecting(Uint32.valueOf(connectingCount)) + .setDisconnected(Uint32.valueOf(disconnectedCount)).setMounted(Uint32.valueOf(mountedCount)) + .setTotal(Uint32.valueOf(connectedCount + connectingCount + disconnectedCount + mountedCount + + unableToConnectCount + undefinedCount + unmountedCount)) + .setUnableToConnect(Uint32.valueOf(unableToConnectCount)).setUndefined(Uint32.valueOf(undefinedCount)) + .setUnmounted(Uint32.valueOf(unmountedCount)); + builder.setNetworkElementConnections(neBuilder.build()); + return new QueryResult(Arrays.asList(builder.build()), 1, 1, 1); + } + + private static String createCountQuery(String key, Entity e, String controllerId) { + return String.format("SELECT `%s`, COUNT(`%s`) " + "FROM `%s` " + "%s " + "GROUP BY `%s`;", key, key, + e.getName(), + controllerId != null ? String.format("WHERE `%s`='%s'", SqlDBMapper.ODLID_DBCOL, controllerId) : "", + key); + } + +} 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 new file mode 100644 index 000000000..c499fefb8 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/CountQuery.java @@ -0,0 +1,82 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +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.EntityInput; +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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey; + +public class CountQuery implements SqlQuery { + + private final Entity entity; + private final List filters; + private final String countField; + public CountQuery(Entity e) { + this(e, "*", null); + } + public CountQuery(Entity e, String controllerId) { + this(e, "*", controllerId); + } + public CountQuery(Entity e, String countField, String controllerId) { + this.entity = e; + this.countField = countField; + this.filters = new ArrayList<>(); + if (controllerId != null) { + this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId); + } + } + + public CountQuery(Entity e, EntityInput input) { + this(e, input, null); + } + + public CountQuery(Entity e, EntityInput input, String controllerId) { + this(e); + Map filter = input != null ? input.getFilter() : null; + if (filter != null && filter.size() > 0) { + this.filters.addAll(filters); + } + if (controllerId != null) { + this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId); + } + } + + public void addFilter(String property, String filtervalue) { + this.filters.add(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build()); + + } + + @Override + public String toSql() { + StringBuilder sb = new StringBuilder(); + sb.append(String.format("SELECT COUNT(`%s`) FROM `%s`", this.countField, this.entity.getName())); + sb.append(SqlQuery.getWhereExpression(this.filters)); + return sb.toString(); + } + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/DeleteQuery.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/DeleteQuery.java new file mode 100644 index 000000000..f49c78a29 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/DeleteQuery.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import java.util.List; +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.entity.input.Filter; + +public class DeleteQuery implements SqlQuery { + + + private final Entity entity; + private final List filters; + + public DeleteQuery(Entity e, List filters) { + this.entity = e; + this.filters = filters; + } + + @Override + public String toSql() { + return String.format("DELETE FROM `%s` %s", this.entity.getName(), SqlQuery.getWhereExpression(this.filters)); + } + +} 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 new file mode 100644 index 000000000..108888ad0 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/InsertQuery.java @@ -0,0 +1,111 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InsertQuery implements SqlQuery { + + private final Logger LOG = LoggerFactory.getLogger(InsertQuery.class); + + protected final Entity entity; + private final String controllerId; + private final T object; + private final boolean ignoreNull; + private String id; + + public InsertQuery(Entity e, T object, String controllerId) { + this.entity = e; + this.controllerId = controllerId; + this.object = object; + this.ignoreNull = true; + this.id = null; + + } + + @Override + public String toSql() { + try { + return this.toSqlWithError(); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | JsonProcessingException e) { + LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e); + } + return null; + } + + protected String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, + JsonProcessingException { + Class cls = this.object.getClass(); + List> kvps = new ArrayList<>(); + List cols = new ArrayList<>(); + List args = new ArrayList<>(); + Object value; + String col; + StringBuilder sb = new StringBuilder(); + sb.append("INSERT INTO `" + entity.getName() + "` ("); + for (Method m : SqlDBMapper.getFilteredMethods(cls, true)) { + + m.setAccessible(true); + value = m.invoke(this.object); + col = SqlDBMapper.getColumnName(m); + if (col.equals("id") && this.id != null) { + value = this.id; + } + if (ignoreNull && value == null) { + continue; + } + DBKeyValuePair 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.controllerId+"'"); + sb.append( String.join(",", cols)); + sb.append(",`" + SqlDBMapper.ODLID_DBCOL + "`) VALUES ("); + sb.append( String.join(",", args)+" )"); + this.appendAdditionalToQuery(sb,kvps); + return sb.toString(); + } + + protected void appendAdditionalToQuery(StringBuilder sb, List> keyValues) { + + } + + public void setId(String id) { + this.id = id; + } +} 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 new file mode 100644 index 000000000..800150814 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SelectQuery.java @@ -0,0 +1,166 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +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.SortOrder; +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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Pagination; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Sortorder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.SortorderKey; + +public class SelectQuery implements SqlQuery { + + private static final long DEFAULT_PAGESIZE = 20; + private static final long DEFAULT_PAGE = 1; + private static final List TIMESTAMPPROPERTYNAMES = Arrays.asList("timestamp", "time-stamp", "start", "end"); + private final String tableName; + private final List filters; + private final List sortExpressions; + private long page; + private long pageSize; + private final List fields; + private final List groups; + + public SelectQuery(String tableName) { + this(tableName, Arrays.asList("*"), null); + } + + public SelectQuery(String tableName, List fields, String controllerId) { + this.tableName = tableName; + this.fields = fields; + this.filters = new ArrayList<>(); + this.sortExpressions = new ArrayList<>(); + this.groups = new ArrayList<>(); + this.page = DEFAULT_PAGE; + this.pageSize = DEFAULT_PAGESIZE; + if (controllerId != null) { + this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId); + } + } + + public SelectQuery(String tableName, String field, String controllerId) { + this(tableName, Arrays.asList(field), controllerId); + } + + public SelectQuery(String tableName, EntityInput input) { + this(tableName, input, null); + } + + public SelectQuery(String tableName, EntityInput input, String controllerId) { + this(tableName); + Map filter = input != null ? input.getFilter() : null; + if (filter != null && filter.size() > 0) { + for (Filter f : filter.values()) { + this.addFilter(f.getProperty(), f.getFiltervalue()); + } + } + if (controllerId != null) { + this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId); + } + + Map so = input.getSortorder(); + if (so != null && so.size() > 0) { + for (Sortorder s : so.values()) { + this.addSortOrder(s.getProperty(), + s.getSortorder() == SortOrder.Ascending ? "ASC" : "DESC"); + } + } + Pagination pagination = input.getPagination(); + if (pagination != null) { + this.setPagination(pagination.getPage().longValue(), pagination.getSize().longValue()); + } else { + this.setPagination(1, 30); + } + + } + + public void addFilter(String property, String filtervalue) { + this.filters.add(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build()); + + } + + public void addSortOrder(String col, String order) { + this.sortExpressions.add(String.format("`%s` %s", col, order)); + } + + public void setPagination(long page, long pageSize) { + this.page = page; + this.pageSize = pageSize; + } + + public void setPagination(@Nullable Pagination pagination) { + long page = DEFAULT_PAGE; + long pageSize = DEFAULT_PAGESIZE; + if(pagination!=null) { + if(pagination.getPage()!=null) { + page = pagination.getPage().longValue(); + } + if(pagination.getSize()!=null) { + pageSize = pagination.getSize().longValue(); + } + } + this.setPagination(page, pageSize); + + } + + @Override + public String toSql() { + StringBuilder sb = new StringBuilder(); + if (this.fields.size() == 1 && this.fields.contains("*")) { + sb.append(String.format("SELECT * FROM `%s`", this.tableName)); + } else { + sb.append(String.format("SELECT `%s` FROM `%s`", String.join("`,`", this.fields), this.tableName)); + } + sb.append(SqlQuery.getWhereExpression(this.filters)); + if (this.groups.size() > 0) { + sb.append(String.format(" GROUP BY `%s`", String.join("`,`", this.groups))); + } + if (this.sortExpressions.size() > 0) { + sb.append(" ORDER BY " + String.join(",", this.sortExpressions)); + } + sb.append(String.format(" LIMIT %d,%d;", (this.page - 1) * this.pageSize, this.pageSize)); + return sb.toString(); + } + + public long getPage() { + return this.page; + } + + public long getPageSize() { + return this.pageSize; + } + + public SelectQuery groupBy(String group) { + this.groups.add(group); + return this; + } + +} 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 new file mode 100644 index 000000000..95e360540 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/SqlQuery.java @@ -0,0 +1,351 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +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; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.RangeSqlDBFilter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.RegexSqlDBFilter; +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.entity.input.Filter; + +public interface SqlQuery { + + String toSql(); + + static final List TIMESTAMPPROPERTYNAMES = Arrays.asList("timestamp", "time-stamp", "start", "end"); + static final String MARIADB_TIMESTAMP_REPLACER = "0000-00-00 00:00:00.000"; + static final String NETCONF_TIMESTAMP_REPLACER = "0000-00-00T00:00:00.000Z"; + 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(); + + public static String getWhereExpression(List filters) { + if (filters == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + if (filters.size() > 0) { + + sb.append(" WHERE (" + getFilterExpression(filters.get(0)) + ")"); + for (int i = 1; i < filters.size(); i++) { + sb.append(" AND (" + getFilterExpression(filters.get(i)) + ")"); + } + } + return sb.toString(); + } + + public static String getFilterExpression(Filter filter) { + String property = filter.getProperty(); + List values = collectValues(filter.getFiltervalue(), filter.getFiltervalues()); + if (values.size() == 1) { + return getFilterExpression(property, values.get(0)); + } else if (values.size() > 1) { + StringBuilder sb = new StringBuilder(); + sb.append(getFilterExpression(property, values.get(0))); + for (int i = 1; i < values.size(); i++) { + sb.append(" OR "); + sb.append(getFilterExpression(property, values.get(i))); + } + return sb.toString(); + } + return null; + } + + public static String getFilterExpression(String property, String value) { + String filter = null;; + if (DbFilter.hasSearchParams(value)) { + if (TIMESTAMPPROPERTYNAMES.contains(property.toLowerCase())) { + if (DbFilter.isComparisonValid(value)) { + filter = getComparisonFilter(property, value, true); + } else { + filter = fromTimestampSearchFilter(property, value); + } + if (filter != null) { + return filter; + } + } + return new RegexSqlDBFilter(property, value).getFilterExpression(); + } else if (DbFilter.isComparisonValid(value)) { + filter = getComparisonFilter(property, value, TIMESTAMPPROPERTYNAMES.contains(property.toLowerCase())); + if (filter != null) { + return filter; + } + } + return new DBFilterKeyValuePair(property, value).getFilterExpression(); + } + + static List collectValues(String filtervalue, List filtervalues) { + if (filtervalues == null) { + return Arrays.asList(filtervalue); + } + List values = new ArrayList<>(); + if (filtervalue != null) { + values.add(filtervalue); + } + values.addAll(filtervalues); + return values; + } + + private static String getComparisonFilter(String property, String filtervalue, boolean asTimeStamp) { + filtervalue = filtervalue.trim(); + String comparator = null; + Object value; + if (filtervalue.startsWith(">=")) { + comparator = ">="; + filtervalue = filtervalue.substring(2).trim(); + if (asTimeStamp) { + filtervalue = netconfToMariaDBTimestamp(fillTimeStamp( + filtervalue.endsWith("*") ? filtervalue : (filtervalue + "*"), MARIADB_TIMESTAMP_REPLACER)); + } + } else if (filtervalue.startsWith(">")) { + comparator = ">"; + filtervalue = filtervalue.substring(1).trim(); + if (asTimeStamp) { + if (isFullTimestamp(filtervalue)) { + filtervalue = netconfToMariaDBTimestamp(filtervalue); + } else { + comparator = ">="; + filtervalue = netconfToMariaDBTimestamp( + fillTimeStamp(filtervalue.endsWith("*") ? filtervalue : (filtervalue + "*"), + NETCONF_TIMESTAMP_REPLACER, true)); + } + } + } else if (filtervalue.startsWith("<=")) { + comparator = "<="; + filtervalue = filtervalue.substring(2).trim(); + if (asTimeStamp) { + if (isFullTimestamp(filtervalue)) { + filtervalue = netconfToMariaDBTimestamp(filtervalue); + } else { + comparator = "<"; + filtervalue = netconfToMariaDBTimestamp( + fillTimeStamp(filtervalue.endsWith("*") ? filtervalue : (filtervalue + "*"), + NETCONF_TIMESTAMP_REPLACER, true)); + } + } + } else if (filtervalue.startsWith("<")) { + comparator = "<"; + filtervalue = filtervalue.substring(1).trim(); + if (asTimeStamp) { + filtervalue = netconfToMariaDBTimestamp(fillTimeStamp( + filtervalue.endsWith("*") ? filtervalue : (filtervalue + "*"), MARIADB_TIMESTAMP_REPLACER)); + } + } else { + return null; + } + value = filtervalue; + return new RangeSqlDBFilter(property, value, comparator).getFilterExpression(); + } + + static boolean isFullTimestamp(String v) { + return v.length() >= MARIADB_TIMESTAMP_REPLACER_MIN_LENGTH; + } + + /** + * Convert timestamp beginning filter expression like 2017* to a full qualified timestamp like '2017-01-01 + * 00:00:00'. + * + * @param value filter input value + * @return fully qualified timestamp + */ + private static String fillTimeStamp(String value) { + return fillTimeStamp(value, NETCONF_TIMESTAMP_REPLACER); + } + + private static String fillTimeStamp(String value, String replacer) { + return fillTimeStamp(value, replacer, false); + } + + private static String fillTimeStamp(String value, String replacer, boolean useUpperEnd) { + int idx = value.lastIndexOf("*"); + String s = null; + if (idx > replacer.length()) { + s = value.substring(0, replacer.length()); + } else { + s = value.substring(0, idx) + replacer.substring(idx); + } + //if month is zero => set to 1 + if (Integer.parseInt(s.substring(5, 7)) == 0) { + s = s.substring(0, 5) + "01-" + s.substring(8); + } + //if day is zero => set to 1 + if (Integer.parseInt(s.substring(8, 10)) == 0) { + s = s.substring(0, 8) + "01" + s.substring(10); + } + if (useUpperEnd) { + s = getTimestampUpperLimit(s, idx); + } + return s; + } + + /** + * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: 2017-01-01 + * 00:00:00, lt:2018-01-01 00:00:00Z + * + * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 + * + */ + private static @Nullable String fromTimestampSearchFilter(String property, String value) { + if (!value.endsWith("*")) { + return null; + } + int idx = value.lastIndexOf("*"); + String lowerEnd = fillTimeStamp(value); + String upperEnd = getTimestampUpperLimit(fillTimeStamp(value, "0000-00-00T00:00:00.0Z"), idx); + return RangeSqlDBFilter.between(property, netconfToMariaDBTimestamp(lowerEnd), true, + netconfToMariaDBTimestamp(upperEnd), false); + } + + private static String netconfToMariaDBTimestamp(String ts) { + String v = ts.replace("T", " ").replace("Z", ""); + return v.length() > MARIADB_TIMESTAMP_REPLACER_MAX_LENGTH + ? v.substring(0, MARIADB_TIMESTAMP_REPLACER_MAX_LENGTH) + : v; + } + + private static String getTimestampUpperLimit(String lowerEnd, int idx) { + + String upperEnd = null; + NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); + Date dt = null; + try { + dt = converter.getDateFromNetconf(lowerEnd); + } catch (Exception e) { + + } + if (dt == null) { + return null; + } + // property.substring(0,idx)+REPLACE.substring(idx+1); + Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + c.setTime(dt); + int tmpvalue; + switch (idx) { + case 1: // (2*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 2: // (20*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 3: // (200*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 4: // (2000*) + case 5: // (2000-*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 6: // switch 10 months (2000-0* or 2000-1*) + tmpvalue = c.get(Calendar.MONTH); + if (tmpvalue < 9) { + c.set(Calendar.MONTH, 9); + } else { + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + c.set(Calendar.MONTH, 0); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + + break; + case 7: // switch one month (2018-01* or 2018-01-*) + case 8: + c.add(Calendar.MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 9: // (2018-01-0*) + tmpvalue = c.get(Calendar.DAY_OF_MONTH); + if (tmpvalue == 1) { + c.set(Calendar.DAY_OF_MONTH, 10); + } else if (tmpvalue == 10) { + c.set(Calendar.DAY_OF_MONTH, 20); + } else if (tmpvalue == 20) { + if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + c.set(Calendar.DAY_OF_MONTH, 30); + } + } else if (tmpvalue == 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + break; + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 10: // (2018-01-01*) + case 11: // (2018-01-01T*) + c.add(Calendar.DAY_OF_MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 12: // (2018-01-01T1*) + tmpvalue = c.get(Calendar.HOUR_OF_DAY); + if (tmpvalue == 20) { + c.set(Calendar.HOUR_OF_DAY, 0); + c.add(Calendar.DAY_OF_MONTH, 1); + } else { + c.add(Calendar.HOUR_OF_DAY, 10); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 13: // (2018-01-01T11*) + case 14: // (2018-01-01T11-*) + c.add(Calendar.HOUR_OF_DAY, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 15: // (2018-01-01T11-3*) + c.add(Calendar.MINUTE, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 16: // (2018-01-01T11-32*) + case 17: // (2018-01-01T11-32-*) + c.add(Calendar.MINUTE, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 18: // (2018-01-01T11-32-1*) + c.add(Calendar.SECOND, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 19: // (2018-01-01T11-32-11*) + case 20: // (2018-01-01T11-32-11.*) + c.add(Calendar.SECOND, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + + default: + break; + } + return upperEnd; + } + +} 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 new file mode 100644 index 000000000..b47b03042 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpdateQuery.java @@ -0,0 +1,111 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateQuery implements SqlQuery { + + private final Logger LOG = LoggerFactory.getLogger(UpdateQuery.class); + + private final Entity entity; + private final String controllerId; + private final T object; + private final boolean ignoreNull; + private String id; + + public UpdateQuery(Entity e, T object) { + this(e, object, null); + } + + public UpdateQuery(Entity e, T object, String controllerId) { + this.entity = e; + this.controllerId = controllerId; + this.object = object; + this.ignoreNull = true; + this.id = null; + } + + @Override + public String toSql() { + try { + return this.toSqlWithError(); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | JsonProcessingException e) { + LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e); + } + return null; + } + + private String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, + JsonProcessingException { + Class cls = this.object.getClass(); + Object value; + String col; + StringBuilder sb = new StringBuilder(); + List args = new ArrayList<>(); + sb.append("UPDATE `" + entity.getName() + "` SET "); + List methods = SqlDBMapper.getFilteredMethods(cls, true); + Method m; + for (int i = 0; i < methods.size(); i++) { + m = methods.get(i); + m.setAccessible(true); + value = m.invoke(this.object); + col = SqlDBMapper.getColumnName(m); + if (col.equals("id")) { + if (this.id == null) { + this.id = String.valueOf(value); + } + continue; + } + if (ignoreNull && value == null) { + continue; + } + DBKeyValuePair kvp = SqlDBMapper.getEscapedKeyValue(m, col, value); + args.add(String.format("%s=%s", kvp.getKey(), kvp.getValue())); + } + sb.append(String.join(",", args)); + sb.append(String.format(" WHERE `id`='%s'", this.id)); + if (this.controllerId != null) { + sb.append(String.format(" AND `%s`='%s'", SqlDBMapper.ODLID_DBCOL, this.controllerId)); + } + + return sb.toString(); + } + + public void setId(String id) { + this.id = id; + } + + + +} 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 new file mode 100644 index 000000000..e6f1e13cb --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/UpsertQuery.java @@ -0,0 +1,56 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query; + +import java.util.List; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.yang.binding.DataObject; + +public class UpsertQuery extends InsertQuery { + + public UpsertQuery(Entity e, T object, String controllerId) { + super(e, object, controllerId); + + } + + @Override + protected void appendAdditionalToQuery(StringBuilder sb, List> keyValues) { + sb.append(" ON DUPLICATE KEY UPDATE "); + boolean comma = false; + for (DBKeyValuePair kvp : keyValues) { + if(kvp.getKey().equals("`id`")) { + continue; + } + //do not update is-required if entry already exists + if (this.entity == Entity.NetworkelementConnection && kvp.getKey().equals("`is-required`") + && (kvp.getValue().equals("false") || kvp.getValue().equals("0"))) { + continue; + } + if (comma) { + sb.append(","); + } + sb.append(String.format("%s=%s", kvp.getKey(), kvp.getValue())); + comma = true; + } + } +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBFilterKeyValuePair.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBFilterKeyValuePair.java new file mode 100644 index 000000000..930f16af6 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBFilterKeyValuePair.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters; + +public class DBFilterKeyValuePair extends DBKeyValuePair { + + public DBFilterKeyValuePair(String key, Object value) { + super(key, value); + } + + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBKeyValuePair.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBKeyValuePair.java new file mode 100644 index 000000000..2474e5227 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/DBKeyValuePair.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters; + +import java.math.BigInteger; + +public class DBKeyValuePair implements SqlDBFilter { + + private final String key; + private final T value; + + public DBKeyValuePair(String key, T value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public T getValue() { + return value; + } + + @Override + public String toString() { + return "DBKeyValuePair [key=" + key + ", value=" + value + "]"; + } + + protected boolean isNumericValue(T v) { + if ((v instanceof Long) || (v instanceof Integer) || (v instanceof Byte) || (v instanceof BigInteger)) { + return true; + } + return false; + } + + @Override + public String getFilterExpression() { + if (isNumericValue(this.value)) { + return String.format("`%s`=%d", this.key, this.value); + } else { + return String.format("`%s`='%s'", this.key, this.value); + } + } +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RangeSqlDBFilter.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RangeSqlDBFilter.java new file mode 100644 index 000000000..c4a5d8abc --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RangeSqlDBFilter.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters; + +import org.eclipse.jdt.annotation.Nullable; + +public class RangeSqlDBFilter extends DBKeyValuePair implements SqlDBFilter { + + private final String comparator; + + public RangeSqlDBFilter(String property, Object value, String comparator) { + super(property, value); + this.comparator = comparator; + } + + @Override + public String getFilterExpression() { + if (isNumericValue(this.getValue())) { + return String.format("`%s`%s%d", this.getKey(), this.comparator, this.getValue()); + } else { + return String.format("`%s`%s'%s'", this.getKey(), this.comparator, this.getValue()); + } + } + + public static @Nullable String between(String property, String lowerEnd, boolean incLowerEnd, String upperEnd, + boolean incUpperEnd) { + return String.format("(%s AND %s)", + new RangeSqlDBFilter(property, lowerEnd, incLowerEnd ? ">=" : ">").getFilterExpression(), + new RangeSqlDBFilter(property, upperEnd, incUpperEnd ? "<=" : "<").getFilterExpression()); + } + +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RegexSqlDBFilter.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RegexSqlDBFilter.java new file mode 100644 index 000000000..4ef1b9a55 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/RegexSqlDBFilter.java @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters; + +public class RegexSqlDBFilter extends DBKeyValuePair implements SqlDBFilter{ + + + public RegexSqlDBFilter(String key, String value) { + super(key, value); + } + + @Override + public String getFilterExpression() { + return String.format("`%s` RLIKE '%s'", this.getKey(), this.getValue()); + } +} diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/SqlDBFilter.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/SqlDBFilter.java new file mode 100644 index 000000000..2997c7040 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/query/filters/SqlDBFilter.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters; + +public interface SqlDBFilter { + + public String getFilterExpression(); + +} diff --git a/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestCRUDMariaDB.java b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestCRUDMariaDB.java new file mode 100644 index 000000000..ad4ab6789 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestCRUDMariaDB.java @@ -0,0 +1,236 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.ParseException; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test.util.MariaDBTestBase; +import ch.vorburger.exec.ManagedProcessException; + +public class TestCRUDMariaDB { + + + private static final String TABLE1_NAME = "table1"; + private static final String TABLE2_NAME = "table2-v6"; + private static final String TABLE3_NAME = "table3-v6"; + private static final String VIEW2_NAME = "table2"; + private static final String VIEW3_NAME = "table3"; + private static final String TABLE1_MAPPING = "col1 INT PRIMARY KEY, col2 NVARCHAR(30), col3 BOOLEAN"; + private static final String TABLE2_MAPPING = "col1 INT PRIMARY KEY, col2 NVARCHAR(30), col3 BOOLEAN"; + private static final String TABLE3_MAPPING = "col1 INT PRIMARY KEY, col2 NVARCHAR(30), col3 BOOLEAN"; + private static final String DELETE_ALL_FORMAT = "DELETE FROM `%s`"; + private static final String READ_ALL_FORMAT = "SELECT * FROM `%s`"; + private static final String TABLE1_INSERT_ENTRY_FORMAT = + "INSERT INTO `" + TABLE1_NAME + "` (col1, col2, col3) VALUES(%d,'%s',%d);"; + private static final String TABLE1_UPDATE_ENTRY_FORMAT = + "UPDATE `" + TABLE1_NAME + "` SET col2='%s', col3=%d WHERE col1=%d;"; + private static final String TABLE1_DELETE_ENTRY_FORMAT = "DELETE FROM `" + TABLE1_NAME + "` WHERE col1=%d;"; + private static String DBNAME = null; + + private static MariaDBTestBase testBase; + private static SqlDBDataProvider dbProvider; + private static SqlDBClient dbClient; + + @BeforeClass + public static void init() throws Exception { + + testBase = new MariaDBTestBase(); + dbProvider = testBase.getDbProvider(); + dbProvider.waitForDatabaseReady(30, TimeUnit.SECONDS); + dbClient = testBase.createRawClient(); + DBNAME = testBase.getDBName(); + } + + @AfterClass + public static void close() { + try { + testBase.close(); + } catch (ManagedProcessException e) { + e.printStackTrace(); + } + } + + @Test + public void test1() { + ResultSet data; + boolean success; + String id = null; + //create test1Table + success = dbClient.createTable(TABLE1_NAME, TABLE1_MAPPING); + assertTrue("failed to create table", success); + //delete all + try { + success = dbClient.delete(String.format(DELETE_ALL_FORMAT, TABLE1_NAME)); + } catch (SQLException e) { + e.printStackTrace(); + } + assertTrue("failed to clear table", success); + //test empty + data = dbClient.read(String.format(READ_ALL_FORMAT, TABLE1_NAME)); + try { + assertEquals(0, countRows(data)); + } catch (SQLException e) { + e.printStackTrace(); + fail("unable to read size"); + } + //create entry + success = false; + try { + success = dbClient.write(String.format(TABLE1_INSERT_ENTRY_FORMAT, 10, "hello", 0)); + } catch (SQLException e) { + e.printStackTrace(); + } + assertTrue("failed to write data", success); + //verify write + data = dbClient.read(String.format(READ_ALL_FORMAT, TABLE1_NAME)); + try { + assertEquals(1, countRows(data)); + assertTrue(data.next()); + assertEquals(10, data.getInt(1)); + assertEquals("hello", data.getString(2)); + assertEquals(false, data.getBoolean(3)); + } catch (SQLException e) { + e.printStackTrace(); + fail("unable to verify write"); + } + //update entry + success = false; + try { + success = dbClient.update(String.format(TABLE1_UPDATE_ENTRY_FORMAT, "hello2", 1, 10)); + } catch (SQLException e) { + e.printStackTrace(); + } + assertTrue("failed to update data", success); + //verify update + data = dbClient.read(String.format(READ_ALL_FORMAT, TABLE1_NAME)); + try { + assertEquals(1, countRows(data)); + assertTrue(data.next()); + assertEquals(10, data.getInt(1)); + assertEquals("hello2", data.getString(2)); + assertEquals(true, data.getBoolean(3)); + } catch (SQLException e) { + e.printStackTrace(); + fail("unable to verify write"); + } + //delete entry + success = false; + try { + success = dbClient.delete(String.format(TABLE1_DELETE_ENTRY_FORMAT, 10)); + } catch (SQLException e) { + e.printStackTrace(); + } + assertTrue("failed to delete data", success); + //verify delete + data = dbClient.read(String.format(READ_ALL_FORMAT, TABLE1_NAME)); + try { + assertEquals(0, data.getFetchSize()); + } catch (SQLException e) { + e.printStackTrace(); + fail("unable to verify delete. size>0"); + } + } + + @Test + public void testDBVersion() { + DatabaseVersion version = null; + try { + version = dbClient.readActualVersion(); + } catch (SQLException | ParseException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + assertTrue(version.getMajor() >= 10); + } + + @Test + public void testTableStuff() { + + boolean success; + //create Tables/Views + success = dbClient.createTable(TABLE2_NAME, TABLE2_MAPPING); + assertTrue(success); + success = dbClient.createTable(TABLE3_NAME, TABLE3_MAPPING); + assertTrue(success); + try { + success = dbClient.createView(TABLE2_NAME, VIEW2_NAME); + assertTrue(success); + success = dbClient.createView(TABLE3_NAME, VIEW3_NAME); + assertTrue(success); + } catch (SQLException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + //read Tables + IndicesEntryList tables = dbClient.readTables(); + assertTrue(tables.stream().filter(t -> t.getName().equals(TABLE2_NAME)).count() == 1); + assertTrue(tables.stream().filter(t -> t.getName().equals(TABLE3_NAME)).count() == 1); + AliasesEntryList views = dbClient.readViews(DBNAME); + assertTrue(views.stream().filter(t -> t.getIndex().equals(TABLE2_NAME) && t.getAlias().equals(VIEW2_NAME)) + .count() == 1); + assertTrue(views.stream().filter(t -> t.getIndex().equals(TABLE3_NAME) && t.getAlias().equals(VIEW3_NAME)) + .count() == 1); + + //delete Tables/Views + try { + success = dbClient.deleteView(VIEW2_NAME); + assertTrue(success); + success = dbClient.deleteView(VIEW3_NAME); + assertTrue(success); + success = dbClient.deleteTable(TABLE2_NAME); + assertTrue(success); + success = dbClient.deleteTable(TABLE3_NAME); + assertTrue(success); + } catch (SQLException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + //verify + tables = dbClient.readTables(); + assertTrue(tables.stream().filter(t->t.getName().equals(TABLE2_NAME)).count()==0); + assertTrue(tables.stream().filter(t->t.getName().equals(TABLE3_NAME)).count()==0); + views = dbClient.readViews(DBNAME); + assertEquals(0,views.size()); + } + + public static int countRows(ResultSet data) throws SQLException { + int rows = 0; + while (data.next()) { + rows++; + } + data.beforeFirst(); + return rows; + } +} diff --git a/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestMariaDataProvider.java b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestMariaDataProvider.java new file mode 100644 index 000000000..da7450089 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestMariaDataProvider.java @@ -0,0 +1,563 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import org.json.JSONArray; +import org.json.JSONException; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.DeleteQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test.util.MariaDBTestBase; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper; +import org.opendaylight.netconf.shaded.sshd.common.util.io.IoUtils; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; +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.ConnectionlogBuilder; +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.CreateMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInputBuilder; +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.EntityInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder; +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.NetworkElementConnectionBuilder; +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.NetworkElementDeviceType; +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.ReadConnectionlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListOutputBuilder; +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.ReadFaultcurrentListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadGuiCutThroughEntryOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadInventoryListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMediatorServerListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadStatusOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInput; +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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey; +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; +import ch.vorburger.exec.ManagedProcessException; + +public class TestMariaDataProvider { + + + private static final String NODEID1 = "node1"; + private static final String NODEID2 = "node2"; + private static final String NODEID3 = "node3"; + private static final String PROBLEM1 = "problem1"; + private static final String TIME1 = "2021-05-25T05:12:55.0Z"; + private static final String TIME2 = "2021-05-25T05:12:56.0Z"; + private static final String TIME3 = "2021-05-25T05:12:57.0Z"; + private static final String PROBLEM2 = "problem2"; + private static final String URI1 = "http://localhost:8181"; + private static final String URI2 = "http://localhost:8181"; + private static final String URI3 = "http://localhost:8181"; + private static MariaDBTestBase testBase; + private static SqlDBDataProvider dbProvider; + private static SqlDBClient dbClient; + private static String CONTROLLERID; + + @BeforeClass + public static void init() throws Exception { + + testBase = new MariaDBTestBase(); + dbProvider = testBase.getDbProvider(); + dbProvider.waitForDatabaseReady(30, TimeUnit.SECONDS); + dbClient = testBase.createRawClient(); + MariaDBTestBase.testCreateTableStructure(dbClient); + dbProvider.setControllerId(); + CONTROLLERID = dbProvider.getControllerId(); + + } + + @AfterClass + public static void close() { + try { + testBase.close(); + } catch (ManagedProcessException e) { + e.printStackTrace(); + } + } + + @Test + public void testFaultcurrent() { + dbProvider.clearFaultsCurrentOfNode(NODEID1); + ReadFaultcurrentListOutputBuilder faultCurrents = + dbProvider.readFaultCurrentList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(0, faultCurrents.getData().size()); + FaultcurrentEntity faultCurrent1 = new FaultcurrentBuilder().setNodeId(NODEID1).setCounter(1).setObjectId("obj") + .setProblem(PROBLEM1).setTimestamp(DateAndTime.getDefaultInstance(TIME1)) + .setSeverity(SeverityType.Major).setId(String.format("%s/%s", NODEID1, PROBLEM1)).build(); + dbProvider.updateFaultCurrent(faultCurrent1); + FaultcurrentEntity faultCurrent2 = new FaultcurrentBuilder().setNodeId(NODEID1).setCounter(1).setObjectId("obj") + .setProblem(PROBLEM2).setTimestamp(DateAndTime.getDefaultInstance(TIME1)) + .setSeverity(SeverityType.Minor).setId(String.format("%s/%s", NODEID1, PROBLEM2)).build(); + dbProvider.updateFaultCurrent(faultCurrent2); + faultCurrents = dbProvider.readFaultCurrentList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(2, faultCurrents.getData().size()); + ReadStatusOutputBuilder status = null; + try { + status = dbProvider.readStatus(); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to read status"); + } + assertEquals(0, status.getData().get(0).getFaults().getCriticals().intValue()); + assertEquals(1, status.getData().get(0).getFaults().getMajors().intValue()); + assertEquals(1, status.getData().get(0).getFaults().getMinors().intValue()); + assertEquals(0, status.getData().get(0).getFaults().getWarnings().intValue()); + + faultCurrent1 = new FaultcurrentBuilder().setNodeId(NODEID1).setCounter(1).setObjectId("obj") + .setProblem(PROBLEM1).setTimestamp(DateAndTime.getDefaultInstance(TIME1)) + .setSeverity(SeverityType.NonAlarmed).setId(String.format("%s/%s", NODEID1, PROBLEM1)).build(); + dbProvider.updateFaultCurrent(faultCurrent1); + faultCurrents = dbProvider.readFaultCurrentList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(1, faultCurrents.getData().size()); + } + + @Test + public void testFaultlog() { + try { + dbClient.delete(new DeleteQuery(Entity.Faultcurrent, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + ReadFaultlogListOutputBuilder faultlogs = dbProvider.readFaultLogList(createInput(1, 20)); + assertEquals(0, faultlogs.getData().size()); + FaultlogEntity fault1 = new FaultlogBuilder().setCounter(1).setNodeId(NODEID1).setObjectId("obj") + .setProblem(PROBLEM1).setSeverity(SeverityType.Major).setSourceType(SourceType.Netconf) + .setTimestamp(DateAndTime.getDefaultInstance(TIME1)).build(); + dbProvider.writeFaultLog(fault1); + FaultlogEntity fault2 = new FaultlogBuilder().setCounter(2).setNodeId(NODEID1).setObjectId("obj") + .setProblem(PROBLEM2).setSeverity(SeverityType.Major).setSourceType(SourceType.Netconf) + .setTimestamp(DateAndTime.getDefaultInstance(TIME1)).build(); + dbProvider.writeFaultLog(fault2); + faultlogs = dbProvider.readFaultLogList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(2, faultlogs.getData().size()); + } + + @Test + public void testConnectionlog() { + try { + dbClient.delete(new DeleteQuery(Entity.Connectionlog, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + ReadConnectionlogListOutputBuilder logs = dbProvider.readConnectionlogList(createInput(1, 20)); + assertEquals(0, logs.getData().size()); + ConnectionlogEntity log1 = new ConnectionlogBuilder().setNodeId(NODEID1) + .setTimestamp(DateAndTime.getDefaultInstance(TIME1)).setStatus(ConnectionLogStatus.Mounted).build(); + dbProvider.writeConnectionLog(log1); + ConnectionlogEntity log2 = new ConnectionlogBuilder().setNodeId(NODEID1) + .setTimestamp(DateAndTime.getDefaultInstance(TIME2)).setStatus(ConnectionLogStatus.Connecting).build(); + dbProvider.writeConnectionLog(log2); + ConnectionlogEntity log3 = new ConnectionlogBuilder().setNodeId(NODEID1) + .setTimestamp(DateAndTime.getDefaultInstance(TIME3)).setStatus(ConnectionLogStatus.Connected).build(); + dbProvider.writeConnectionLog(log3); + logs = dbProvider.readConnectionlogList(createInput(1, 20)); + assertEquals(3, logs.getData().size()); + } + + @Test + public void testEventlog() { + try { + dbClient.delete(new DeleteQuery(Entity.Eventlog, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + ReadEventlogListOutputBuilder logs = null; + try { + logs = dbProvider.readEventlogList(createInput(1, 20)); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + assertEquals(0, logs.getData().size()); + EventlogEntity log1 = new EventlogBuilder().setCounter(1).setNodeId(NODEID1).setObjectId("obj") + .setTimestamp(DateAndTime.getDefaultInstance(TIME1)).setAttributeName("attr").setNewValue("new-value") + .setSourceType(SourceType.Netconf).build(); + dbProvider.writeEventLog(log1); + EventlogEntity log2 = new EventlogBuilder().setCounter(1).setNodeId(NODEID1).setObjectId("obj") + .setTimestamp(DateAndTime.getDefaultInstance(TIME2)).setAttributeName("attr").setNewValue("new-value2") + .setSourceType(SourceType.Netconf).build(); + dbProvider.writeEventLog(log2); + EventlogEntity log3 = new EventlogBuilder().setCounter(1).setNodeId(NODEID1).setObjectId("obj") + .setTimestamp(DateAndTime.getDefaultInstance(TIME3)).setAttributeName("attr").setNewValue("new-value3") + .setSourceType(SourceType.Netconf).build(); + dbProvider.writeEventLog(log3); + try { + logs = dbProvider.readEventlogList(createInput(1, 20)); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + assertEquals(3, logs.getData().size()); + } + + @Test + public void testGuicutthrough() { + try { + dbClient.delete(new DeleteQuery(Entity.Guicutthrough, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + Guicutthrough gc1 = new GuicutthroughBuilder().setName(NODEID1).setWeburi(URI1).build(); + dbProvider.writeGuiCutThroughData(gc1, NODEID1); + Guicutthrough gc2 = new GuicutthroughBuilder().setName(NODEID2).setWeburi(URI2).build(); + dbProvider.writeGuiCutThroughData(gc2, NODEID2); + Guicutthrough gc3 = new GuicutthroughBuilder().setName(NODEID3).setWeburi(URI3).build(); + dbProvider.writeGuiCutThroughData(gc3, NODEID3); + ReadGuiCutThroughEntryOutputBuilder data = dbProvider.readGuiCutThroughEntry(createInput(1, 20)); + assertEquals(3, data.getData().size()); + data = dbProvider.readGuiCutThroughEntry(createInput("name", NODEID1, 1, 20)); + assertEquals(1, data.getData().size()); + + } + + @Test + public void testInventory() { + try { + dbClient.delete(new DeleteQuery(Entity.Inventoryequipment, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + ReadInventoryListOutputBuilder data = dbProvider.readInventoryList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + List list = null; + try { + list = loadListFile("/inventory.json", Inventory.class); + } catch (IOException e) { + e.printStackTrace(); + fail("problem loading inventory data"); + + } + dbProvider.writeInventory(NODEID1, list); + data = dbProvider.readInventoryList(createInput(1, 50)); + assertEquals(22, data.getData().size()); + data = dbProvider.readInventoryList(createInput("tree-level", "0", 1, 50)); + assertEquals(5, data.getData().size()); + } + + @Test + public void testMaintenance() { + try { + dbClient.delete(new DeleteQuery(Entity.Maintenancemode, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing faultlog"); + } + ReadMaintenanceListOutputBuilder data = dbProvider.readMaintenanceList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + CreateMaintenanceInput maint1 = new CreateMaintenanceInputBuilder().setId(NODEID1).setNodeId(NODEID1) + .setActive(true).setDescription("desc").setObjectIdRef("ref").setProblem("problem") + .setStart(DateAndTime.getDefaultInstance(TIME1)).setEnd(DateAndTime.getDefaultInstance(TIME3)).build(); + CreateMaintenanceInput maint2 = new CreateMaintenanceInputBuilder().setId(NODEID2).setNodeId(NODEID2) + .setActive(true).setDescription("desc").setObjectIdRef("ref").setProblem("problem2") + .setStart(DateAndTime.getDefaultInstance(TIME1)).setEnd(DateAndTime.getDefaultInstance(TIME3)).build(); + CreateMaintenanceInput maint3 = new CreateMaintenanceInputBuilder().setId(NODEID3).setNodeId(NODEID3) + .setActive(true).setDescription("desc").setObjectIdRef("ref").setProblem("problem3") + .setStart(DateAndTime.getDefaultInstance(TIME1)).setEnd(DateAndTime.getDefaultInstance(TIME3)).build(); + try { + dbProvider.createMaintenance(maint1); + dbProvider.createMaintenance(maint2); + dbProvider.createMaintenance(maint3); + } catch (IOException e) { + e.printStackTrace(); + fail("unable to create maintenance data"); + } + data = dbProvider.readMaintenanceList(createInput(1, 20)); + assertEquals(3, data.getData().size()); + + } + + @Test + public void testMediatorserver() { + try { + dbClient.delete(new DeleteQuery(Entity.MediatorServer, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing mediator server"); + } + ReadMediatorServerListOutputBuilder data = dbProvider.readMediatorServerList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + CreateMediatorServerInput mediator1 = + new CreateMediatorServerInputBuilder().setName("server1").setUrl("http://10.20.30.40:7070").build(); + CreateMediatorServerInput mediator2 = + new CreateMediatorServerInputBuilder().setName("server2").setUrl("http://10.20.30.42:7070").build(); + CreateMediatorServerInput mediator3 = + new CreateMediatorServerInputBuilder().setName("server3").setUrl("http://10.20.30.43:7070").build(); + CreateMediatorServerOutputBuilder output1 = null, output2 = null; + try { + output1 = dbProvider.createMediatorServer(mediator1); + output2 = dbProvider.createMediatorServer(mediator2); + dbProvider.createMediatorServer(mediator3); + } catch (IOException e) { + e.printStackTrace(); + fail("problem creating mediator servers"); + } + data = dbProvider.readMediatorServerList(createInput(1, 20)); + assertEquals(3, data.getData().size()); + UpdateMediatorServerInput update1 = new UpdateMediatorServerInputBuilder().setId(output1.getId()) + .setName("server1").setUrl("http://10.20.30.40:7071").build(); + try { + dbProvider.updateMediatorServer(update1); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to update mediator server"); + } + data = dbProvider.readMediatorServerList(createInput("id", output1.getId(), 1, 20)); + assertEquals(1, data.getData().size()); + assertEquals(output1.getId(), data.getData().get(0).getId()); + assertEquals("server1", data.getData().get(0).getName()); + assertEquals("http://10.20.30.40:7071", data.getData().get(0).getUrl()); + + DeleteMediatorServerInput delete2 = new DeleteMediatorServerInputBuilder().setId(output2.getId()).build(); + try { + dbProvider.deleteMediatorServer(delete2); + } catch (IOException e) { + e.printStackTrace(); + fail("unable to delete mediator server"); + } + data = dbProvider.readMediatorServerList(createInput("id", output2.getId(), 1, 20)); + assertEquals(0, data.getData().size()); + data = dbProvider.readMediatorServerList(createInput(1, 20)); + assertEquals(2, data.getData().size()); + } + + @Test + public void testNeConnection() { + try { + dbClient.delete(new DeleteQuery(Entity.NetworkelementConnection, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing neconnection"); + } + ReadNetworkElementConnectionListOutputBuilder data = + dbProvider.readNetworkElementConnectionList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + NetworkElementConnectionEntity ne1 = new NetworkElementConnectionBuilder().setNodeId(NODEID1) + .setHost("10.20.30.50").setPort(Uint32.valueOf(8300)).setIsRequired(true).setUsername("user") + .setPassword("passwd").build(); + NetworkElementConnectionEntity ne2 = new NetworkElementConnectionBuilder().setNodeId(NODEID2) + .setHost("10.20.30.55").setPort(Uint32.valueOf(8300)).setIsRequired(false).setUsername("user") + .setPassword("passwd").setStatus(ConnectionLogStatus.Connecting).build(); + try { + dbProvider.createNetworkElementConnection(ne1); + dbProvider.createNetworkElementConnection(ne2); + } catch (IOException e) { + e.printStackTrace(); + fail("problem creating neconnection"); + } + data = dbProvider.readNetworkElementConnectionList(createInput(1, 20)); + assertEquals(2, data.getData().size()); + NetworkElementConnectionEntity update1 = new NetworkElementConnectionBuilder() + .setStatus(ConnectionLogStatus.Connected).setDeviceType(NetworkElementDeviceType.ORAN).build(); + dbProvider.updateNetworkConnectionDeviceType(update1, NODEID1); + data = dbProvider.readNetworkElementConnectionList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(1, data.getData().size()); + assertEquals(NetworkElementDeviceType.ORAN, data.getData().get(0).getDeviceType()); + assertEquals(true, data.getData().get(0).isIsRequired()); + UpdateNetworkElementConnectionInput update2 = new UpdateNetworkElementConnectionInputBuilder().setId(NODEID2) + .setHost("10.20.55.44").setIsRequired(true).build(); + try { + dbProvider.updateNetworkElementConnection(update2); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to update neconnection"); + } + data = dbProvider.readNetworkElementConnectionList(createInput("node-id", NODEID2, 1, 20)); + assertEquals(1, data.getData().size()); + assertEquals("10.20.55.44", data.getData().get(0).getHost()); + assertEquals(true, data.getData().get(0).isIsRequired()); + + ReadStatusOutputBuilder status = null; + try { + status = dbProvider.readStatus(); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to read status"); + } + assertEquals(1, status.getData().get(0).getNetworkElementConnections().getConnected().intValue()); + assertEquals(1, status.getData().get(0).getNetworkElementConnections().getConnecting().intValue()); + assertEquals(0, status.getData().get(0).getNetworkElementConnections().getDisconnected().intValue()); + assertEquals(0, status.getData().get(0).getNetworkElementConnections().getMounted().intValue()); + assertEquals(2, status.getData().get(0).getNetworkElementConnections().getTotal().intValue()); + assertEquals(0, status.getData().get(0).getNetworkElementConnections().getUnableToConnect().intValue()); + assertEquals(0, status.getData().get(0).getNetworkElementConnections().getUndefined().intValue()); + assertEquals(0, status.getData().get(0).getNetworkElementConnections().getUnmounted().intValue()); + + + DeleteNetworkElementConnectionInput delete1 = + new DeleteNetworkElementConnectionInputBuilder().setId(NODEID1).build(); + try { + dbProvider.deleteNetworkElementConnection(delete1); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to delete neconnection"); + } + data = dbProvider.readNetworkElementConnectionList(createInput("node-id", NODEID1, 1, 20)); + assertEquals(0, data.getData().size()); + data = dbProvider.readNetworkElementConnectionList(createInput(1, 20)); + assertEquals(1, data.getData().size()); + } + + @Test + public void testpm15m() { + try { + dbClient.delete(new DeleteQuery(Entity.Historicalperformance15min, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing pmdata15m"); + } + ReadPmdata15mListOutputBuilder data = dbProvider.readPmdata15mList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + List list = null; + try { + list = loadListFile("/pmdata15m.json", PmdataEntity.class); + } catch (JSONException | IOException e) { + e.printStackTrace(); + fail("failed to load pmdata15m"); + } + dbProvider.doWritePerformanceData(list); + data = dbProvider.readPmdata15mList(createInput(1, 20)); + assertEquals(10, data.getData().size()); + ReadPmdata15mLtpListOutputBuilder ltpdata = null; + try { + ltpdata = dbProvider.readPmdata15mLtpList(createInput("node-name","sim12600",1, 20)); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to read pmdata15m ltp list"); + } + assertEquals(3, ltpdata.getData().size()); + ReadPmdata15mDeviceListOutputBuilder devicedata = null; + try { + devicedata = dbProvider.readPmdata15mDeviceList(createInput(1,20)); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to read pmdata15m devices list"); + } + assertEquals(1, devicedata.getData().size()); + } + + @Ignore + @Test + public void testpm24h() { + try { + dbClient.delete(new DeleteQuery(Entity.Historicalperformance24h, null).toSql()); + } catch (SQLException e) { + e.printStackTrace(); + fail("problem clearing pmdata24h"); + } + ReadPmdata24hListOutputBuilder data = dbProvider.readPmdata24hList(createInput(1, 20)); + assertEquals(0, data.getData().size()); + List list = null; + try { + list = loadListFile("/pmdata24h.json", PmdataEntity.class); + } catch (JSONException | IOException e) { + e.printStackTrace(); + fail("failed to load pmdata24h"); + } + dbProvider.doWritePerformanceData(list); + } + + private static EntityInput createInput(int page, int size) { + return createInput(null, null, page, size); + } + + private static List loadListFile(String filename, Class clazz) throws JSONException, IOException { + List list = new ArrayList<>(); + JSONArray a = new JSONArray(loadFile(filename)); + for (int i = 0; i < a.length(); i++) { + list.add(loadData(a.getJSONObject(i).toString(), clazz)); + } + return list; + } + + private static T loadData(String content, Class clazz) throws IOException { + YangToolsMapper mapper = new YangToolsMapper(); + return mapper.readValue(content, clazz); + } + + private static String loadFile(String filename) throws IOException { + return String.join("\n", IoUtils.readAllLines(TestMariaDataProvider.class.getResourceAsStream(filename))); + } + + private static EntityInput createInput(String filter, String filterValue, int page, int size) { + ReadFaultcurrentListInputBuilder builder = new ReadFaultcurrentListInputBuilder().setPagination( + new PaginationBuilder().setPage(Uint64.valueOf(page)).setSize(Uint32.valueOf(size)).build()); + if (filter != null && filterValue != null) { + Filter f = new FilterBuilder().setProperty(filter).setFiltervalue(filterValue).build(); + Map fmap = new HashMap<>(); + fmap.put(f.key(), f); + builder.setFilter(fmap); + } + return builder.build(); + } + +} diff --git a/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestQuerySyntax.java b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestQuerySyntax.java new file mode 100644 index 000000000..758b72f9c --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/TestQuerySyntax.java @@ -0,0 +1,157 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test; + +import static org.junit.Assert.assertTrue; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery; +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.ReadEventlogListInputBuilder; +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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey; + +public class TestQuerySyntax { + + + private static final String TABLENAME1 = "table1"; + private static final String CONTROLLERID = "controllerid1"; + + @Test + public void testTimestampFilter() { + Map filterMap = new HashMap<>(); + Filter filter = new FilterBuilder().setProperty("timestamp").setFiltervalue("2021*").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`>='2021-01-01 00:00:00.000'")); + assertTrue(sql.contains("`timestamp`<'2022-01-01 00:00:00.0'")); + } + + @Test + public void testTimestampFilter2() { + Map filterMap = new HashMap<>(); + Filter filter = new FilterBuilder().setProperty("timestamp").setFiltervalue(">2021").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`>='2022-01-01 00:00:00.0'")); + } + + @Test + public void testTimestampFilter3() { + Map filterMap = new HashMap<>(); + Filter filter = new FilterBuilder().setProperty("timestamp").setFiltervalue(">=2021").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`>='2021-01-01 00:00:00.000'")); + + } + + @Test + public void testTimestampFilter4() { + Map filterMap = new HashMap<>(); + Filter filter = new FilterBuilder().setProperty("timestamp").setFiltervalue("<2021").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`<'2021-01-01 00:00:00.000'")); + } + + @Test + public void testTimestampFilter5() { + Map filterMap = new HashMap<>(); + Filter filter = new FilterBuilder().setProperty("timestamp").setFiltervalue("<=2021").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`<'2022-01-01 00:00:00.0'")); + } + + @Test + public void testTimestampFilter6() { + Map filterMap = new HashMap<>(); + Filter filter = + new FilterBuilder().setProperty("timestamp").setFiltervalue(">=2022-01-01T00:00:00.000Z").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`>='2022-01-01 00:00:00.000'")); + } + + @Test + public void testTimestampFilter8() { + Map filterMap = new HashMap<>(); + Filter filter = + new FilterBuilder().setProperty("timestamp").setFiltervalue(">2022-01-01T00:00:00.000Z").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`>'2022-01-01 00:00:00.000'")); + } + + @Test + public void testTimestampFilter9() { + Map filterMap = new HashMap<>(); + Filter filter = + new FilterBuilder().setProperty("timestamp").setFiltervalue("<2022-01-01T00:00:00.000Z").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`<'2022-01-01 00:00:00.000'")); + } + + @Test + public void testTimestampFilter10() { + Map filterMap = new HashMap<>(); + Filter filter = + new FilterBuilder().setProperty("timestamp").setFiltervalue("<=2022-01-01T00:00:00.000Z").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`<='2022-01-01 00:00:00.000'")); + } + + @Test + public void testTimestampFilter11() { + Map filterMap = new HashMap<>(); + Filter filter = + new FilterBuilder().setProperty("timestamp").setFiltervalue("<=2022-01-01T00:00:00.000222Z").build(); + filterMap.put(filter.key(), filter); + EntityInput input = new ReadEventlogListInputBuilder().setFilter(filterMap).build(); + SelectQuery query = new SelectQuery(TABLENAME1, input, CONTROLLERID); + final String sql = query.toSql(); + assertTrue(sql.contains("`timestamp`<='2022-01-01 00:00:00.000'")); + } +} diff --git a/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/util/MariaDBTestBase.java b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/util/MariaDBTestBase.java new file mode 100644 index 000000000..b065519f8 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/dblib/test/util/MariaDBTestBase.java @@ -0,0 +1,192 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test.util; + +import static org.junit.Assert.fail; +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section.EnvGetter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper.UnableToMapClassException; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +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.MediatorServerEntity; +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.Pmdata15mEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata24hEntity; +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import ch.vorburger.mariadb4j.DBConfigurationBuilder; + +public class MariaDBTestBase { + + private final SqlDBDataProvider dbProvider; + private final DB db; + private SqlDBConfig config; + private static final Map envDefaultValues = initEnvDefaultValues(); + private static final String SDNRDBDATABASETEST = "test"; + + public MariaDBTestBase() throws ManagedProcessException { + this(new Random().nextInt(1000) + 50000); + } + + private static String dbUrl(String host, int port, String dbName) { + return String.format("jdbc:mysql://%s:%d/%s", host, port, dbName); + } + + private static Map initEnvDefaultValues() { + Map defaults = new HashMap<>(); + defaults.put("SDNRDBURL", dbUrl("localhost", 3306, SDNRDBDATABASETEST)); + defaults.put("SDNRDBDATABASE", "test"); + + return defaults; + } + + public MariaDBTestBase(String host, int port) { + this(host, port, SDNRDBDATABASETEST); + } + + public MariaDBTestBase(String host, int port, String dbName) { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBURL", dbUrl(host, port, dbName)); + envDefaultValues.put("SDNRDBUSERNAME", "sdnrdb"); + envDefaultValues.put("SDNRDBPASSWORD", "sdnrdb"); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + this.config = new SqlDBConfig(new ConfigurationFileRepresentation("test.properties")); + this.config.setDbSuffix(""); + this.config.setControllerId("test123"); + this.db = null; + this.dbProvider = new SqlDBDataProvider(config, false); + } + + public MariaDBTestBase(int port) throws ManagedProcessException { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBURL", dbUrl("localhost", port, SDNRDBDATABASETEST)); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + this.config = new SqlDBConfig(new ConfigurationFileRepresentation("test.properties")); + this.config.setDbSuffix(""); + this.config.setControllerId("test123"); + //start db server + this.db = startDatabase(port); + //create db with name sdnrdb + this.dbProvider = new SqlDBDataProvider(config, false); + } + + public void close() throws ManagedProcessException { + if (db != null) { + this.db.stop(); + } + File f = new File("test.properties"); + if(f.exists()) { + f.delete(); + } + } + + public SqlDBDataProvider getDbProvider() { + return dbProvider; + } + + public DB getDb() { + return db; + } + + private static DB startDatabase(int port) throws ManagedProcessException { + // Start MariaDB4j database + DBConfigurationBuilder dbconfig = DBConfigurationBuilder.newBuilder(); + dbconfig.setPort(port); // 0 => autom. detect free port + DB db = DB.newEmbeddedDB(dbconfig.build()); + db.start(); + return db; + } + + public String getDBUrl() { + return envDefaultValues.get("SDNRDBURL"); + } + + public String getDBUsername() { + return envDefaultValues.getOrDefault("SDNRDBUSERNAME", ""); + } + + public String getDBPassword() { + return envDefaultValues.getOrDefault("SDNRDBPASSWORD", ""); + } + + public SqlDBClient createRawClient() { + return new SqlDBClient(this.config.getUrl(), this.config.getUsername(), this.config.getPassword()); + } + + public String getDBName() { + String url = this.getDBUrl(); + return url.substring(url.lastIndexOf("/") + 1); + } + + public static void testCreateTableStructure(SqlDBClient dbService) { + createTableOdl(dbService); + createTable(dbService, ConnectionlogEntity.class, Entity.Connectionlog, true); + createTable(dbService, EventlogEntity.class, Entity.Eventlog, true); + createTable(dbService, FaultcurrentEntity.class, Entity.Faultcurrent, false); + createTable(dbService, FaultlogEntity.class, Entity.Faultlog, true); + createTable(dbService, GuicutthroughEntity.class, Entity.Guicutthrough, false); + createTable(dbService, Pmdata15mEntity.class, Entity.Historicalperformance15min, false); + createTable(dbService, Pmdata24hEntity.class, Entity.Historicalperformance24h, false); + createTable(dbService, InventoryEntity.class, Entity.Inventoryequipment, false); + createTable(dbService, MaintenanceEntity.class, Entity.Maintenancemode, false); + createTable(dbService, MediatorServerEntity.class, Entity.MediatorServer, true); + createTable(dbService, NetworkElementConnectionEntity.class, Entity.NetworkelementConnection, false); + } + + public static boolean createTableOdl(SqlDBClient dbService) { + String createStatement = null; + createStatement = SqlDBMapper.createTableOdl(); + System.out.println(createStatement); + return dbService.createTable(createStatement); + } + + public static boolean createTable(SqlDBClient dbService, Class cls, Entity entity, boolean autoIndex) { + String createStatement = null; + try { + createStatement = SqlDBMapper.createTable(cls, entity, "", autoIndex); + } catch (UnableToMapClassException e) { + fail(e.getMessage()); + } + System.out.println(createStatement); + return dbService.createTable(createStatement); + } +} + diff --git a/sdnr/wt/data-provider/dblib/src/test/resources/inventory.json b/sdnr/wt/data-provider/dblib/src/test/resources/inventory.json new file mode 100644 index 000000000..9a29325dc --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/resources/inventory.json @@ -0,0 +1,381 @@ +[ + { + "description": "WS/p8.module/a2.module#5", + "date": "2013-04-13T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.1.5.5", + "parent-uuid": "CARD-1.1.5.0", + "contained-holder": [ + "SUBRACK-1.55.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "310330015", + "part-type-id": "3EM23141AD01", + "model-identifier": "CRPQABVFAA", + "type-name": "a2.module" + }, + { + "description": "WS/DS1", + "date": "2007-08-27T00:00:00.0Z", + "version": "p1.module", + "node-id": "sim1", + "uuid": "CARD-1.1.7.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "CIT", + "serial": "serial-number-s3s", + "part-type-id": "part-number-s3s", + "model-identifier": "model-id-s3s", + "type-name": "p1.module_A" + }, + { + "description": "MWR#55Ch#1/a2.moduletraff", + "date": "2013-04-13T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.55.1.2", + "parent-uuid": "IDU-1.55.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "310330015", + "part-type-id": "3EM23141AD01", + "model-identifier": "CRPQABVFAA", + "type-name": "a2.module" + }, + { + "description": "MWR#65Ch#1/a2.moduletraff", + "date": "2013-04-13T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.65.1.2", + "parent-uuid": "IDU-1.65.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "310330008", + "part-type-id": "3EM23141AD01", + "model-identifier": "CRPQABVFAA", + "type-name": "a2.module" + }, + { + "description": "WS-8", + "date": "2017-09-09T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "SHELF-1.1.0.0", + "parent-uuid": "network-element", + "contained-holder": [ + "SLOT-1.1.1.0", + "SLOT-1.1.2.0", + "SLOT-1.1.3.0", + "SLOT-1.1.4.0", + "SLOT-1.1.5.0", + "SLOT-1.1.6.0", + "SLOT-1.1.7.0", + "SLOT-1.1.8.0", + "SLOT-1.1.9.0" + ], + "tree-level": 0, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "Serial1", + "part-type-id": "Partnumber", + "model-identifier": "model-id", + "type-name": "WS-8" + }, + { + "description": "WS/CORE-MAIN/a2.module#5", + "date": "2005-11-09T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.1.1.5", + "parent-uuid": "CARD-1.1.1.0", + "contained-holder": [ + "SUBRACK-1.15.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "0003548168", + "part-type-id": "3FE25774AA01", + "model-identifier": "VAUIAEYAAA", + "type-name": "a2.module" + }, + { + "description": "WS/CORE-MAIN/a2.module#8", + "date": "2010-02-05T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.1.1.8", + "parent-uuid": "CARD-1.1.1.0", + "contained-holder": [ + "SUBRACK-1.18.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "01T441601301", + "part-type-id": "1AB376720002", + "model-identifier": "NGI7AMLMAA", + "type-name": "a2.module" + }, + { + "description": "WS/p8.module/a2.module#5", + "date": "2013-04-13T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.1.6.5", + "parent-uuid": "CARD-1.1.6.0", + "contained-holder": [ + "SUBRACK-1.65.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "310330008", + "part-type-id": "3EM23141AD01", + "model-identifier": "CRPQABVFAA", + "type-name": "a2.module" + }, + { + "description": "MWR-hyper Dir#5.6-Ch#1", + "date": "", + "version": "extrem-hyper", + "node-id": "sim1", + "uuid": "ODU-1.56.0.0", + "parent-uuid": "network-element", + "contained-holder": [ + "PORT-1.56.1.2", + "PORT-1.56.1.3", + "PORT-1.56.1.4" + ], + "tree-level": 0, + "manufacturer-identifier": "", + "serial": "", + "part-type-id": "", + "model-identifier": "", + "type-name": "MWR-hyper" + }, + { + "description": "MWR#56Ch#1/a2.moduletraff", + "date": "2017-09-09T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.56.1.2", + "parent-uuid": "ODU-1.56.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "Serial1", + "part-type-id": "Partnumber", + "model-identifier": "model-id", + "type-name": "a2.module" + }, + { + "description": "MWR-ng Dir#6.5-Ch#1", + "date": "2014-01-16T00:00:00.0Z", + "version": "MWR-ng", + "node-id": "sim1", + "uuid": "IDU-1.65.0.0", + "parent-uuid": "network-element", + "contained-holder": [ + "PORT-1.65.1.4", + "PORT-1.65.1.2" + ], + "tree-level": 0, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "WAUZZI", + "part-type-id": "3DB76047BAAA02", + "model-identifier": "model-id-s3s", + "type-name": "MWR-ng" + }, + { + "description": "MWR#55Ch#0/RxDiv", + "date": "2014-01-08T00:00:00.0Z", + "version": "2017", + "node-id": "sim1", + "uuid": "CARD-1.65.1.4", + "parent-uuid": "IDU-1.65.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "Serie2017-13", + "part-type-id": "partNo2017-12", + "model-identifier": "model-id-s3s", + "type-name": "RxDiv" + }, + { + "description": "WS/p8.module", + "date": "2013-11-23T00:00:00.0Z", + "version": "234", + "node-id": "sim1", + "uuid": "CARD-1.1.6.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [ + "PORT-1.1.6.5", + "PORT-1.1.6.7", + "PORT-1.1.6.6", + "PORT-1.1.6.8" + ], + "tree-level": 1, + "manufacturer-identifier": "SAN", + "serial": "serial-number-124", + "part-type-id": "part-number-12", + "model-identifier": "model-id-12", + "type-name": "p8.module" + }, + { + "description": "WS/DS3", + "date": "2008-10-21T00:00:00.0Z", + "version": "unknown", + "node-id": "sim1", + "uuid": "CARD-1.1.8.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "sd-dsa-eqw", + "part-type-id": "unknown", + "model-identifier": "model-id-s3s", + "type-name": "p4.module" + }, + { + "description": "WS/wind", + "date": "2007-02-19T00:00:00.0Z", + "version": "wind", + "node-id": "sim1", + "uuid": "CARD-1.1.9.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "CIT", + "serial": "proto-type", + "part-type-id": "party-yea", + "model-identifier": "model-id-s3s", + "type-name": "wind" + }, + { + "description": "MWR#55Ch#1/RxDiv", + "date": "2014-01-07T00:00:00.0Z", + "version": "2017", + "node-id": "sim1", + "uuid": "CARD-1.55.1.4", + "parent-uuid": "IDU-1.55.0.0", + "contained-holder": [], + "tree-level": 1, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "Serie2017-12", + "part-type-id": "partNo2017-12", + "model-identifier": "model-id-s3s", + "type-name": "RxDiv" + }, + { + "description": "WS/CORE-MAIN/a2.module#7", + "date": "2009-01-19T00:00:00.0Z", + "version": "a2.module-newest", + "node-id": "sim1", + "uuid": "a2.module-1.1.1.7", + "parent-uuid": "CARD-1.1.1.0", + "contained-holder": [ + "SUBRACK-1.17.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "91T403003322", + "part-type-id": "1AB187280031", + "model-identifier": "mod2", + "type-name": "a2.module" + }, + { + "description": "MWR-ng Dir#5.5-Ch#1", + "date": "2014-01-15T00:00:00.0Z", + "version": "MWR-ng", + "node-id": "sim1", + "uuid": "IDU-1.55.0.0", + "parent-uuid": "network-element", + "contained-holder": [ + "PORT-1.55.1.2", + "PORT-1.55.1.4" + ], + "tree-level": 0, + "manufacturer-identifier": "ONF-Wireless-Transport", + "serial": "Serie2017-14", + "part-type-id": "3DB76047BAAA02", + "model-identifier": "model-id-s3s", + "type-name": "MWR-ng" + }, + { + "description": "WS/CORE-MAIN", + "date": "2015-08-17T00:00:00.0Z", + "version": "123", + "node-id": "sim1", + "uuid": "CARD-1.1.1.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [ + "PORT-1.1.1.6", + "PORT-1.1.1.5", + "PORT-1.1.1.8", + "PORT-1.1.1.7" + ], + "tree-level": 1, + "manufacturer-identifier": "SAN", + "serial": "asdf-asdasd-asd", + "part-type-id": "part-number-2", + "model-identifier": "model-id-2", + "type-name": "latest" + }, + { + "description": "WS/p8.module", + "date": "2013-10-21T00:00:00.0Z", + "version": "234", + "node-id": "sim1", + "uuid": "CARD-1.1.5.0", + "parent-uuid": "SHELF-1.1.0.0", + "contained-holder": [ + "PORT-1.1.5.6", + "PORT-1.1.5.5", + "PORT-1.1.5.8", + "PORT-1.1.5.7" + ], + "tree-level": 1, + "manufacturer-identifier": "SAN", + "serial": "africa", + "part-type-id": "part-number-12", + "model-identifier": "model-id-12", + "type-name": "p8.module" + }, + { + "description": "WS/p8.module/a2.module#6", + "date": "", + "version": "", + "node-id": "sim1", + "uuid": "a2.module-1.1.5.6", + "parent-uuid": "CARD-1.1.5.0", + "contained-holder": [ + "SUBRACK-1.56.0.0" + ], + "tree-level": 2, + "manufacturer-identifier": "", + "serial": "", + "part-type-id": "", + "model-identifier": "", + "type-name": "a2.module" + }, + { + "description": "WS/p8.module/a2.module#6", + "date": "", + "version": "", + "node-id": "sim2", + "uuid": "a2.module-1.1.5.6", + "parent-uuid": "network-element", + "contained-holder": [ + "SUBRACK-1.56.0.0" + ], + "tree-level": 0, + "manufacturer-identifier": "", + "serial": "", + "part-type-id": "", + "model-identifier": "", + "type-name": "a2.module" + } +] \ No newline at end of file diff --git a/sdnr/wt/data-provider/dblib/src/test/resources/pmdata15m.json b/sdnr/wt/data-provider/dblib/src/test/resources/pmdata15m.json new file mode 100644 index 000000000..88dd87f55 --- /dev/null +++ b/sdnr/wt/data-provider/dblib/src/test/resources/pmdata15m.json @@ -0,0 +1,532 @@ +[ + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_03", + "time-stamp": "2017-07-04T15:30:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_01", + "time-stamp": "2017-07-04T16:00:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_6", + "time-stamp": "2017-07-04T14:45:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_4", + "time-stamp": "2017-07-04T15:15:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_12", + "time-stamp": "2017-07-04T13:15:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-01", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_15", + "time-stamp": "2017-07-04T12:30:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-03", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_16", + "time-stamp": "2017-07-04T12:15:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-02", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_02", + "time-stamp": "2017-07-04T15:45:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-02", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_11", + "time-stamp": "2017-07-04T13:30:00.0+00:00", + "node-name": "sim12600" + }, + { + "uuid-interface": "LP-MWPS-TTP-02", + "performance-data": { + "time-period": 900, + "es": 0, + "ses": 0, + "cses": 0, + "unavailability": 0, + "tx-level-min": 25, + "tx-level-max": 25, + "tx-level-avg": 25, + "rx-level-min": -41, + "rx-level-max": -41, + "rx-level-avg": -41, + "time2-states": -1, + "time4-states-s": 0, + "time4-states": 0, + "time8-states": 0, + "time16-states-s": -1, + "time16-states": 0, + "time32-states": 0, + "time64-states": 0, + "time128-states": 0, + "time256-states": 900, + "time512-states": -1, + "time512-states-l": -1, + "time1024-states": -1, + "time1024-states-l": -1, + "time2048-states": -1, + "time2048-states-l": -1, + "time4096-states": -1, + "time4096-states-l": -1, + "time8192-states": -1, + "time8192-states-l": -1, + "snir-min": -99, + "snir-max": -99, + "snir-avg": -99, + "xpd-min": -99, + "xpd-max": -99, + "xpd-avg": -99, + "rf-temp-min": -99, + "rf-temp-max": -99, + "rf-temp-avg": -99, + "defect-blocks-sum": -1 + }, + "layer-protocol-name": "MWPS", + "radio-signal-id": "Test11", + "suspect-interval-flag": false, + "granularity-period": "Period15Min", + "scanner-id": "PM_RADIO_15M_13", + "time-stamp": "2017-07-04T13:00:00.0+00:00", + "node-name": "sim12600" + } +] \ No newline at end of file diff --git a/sdnr/wt/data-provider/feature/pom.xml b/sdnr/wt/data-provider/feature/pom.xml index 16d55c997..2ac56666d 100644 --- a/sdnr/wt/data-provider/feature/pom.xml +++ b/sdnr/wt/data-provider/feature/pom.xml @@ -41,6 +41,10 @@ ccsdk-features :: ${project.artifactId} + + com.highstreet-technologies.opendaylight + odl-ready + ${project.groupId} sdnr-wt-data-provider-provider diff --git a/sdnr/wt/data-provider/installer/pom.xml b/sdnr/wt/data-provider/installer/pom.xml index 4eda82cde..1e5a191a8 100755 --- a/sdnr/wt/data-provider/installer/pom.xml +++ b/sdnr/wt/data-provider/installer/pom.xml @@ -69,6 +69,10 @@ ${application.name}-setup ${project.version} + + com.highstreet-technologies.opendaylight + odl-ready + diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/ArchiveCleanProvider.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/ArchiveCleanProvider.java index 2af7324e5..15c857c62 100644 --- a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/ArchiveCleanProvider.java +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/ArchiveCleanProvider.java @@ -23,7 +23,7 @@ public interface ArchiveCleanProvider { /** * Remove all index data older, than specified from faultlog and eventlog - * + * * @param olderAreOutdated time to specify older elements * @return number of removed elements */ @@ -31,11 +31,11 @@ public interface ArchiveCleanProvider { /** * Get number of elements older then - * + * * @param olderAreOutdated date to compare with and specify older elements * @return number of elements */ - public int getNumberOfOldObjects(Date olderAreOutdated); + public long getNumberOfOldObjects(Date olderAreOutdated); } diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/DataProvider.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/DataProvider.java index 0de5dc246..128138ee3 100644 --- a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/DataProvider.java +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/DataProvider.java @@ -17,7 +17,6 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.model; -import java.util.Date; import java.util.List; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionlogEntity; @@ -79,6 +78,13 @@ public interface DataProvider extends ArchiveCleanProvider { */ void writeGuiCutThroughData(Guicutthrough gcData, String nodeId); + /** + * + * @param nodeName + * @return number of entries + */ + public int clearGuiCutThroughEntriesOfNode(String nodeName); + /** * * @param networkElementConnectionEntitiy to wirte to DB @@ -99,12 +105,6 @@ public interface DataProvider extends ArchiveCleanProvider { void removeNetworkConnection(String nodeId); - @Override - int doIndexClean(Date olderAreOutdated); - - @Override - int getNumberOfOldObjects(Date olderAreOutdated); - List getNetworkElementConnections(); /** diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/HtDatabaseMediatorserver.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/HtDatabaseMediatorserver.java new file mode 100644 index 000000000..2fc62da01 --- /dev/null +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/HtDatabaseMediatorserver.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.model; + +import java.util.List; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.MediatorServerEntity; + +public interface HtDatabaseMediatorserver { + + List getAll(); +} diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEntityDataProvider.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEntityDataProvider.java index 2a91d7816..04239f4c4 100644 --- a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEntityDataProvider.java +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEntityDataProvider.java @@ -29,9 +29,6 @@ public interface IEntityDataProvider { public HtUserdataManager getHtDatabaseUserManager(); - /** Set overall ready status from outside of this data-provider **/ - public void setReadyStatus(boolean status); - /** Set some static status information after startup */ public void setStatus(StatusKey key, String value); diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEsConfig.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEsConfig.java index 0f8f5e37d..18e07f148 100644 --- a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEsConfig.java +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/IEsConfig.java @@ -33,6 +33,8 @@ public interface IEsConfig { */ long getArchiveLifetimeSeconds(); + boolean doFullsizeRequests(); + /** * @param archiveCleanService */ @@ -44,5 +46,4 @@ public interface IEsConfig { void registerConfigChangedListener(IConfigChangedListener archiveCleanService); - } diff --git a/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/SdnrDbType.java b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/SdnrDbType.java new file mode 100644 index 000000000..80efe6fb3 --- /dev/null +++ b/sdnr/wt/data-provider/model/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/model/SdnrDbType.java @@ -0,0 +1,26 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.model; + +public enum SdnrDbType { + ELASTICSEARCH, MARIADB +} diff --git a/sdnr/wt/data-provider/model/src/main/yang/data-provider@2020-11-10.yang b/sdnr/wt/data-provider/model/src/main/yang/data-provider@2020-11-10.yang index 84c43d52b..31c7fd875 100644 --- a/sdnr/wt/data-provider/model/src/main/yang/data-provider@2020-11-10.yang +++ b/sdnr/wt/data-provider/model/src/main/yang/data-provider@2020-11-10.yang @@ -1313,6 +1313,50 @@ module data-provider { description "An object containing the number of current faults per its severity."; } + container network-element-connections { + leaf total { + type uint32; + description + "The number of all network-elements."; + } + leaf Mounted { + type uint32; + description + "The number of network-elements with state 'Mounted'."; + } + leaf Unmounted { + type uint32; + description + "The number of network-elements with state 'Unmounted'."; + } + leaf Connecting { + type uint32; + description + "The number of network-elements with state 'Connecting'."; + } + leaf Connected { + type uint32; + description + "The number of network-elements with state 'Connected'."; + } + leaf UnableToConnect { + type uint32; + description + "The number of network-elements with state 'UnableToConnect'."; + } + leaf Disconnected { + type uint32; + description + "The number of network-elements with state 'Disconnected'."; + } + leaf Undefined { + type uint32; + description + "The number of network-elements with state 'Undefined'."; + } + description + "An object containing the number of network-elements per its connection-state."; + } description "An object class defining an status entity for current faults (also called active alarms)."; diff --git a/sdnr/wt/data-provider/pom.xml b/sdnr/wt/data-provider/pom.xml index 71c745144..786cec714 100755 --- a/sdnr/wt/data-provider/pom.xml +++ b/sdnr/wt/data-provider/pom.xml @@ -43,6 +43,7 @@ setup model + dblib provider feature installer diff --git a/sdnr/wt/data-provider/provider/pom.xml b/sdnr/wt/data-provider/provider/pom.xml index 55a770564..f0da429ac 100644 --- a/sdnr/wt/data-provider/provider/pom.xml +++ b/sdnr/wt/data-provider/provider/pom.xml @@ -83,6 +83,11 @@ sdnr-wt-data-provider-model ${project.version} + + ${project.groupId} + sdnr-wt-data-provider-dblib + ${project.version} + javax.servlet javax.servlet-api @@ -105,11 +110,6 @@ org.apache.karaf.shell.core provided - - - - - org.opendaylight.netconf sal-netconf-connector @@ -135,6 +135,13 @@ json provided + + + ch.vorburger.mariaDB4j + mariaDB4j + test + + @@ -204,6 +211,18 @@ + + + + + + + + + + + + org.apache.maven.plugins maven-surefire-plugin diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/ElasticSearchDataProvider.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/ElasticSearchDataProvider.java deleted file mode 100644 index 0a966aef6..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/ElasticSearchDataProvider.java +++ /dev/null @@ -1,455 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorPm; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorPm.Intervall; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorStatus; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorWithId; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity.HtDatabaseEventsService; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity.HtDatabaseMaintenanceService; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryResult; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInput; -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.CreateMediatorServerInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateNetworkElementConnectionOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionOutputBuilder; -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.EntityInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; -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.MediatorServerEntity; -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.ReadConnectionlogListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultcurrentListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultlogListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadGuiCutThroughEntryOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadInventoryListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMediatorServerListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mDeviceListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mLtpListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hDeviceListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hLtpListOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadStatusOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionOutputBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ElasticSearchDataProvider { - - private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchDataProvider.class); - - private static final String EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE = "unable to write data to database"; - private static final String EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE = "unable to update data in database"; - private static final String EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE = "unable to remove data from database"; - - private final HtDatabaseClient dbClient; - private final DataObjectAcessorWithId eventRWFaultCurrent; - private final DataObjectAcessorWithId eventRWFaultLog; - private final DataObjectAcessorWithId mediatorserverRW; - private final DataObjectAcessorWithId maintenanceRW; - private final DataObjectAcessorWithId guicutthroughRW; - private final DataObjectAcessorWithId equipmentRW; - private final DataObjectAcessorWithId connnectionlogRW; - private final DataObjectAcessorWithId eventlogRW; - private final DataObjectAcessorWithId networkelementConnectionRW; - private final DataObjectAcessorPm pm15mRW; - private final DataObjectAcessorPm pm24hRW; - - private final DataObjectAcessorStatus readStatus; - private final HtDatabaseEventsService databaseService; - private final HtDatabaseMaintenanceService databaseMaintenanceService; - - public HtDatabaseClient getRawClient() { - return this.dbClient; - } - - public ElasticSearchDataProvider(HostInfo[] hosts) throws Exception { - this(hosts, null, null, HtDatabaseClient.TRUSTALL_DEFAULT); - } - - public ElasticSearchDataProvider(HostInfo[] hosts, String authUsername, String authPassword, boolean trustAllCerts) - throws Exception { - super(); - LOG.info("Start {}", this.getClass().getName()); - - - this.dbClient = HtDatabaseClient.getClient(hosts, authUsername, authPassword, trustAllCerts); - this.mediatorserverRW = new DataObjectAcessorWithId<>(dbClient, Entity.MediatorServer, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.Data.class); - this.mediatorserverRW.setWriteInterface(MediatorServerEntity.class); - - this.maintenanceRW = new DataObjectAcessorWithId<>(dbClient, Entity.Maintenancemode, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.Data.class); - this.maintenanceRW.setWriteInterface(MaintenanceEntity.class); - - this.guicutthroughRW = new DataObjectAcessorWithId<>(dbClient, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity.Guicutthrough, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.Data.class); - this.guicutthroughRW.setWriteInterface(Guicutthrough.class); - - this.equipmentRW = new DataObjectAcessorWithId<>(dbClient, Entity.Inventoryequipment, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.Data.class); - - this.eventRWFaultCurrent = new DataObjectAcessorWithId<>(dbClient, Entity.Faultcurrent, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.Data.class); - - this.eventRWFaultLog = new DataObjectAcessorWithId<>(dbClient, Entity.Faultlog, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.Data.class); - - this.connnectionlogRW = new DataObjectAcessorWithId<>(dbClient, Entity.Connectionlog, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.Data.class); - - this.eventlogRW = new DataObjectAcessorWithId<>(dbClient, Entity.Eventlog, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.Data.class); - - this.networkelementConnectionRW = new DataObjectAcessorWithId<>(dbClient, Entity.NetworkelementConnection, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.Data.class); - this.networkelementConnectionRW.setWriteInterface(NetworkElementConnectionEntity.class); - - this.pm15mRW = new DataObjectAcessorPm<>(dbClient, Intervall.PMDATA15M, Entity.Historicalperformance15min, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.Data.class); - - this.pm24hRW = new DataObjectAcessorPm<>(dbClient, Intervall.PMDATA24H, Entity.Historicalperformance24h, - org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.Data.class); - - this.readStatus = new DataObjectAcessorStatus(dbClient, Entity.Faultcurrent); - - this.databaseService = new HtDatabaseEventsService(dbClient, this); - this.databaseMaintenanceService = new HtDatabaseMaintenanceService(dbClient); - } - - /*------------------------- - * Provide access to model API - */ - - public ReadFaultcurrentListOutputBuilder readFaultCurrentList(EntityInput input) { - - ReadFaultcurrentListOutputBuilder outputBuilder = new ReadFaultcurrentListOutputBuilder(); - - QueryResult result = - this.eventRWFaultCurrent.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadFaultlogListOutputBuilder readFaultLogList(EntityInput input) { - ReadFaultlogListOutputBuilder outputBuilder = new ReadFaultlogListOutputBuilder(); - QueryResult result = - this.eventRWFaultLog.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadMaintenanceListOutputBuilder readMaintenanceList(EntityInput input) { - ReadMaintenanceListOutputBuilder outputBuilder = new ReadMaintenanceListOutputBuilder(); - QueryResult result = - this.maintenanceRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadMediatorServerListOutputBuilder readMediatorServerList(EntityInput input) { - - ReadMediatorServerListOutputBuilder outputBuilder = new ReadMediatorServerListOutputBuilder(); - QueryResult result = - this.mediatorserverRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadNetworkElementConnectionListOutputBuilder readNetworkElementConnectionList(EntityInput input) { - ReadNetworkElementConnectionListOutputBuilder outputBuilder = - new ReadNetworkElementConnectionListOutputBuilder(); - QueryResult result = - this.networkelementConnectionRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadInventoryListOutputBuilder readInventoryList(EntityInput input) { - ReadInventoryListOutputBuilder outputBuilder = new ReadInventoryListOutputBuilder(); - QueryResult result = - this.equipmentRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadConnectionlogListOutputBuilder readConnectionlogList(EntityInput input) { - ReadConnectionlogListOutputBuilder outputBuilder = new ReadConnectionlogListOutputBuilder(); - QueryResult result = - this.connnectionlogRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadEventlogListOutputBuilder readEventlogList(ReadEventlogListInput input) throws IOException { - ReadEventlogListOutputBuilder outputBuilder = new ReadEventlogListOutputBuilder(); - QueryResult result = - this.eventlogRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadPmdata15mListOutputBuilder readPmdata15mList(EntityInput input) { - ReadPmdata15mListOutputBuilder outputBuilder = new ReadPmdata15mListOutputBuilder(); - QueryResult result = - this.pm15mRW.getData(input); - LOG.debug("Read data: readPmdata15mList: {}", result); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadPmdata24hListOutputBuilder readPmdata24hList(EntityInput input) { - ReadPmdata24hListOutputBuilder outputBuilder = new ReadPmdata24hListOutputBuilder(); - QueryResult result = - this.pm24hRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.PaginationBuilder( - result.getPagination()).build()); - return outputBuilder; - } - - public ReadPmdata15mLtpListOutputBuilder readPmdata15mLtpList(EntityInput input) throws IOException { - ReadPmdata15mLtpListOutputBuilder outputBuilder = new ReadPmdata15mLtpListOutputBuilder(); - QueryResult result = pm15mRW.getDataLtpList(input); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.ltp.list.output.PaginationBuilder( - result.getPagination()).build()); - outputBuilder.setData(result.getResult().getHits()); - return outputBuilder; - } - - public ReadPmdata15mDeviceListOutputBuilder readPmdata15mDeviceList(EntityInput input) throws IOException { - ReadPmdata15mDeviceListOutputBuilder outputBuilder = new ReadPmdata15mDeviceListOutputBuilder(); - QueryResult result = pm15mRW.getDataDeviceList(input); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.device.list.output.PaginationBuilder( - result.getPagination()).build()); - outputBuilder.setData(result.getResult().getHits()); - return outputBuilder; - } - - public ReadPmdata24hLtpListOutputBuilder readPmdata24hLtpList(EntityInput input) throws IOException { - - QueryResult result = pm24hRW.getDataLtpList(input); - - ReadPmdata24hLtpListOutputBuilder outputBuilder = new ReadPmdata24hLtpListOutputBuilder(); - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder(); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder( - result.getPagination()).build()); - outputBuilder.setData(result.getResult().getHits()); - return outputBuilder; - } - - public ReadPmdata24hDeviceListOutputBuilder readPmdata24hDeviceList(EntityInput input) throws IOException { - - QueryResult result = pm24hRW.getDataDeviceList(input); - - ReadPmdata24hDeviceListOutputBuilder outputBuilder = new ReadPmdata24hDeviceListOutputBuilder(); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.device.list.output.PaginationBuilder( - result.getPagination()).build()); - outputBuilder.setData(result.getResult().getHits()); - return outputBuilder; - } - - public ReadStatusOutputBuilder readStatus() throws IOException { - QueryResult result = - readStatus.getDataStatus(); - - ReadStatusOutputBuilder outputBuilder = new ReadStatusOutputBuilder(); - outputBuilder.setData(result.getResult().getHits()); - return outputBuilder; - } - - public boolean waitForYellowDatabaseStatus(long timeout, TimeUnit unit) { - return this.dbClient.waitForYellowStatus(unit.toMillis(timeout)); - } - - public CreateNetworkElementConnectionOutputBuilder createNetworkElementConnection( - NetworkElementConnectionEntity input) throws IOException { - String id = this.networkelementConnectionRW.update(input, input.getNodeId()); - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); - } - CreateNetworkElementConnectionOutputBuilder builder = new CreateNetworkElementConnectionOutputBuilder(); - builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) - .setUsername(input.getUsername()).setPassword(input.getPassword()).setIsRequired(input.isIsRequired()) - .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); - return builder; - } - - public UpdateNetworkElementConnectionOutputBuilder updateNetworkElementConnection( - UpdateNetworkElementConnectionInput input) throws IOException { - String id = this.networkelementConnectionRW.update(input, input.getId()); - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); - } - UpdateNetworkElementConnectionOutputBuilder builder = new UpdateNetworkElementConnectionOutputBuilder(); - builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) - .setUsername(input.getUsername()).setPassword(input.getPassword()) - .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); - return builder; - } - - public DeleteNetworkElementConnectionOutputBuilder deleteNetworkElementConnection( - DeleteNetworkElementConnectionInput input) throws IOException { - boolean removed = this.networkelementConnectionRW.remove(input.getId()); - if (!removed) { - throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); - } - return new DeleteNetworkElementConnectionOutputBuilder(); - } - - public DeleteMediatorServerOutputBuilder deleteMediatorServer(DeleteMediatorServerInput input) throws IOException { - boolean removed = this.mediatorserverRW.remove(input.getId()); - if (!removed) { - throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); - } - return new DeleteMediatorServerOutputBuilder(); - } - - public DeleteMaintenanceOutputBuilder deleteMaintenance(DeleteMaintenanceInput input) throws IOException { - boolean removed = this.maintenanceRW.remove(input.getId()); - if (!removed) { - throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); - } - return new DeleteMaintenanceOutputBuilder(); - } - - public UpdateMaintenanceOutputBuilder updateMaintenance(UpdateMaintenanceInput input) throws IOException { - if (input.getId() == null) { - throw new IOException("please give the id for updating entry"); - } - String id = this.maintenanceRW.update(input, input.getId()); - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); - } - UpdateMaintenanceOutputBuilder builder = new UpdateMaintenanceOutputBuilder(input).setId(id); - return builder; - } - - public UpdateMediatorServerOutputBuilder updateMediatorServer(UpdateMediatorServerInput input) throws IOException { - if (input.getId() == null) { - throw new IOException("please give the id for updating entry"); - } - String id = this.mediatorserverRW.update(input, input.getId()); - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); - } - UpdateMediatorServerOutputBuilder builder = new UpdateMediatorServerOutputBuilder(); - builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); - return builder; - } - - public CreateMaintenanceOutputBuilder createMaintenance(CreateMaintenanceInput input) throws IOException { - String id = this.maintenanceRW.write(input, input.getNodeId()); - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); - } - CreateMaintenanceOutputBuilder builder = new CreateMaintenanceOutputBuilder(input).setId(id); - return builder; - } - - public CreateMediatorServerOutputBuilder createMediatorServer(CreateMediatorServerInput input) throws IOException { - String id = this.mediatorserverRW.write(input, null); - - if (id == null) { - throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); - } - CreateMediatorServerOutputBuilder builder = new CreateMediatorServerOutputBuilder(); - builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); - return builder; - } - - public ReadGuiCutThroughEntryOutputBuilder readGuiCutThroughEntry(EntityInput input) { - ReadGuiCutThroughEntryOutputBuilder outputBuilder = new ReadGuiCutThroughEntryOutputBuilder(); - QueryResult result = - this.guicutthroughRW.getData(input); - outputBuilder.setData(result.getResult().getHits()); - outputBuilder.setPagination( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.PaginationBuilder( - ).build()); - return outputBuilder; - } - - public DataProvider getDataProvider() { - return this.databaseService; - } - - public HtDatabaseMaintenance getHtDatabaseMaintenance() { - return this.databaseMaintenanceService; - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/HtUserdataManagerImpl.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/HtUserdataManagerImpl.java deleted file mode 100644 index 73cc1a212..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/HtUserdataManagerImpl.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import org.json.JSONException; -import org.json.JSONObject; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class HtUserdataManagerImpl implements HtUserdataManager { - - private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerImpl.class); - - private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json"; - private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults(); - - private static JSONObject loadDefaults() { - File f = new File(USERDATA_DEFAULTS_FILENAME); - String content; - JSONObject o = null; - if (f.exists()) { - try { - content = Files.readString(f.toPath()); - o = new JSONObject(content); - } catch (IOException e) { - LOG.warn("problem loading defaults: ", e); - } catch (JSONException e) { - LOG.warn("problem parsing defaults: ", e); - } - } - return o; - } - - - private final HtDatabaseClient dbClient; - - public HtUserdataManagerImpl(HtDatabaseClient rawClient) { - this.dbClient = rawClient; - } - - @Override - public String getUserdata(String username) { - SearchResult result = this.dbClient.doReadByQueryJsonData(Entity.Userdata.getName(), - QueryBuilders.matchQuery("_id", username)); - String json = result.getHits().size() > 0 ? result.getHits().get(0).getSourceAsString() : "{}"; - if (USERDATA_DEFAULTS_CONTENT != null) { - JSONObject merge = mergeData(new JSONObject(json), USERDATA_DEFAULTS_CONTENT); - json = merge.toString(); - } - return json; - } - - @Override - public String getUserdata(String username, String key) { - JSONObject o = new JSONObject(this.getUserdata(username)); - return o.has(key) ? o.get(key).toString() : "{}"; - } - - @Override - public boolean setUserdata(String username, String data) { - JSONObject o = new JSONObject(this.getUserdata(username)); - JSONObject merge = mergeData(o, new JSONObject(data)); - return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, merge.toString()) != null; - } - - @Override - public boolean setUserdata(String username, String key, String data) { - JSONObject o = new JSONObject(this.getUserdata(username)); - o = mergeData(o, key, new JSONObject(data)); - return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, o.toString()) != null; - } - - @Override - public boolean removeUserdata(String username) { - return this.dbClient.doRemove(Entity.Userdata.getName(), username); - } - - @Override - public boolean removeUserdata(String username, String key) { - JSONObject o = new JSONObject(this.getUserdata(username)); - if (o.has(key)) { - o.remove(key); - return this.setUserdata(username, o.toString()); - } - return true; - } - - private static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) { - if (!o.has(key)) { - o.put(key, subObject); - } else { - JSONObject tmp = new JSONObject(); - tmp.put(key, subObject); - o = mergeData(tmp, o); - } - return o; - } - - private static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException { - String[] keys = JSONObject.getNames(source); - if (keys == null) { - return target; - } - for (String key : keys) { - Object value = source.get(key); - if (!target.has(key)) { - // new value for "key": - target.put(key, value); - } else { - // existing value for "key" - recursively deep merge: - if (value instanceof JSONObject) { - JSONObject valueJson = (JSONObject) value; - mergeData(valueJson, target.getJSONObject(key)); - } else { - target.put(key, value); - } - } - } - return target; - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/MediatorServerDataProvider.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/MediatorServerDataProvider.java deleted file mode 100644 index c4fe672cd..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/MediatorServerDataProvider.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorWithId; -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.read.mediator.server.list.output.Data; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MediatorServerDataProvider implements AutoCloseable { - - private static final Logger LOG = LoggerFactory.getLogger(MediatorServerDataProvider.class); - - private final HtDatabaseClient dbClient; - private final DataObjectAcessorWithId mediatorserverRW; - private final int REFRESH_INTERVAL = 60; - private final Map entries; - private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); - private boolean isRunning; - - public MediatorServerDataProvider(HostInfo[] hosts) throws Exception { - this(hosts, null, null, false); - } - - public MediatorServerDataProvider(HostInfo[] hosts, String authUsername, String authPassword, boolean trustAll) throws Exception { - super(); - LOG.info("Start {}", this.getClass().getName()); - this.entries = new HashMap<>(); - this.dbClient = HtDatabaseClient.getClient(hosts, authUsername, authPassword, trustAll); - this.mediatorserverRW = new DataObjectAcessorWithId<>(dbClient, Entity.MediatorServer, Data.class); - this.scheduler.scheduleAtFixedRate(onTick, this.REFRESH_INTERVAL, this.REFRESH_INTERVAL, TimeUnit.SECONDS); - } - - private final Runnable onTick = new Runnable() { - - @Override - public void run() { - isRunning = true; - runIt(); - isRunning = false; - } - - }; - - private void runIt() { - SearchResult result = MediatorServerDataProvider.this.mediatorserverRW.doReadAll(); - List data = result.getHits(); - for (Data item : data) { - MediatorServerDataProvider.this.entries.put(item.getId(), item); - } - } - - /** - * - * @param dbServerId - * @return url or null if not exists - */ - public String getHostUrl(String dbServerId) { - Data info = this.entries.getOrDefault(dbServerId, null); - return info == null ? null : info.getUrl(); - } - - public boolean triggerReloadSync() { - if (!isRunning) { - runIt(); - } - return true; - } - - @Override - public void close() throws Exception { - this.scheduler.shutdown(); - } -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessor.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessor.java deleted file mode 100644 index 0cd327bfc..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessor.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor; - -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryByFilter; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryResult; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.EsDataObjectReaderWriter2; -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.EntityInput; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DataObjectAcessor extends EsDataObjectReaderWriter2 { - - private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessor.class); - - DataObjectAcessor(HtDatabaseClient dbClient, Entity entity, Class clazz) throws ClassNotFoundException { - super(dbClient, entity, clazz); - LOG.info("Create {}", this.getClass().getName()); - } - - public QueryResult getData(EntityInput input) { - - QueryByFilter queryByFilter = new QueryByFilter(input); - QueryBuilder queryBuilder = queryByFilter.getQueryBuilderByFilter(); - // Exception handling during user input for invalid filter input. - // This wrong filter by user is allowed an results into empty data. - boolean ignoreException = queryBuilder.contains("range"); - - LOG.info("Request: {} filter {} ignoreException{}:", this.getDataTypeName(), queryByFilter, ignoreException); - - SearchResult result = doReadAll(queryBuilder, ignoreException); - return new QueryResult<>(queryByFilter, result); - } - - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorPm.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorPm.java deleted file mode 100644 index 9280a0602..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorPm.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor; - -import java.io.IOException; - -import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryByFilter; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryResult; -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.EntityInput; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DataObjectAcessorPm extends DataObjectAcessor { - - private final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); - - private static final String UUID_KEY = "uuid-interface"; - private static final String NODE_KEY = "node-name"; - private static final String KEY = "node-name"; - - - public enum Intervall { - PMDATA15M("historicalperformance15min", "historicalperformance15min"), PMDATA24H("historicalperformance24h", - "historicalperformance24h"); - - String index; - String type; - - Intervall(String index, String type) { - this.index = index; - this.type = type; - } - - public String getIndex() { - return index; - } - - public String getType() { - return type; - } - } - - private ExtRestClient dbClient; - private Intervall mode; - - public DataObjectAcessorPm(HtDatabaseClient dbClient, Intervall mode, Entity entity, Class clazz) - throws ClassNotFoundException { - super(dbClient, entity, clazz); - LOG.info("DataObjectAcessorPm"); - this.dbClient = dbClient; - this.mode = mode; - } - - /** - * get aggregated list of ltps for filter NODE_KEY - * - * @param input - * @return - * @throws IOException - */ - public QueryResult getDataLtpList(EntityInput input) throws IOException { - - QueryByFilter queryByFilter = new QueryByFilter(input); - SearchRequest request = - queryByFilter.getSearchRequestByFilter(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType()); - try { - SearchResponse response = this.dbClient.search(request); - AggregationEntries aggs = response.getAggregations(UUID_KEY); - String[] uuids = - aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex()); - long totalSize = aggs.size(); - return new QueryResult(queryByFilter.getPage(), queryByFilter.getPageSize(), - new SearchResult(uuids, totalSize)); - } catch (IOException e) { - throw new IOException("problem reading ltps for req=" + request, e); - } - } - - /** - * get aggregated devices list - * - * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name') - * @return - * @throws IOException - */ - public QueryResult getDataDeviceList(EntityInput input) throws IOException { - - QueryByFilter queryByFilter = new QueryByFilter(input); - SearchRequest request = - queryByFilter.getSearchRequestBySortOrder(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType()); - try { - SearchResponse response = this.dbClient.search(request); - AggregationEntries aggs = response.getAggregations(KEY); - String[] uuids = - aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex()); - long totalSize = aggs.size(); - return new QueryResult(queryByFilter.getPage(), queryByFilter.getPageSize(), - new SearchResult(uuids, totalSize)); - } catch (IOException e) { - throw new IOException("problem reading nodes for req=" + request, e); - } - - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorStatus.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorStatus.java deleted file mode 100644 index efb52213d..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorStatus.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor; - -import java.io.IOException; -import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryResult; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; -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.read.status.output.Data; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.DataBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.FaultsBuilder; - -public class DataObjectAcessorStatus extends DataObjectAcessor { - - final String ESDATATYPE_FAULTCURRENT_SEVERITY_KEY = "severity"; - - private final ExtRestClient dbClient; - private final Entity entity; - - public DataObjectAcessorStatus(HtDatabaseClient dbClient, Entity entity) throws ClassNotFoundException { - super(dbClient, entity, Data.class); - this.dbClient = dbClient; - this.entity = entity; - } - - public QueryResult getDataStatus() throws IOException { - SearchRequest request = getNewInstanceOfSearchRequest(entity); - request.setQuery(QueryBuilders.matchAllQuery().aggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY).size(0)); - SearchResponse response = this.dbClient.search(request); - AggregationEntries aggs = response.getAggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY); - - Data[] data = {new DataBuilder() - .setFaults( - new FaultsBuilder().setCriticals(YangHelper2.getLongOrUint32(aggs.getOrDefault("Critical", 0L))) - .setMajors(YangHelper2.getLongOrUint32(aggs.getOrDefault("Major", 0L))) - .setMinors(YangHelper2.getLongOrUint32(aggs.getOrDefault("Minor", 0L))) - .setWarnings(YangHelper2.getLongOrUint32(aggs.getOrDefault("Warning", 0L))).build()) - .build()}; - long toalsize = data.length; - return new QueryResult(1L, 1L, new SearchResult(data, toalsize)); - - } - - - private static SearchRequest getNewInstanceOfSearchRequest(Entity entity) { - return new SearchRequest(entity.getName(), entity.getName()); - } - - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorWithId.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorWithId.java deleted file mode 100644 index a977890c9..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/acessor/DataObjectAcessorWithId.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor; - -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DataObjectAcessorWithId extends DataObjectAcessor { - - private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorWithId.class); - - public DataObjectAcessorWithId(HtDatabaseClient dbClient, Entity entity, Class clazz) - throws ClassNotFoundException { - super(dbClient, entity, clazz); - setEsIdAttributeName("_id"); - LOG.info("Create {}", this.getClass().getName()); - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/FaultEntityManager.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/FaultEntityManager.java deleted file mode 100644 index f1a43359c..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/FaultEntityManager.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.annotation.Nonnull; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Fault; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; - -public class FaultEntityManager { - - private static final Pattern pattern = Pattern.compile(".*\\[layerProtocol=(.*)\\]"); - - /** - * The leading indication for notification or events that are not in the currentProblem data of the ONF Coremodel - */ - private static final String NOCURRENTPROBLEMINDICATION = "#"; - - /** - * Specific problems are not moving into current problem list - * - * @param problemName to be verified - * @return true if problem is current - */ - public static boolean isManagedAsCurrentProblem(String problemName) { - return !problemName.startsWith(NOCURRENTPROBLEMINDICATION); - } - - public static boolean isManagedAsCurrentProblem(Fault problem) { - return isManagedAsCurrentProblem(problem.getProblem()); - } - - /** - * Specific problems are not moving into current problem list - * - * @param fault to be verified - * @return true if cleared indication - */ - public static boolean isNoAlarmIndication(@Nonnull Fault fault) { - return SeverityType.NonAlarmed.equals(fault.getSeverity()); - } - - /** - * Create a specific ES id for the current log. - * - * @return a string with the generated ES Id - */ - public static String genSpecificEsId(String nodeName, String objectId, String problemName) { - - String uuId; - - Matcher matcher = pattern.matcher(objectId); - if (matcher.matches() && matcher.groupCount() == 1) { - uuId = matcher.group(1); - } else { - uuId = objectId; - } - - StringBuffer strBuf = new StringBuffer(); - strBuf.append(nodeName); - strBuf.append("/"); - strBuf.append(uuId); - strBuf.append("/"); - strBuf.append(problemName); - return strBuf.toString(); - } - - /** - * Create Es id - * - * @param fault used to create uuid for faultcurrent - * @return String with Id - */ - public static String genSpecificEsId(FaultcurrentEntity fault) { - return genSpecificEsId(fault.getNodeId(), fault.getObjectId(), fault.getProblem()); - } -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseEventsService.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseEventsService.java deleted file mode 100644 index b23d882c8..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseEventsService.java +++ /dev/null @@ -1,628 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import javax.annotation.Nonnull; -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.EsDataObjectReaderWriter2; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.ArchiveCleanProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataInconsistencyException; -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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; -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.ConnectionlogBuilder; -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.EventlogBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GranularityPeriodType; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; -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.InventoryBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder; -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.PmdataEntityBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInputBuilder; -import org.opendaylight.yangtools.yang.common.Uint32; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Event service, writing all events into the database into the appropriate index. - * - * @author herbert - */ -public class HtDatabaseEventsService implements ArchiveCleanProvider, DataProvider { - private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseEventsService.class); - - private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter(); - - private HtDatabaseClient client; - private EsDataObjectReaderWriter2 eventRWEventLogDevicemanager; - private EsDataObjectReaderWriter2 eventRWEquipment; - private EsDataObjectReaderWriter2 eventRWFaultCurrentDB; - private EsDataObjectReaderWriter2 eventRWFaultLogDB; - private EsDataObjectReaderWriter2 eventRWConnectionLogDB; - private final EsDataObjectReaderWriter2 networkelementConnectionDB; - private final EsDataObjectReaderWriter2 guiCutThroughDB; - private final EsDataObjectReaderWriter2 pmData15mDB; - private final EsDataObjectReaderWriter2 pmData24hDB; - - @SuppressWarnings("unused") - private final ElasticSearchDataProvider dataProvider; - // --- Construct and initialize - - - public HtDatabaseEventsService(HtDatabaseClient client, ElasticSearchDataProvider elasticSearchDataProvider) - throws Exception { - - LOG.info("Create {} start", HtDatabaseEventsService.class); - this.dataProvider = elasticSearchDataProvider; - - try { - // Create control structure - this.client = client; - - eventRWEventLogDevicemanager = new EsDataObjectReaderWriter2<>(client, Entity.Eventlog, - EventlogEntity.class, EventlogBuilder.class); - - eventRWEquipment = new EsDataObjectReaderWriter2<>(client, Entity.Inventoryequipment, InventoryEntity.class, - InventoryBuilder.class); - - eventRWFaultCurrentDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultcurrent, - FaultcurrentEntity.class, FaultcurrentBuilder.class); - - eventRWFaultLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultlog, FaultlogEntity.class, - FaultlogBuilder.class); - - eventRWConnectionLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Connectionlog, - ConnectionlogEntity.class, ConnectionlogBuilder.class); - - networkelementConnectionDB = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection, - NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class, true) - .setEsIdAttributeName("_id"); - - guiCutThroughDB = new EsDataObjectReaderWriter2<>(client, Entity.Guicutthrough, GuicutthroughEntity.class, - GuicutthroughBuilder.class); - - pmData15mDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance15min, PmdataEntity.class, - PmdataEntityBuilder.class); - - pmData24hDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance24h, PmdataEntity.class, - PmdataEntityBuilder.class); - - } catch (Exception e) { - LOG.error("Can not start database client. Exception: {}", e); - throw new Exception("Can not start database client. Exception: {}", e); - } - LOG.info("Create {} finished. DB Service {} started.", HtDatabaseEventsService.class, - client != null ? "sucessfully" : "not"); - } - - // --- Function - - // -- Connection log - @Override - public void writeConnectionLog(ConnectionlogEntity event) { - if (assertIfClientNull(event)) { - return; - } - LOG.debug("Write event: {}", event); - eventRWConnectionLogDB.write(event, null); - - } - - // -- Event log - @Override - public void writeEventLog(EventlogEntity event) { - if (assertIfClientNull("No client to write {}", event)) { - return; - } - - LOG.debug("Write event: {}", event.toString()); - eventRWEventLogDevicemanager.write(event, null); - } - - - // -- Fault log - - @Override - public void writeFaultLog(FaultlogEntity fault) { - if (assertIfClientNull(fault)) { - return; - } - - LOG.debug("Write fault to faultlog: {}", fault.toString()); - eventRWFaultLogDB.write(fault, null); - } - - // -- Fault current - - @Override - public void updateFaultCurrent(FaultcurrentEntity fault) { - if (assertIfClientNull(fault)) { - return; - } - - if (FaultEntityManager.isManagedAsCurrentProblem(fault)) { - if (FaultEntityManager.isNoAlarmIndication(fault)) { - LOG.debug("Remove from currentFaults: {}", fault.toString()); - eventRWFaultCurrentDB.remove(FaultEntityManager.genSpecificEsId(fault)); - } else { - LOG.debug("Write to currentFaults: {}", fault.toString()); - eventRWFaultCurrentDB.write(fault, FaultEntityManager.genSpecificEsId(fault)); - } - } else { - LOG.debug("Ingnore for currentFaults: {}", fault.toString()); - } - } - - /** - * Remove all entries for one node - * - * @param nodeName contains the mountpointname - * @return number of deleted entries - */ - @Override - public int clearFaultsCurrentOfNode(String nodeName) { - if (assertIfClientNullForNodeName(nodeName)) { - return -1; - } - - LOG.debug("Remove from currentFaults all faults for node: {}", nodeName); - return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNode(nodeName)); - } - - /** - * Remove all entries for one node - * - * @param nodeName contains the mountpointname - * @param objectId of element to be deleted - * @return number of deleted entries - */ - @Override - public int clearFaultsCurrentOfNodeWithObjectId(String nodeName, String objectId) { - if (assertIfClientNullForNodeName(nodeName)) { - return -1; - } - - LOG.debug("Remove from currentFaults all faults for node/objectId: {}/{}", nodeName, objectId); - return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNodeAndObjectId(nodeName, objectId)); - } - - /** - * Deliver list with all mountpoint/node-names in the database. - * - * @return List of all mountpoint/node-names the had active alarms. - */ - @Override - public @Nonnull List getAllNodesWithCurrentAlarms() { - if (assertIfClientNull("No DB, can not delete for all nodes", null)) { - return new ArrayList<>(); - } - - LOG.debug("Remove from currentFaults faults for all node"); - List nodeNames = new ArrayList<>(); - - for (FaultcurrentEntity fault : eventRWFaultCurrentDB.doReadAll().getHits()) { - String nodeName = fault.getNodeId(); - if (!nodeNames.contains(nodeName)) { - // this.clearFaultsCurrentOfNode(nodeName); -> Function shifted - nodeNames.add(nodeName); - } - } - return nodeNames; - } - - // -- Inventory and equipment current - - /** - * write internal equipment to database - * - * @param internalEquipment with mandatory fields. - */ - - private void writeInventory(Inventory internalEquipment) { - - if (internalEquipment.getManufacturerIdentifier() == null) { - internalEquipment = new InventoryBuilder(internalEquipment).setManufacturerIdentifier("").build(); - } - if (internalEquipment.getDate() == null) { - internalEquipment = new InventoryBuilder(internalEquipment).setDate("").build(); - } - eventRWEquipment.write(internalEquipment, internalEquipment.getNodeId() + "/" + internalEquipment.getUuid()); - } - - /** - * write internal equipment to database - * - * @param nodeId - * @param list - */ - @Override - public void writeInventory(String nodeId, List list) { - - try { - checkConsistency(nodeId, list); - } catch (DataInconsistencyException e) { - LOG.warn("inventory list for node {} is not consistent", nodeId, e); - list = e.getRepairedList(); - } - - for (Inventory internalEquipment : list) { - this.writeInventory(internalEquipment); - } - } - - private static void checkConsistency(String nodeId, List list) throws DataInconsistencyException { - final String UNBOUND_INVENTORY_UUID = "unbound"; - List failures = new ArrayList<>(); - long treeLevel; - int failCounter = 0; - Map repairList = new HashMap<>(); - InventoryBuilder repairedItem; - InventoryBuilder unboundItem = new InventoryBuilder().setNodeId(nodeId).setUuid(UNBOUND_INVENTORY_UUID) - .setTreeLevel(Uint32.valueOf(0));; - for (Inventory item : list) { - repairedItem = new InventoryBuilder(item); - //check missing tree-level - if (!nodeId.equals(item.getNodeId())) { - failures.add(String.format("missing node-id for equipment(uuid=%s)", item.getUuid())); - repairedItem.setNodeId(nodeId); - failCounter++; - } - if (item.getTreeLevel() == null) { - failures.add(String.format("missing tree-level for equipment(uuid=%s)", item.getUuid())); - repairedItem.setTreeLevel(Uint32.valueOf(1)); - failCounter++; - - } else { - treeLevel = item.getTreeLevel().longValue(); - if (treeLevel > 0) { - //check non root elem and missing parent - if (item.getParentUuid() == null) { - failures.add(String.format("Non root level element (uuid=%s) has to have a parent element", - item.getUuid())); - failCounter++; - repairedItem.setParentUuid(UNBOUND_INVENTORY_UUID); - repairList.put(unboundItem.getUuid(), unboundItem.build()); - } - //check that parent exists in list and is tree-level -1 - else { - Optional parent = - list.stream().filter(e -> item.getParentUuid().equals(e.getUuid())).findFirst(); - if (parent.isEmpty()) { - failures.add(String.format("no parent found for uuid=%s with parent-uuid=%s", - item.getUuid(), item.getParentUuid())); - repairedItem.setParentUuid(UNBOUND_INVENTORY_UUID); - failCounter++; - } - } - } - //check for duplicated uui - Optional duplicate = list - .stream().filter(e -> !item.equals(e) && item.getUuid() != null - && item.getUuid().equals(e.getUuid()) && repairList.containsKey(e.getUuid())) - .findFirst(); - if (duplicate.isPresent()) { - failures.add(String.format("found duplicate uuid=%s", item.getUuid())); - failCounter++; - continue; - - } - if (failCounter > 0) { - repairList.put(repairedItem.getUuid(), repairedItem.build()); - } else { - repairList.put(item.getUuid(), item); - } - } - } - - if (!failures.isEmpty()) { - throw new DataInconsistencyException(new ArrayList<>(repairList.values()), - "inventory list is not consistent;\n" + String.join("\n", failures)); - } - } - - // -- Networkelement - - - /** - * join base with parameters of toJoin (only non null values) - * - * @param base base object - * @param toJoin object with new property values - * @return new joined object - */ - @SuppressWarnings("unused") - private NetworkElementConnectionEntity joinNe(NetworkElementConnectionEntity base, - NetworkElementConnectionEntity toJoin) { - if (base == null) { - return toJoin; - } - NetworkElementConnectionBuilder builder = new NetworkElementConnectionBuilder(base); - if (toJoin != null) { - if (toJoin.isIsRequired() != null) { - builder.setIsRequired(toJoin.isIsRequired()); - } - if (toJoin.getCoreModelCapability() != null) { - builder.setCoreModelCapability(toJoin.getCoreModelCapability()); - } - if (toJoin.getDeviceType() != null) { - builder.setDeviceType(toJoin.getDeviceType()); - } - if (toJoin.getHost() != null) { - builder.setHost(toJoin.getHost()); - } - if (toJoin.getNodeDetails() != null) { - builder.setNodeDetails(toJoin.getNodeDetails()); - } - if (toJoin.getPassword() != null) { - builder.setPassword(toJoin.getPassword()); - } - if (toJoin.getPort() != null) { - builder.setPort(toJoin.getPort()); - } - if (toJoin.getStatus() != null) { - builder.setStatus(toJoin.getStatus()); - } - if (toJoin.getUsername() != null) { - builder.setUsername(toJoin.getUsername()); - } - } - return builder.build(); - } - - /** - * - * @param networkElementConnectionEntitiy to wirte to DB - * @param nodeId Id for this DB element - */ - @Override - public boolean updateNetworkConnectionDeviceType(NetworkElementConnectionEntity networkElementConnectionEntitiy, - String nodeId) { - return this.networkelementConnectionDB.update(networkElementConnectionEntitiy, nodeId) != null; - // NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId); - // this.networkelementConnectionDB.write(this.joinNe(e, networkElementConnectionEntitiy), nodeId); - } - - /** - * Update after new mountpoint registration - * - * @param networkElementConnectionEntitiy data - * @param nodeId of device (mountpoint name) - */ - @Override - public boolean updateNetworkConnection22(NetworkElementConnectionEntity networkElementConnectionEntitiy, - String nodeId) { - LOG.info("update networkelement-connection for {} with data {}", nodeId, networkElementConnectionEntitiy); - return this.networkelementConnectionDB.updateOrCreate(networkElementConnectionEntitiy, nodeId, - Arrays.asList("is-required", "username", "password")) != null; - // NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId); - // this.networkelementConnectionDB.write(this.joinNe(e, networkElementConnectionEntitiy), nodeId); - - } - - /* please do not remove */ - // public void cleanNetworkElementConnections() { - // this.networkelementConnectionDB.remove(QueryBuilders.matchQuery("is-required", false)); - // CreateNetworkElementConnectionInput x = new CreateNetworkElementConnectionInputBuilder().setStatus(ConnectionLogStatus.Disconnected).build(); - // this.networkelementConnectionDB.update(x,QueryBuilders.matchAllQuery()); - // } - - @Override - public void removeNetworkConnection(String nodeId) { - Boolean isRequired; - NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId); - if (e != null && (isRequired = e.isIsRequired()) != null) { - if (isRequired) { - LOG.debug("updating connection status for {} of required ne to disconnected", nodeId); - this.networkelementConnectionDB.update(new UpdateNetworkElementConnectionInputBuilder() - .setStatus(ConnectionLogStatus.Disconnected).build(), nodeId); - } else { - LOG.debug("remove networkelement-connection for {} entry because of non-required", nodeId); - this.networkelementConnectionDB.remove(nodeId); - } - } else { - LOG.warn("Unable to update connection-status. dbentry for {} not found in networkelement-connection", - nodeId); - } - } - - // -- Multiple areas - - @Override - public int doIndexClean(Date olderAreOutdated) { - - String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated); - int removed = 0; - - QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp); - removed += eventRWEventLogDevicemanager.remove(queryEventBase); - - QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp); - removed += eventRWFaultLogDB.remove(queryFaultLog); - return removed; - } - - @Override - public int getNumberOfOldObjects(Date olderAreOutdated) { - - String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated); - int numberOfElements = 0; - - QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp); - numberOfElements += eventRWEventLogDevicemanager.doReadAll(queryEventBase).getTotal(); - - QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp); - numberOfElements += eventRWFaultLogDB.doReadAll(queryFaultLog).getTotal(); - - return numberOfElements; - } - - // -- Helper - - /** - * Verify status of client - * - * @param event that is printed with message - * @return true if client is null - */ - private boolean assertIfClientNull(Object event) { - return assertIfClientNull("No DB, can not write: {}", event); - } - - private boolean assertIfClientNullForNodeName(Object object) { - return assertIfClientNull("No DB, can not handle node: {}", object); - } - - /** - * Verify status of client - * - * @param message to print including {} for object printout. - * @return true if client is null - */ - private boolean assertIfClientNull(String message, Object object) { - if (client == null) { - LOG.debug(message, object); - return true; - } - return false; - } - - // ### sub classes - - - private static class EsEventBase { - /** - * Query to get older Elements - * - * @param netconfTimeStamp to identify older Elements - * @return QueryBuilder for older elements related to timestamp - */ - private static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) { - return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp); - } - } - private static class EsFaultLogDevicemanager { - /** - * Get older Elements - * - * @param netconfTimeStamp to identify query elements older than this timestamp. - * @return QueryBuilder for related elements - */ - public static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) { - return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp); - } - } - public static class EsFaultCurrent { - /** - * @param nodeName name of the node - * @return query builder - */ - public static QueryBuilder getQueryForOneNode(String nodeName) { - return QueryBuilders.matchQuery("node-id", nodeName); - } - - public static QueryBuilder getQueryForOneNodeAndObjectId(String nodeName, String objectId) { - BoolQueryBuilder bq = QueryBuilders.boolQuery(); - bq.must(QueryBuilders.matchQuery("node-id", nodeName)); - bq.must(QueryBuilders.matchQuery("object-id", objectId)); - return bq; - } - } - - @Override - public List getNetworkElementConnections() { - return this.networkelementConnectionDB.doReadAll().getHits(); - } - - @Override - public void doWritePerformanceData(List list) { - - list.forEach(elem -> { - GranularityPeriodType granularityPeriod = nnGetGranularityPeriodType(elem.getGranularityPeriod()); - //_id": "Sim12600/LP-MWPS-TTP-01/2017-07-04T15:15:00.0+00:00" - StringBuffer id = new StringBuffer(); - DateAndTime date = elem.getTimeStamp(); - id.append(elem.getNodeName()); - id.append("/"); - id.append(elem.getUuidInterface()); - id.append("/"); - id.append(date != null ? date.getValue() : "null"); - - switch (granularityPeriod) { - case Period15Min: - pmData15mDB.write(elem, id.toString()); - break; - case Period24Hours: - pmData24hDB.write(elem, id.toString()); - break; - case Unknown: - default: - LOG.debug("Unknown granularity {} id {}", granularityPeriod, id); - break; - } - }); - - } - - @NonNull - GranularityPeriodType nnGetGranularityPeriodType(@Nullable GranularityPeriodType granularityPeriod) { - return granularityPeriod != null ? granularityPeriod : GranularityPeriodType.Unknown; - } - - @Override - public HtDatabaseClient getRawClient() { - return this.client; - } - - @Override - public void writeGuiCutThroughData(Guicutthrough gcData, String nodeId) { - guiCutThroughDB.write(gcData, nodeId); - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseMaintenanceService.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseMaintenanceService.java deleted file mode 100644 index 380526d9a..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/entity/HtDatabaseMaintenanceService.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity; - -import java.util.ArrayList; -import java.util.List; -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; -import org.onap.ccsdk.features.sdnr.wt.common.HtAssert; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.EsDataObjectReaderWriter2; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; -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.MaintenanceBuilder; -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.NetworkElementConnectionBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionEntity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class HtDatabaseMaintenanceService implements HtDatabaseMaintenance { - - private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseMaintenanceService.class); - - private final EsDataObjectReaderWriter2 maintenanceRW; - private final EsDataObjectReaderWriter2 requiredNeRW; - - public HtDatabaseMaintenanceService(@NonNull HtDatabaseClient client) throws ClassNotFoundException { - HtAssert.nonnull(client); - - // Create control structure - maintenanceRW = new EsDataObjectReaderWriter2<>(client, Entity.Maintenancemode, MaintenanceEntity.class, - MaintenanceBuilder.class, true).setEsIdAttributeName("_id"); - - requiredNeRW = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection, - NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class, true) - .setEsIdAttributeName("_id"); - - } - - /** - * Get existing object for mountpoint to manage maintenance mode - * - * @return Object with configuration - */ - @Override - @Nullable - public MaintenanceEntity getMaintenance(@Nullable String mountpointId) { - MaintenanceEntity deviceMaintenanceMode = null; - if (maintenanceRW != null && mountpointId != null) { - deviceMaintenanceMode = maintenanceRW.read(mountpointId); - } - return deviceMaintenanceMode; - } - - @Override - public MaintenanceEntity setMaintenance(MaintenanceEntity m) { - if (maintenanceRW != null) { - if (maintenanceRW.write(m, m.getNodeId()) == null) { - throw new IllegalArgumentException("Problem writing to database: " + m.getId()); - } - LOG.info("Wrote maintenance object {}", m.toString()); - } - return m; - } - - @Override - public List getAll() { - return maintenanceRW != null ? maintenanceRW.doReadAll().getHits() : new ArrayList<>(); - } - - @Override - public MaintenanceEntity createIfNotExists(String mountpointId) { - MaintenanceEntity deviceMaintenanceMode = null; - if (maintenanceRW != null) { - deviceMaintenanceMode = maintenanceRW.read(mountpointId); - if (deviceMaintenanceMode == null) { - LOG.debug("creating empty maintenance object in database"); - deviceMaintenanceMode = getDefaultMaintenance(mountpointId); - maintenanceRW.write(deviceMaintenanceMode, mountpointId); - } else { - LOG.debug("maintenance object already exists in database"); - } - } else { - LOG.warn("cannot create maintenance obj. db reader/writer is null"); - } - return deviceMaintenanceMode; - } - - @Override - public void deleteIfNotRequired(String mountPointNodeName) { - - if (!this.isRequireNe(mountPointNodeName)) { - if (maintenanceRW != null) { - LOG.debug("removing maintenance object in database for " + mountPointNodeName); - maintenanceRW.remove(mountPointNodeName); - } else { - LOG.warn("cannot create maintenance obj. db reader/writer is null"); - } - } - - - } - - /** - * Provide default maintenanceinformation for a device - * - * @param mountpointId nodeId of device - * @return default data - */ - static private MaintenanceEntity getDefaultMaintenance(String mountpointId) { - - DateAndTime now = NetconfTimeStampImpl.getConverter().getTimeStamp(); - - MaintenanceBuilder deviceMaintenanceModeBuilder = new MaintenanceBuilder(); - deviceMaintenanceModeBuilder.setNodeId(mountpointId).setId(mountpointId); - // Use time from mountpoint creation - deviceMaintenanceModeBuilder.setDescription(""); - // Use time from mountpoint creation - deviceMaintenanceModeBuilder.setStart(now); - deviceMaintenanceModeBuilder.setEnd(now); - deviceMaintenanceModeBuilder.setActive(false); - - // Reference to all - //consistent to UI input to null/not empty string - //deviceMaintenanceModeBuilder.setObjectIdRef(""); - //deviceMaintenanceModeBuilder.setProblem(""); - - return deviceMaintenanceModeBuilder.build(); - } - - // -- Private - /** - * Check in required ne if entry exists for mountpointNodeName - * - * @param mountPointNodeName - * @return - */ - @SuppressWarnings("null") - private boolean isRequireNe(String mountPointNodeName) { - NetworkElementConnectionEntity ne = null; - if (requiredNeRW != null) { - LOG.debug("searching for entry in required-networkelement for " + mountPointNodeName); - ne = requiredNeRW.read(mountPointNodeName); - } else { - LOG.warn("cannot read db. no db reader writer initialized"); - } - if (ne != null && ne.isIsRequired() != null) { - return ne.isIsRequired(); - } else { - return false; - } - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilter.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilter.java deleted file mode 100644 index 627d125ae..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilter.java +++ /dev/null @@ -1,433 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import org.eclipse.jdt.annotation.Nullable; -import org.onap.ccsdk.features.sdnr.wt.common.YangHelper; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorPm; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; -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.SortOrder; -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.Pagination; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Sortorder; -import org.opendaylight.yangtools.yang.common.Uint32; -import org.opendaylight.yangtools.yang.common.Uint64; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class QueryByFilter { - - private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); - private static final List timestampValueNames = Arrays.asList("timestamp", "start", "end"); - - private static List emptySortOrderList = new ArrayList<>(); - private static List emptyFilterList = new ArrayList<>(); - - // Derived from input - private long page; - private long pageSize; - private long fromPage; - private List filterList; - private List sortOrder; - - /** - * Process input from RPC into Queries to database - * - * @param input Input from RPC, for test it could be null - */ - public QueryByFilter(EntityInput input) { - page = -1; - pageSize = -1; - if (input != null) { - @Nullable - Pagination pagination = input.getPagination(); - if (pagination != null) { - @Nullable - Uint64 pageOrNull = YangHelper2.getUint64(pagination.getPage()); - if (pageOrNull != null) { - page = pageOrNull.longValue(); - } - @Nullable - Uint32 pageSizeOrNull = YangHelper2.getUint32(pagination.getSize()); - if (pageSizeOrNull != null) { - pageSize = pageSizeOrNull.longValue(); - } - } - } - if (page < 0) - page = 1; - if (pageSize < 0) - pageSize = 1; - - fromPage = (page - 1) * pageSize; - if (fromPage < 0 || pageSize > 10000) - throw new IllegalArgumentException("mismatching input parameters. From:" + fromPage + " size:" + pageSize); - - filterList = YangHelper.getList(input.getFilter()); - if (filterList == null) - filterList = emptyFilterList; - sortOrder = YangHelper.getList(input.getSortorder()); - if (sortOrder == null) - sortOrder = emptySortOrderList; - - } - - public QueryBuilder getQueryBuilderByFilter() { - return getQueryBuilderByFilter(""); - } - - public QueryBuilder getQueryBuilderByFilter(String prefix) { - QueryBuilder queryBuilder = fromFilter(filterList, prefix).from(fromPage).size(pageSize); - setSortOrder(queryBuilder, sortOrder, prefix); - return queryBuilder; - } - - public SearchRequest getSearchRequestByFilter(String nodeKey, String uuidKey, String index, String dataType) { - Filter nodeFilter = getFilter(filterList, nodeKey); - if (nodeFilter != null) { - SearchRequest request = new SearchRequest(index, dataType); - request.setQuery( - QueryBuilders.matchQuery(nodeKey, nodeFilter.getFiltervalue()).aggregations(uuidKey).size(0)); - return request; - } else { - String msg = "no nodename in filter found "; - LOG.debug(msg); - throw new IllegalArgumentException(msg); - } - } - - public SearchRequest getSearchRequestBySortOrder(String nodeKey, String uuidKey, String index, String dataType) { - Sortorder soNode = getSortOrder(sortOrder, nodeKey); - SearchRequest request = new SearchRequest(index, dataType); - QueryBuilder query = null; - if (soNode != null) { - query = QueryBuilders.matchAllQuery().aggregations(nodeKey, convert(soNode.getSortorder())).size(0); - } else { - query = QueryBuilders.matchAllQuery().aggregations(nodeKey).size(0); - } - request.setQuery(query); - return request; - } - - public long getPage() { - return page; - } - - public long getPageSize() { - return pageSize; - } - - public long getPageStartIndex() { - return fromPage; - } - - @Override - public String toString() { - return "QueryByFilter [page=" + page + ", pageSize=" + pageSize + ", fromPage=" + fromPage + ", filterList=" - + filterList + ", sortOrder=" + sortOrder + "]"; - } - - /* - * Private and static implementations - */ - private static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder, String prefix) { - if (sortorder != null && sortorder.size() > 0) { - for (Sortorder so : sortorder) { - query.sort(handlePrefix(prefix, so.getProperty()), convert(so.getSortorder())); - } - } - return query; - } - - private static org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder convert(SortOrder sortOrder) { - return sortOrder == SortOrder.Ascending - ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING - : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING; - }; - - private static Sortorder getSortOrder(@Nullable List list, String prop) { - if (list == null) { - return null; - } - for (Sortorder o : list) { - if (prop.equals(o.getProperty())) { - return o; - } - } - return null; - } - - private static Filter getFilter(@Nullable List list, String prop) { - if (list == null) { - return null; - } - for (Filter f : list) { - if (prop.equals(f.getProperty())) { - return f; - } - } - return null; - } - - private static String fillTimeStamp(String value) { - int idx = value.lastIndexOf("*"); - final String REPLACE = "0000-00-00T00:00:00.0Z"; - String s = value.substring(0, idx) + REPLACE.substring(idx); - if (Integer.parseInt(s.substring(5, 7)) == 0) { - s = s.substring(0, 5) + "01-" + s.substring(8); - } - if (Integer.parseInt(s.substring(8, 10)) == 0) { - s = s.substring(0, 8) + "01" + s.substring(10); - } - - return s; - } - - /** - * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: - * 2017-01-01T00:00:00Z, lt:2018-01-01T00:00:00Z - * - * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 - * - */ - private static @Nullable QueryBuilder fromTimestampSearchFilter(String property, String value) { - if (!value.endsWith("*")) { - return null; - } - int idx = value.lastIndexOf("*"); - String lowerEnd = fillTimeStamp(value); - String upperEnd = null; - NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); - Date dt = null; - try { - dt = converter.getDateFromNetconf(lowerEnd); - } catch (Exception e) { - - } - if (dt == null) { - return null; - } - // property.substring(0,idx)+REPLACE.substring(idx+1); - Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - c.setTime(dt); - int tmpvalue; - switch (idx) { - case 1: // (2*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 2: // (20*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 3: // (200*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 4: // (2000*) - case 5: // (2000-*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 6: // switch 10 months (2000-0* or 2000-1*) - tmpvalue = c.get(Calendar.MONTH); - if (tmpvalue < 9) { - c.set(Calendar.MONTH, 9); - } else { - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); - c.set(Calendar.MONTH, 0); - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - - break; - case 7: // switch one month (2018-01* or 2018-01-*) - case 8: - c.add(Calendar.MONTH, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 9: // (2018-01-0*) - tmpvalue = c.get(Calendar.DAY_OF_MONTH); - if (tmpvalue == 1) { - c.set(Calendar.DAY_OF_MONTH, 10); - } else if (tmpvalue == 10) { - c.set(Calendar.DAY_OF_MONTH, 20); - } else if (tmpvalue == 20) { - if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { - c.set(Calendar.DAY_OF_MONTH, 1); - c.add(Calendar.MONTH, 1); - } else { - c.set(Calendar.DAY_OF_MONTH, 30); - } - } else if (tmpvalue == 30) { - c.set(Calendar.DAY_OF_MONTH, 1); - c.add(Calendar.MONTH, 1); - } else { - break; - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 10: // (2018-01-01*) - case 11: // (2018-01-01T*) - c.add(Calendar.DAY_OF_MONTH, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 12: // (2018-01-01T1*) - tmpvalue = c.get(Calendar.HOUR_OF_DAY); - if (tmpvalue == 20) { - c.set(Calendar.HOUR_OF_DAY, 0); - c.add(Calendar.DAY_OF_MONTH, 1); - } else { - c.add(Calendar.HOUR_OF_DAY, 10); - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 13: // (2018-01-01T11*) - case 14: // (2018-01-01T11-*) - c.add(Calendar.HOUR_OF_DAY, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 15: // (2018-01-01T11-3*) - c.add(Calendar.MINUTE, 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 16: // (2018-01-01T11-32*) - case 17: // (2018-01-01T11-32-*) - c.add(Calendar.MINUTE, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 18: // (2018-01-01T11-32-1*) - c.add(Calendar.SECOND, 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 19: // (2018-01-01T11-32-11*) - case 20: // (2018-01-01T11-32-11.*) - c.add(Calendar.SECOND, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - - default: - break; - } - - if (upperEnd == null) { - return null; - } - return QueryBuilders.rangeQuery(property).gte(lowerEnd).lt(upperEnd); - - } - - private static List collectValues(Filter filter) { - List values = new ArrayList(); - if (filter.getFiltervalue() != null) { - values.add(filter.getFiltervalue()); - } - if (filter.getFiltervalues() != null) { - values.addAll(filter.getFiltervalues()); - } - return values; - } - - private static QueryBuilder fromFilter(@Nullable List filters, String prefix) { - if (filters == null || filters.size() == 0) { - return QueryBuilders.matchAllQuery(); - - } else if (filters.size() == 1) { - String property = filters.get(0).getProperty(); - List values = collectValues(filters.get(0)); - if ("id".equals(property)) { - property = "_id"; - } - if (values.size() == 1) { - return getSinglePropertyQuery(property, values.get(0), prefix); - } else { - BoolQueryBuilder bquery = new BoolQueryBuilder(); - for (String v : values) { - bquery.should(getSinglePropertyQuery(property, v, prefix)); - } - return bquery; - - } - } else { - BoolQueryBuilder query = new BoolQueryBuilder(); - for (Filter fi : filters) { - String p = fi.getProperty(); - List values = collectValues(fi); - if ("id".equals(p)) { - p = "_id"; - } - if (values.size() == 1) { - query.must(getSinglePropertyQuery(p, values.get(0), prefix)); - } else { - BoolQueryBuilder tmpQuery = QueryBuilders.boolQuery(); - for (String v : values) { - tmpQuery.should(getSinglePropertyQuery(p, v, prefix)); - } - query.must(tmpQuery); - tmpQuery = QueryBuilders.boolQuery(); - } - } - LOG.trace("Query result. {}", query.toJSON()); - return query; - } - } - - private static QueryBuilder getSinglePropertyQuery(String p, String v, String prefix) { - QueryBuilder query = null; - if (DbFilter.hasSearchParams(v)) { - if (p != null && timestampValueNames.contains(p.toLowerCase())) { - query = fromTimestampSearchFilter(p, v); - if (query == null) { - query = QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v)); - } - } else { - query = QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v)); - } - } else if (DbFilter.isComparisonValid(v)) { - query = DbFilter.getRangeQuery(handlePrefix(prefix, p), v); - if (query == null) { - query = QueryBuilders.matchQuery(handlePrefix(prefix, p), v); - } - } else { - query = QueryBuilders.matchQuery(handlePrefix(prefix, p), v); - } - return query; - } - - private static String handlePrefix(String prefix, String p) { - return (prefix != null ? prefix : "") + p; - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilterStatic.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilterStatic.java deleted file mode 100644 index b76e0a872..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryByFilterStatic.java +++ /dev/null @@ -1,337 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper; - -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import org.eclipse.jdt.annotation.Nullable; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.acessor.DataObjectAcessorPm; -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.EntityInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SortOrder; -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.Sortorder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class QueryByFilterStatic { - - private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); - private static final List timestampValueNames = Arrays.asList("timestamp", "start", "end"); - - private QueryByFilterStatic() { - //Hide - } - - static long getPage(EntityInput input) { - return getPage(input, 1); - } - - private static long getPage(EntityInput input, long defaultValue) { - return input.getPagination() != null ? input.getPagination().getPage().longValue() : defaultValue; - } - - static long getPageSize(EntityInput input) { - return getPageSize(input, 1); - } - - private static long getPageSize(EntityInput input, long defaultValue) { - return input.getPagination() != null ? input.getPagination().getSize().longValue() : defaultValue; - } - - - public static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder) { - return setSortOrder(query, sortorder, ""); - } - - private static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder, String prefix) { - if (sortorder != null && sortorder.size() > 0) { - for (Sortorder so : sortorder) { - query.sort((prefix != null ? prefix : "") + so.getProperty(), - so.getSortorder() == SortOrder.Ascending - ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING - : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING); - } - } - return query; - - } - - - public static Sortorder getSortOrder(@Nullable List list, String prop) { - if (list == null) { - return null; - } - for (Sortorder o : list) { - if (prop.equals(o.getProperty())) { - return o; - } - } - return null; - } - - public static Filter getFilter(@Nullable List list, String prop) { - if (list == null) { - return null; - } - for (Filter f : list) { - if (prop.equals(f.getProperty())) { - return f; - } - } - return null; - } - - public static QueryBuilder fromFilter(@Nullable List filters) { - return fromFilter(filters, ""); - } - - private static String fillTimeStamp(String value) { - int idx = value.lastIndexOf("*"); - final String REPLACE = "0000-00-00T00:00:00.0Z"; - String s = value.substring(0, idx) + REPLACE.substring(idx); - if (Integer.parseInt(s.substring(5, 7)) == 0) { - s = s.substring(0, 5) + "01-" + s.substring(8); - } - if (Integer.parseInt(s.substring(8, 10)) == 0) { - s = s.substring(0, 8) + "01" + s.substring(10); - } - - return s; - } - - /** - * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: - * 2017-01-01T00:00:00Z, lt:2018-01-01T00:00:00Z - * - * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 - * - */ - private static @Nullable QueryBuilder fromTimestampSearchFilter(String property, String value) { - if (!value.endsWith("*")) { - return null; - } - int idx = value.lastIndexOf("*"); - String lowerEnd = fillTimeStamp(value); - String upperEnd = null; - NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); - Date dt = null; - try { - dt = converter.getDateFromNetconf(lowerEnd); - } catch (Exception e) { - - } - if (dt == null) { - return null; - } - // property.substring(0,idx)+REPLACE.substring(idx+1); - Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - c.setTime(dt); - int tmpvalue; - switch (idx) { - case 1: // (2*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 2: // (20*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 3: // (200*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 4: // (2000*) - case 5: // (2000-*) - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 6: //switch 10 months (2000-0* or 2000-1*) - tmpvalue = c.get(Calendar.MONTH); - if (tmpvalue < 9) { - c.set(Calendar.MONTH, 9); - } else { - c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); - c.set(Calendar.MONTH, 0); - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - - break; - case 7: //switch one month (2018-01* or 2018-01-*) - case 8: - c.add(Calendar.MONTH, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 9: // (2018-01-0*) - tmpvalue = c.get(Calendar.DAY_OF_MONTH); - if (tmpvalue == 1) { - c.set(Calendar.DAY_OF_MONTH, 10); - } else if (tmpvalue == 10) { - c.set(Calendar.DAY_OF_MONTH, 20); - } else if (tmpvalue == 20) { - if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { - c.set(Calendar.DAY_OF_MONTH, 1); - c.add(Calendar.MONTH, 1); - } else { - c.set(Calendar.DAY_OF_MONTH, 30); - } - } else if (tmpvalue == 30) { - c.set(Calendar.DAY_OF_MONTH, 1); - c.add(Calendar.MONTH, 1); - } else { - break; - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 10: // (2018-01-01*) - case 11: // (2018-01-01T*) - c.add(Calendar.DAY_OF_MONTH, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 12: // (2018-01-01T1*) - tmpvalue = c.get(Calendar.HOUR_OF_DAY); - if (tmpvalue == 20) { - c.set(Calendar.HOUR_OF_DAY, 0); - c.add(Calendar.DAY_OF_MONTH, 1); - } else { - c.add(Calendar.HOUR_OF_DAY, 10); - } - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 13: // (2018-01-01T11*) - case 14: // (2018-01-01T11-*) - c.add(Calendar.HOUR_OF_DAY, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 15: // (2018-01-01T11-3*) - c.add(Calendar.MINUTE, 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 16: // (2018-01-01T11-32*) - case 17: // (2018-01-01T11-32-*) - c.add(Calendar.MINUTE, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 18: // (2018-01-01T11-32-1*) - c.add(Calendar.SECOND, 10); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - case 19: // (2018-01-01T11-32-11*) - case 20: // (2018-01-01T11-32-11.*) - c.add(Calendar.SECOND, 1); - upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); - break; - - default: - break; - } - - if (upperEnd == null) { - return null; - } - return QueryBuilders.rangeQuery(property).gte(lowerEnd).lt(upperEnd); - - } - - private static QueryBuilder fromFilter(@Nullable List filters, String prefix) { - if (filters == null || filters.size() == 0) { - return QueryBuilders.matchAllQuery(); - - } else if (filters.size() == 1) { - QueryBuilder query; - String p = filters.get(0).getProperty(); - String v = filters.get(0).getFiltervalue(); - if ("id".equals(p)) { - p = "_id"; - } else { - // v=v.toLowerCase(); - } - if (DbFilter.hasSearchParams(v)) { - if (p != null && timestampValueNames.contains(p.toLowerCase())) { - query = fromTimestampSearchFilter(p, v); - if (query != null) { - return query; - } - } - return QueryBuilders.regex(p, DbFilter.createDatabaseRegex(v)); - - - } else if (DbFilter.isComparisonValid(v)) { - RangeQueryBuilder q = DbFilter.getRangeQuery((prefix != null ? prefix : "") + p, v); - if (q != null) { - return q; - } else { - return QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v); - } - } else { - return QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v); - } - } else { - BoolQueryBuilder query = new BoolQueryBuilder(); - QueryBuilder tmpQuery; - for (Filter fi : filters) { - String p = fi.getProperty(); - String v = fi.getFiltervalue(); - if ("id".equals(p)) { - p = "_id"; - } else { - // v=v.toLowerCase(); - } - if (DbFilter.hasSearchParams(v)) { - if (p != null && timestampValueNames.contains(p.toLowerCase())) { - tmpQuery = fromTimestampSearchFilter(p, v); - if (tmpQuery != null) { - query.must(tmpQuery); - } else { - query.must(QueryBuilders.regex((prefix != null ? prefix : "") + p, - DbFilter.createDatabaseRegex(v))); - } - } else { - query.must(QueryBuilders.regex((prefix != null ? prefix : "") + p, - DbFilter.createDatabaseRegex(v))); - } - } else if (DbFilter.isComparisonValid(v)) { - RangeQueryBuilder q = DbFilter.getRangeQuery((prefix != null ? prefix : "") + p, v); - if (q != null) { - query.must(q); - } else { - query.must(QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v)); - } - } else { - query.must(QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v)); - } - } - LOG.trace("Query result. {}", query.toJSON()); - return query; - } - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryResult.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryResult.java deleted file mode 100644 index 6e653baf2..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/data/rpctypehelper/QueryResult.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper; - -import java.math.BigInteger; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PaginationOutputG; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder; - -public class QueryResult { - - private SearchResult result; - private PaginationOutputG pagination; - - public QueryResult(long page, long pageSize, SearchResult result) { - this.result = result; - - PaginationBuilder x = new PaginationBuilder(); - x.setPage(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(page))); - x.setSize(YangHelper2.getLongOrUint32(pageSize)); - x.setTotal(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(result.getTotal()))); - pagination = x.build(); - } - - public QueryResult(QueryByFilter queryByFilter, SearchResult result) { - this.result = result; - - PaginationBuilder x = new PaginationBuilder(); - x.setPage(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(queryByFilter.getPage()))); - x.setSize(YangHelper2.getLongOrUint32(queryByFilter.getPageSize())); - x.setTotal(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(result.getTotal()))); - pagination = x.build(); - } - - - public SearchResult getResult() { - return result; - } - - public PaginationOutputG getPagination() { - return pagination; - } - - @Override - public String toString() { - return "QueryResult [result=" + result + ", pagination=" + pagination + "]"; - } - - - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter2.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter2.java deleted file mode 100644 index 153022b4c..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter2.java +++ /dev/null @@ -1,405 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.database; - -import com.fasterxml.jackson.core.JsonProcessingException; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.List; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.eclipse.jdt.annotation.NonNull; -import org.onap.ccsdk.features.sdnr.wt.common.database.DatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper2; -import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Class to rw yang-tool generated objects into elasticsearch database. For "ES _id" exchange the esIdAddAtributteName - * is used. This attribute mast be of type String and contains for read and write operations the object id. The function - * can be used without id handling. If id handling is required the parameter needs to be specified by class definition - * in yang and setting the name by using setAttributeName() - * - * Due to using Jackson base interfaces the org.eclipse.jdt.annotation.NonNull needs to be used here to get rid of - * warnings - * - * @param Yang tools generated class object. - */ -public class EsDataObjectReaderWriter2 { - - private final Logger LOG = LoggerFactory.getLogger(EsDataObjectReaderWriter2.class); - - /** Typename for elastic search data schema **/ - private String dataTypeName; - - /** Elasticsearch Database client to be used **/ - private DatabaseClient db; - - /** Mapper with configuration to use opendaylight yang-tools builder pattern for object creation **/ - private YangToolsMapper2 yangtoolsMapper; - - /** Class of T as attribute to allow JSON to Class object mapping **/ - private Class clazz; - - /** Field is used to write id. If null no id handling **/ - private @Nullable Field field; - - /** Attribute that is used as id field for the database object **/ - private @Nullable String esIdAddAtributteName; - - /** Interface to be used for write operations. Rule for write: T extends S and **/ - private Class writeInterfaceClazz; // == "S" - /** Flag true to sync this attribute during write always, what is slow and false do not sync */ - private final boolean syncAfterWrite; - - /** - * Elasticsearch database read and write for specific class, defined by opendaylight yang-tools. - * - * @param - * @param - * @param db Database access client - * @param dataTypeName typename in database schema - * @param clazz class of type to be handled - * @param builderClazz class to build related object if builder pattern should be used. - * @param syncAfterWrite - * @throws ClassNotFoundException - */ - public > EsDataObjectReaderWriter2(DatabaseClient db, - String dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz, boolean syncAfterWrite) - throws ClassNotFoundException { - LOG.info("Create {} for datatype {} class {}", this.getClass().getName(), dataTypeName, clazz.getName()); - - this.esIdAddAtributteName = null; - this.field = null; - this.writeInterfaceClazz = clazz; - this.db = db; - this.dataTypeName = dataTypeName; - this.yangtoolsMapper = new YangToolsMapper2<>(clazz, builderClazz); - this.clazz = clazz; - this.syncAfterWrite = syncAfterWrite; - } - - public > EsDataObjectReaderWriter2(DatabaseClient db, - Entity dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz) - throws ClassNotFoundException { - this(db, dataTypeName.getName(), clazz, builderClazz, false); - } - - public > EsDataObjectReaderWriter2(DatabaseClient db, - Entity dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz, boolean syncAfterWrite) - throws ClassNotFoundException { - this(db, dataTypeName.getName(), clazz, builderClazz, syncAfterWrite); - } - - public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, @Nonnull Class clazz, - boolean syncAfterWrite) throws ClassNotFoundException { - this(db, dataTypeName.getName(), clazz, null, syncAfterWrite); - } - - public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, Class clazz) - throws ClassNotFoundException { - this(db, dataTypeName.getName(), clazz, null, false); - } - - /** - * Get Datatype name - * - * @return string with dataTypeName - */ - public String getDataTypeName() { - return dataTypeName; - } - - /** - * Simlar to {@link #setEsIdAttributeName()}, but adapts the parameter to yangtools attribute naming schema - * - * @param esIdAttributeName is converted to UnderscoreCamelCase - * @return this for further operations. - */ - public EsDataObjectReaderWriter2 setEsIdAttributeNameCamelized(String esIdAttributeName) { - return setEsIdAttributeName(YangToolsMapperHelper.toCamelCaseAttributeName(esIdAttributeName)); - } - - /** - * Attribute name of class that is containing the object id - * - * @param esIdAttributeName of the implementation class for the yangtools interface. Expected attribute name format - * is CamelCase with leading underline. @ - * @return this for further operations. - * @throws SecurityException if no access or IllegalArgumentException if wrong type or no attribute with this name. - */ - public EsDataObjectReaderWriter2 setEsIdAttributeName(String esIdAttributeName) { - LOG.debug("Set attribute '{}'", esIdAttributeName); - this.esIdAddAtributteName = null; // Reset status - this.field = null; - - Field attributeField; - try { - Builder builder = yangtoolsMapper.getBuilder(clazz); - if (builder == null) { - String msg = "No builder for " + clazz; - LOG.debug(msg); - throw new IllegalArgumentException(msg); - } else { - T object = builder.build(); - attributeField = object.getClass().getDeclaredField(esIdAttributeName); - if (attributeField.getType().equals(String.class)) { - attributeField.setAccessible(true); - this.esIdAddAtributteName = esIdAttributeName; // Set new status if everything OK - this.field = attributeField; - } else { - String msg = "Wrong field type " + attributeField.getType().getName() + " of " + esIdAttributeName; - LOG.debug(msg); - throw new IllegalArgumentException(msg); - } - } - } catch (NoSuchFieldException e) { - // Convert to run-time exception - String msg = "NoSuchFieldException for '" + esIdAttributeName + "' in class " + clazz.getName(); - LOG.debug(msg); - throw new IllegalArgumentException(msg); - } catch (SecurityException e) { - LOG.debug("Access problem " + esIdAttributeName, e); - throw e; - } - return this; - } - - /** - * Specify subclass of T for write operations. - * - * @param writeInterfaceClazz - */ - public EsDataObjectReaderWriter2 setWriteInterface(@Nonnull Class writeInterfaceClazz) { - LOG.debug("Set write interface to {}", writeInterfaceClazz); - if (writeInterfaceClazz == null) { - throw new IllegalArgumentException("Null not allowed here."); - } - - this.writeInterfaceClazz = writeInterfaceClazz; - return this; - } - - public interface IdGetter { - String getId(S object); - } - - public void write(List objectList, IdGetter idGetter) { - for (S object : objectList) { - write(object, idGetter.getId(object)); - } - } - - /** - * Write child object to database with specific id - * - * @param object to be written - * @param esId use the id or if null generate unique id - * @return String with id or null - */ - public @Nullable String write(S object, @Nullable String esId) { - if (object != null && writeInterfaceClazz.isInstance(object)) { - try { - String json = yangtoolsMapper.writeValueAsString(object); - return db.doWriteRaw(dataTypeName, esId, json, this.syncAfterWrite); - } catch (JsonProcessingException e) { - LOG.error("Write problem: ", e); - } - } else { - LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", - writeInterfaceClazz.getName()); - } - return null; - } - - /** - * Update partial child object to database with match/term query - * - * @param of object - * @param object to write - * @param query for write of specific attributes - * @return json string with new Object - */ - public @Nullable boolean update(S object, QueryBuilder query) { - if (object != null && writeInterfaceClazz.isInstance(object)) { - try { - String json = yangtoolsMapper.writeValueAsString(object); - return db.doUpdate(this.dataTypeName, json, query); - } catch (JsonProcessingException e) { - LOG.error("Update problem: ", e); - } - } else { - LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", - writeInterfaceClazz.getName()); - } - return false; - } - - /** - * Write/ update partial child object to database with specific id Write if not exists, else update - * - * @param object - * @param esId - * @return String with esId or null - */ - public @Nullable String update(S object, String esId) { - return this.updateOrCreate(object, esId, null); - } - - /** - * See {@link doUpdateOrCreate(String dataTypeName, String esId, String json, List doNotUpdateField) } - */ - public @Nullable String updateOrCreate(S object, String esId, List onlyForInsert) { - if (object != null && writeInterfaceClazz.isInstance(object)) { - try { - String json = yangtoolsMapper.writeValueAsString(object); - return db.doUpdateOrCreate(dataTypeName, esId, json, onlyForInsert); - } catch (JsonProcessingException e) { - LOG.error("Update problem: ", e); - } - } else { - LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", - writeInterfaceClazz.getName()); - } - return null; - } - - /** - * Read object from database, by using the id field - * - * @param object - * @return - */ - public @Nullable T read(String esId) { - @Nullable - T res = null; - if (esId != null) { - String json = db.doReadJsonData(dataTypeName, esId); - if (json != null) { - try { - res = yangtoolsMapper.readValue(json.getBytes(), clazz); - } catch (IOException e) { - LOG.error("Problem: ", e); - } - } else { - LOG.debug("Can not read from DB id {} type {}", esId, dataTypeName); - } - } - return res; - } - - /** - * Remove object - * - * @param esId to identify the object. - * @return success - */ - public boolean remove(String esId) { - return db.doRemove(this.dataTypeName, esId); - } - - public int remove(QueryBuilder query) { - return this.db.doRemove(this.dataTypeName, query); - } - - /** - * Get all elements of related type - * - * @return all Elements - */ - public SearchResult doReadAll() { - return doReadAll(null); - } - - public SearchResult doReadAll(QueryBuilder query) { - return this.doReadAll(query, false); - } - - /** - * Read all existing objects of a type - * - * @param query for the elements - * @return the list of all objects - */ - - public SearchResult doReadAll(QueryBuilder query, boolean ignoreException) { - - SearchResult res = new SearchResult(); - - SearchResult result; - List hits; - if (query != null) { - LOG.debug("read data in {} with query {}", dataTypeName, query.toJSON()); - result = db.doReadByQueryJsonData(dataTypeName, query, ignoreException); - } else { - result = db.doReadAllJsonData(dataTypeName, ignoreException); - } - hits = result.getHits(); - LOG.debug("Read: {} elements: {}", dataTypeName, hits.size()); - - T object; - for (SearchHit hit : hits) { - object = getT(hit.getSourceAsString()); - LOG.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n", hit.getId(), - hit.getSourceAsString(), object); - if (object != null) { - setEsId(object, hit.getId()); - res.add(object); - } else { - LOG.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString()); - } - } - res.setTotal(result.getTotal()); - return res; - } - - /* --------------------------------------------- - * Private functions - */ - - private void setEsId(T object, String esId) { - if (field != null) { - try { - field.set(object, esId); - } catch (IllegalArgumentException | IllegalAccessException e) { - LOG.debug("Field set problem.", e); - } - } - } - - private @Nullable T getT(String jsonString) { - try { - return yangtoolsMapper.readValue(jsonString, clazz); - } catch (IOException e) { - LOG.info("Mapping problem {}:", clazz.getName(), e); - return null; - } - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsConfig.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsConfig.java new file mode 100644 index 000000000..c428b67d3 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsConfig.java @@ -0,0 +1,224 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener; +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EsConfig implements Configuration, IEsConfig { + + private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class); + + public static final String SECTION_MARKER_ES = "es"; + + private static final String PROPERTY_KEY_DBHOSTS = "esHosts"; + private static final String PROPERTY_KEY_TRUSTALLCERTS = "esTrustAllCerts"; + private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds"; + private static final String PROPERTY_KEY_CLUSTER = "esCluster"; + private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds"; + private static final String PROPERTY_KEY_NODE = "esNode"; + private static final String PROPERTY_KEY_AUTH_USERNAME = "esAuthUsername"; + private static final String PROPERTY_KEY_AUTH_PASSWORD = "esAuthPassword"; + private static final String PROPERTY_KEY_FULLSIZE = "esFullsize"; + + + private static String defaultHostinfo = "${SDNRDBURL}"; + private static final String DEFAULT_VALUE_CLUSTER = ""; + /** check db data in this interval [in seconds] 0 deactivated */ + private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0"; + /** keep data for this time [in seconds] 30 days */ + private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L); + private static final String DEFAULT_VALUE_NODE = "elasticsearchnode"; + private static final String DEFAULT_VALUE_DBUSERNAME = "${SDNRDBUSERNAME}"; + private static final String DEFAULT_VALUE_DBPASSWORD = "${SDNRDBPASSWORD}"; + private static final String DEFAULT_VALUE_TRUSTALLCERTS = "${SDNRDBTRUSTALLCERTS}"; + private static final String DEFAULT_VALUE_FULLSIZE = "${SDNRDBFULLSIZEREQUESTS}"; + + private final ConfigurationFileRepresentation configuration; + + public EsConfig(ConfigurationFileRepresentation configuration) { + + this.configuration = configuration; + this.configuration.addSection(SECTION_MARKER_ES); + defaults(); + } + + /* + * Setter + */ + + public void setNode(String nodeName) { + configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName); + } + + /* + * Getter + */ + + public String getNode() { + return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE); + } + + public HostInfo[] getHosts() { + String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS); + return parseHosts(dbHosts); + } + + public void setHosts(HostInfo[] hosts) { + this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts)); + } + + @Override + public String getCluster() { + return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL); + } + + public void setCluster(String cluster) { + configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster); + } + + public boolean hasBasicAuthCredentials() { + return this.getBasicAuthUsername() != null && this.getBasicAuthPassword() != null + && !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty(); + } + + public String getBasicAuthUsername() { + return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME); + } + + public String getBasicAuthPassword() { + return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD); + } + + @Override + public long getArchiveCheckIntervalSeconds() { + return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L); + } + + public boolean trustAllCerts() { + return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS); + } + + public void setArchiveCheckIntervalSeconds(long seconds) { + configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds); + } + + @Override + public long getArchiveLifetimeSeconds() { + return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L); + } + + public void setArchiveLimit(long seconds) { + configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds); + } + + @Override + public String getSectionName() { + return SECTION_MARKER_ES; + } + + @Override + public synchronized void defaults() { + // Add default if not available + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, + DEFAULT_ARCHIVE_LIMIT_SEC); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, + DEFAULT_ARCHIVE_INTERVAL_SEC); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_VALUE_NODE); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME, + DEFAULT_VALUE_DBUSERNAME); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD, + DEFAULT_VALUE_DBPASSWORD); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS, + DEFAULT_VALUE_TRUSTALLCERTS); + configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_FULLSIZE, + DEFAULT_VALUE_FULLSIZE); + + } + + @Override + public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) { + configuration.unregisterConfigChangedListener(archiveCleanService); + } + + @Override + public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) { + configuration.registerConfigChangedListener(archiveCleanService); + } + + /** @TODO Shift to own class **/ + private static String printHosts(HostInfo[] h) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < h.length; i++) { + sb.append(h[i].toUrl()); + if (i != h.length - 1) { + sb.append(","); + } + } + return sb.toString(); + } + + /** @TODO Shift to own class **/ + private static HostInfo[] parseHosts(String string) { + List infos = new ArrayList<>(); + String[] list = string.split(","); + if (list.length > 0) { + for (String item : list) { + try { + URL url = new URL(item); + infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol()))); + } catch (MalformedURLException e) { + LOG.warn("problem parsing url {} : {}", item, e.getMessage()); + } + } + } + HostInfo[] a = new HostInfo[infos.size()]; + return infos.toArray(a); + } + + @Override + public String toString() { + return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()=" + + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds() + + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()=" + + getSectionName() + "]"; + } + + @Override + public boolean doFullsizeRequests() { + return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_FULLSIZE); + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsDataObjectReaderWriter2.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsDataObjectReaderWriter2.java new file mode 100644 index 000000000..a86ecdde1 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/EsDataObjectReaderWriter2.java @@ -0,0 +1,413 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.eclipse.jdt.annotation.NonNull; +import org.onap.ccsdk.features.sdnr.wt.common.database.DatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper2; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to rw yang-tool generated objects into elasticsearch database. For "ES _id" exchange the esIdAddAtributteName + * is used. This attribute mast be of type String and contains for read and write operations the object id. The function + * can be used without id handling. If id handling is required the parameter needs to be specified by class definition + * in yang and setting the name by using setAttributeName() + * + * Due to using Jackson base interfaces the org.eclipse.jdt.annotation.NonNull needs to be used here to get rid of + * warnings + * + * @param Yang tools generated class object. + */ +public class EsDataObjectReaderWriter2 { + + private final Logger LOG = LoggerFactory.getLogger(EsDataObjectReaderWriter2.class); + + /** Typename for elastic search data schema **/ + private String dataTypeName; + + /** Elasticsearch Database client to be used **/ + private DatabaseClient db; + + /** Mapper with configuration to use opendaylight yang-tools builder pattern for object creation **/ + private YangToolsMapper2 yangtoolsMapper; + + /** Class of T as attribute to allow JSON to Class object mapping **/ + private Class clazz; + + /** Field is used to write id. If null no id handling **/ + private @Nullable Field field; + + /** Attribute that is used as id field for the database object **/ + private @Nullable String esIdAddAtributteName; + + /** Interface to be used for write operations. Rule for write: T extends S and **/ + private Class writeInterfaceClazz; // == "S" + /** Flag true to sync this attribute during write always, what is slow and false do not sync */ + private final boolean syncAfterWrite; + + protected boolean doFullsizeRequest; + + /** + * Elasticsearch database read and write for specific class, defined by opendaylight yang-tools. + * + * @param + * @param + * @param db Database access client + * @param dataTypeName typename in database schema + * @param clazz class of type to be handled + * @param builderClazz class to build related object if builder pattern should be used. + * @param syncAfterWrite + * @throws ClassNotFoundException + */ + public > EsDataObjectReaderWriter2(DatabaseClient db, + String dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz, boolean syncAfterWrite) + throws ClassNotFoundException { + LOG.info("Create {} for datatype {} class {}", this.getClass().getName(), dataTypeName, clazz.getName()); + + this.esIdAddAtributteName = null; + this.field = null; + this.writeInterfaceClazz = clazz; + this.db = db; + this.dataTypeName = dataTypeName; + this.yangtoolsMapper = new YangToolsMapper2<>(clazz, builderClazz); + this.clazz = clazz; + this.syncAfterWrite = syncAfterWrite; + this.doFullsizeRequest = false; + } + + public void setFullsizeRequest(boolean fullsizeRequest) { + this.doFullsizeRequest = fullsizeRequest; + } + public > EsDataObjectReaderWriter2(DatabaseClient db, + Entity dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz) + throws ClassNotFoundException { + this(db, dataTypeName.getName(), clazz, builderClazz, false); + } + + public > EsDataObjectReaderWriter2(DatabaseClient db, + Entity dataTypeName, @Nonnull Class clazz, @Nullable Class builderClazz, boolean syncAfterWrite) + throws ClassNotFoundException { + this(db, dataTypeName.getName(), clazz, builderClazz, syncAfterWrite); + } + + public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, @Nonnull Class clazz, + boolean syncAfterWrite) throws ClassNotFoundException { + this(db, dataTypeName.getName(), clazz, null, syncAfterWrite); + } + + public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, Class clazz) + throws ClassNotFoundException { + this(db, dataTypeName.getName(), clazz, null, false); + } + + /** + * Get Datatype name + * + * @return string with dataTypeName + */ + public String getDataTypeName() { + return dataTypeName; + } + + /** + * Simlar to {@link #setEsIdAttributeName()}, but adapts the parameter to yangtools attribute naming schema + * + * @param esIdAttributeName is converted to UnderscoreCamelCase + * @return this for further operations. + */ + public EsDataObjectReaderWriter2 setEsIdAttributeNameCamelized(String esIdAttributeName) { + return setEsIdAttributeName(YangToolsMapperHelper.toCamelCaseAttributeName(esIdAttributeName)); + } + + /** + * Attribute name of class that is containing the object id + * + * @param esIdAttributeName of the implementation class for the yangtools interface. Expected attribute name format + * is CamelCase with leading underline. @ + * @return this for further operations. + * @throws SecurityException if no access or IllegalArgumentException if wrong type or no attribute with this name. + */ + public EsDataObjectReaderWriter2 setEsIdAttributeName(String esIdAttributeName) { + LOG.debug("Set attribute '{}'", esIdAttributeName); + this.esIdAddAtributteName = null; // Reset status + this.field = null; + + Field attributeField; + try { + Builder builder = yangtoolsMapper.getBuilder(clazz); + if (builder == null) { + String msg = "No builder for " + clazz; + LOG.debug(msg); + throw new IllegalArgumentException(msg); + } else { + T object = builder.build(); + attributeField = object.getClass().getDeclaredField(esIdAttributeName); + if (attributeField.getType().equals(String.class)) { + attributeField.setAccessible(true); + this.esIdAddAtributteName = esIdAttributeName; // Set new status if everything OK + this.field = attributeField; + } else { + String msg = "Wrong field type " + attributeField.getType().getName() + " of " + esIdAttributeName; + LOG.debug(msg); + throw new IllegalArgumentException(msg); + } + } + } catch (NoSuchFieldException e) { + // Convert to run-time exception + String msg = "NoSuchFieldException for '" + esIdAttributeName + "' in class " + clazz.getName(); + LOG.debug(msg); + throw new IllegalArgumentException(msg); + } catch (SecurityException e) { + LOG.debug("Access problem " + esIdAttributeName, e); + throw e; + } + return this; + } + + /** + * Specify subclass of T for write operations. + * + * @param writeInterfaceClazz + */ + public EsDataObjectReaderWriter2 setWriteInterface(@Nonnull Class writeInterfaceClazz) { + LOG.debug("Set write interface to {}", writeInterfaceClazz); + if (writeInterfaceClazz == null) { + throw new IllegalArgumentException("Null not allowed here."); + } + + this.writeInterfaceClazz = writeInterfaceClazz; + return this; + } + + public interface IdGetter { + String getId(S object); + } + + public void write(List objectList, IdGetter idGetter) { + for (S object : objectList) { + write(object, idGetter.getId(object)); + } + } + + /** + * Write child object to database with specific id + * + * @param object to be written + * @param esId use the id or if null generate unique id + * @return String with id or null + */ + public @Nullable String write(S object, @Nullable String esId) { + if (object != null && writeInterfaceClazz.isInstance(object)) { + try { + String json = yangtoolsMapper.writeValueAsString(object); + return db.doWriteRaw(dataTypeName, esId, json, this.syncAfterWrite); + } catch (JsonProcessingException e) { + LOG.error("Write problem: ", e); + } + } else { + LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", + writeInterfaceClazz.getName()); + } + return null; + } + + /** + * Update partial child object to database with match/term query + * + * @param of object + * @param object to write + * @param query for write of specific attributes + * @return json string with new Object + */ + public @Nullable boolean update(S object, QueryBuilder query) { + if (object != null && writeInterfaceClazz.isInstance(object)) { + try { + String json = yangtoolsMapper.writeValueAsString(object); + return db.doUpdate(this.dataTypeName, json, query); + } catch (JsonProcessingException e) { + LOG.error("Update problem: ", e); + } + } else { + LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", + writeInterfaceClazz.getName()); + } + return false; + } + + /** + * Write/ update partial child object to database with specific id Write if not exists, else update + * + * @param object + * @param esId + * @return String with esId or null + */ + public @Nullable String update(S object, String esId) { + return this.updateOrCreate(object, esId, null); + } + + /** + * See {@link doUpdateOrCreate(String dataTypeName, String esId, String json, List doNotUpdateField) } + */ + public @Nullable String updateOrCreate(S object, String esId, List onlyForInsert) { + if (object != null && writeInterfaceClazz.isInstance(object)) { + try { + String json = yangtoolsMapper.writeValueAsString(object); + return db.doUpdateOrCreate(dataTypeName, esId, json, onlyForInsert); + } catch (JsonProcessingException e) { + LOG.error("Update problem: ", e); + } + } else { + LOG.error("Type {} does not provide interface {}", object != null ? object.getClass().getName() : "null", + writeInterfaceClazz.getName()); + } + return null; + } + + /** + * Read object from database, by using the id field + * + * @param object + * @return + */ + public @Nullable T read(String esId) { + @Nullable + T res = null; + if (esId != null) { + String json = db.doReadJsonData(dataTypeName, esId); + if (json != null) { + try { + res = yangtoolsMapper.readValue(json.getBytes(), clazz); + } catch (IOException e) { + LOG.error("Problem: ", e); + } + } else { + LOG.debug("Can not read from DB id {} type {}", esId, dataTypeName); + } + } + return res; + } + + /** + * Remove object + * + * @param esId to identify the object. + * @return success + */ + public boolean remove(String esId) { + return db.doRemove(this.dataTypeName, esId); + } + + public int remove(QueryBuilder query) { + return this.db.doRemove(this.dataTypeName, query); + } + + /** + * Get all elements of related type + * + * @return all Elements + */ + public SearchResult doReadAll() { + return doReadAll(null); + } + + public SearchResult doReadAll(QueryBuilder query) { + return this.doReadAll(query, false); + } + + /** + * Read all existing objects of a type + * + * @param query for the elements + * @return the list of all objects + */ + + public SearchResult doReadAll(QueryBuilder query, boolean ignoreException) { + + if(this.doFullsizeRequest) { + query.doFullsizeRequest(); + } + SearchResult res = new SearchResult(); + SearchResult result; + List hits; + if (query != null) { + LOG.debug("read data in {} with query {}", dataTypeName, query.toJSON()); + result = db.doReadByQueryJsonData(dataTypeName, query, ignoreException); + } else { + result = db.doReadAllJsonData(dataTypeName, ignoreException); + } + hits = result.getHits(); + LOG.debug("Read: {} elements: {}", dataTypeName, hits.size()); + + T object; + for (SearchHit hit : hits) { + object = getT(hit.getSourceAsString()); + LOG.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n", hit.getId(), + hit.getSourceAsString(), object); + if (object != null) { + setEsId(object, hit.getId()); + res.add(object); + } else { + LOG.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString()); + } + } + res.setTotal(result.getTotal()); + return res; + } + + /* --------------------------------------------- + * Private functions + */ + + private void setEsId(T object, String esId) { + if (field != null) { + try { + field.set(object, esId); + } catch (IllegalArgumentException | IllegalAccessException e) { + LOG.debug("Field set problem.", e); + } + } + } + + private @Nullable T getT(String jsonString) { + try { + return yangtoolsMapper.readValue(jsonString, clazz); + } catch (IOException e) { + LOG.info("Mapping problem {}:", clazz.getName(), e); + return null; + } + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessor.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessor.java new file mode 100644 index 000000000..ec467eab1 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessor.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor; + +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsDataObjectReaderWriter2; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryByFilter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryResult; +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.EntityInput; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataObjectAcessor extends EsDataObjectReaderWriter2 { + + private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessor.class); + + DataObjectAcessor(HtDatabaseClient dbClient, Entity entity, Class clazz, boolean doFullsizeRequest) + throws ClassNotFoundException { + super(dbClient, entity, clazz); + this.setFullsizeRequest(doFullsizeRequest); + LOG.info("Create {}", this.getClass().getName()); + } + + public QueryResult getData(EntityInput input) { + + QueryByFilter queryByFilter = new QueryByFilter(input); + QueryBuilder queryBuilder = queryByFilter.getQueryBuilderByFilter(); + // Exception handling during user input for invalid filter input. + // This wrong filter by user is allowed an results into empty data. + boolean ignoreException = queryBuilder.contains("range"); + + LOG.debug("Request: {} filter {} ignoreException{}:", this.getDataTypeName(), queryByFilter, ignoreException); + + SearchResult result = doReadAll(queryBuilder, ignoreException); + return new QueryResult<>(queryByFilter, result); + } +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorPm.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorPm.java new file mode 100644 index 000000000..04849004f --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorPm.java @@ -0,0 +1,131 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor; + +import java.io.IOException; +import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryByFilter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryResult; +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.EntityInput; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataObjectAcessorPm extends DataObjectAcessor { + + private final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); + + private static final String UUID_KEY = "uuid-interface"; + private static final String NODE_KEY = "node-name"; + private static final String KEY = "node-name"; + + + public enum Intervall { + PMDATA15M("historicalperformance15min", "historicalperformance15min"), PMDATA24H("historicalperformance24h", + "historicalperformance24h"); + + String index; + String type; + + Intervall(String index, String type) { + this.index = index; + this.type = type; + } + + public String getIndex() { + return index; + } + + public String getType() { + return type; + } + } + + private ExtRestClient dbClient; + private Intervall mode; + + public DataObjectAcessorPm(HtDatabaseClient dbClient, Intervall mode, Entity entity, Class clazz, + boolean doFullsizeRequest) throws ClassNotFoundException { + super(dbClient, entity, clazz, doFullsizeRequest); + LOG.info("DataObjectAcessorPm"); + this.dbClient = dbClient; + this.mode = mode; + } + + /** + * get aggregated list of ltps for filter NODE_KEY + * + * @param input + * @return + * @throws IOException + */ + public QueryResult getDataLtpList(EntityInput input) throws IOException { + + QueryByFilter queryByFilter = new QueryByFilter(input); + SearchRequest request = queryByFilter.getSearchRequestByFilter(NODE_KEY, UUID_KEY, mode.getIndex(), + mode.getType(), this.doFullsizeRequest); + try { + SearchResponse response = this.dbClient.search(request); + AggregationEntries aggs = response.getAggregations(UUID_KEY); + String[] uuids = + aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex()); + long totalSize = aggs.size(); + return new QueryResult(queryByFilter.getPage(), queryByFilter.getPageSize(), + new SearchResult(uuids, totalSize)); + } catch (IOException e) { + throw new IOException("problem reading ltps for req=" + request, e); + } + } + + /** + * get aggregated devices list + * + * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name') + * @return + * @throws IOException + */ + public QueryResult getDataDeviceList(EntityInput input) throws IOException { + + QueryByFilter queryByFilter = new QueryByFilter(input); + SearchRequest request = + queryByFilter.getSearchRequestBySortOrder(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType(), this.doFullsizeRequest); + try { + SearchResponse response = this.dbClient.search(request); + AggregationEntries aggs = response.getAggregations(KEY); + String[] uuids = + aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex()); + long totalSize = aggs.size(); + return new QueryResult(queryByFilter.getPage(), queryByFilter.getPageSize(), + new SearchResult(uuids, totalSize)); + } catch (IOException e) { + throw new IOException("problem reading nodes for req=" + request, e); + } + + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorStatus.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorStatus.java new file mode 100644 index 000000000..5811550e6 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorStatus.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor; + +import java.io.IOException; +import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; +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.Entity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.Data; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.DataBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.FaultsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.NetworkElementConnectionsBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; + +public class DataObjectAcessorStatus extends DataObjectAcessor { + + final String ESDATATYPE_FAULTCURRENT_SEVERITY_KEY = "severity"; + final String ESDATATYPE_NECON_CONNECTIONSTATE_KEY = "status"; + + private final ExtRestClient dbClient; + + public DataObjectAcessorStatus(HtDatabaseClient dbClient, Entity entity, boolean doFullsizeRequests) + throws ClassNotFoundException { + super(dbClient, entity, Data.class, doFullsizeRequests); + this.dbClient = dbClient; + + } + + public QueryResult getDataStatus() throws IOException { + SearchRequest request = getNewInstanceOfSearchRequest(Entity.Faultcurrent); + QueryBuilder query = QueryBuilders.matchAllQuery().aggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY).size(0); + if(this.doFullsizeRequest) { + query.doFullsizeRequest(); + } + request.setQuery(query); + SearchResponse response = this.dbClient.search(request); + AggregationEntries aggs = response.getAggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY); + + DataBuilder builder = new DataBuilder().setFaults( + new FaultsBuilder().setCriticals(YangHelper2.getLongOrUint32(aggs.getOrDefault("Critical", 0L))) + .setMajors(YangHelper2.getLongOrUint32(aggs.getOrDefault("Major", 0L))) + .setMinors(YangHelper2.getLongOrUint32(aggs.getOrDefault("Minor", 0L))) + .setWarnings(YangHelper2.getLongOrUint32(aggs.getOrDefault("Warning", 0L))).build()); + + request = getNewInstanceOfSearchRequest(Entity.NetworkelementConnection); + query = QueryBuilders.matchAllQuery().aggregations(ESDATATYPE_NECON_CONNECTIONSTATE_KEY).size(0); + if(this.doFullsizeRequest) { + query.doFullsizeRequest(); + } + request.setQuery(query); + response = this.dbClient.search(request); + aggs = response.getAggregations(ESDATATYPE_NECON_CONNECTIONSTATE_KEY); + builder.setNetworkElementConnections(new NetworkElementConnectionsBuilder() + .setConnected(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Connected.getName(), 0L))) + .setConnecting(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Connecting.getName(), 0L))) + .setDisconnected(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Disconnected.getName(), 0L))) + .setUnableToConnect( + Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.UnableToConnect.getName(), 0L))) + .setMounted(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Mounted.getName(), 0L))) + .setUnmounted(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Unmounted.getName(), 0L))) + .setUndefined(Uint32.valueOf(aggs.getOrDefault(ConnectionLogStatus.Undefined.getName(), 0L))) + .setTotal(Uint32.valueOf(response.getTotal())).build()); + + long toalsize = 1; + return new QueryResult(1L, 1L, new SearchResult(new Data[] {builder.build()}, toalsize)); + + } + + + private static SearchRequest getNewInstanceOfSearchRequest(Entity entity) { + return new SearchRequest(entity.getName(), entity.getName()); + } + + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorWithId.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorWithId.java new file mode 100644 index 000000000..da2b4b151 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/acessor/DataObjectAcessorWithId.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor; + +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataObjectAcessorWithId extends DataObjectAcessor { + + private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorWithId.class); + + public DataObjectAcessorWithId(HtDatabaseClient dbClient, Entity entity, Class clazz, boolean doFullsizeRequest) + throws ClassNotFoundException { + super(dbClient, entity, clazz, doFullsizeRequest); + setEsIdAttributeName("_id"); + LOG.info("Create {}", this.getClass().getName()); + } +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/FaultEntityManager.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/FaultEntityManager.java new file mode 100644 index 000000000..36ab39845 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/FaultEntityManager.java @@ -0,0 +1,98 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Fault; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; + +public class FaultEntityManager { + + private static final Pattern pattern = Pattern.compile(".*\\[layerProtocol=(.*)\\]"); + + /** + * The leading indication for notification or events that are not in the currentProblem data of the ONF Coremodel + */ + private static final String NOCURRENTPROBLEMINDICATION = "#"; + + /** + * Specific problems are not moving into current problem list + * + * @param problemName to be verified + * @return true if problem is current + */ + public static boolean isManagedAsCurrentProblem(String problemName) { + return !problemName.startsWith(NOCURRENTPROBLEMINDICATION); + } + + public static boolean isManagedAsCurrentProblem(Fault problem) { + return isManagedAsCurrentProblem(problem.getProblem()); + } + + /** + * Specific problems are not moving into current problem list + * + * @param fault to be verified + * @return true if cleared indication + */ + public static boolean isNoAlarmIndication(@Nonnull Fault fault) { + return SeverityType.NonAlarmed.equals(fault.getSeverity()); + } + + /** + * Create a specific ES id for the current log. + * + * @return a string with the generated ES Id + */ + public static String genSpecificEsId(String nodeName, String objectId, String problemName) { + + String uuId; + + Matcher matcher = pattern.matcher(objectId); + if (matcher.matches() && matcher.groupCount() == 1) { + uuId = matcher.group(1); + } else { + uuId = objectId; + } + + StringBuffer strBuf = new StringBuffer(); + strBuf.append(nodeName); + strBuf.append("/"); + strBuf.append(uuId); + strBuf.append("/"); + strBuf.append(problemName); + return strBuf.toString(); + } + + /** + * Create Es id + * + * @param fault used to create uuid for faultcurrent + * @return String with Id + */ + public static String genSpecificEsId(FaultcurrentEntity fault) { + return genSpecificEsId(fault.getNodeId(), fault.getObjectId(), fault.getProblem()); + } +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseEventsService.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseEventsService.java new file mode 100644 index 000000000..41e94e3f6 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseEventsService.java @@ -0,0 +1,638 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import javax.annotation.Nonnull; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsDataObjectReaderWriter2; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.ArchiveCleanProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataInconsistencyException; +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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; +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.ConnectionlogBuilder; +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.EventlogBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GranularityPeriodType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; +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.InventoryBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder; +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.PmdataEntityBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInputBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Event service, writing all events into the database into the appropriate index. + * + * @author herbert + */ +public class HtDatabaseEventsService implements ArchiveCleanProvider, DataProvider { + private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseEventsService.class); + + private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter(); + + private HtDatabaseClient client; + private EsDataObjectReaderWriter2 eventRWEventLogDevicemanager; + private EsDataObjectReaderWriter2 eventRWEquipment; + private EsDataObjectReaderWriter2 eventRWFaultCurrentDB; + private EsDataObjectReaderWriter2 eventRWFaultLogDB; + private EsDataObjectReaderWriter2 eventRWConnectionLogDB; + private final EsDataObjectReaderWriter2 networkelementConnectionDB; + private final EsDataObjectReaderWriter2 guiCutThroughDB; + private final EsDataObjectReaderWriter2 pmData15mDB; + private final EsDataObjectReaderWriter2 pmData24hDB; + + @SuppressWarnings("unused") + private final DatabaseDataProvider dataProvider; + // --- Construct and initialize + + public HtDatabaseEventsService(HtDatabaseClient client, DatabaseDataProvider elasticSearchDataProvider) + throws Exception { + + LOG.info("Create {} start", HtDatabaseEventsService.class); + this.dataProvider = elasticSearchDataProvider; + + try { + // Create control structure + this.client = client; + + eventRWEventLogDevicemanager = new EsDataObjectReaderWriter2<>(client, Entity.Eventlog, + EventlogEntity.class, EventlogBuilder.class); + + eventRWEquipment = new EsDataObjectReaderWriter2<>(client, Entity.Inventoryequipment, InventoryEntity.class, + InventoryBuilder.class); + + eventRWFaultCurrentDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultcurrent, + FaultcurrentEntity.class, FaultcurrentBuilder.class); + + eventRWFaultLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultlog, FaultlogEntity.class, + FaultlogBuilder.class); + + eventRWConnectionLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Connectionlog, + ConnectionlogEntity.class, ConnectionlogBuilder.class); + + networkelementConnectionDB = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection, + NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class, true) + .setEsIdAttributeName("_id"); + + guiCutThroughDB = new EsDataObjectReaderWriter2<>(client, Entity.Guicutthrough, GuicutthroughEntity.class, + GuicutthroughBuilder.class); + + pmData15mDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance15min, PmdataEntity.class, + PmdataEntityBuilder.class); + + pmData24hDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance24h, PmdataEntity.class, + PmdataEntityBuilder.class); + + } catch (Exception e) { + LOG.error("Can not start database client. Exception: {}", e); + throw new Exception("Can not start database client. Exception: {}", e); + } + LOG.info("Create {} finished. DB Service {} started.", HtDatabaseEventsService.class, + client != null ? "sucessfully" : "not"); + } + + // --- Function + + // -- Connection log + @Override + public void writeConnectionLog(ConnectionlogEntity event) { + if (assertIfClientNull(event)) { + return; + } + LOG.debug("Write event: {}", event); + eventRWConnectionLogDB.write(event, null); + + } + + // -- Event log + @Override + public void writeEventLog(EventlogEntity event) { + if (assertIfClientNull("No client to write {}", event)) { + return; + } + + LOG.debug("Write event: {}", event.toString()); + eventRWEventLogDevicemanager.write(event, null); + } + + // -- Fault log + + @Override + public void writeFaultLog(FaultlogEntity fault) { + if (assertIfClientNull(fault)) { + return; + } + + LOG.debug("Write fault to faultlog: {}", fault.toString()); + eventRWFaultLogDB.write(fault, null); + } + + // -- Fault current + + @Override + public void updateFaultCurrent(FaultcurrentEntity fault) { + if (assertIfClientNull(fault)) { + return; + } + + if (FaultEntityManager.isManagedAsCurrentProblem(fault)) { + if (FaultEntityManager.isNoAlarmIndication(fault)) { + LOG.debug("Remove from currentFaults: {}", fault.toString()); + eventRWFaultCurrentDB.remove(FaultEntityManager.genSpecificEsId(fault)); + } else { + LOG.debug("Write to currentFaults: {}", fault.toString()); + eventRWFaultCurrentDB.write(fault, FaultEntityManager.genSpecificEsId(fault)); + } + } else { + LOG.debug("Ingnore for currentFaults: {}", fault.toString()); + } + } + + /** + * Remove all entries for one node + * + * @param nodeName contains the mountpointname + * @return number of deleted entries + */ + @Override + public int clearFaultsCurrentOfNode(String nodeName) { + if (assertIfClientNullForNodeName(nodeName)) { + return -1; + } + + LOG.debug("Remove from currentFaults all faults for node: {}", nodeName); + return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNode(nodeName)); + } + + /** + * Remove all entries for one node + * + * @param nodeName contains the mountpointname + * @param objectId of element to be deleted + * @return number of deleted entries + */ + @Override + public int clearFaultsCurrentOfNodeWithObjectId(String nodeName, String objectId) { + if (assertIfClientNullForNodeName(nodeName)) { + return -1; + } + + LOG.debug("Remove from currentFaults all faults for node/objectId: {}/{}", nodeName, objectId); + return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNodeAndObjectId(nodeName, objectId)); + } + + /** + * Deliver list with all mountpoint/node-names in the database. + * + * @return List of all mountpoint/node-names the had active alarms. + */ + @Override + public @Nonnull List getAllNodesWithCurrentAlarms() { + if (assertIfClientNull("No DB, can not delete for all nodes", null)) { + return new ArrayList<>(); + } + + LOG.debug("Remove from currentFaults faults for all node"); + List nodeNames = new ArrayList<>(); + + for (FaultcurrentEntity fault : eventRWFaultCurrentDB.doReadAll().getHits()) { + String nodeName = fault.getNodeId(); + if (!nodeNames.contains(nodeName)) { + // this.clearFaultsCurrentOfNode(nodeName); -> Function shifted + nodeNames.add(nodeName); + } + } + return nodeNames; + } + + // -- Inventory and equipment current + + /** + * write internal equipment to database + * + * @param internalEquipment with mandatory fields. + */ + + private void writeInventory(Inventory internalEquipment) { + + if (internalEquipment.getManufacturerIdentifier() == null) { + internalEquipment = new InventoryBuilder(internalEquipment).setManufacturerIdentifier("").build(); + } + if (internalEquipment.getDate() == null) { + internalEquipment = new InventoryBuilder(internalEquipment).setDate("").build(); + } + eventRWEquipment.write(internalEquipment, internalEquipment.getNodeId() + "/" + internalEquipment.getUuid()); + } + + /** + * write internal equipment to database + * + * @param nodeId + * @param list + */ + @Override + public void writeInventory(String nodeId, List list) { + + try { + checkConsistency(nodeId, list); + } catch (DataInconsistencyException e) { + LOG.warn("inventory list for node {} is not consistent", nodeId, e); + list = e.getRepairedList(); + } + + for (Inventory internalEquipment : list) { + this.writeInventory(internalEquipment); + } + } + + private static void checkConsistency(String nodeId, List list) throws DataInconsistencyException { + final String UNBOUND_INVENTORY_UUID = "unbound"; + List failures = new ArrayList<>(); + long treeLevel; + int failCounter = 0; + Map repairList = new HashMap<>(); + InventoryBuilder repairedItem; + InventoryBuilder unboundItem = new InventoryBuilder().setNodeId(nodeId).setUuid(UNBOUND_INVENTORY_UUID) + .setTreeLevel(Uint32.valueOf(0));; + for (Inventory item : list) { + repairedItem = new InventoryBuilder(item); + // check missing tree-level + if (!nodeId.equals(item.getNodeId())) { + failures.add(String.format("missing node-id for equipment(uuid=%s)", item.getUuid())); + repairedItem.setNodeId(nodeId); + failCounter++; + } + if (item.getTreeLevel() == null) { + failures.add(String.format("missing tree-level for equipment(uuid=%s)", item.getUuid())); + repairedItem.setTreeLevel(Uint32.valueOf(1)); + failCounter++; + + } else { + treeLevel = item.getTreeLevel().longValue(); + if (treeLevel > 0) { + // check non root elem and missing parent + if (item.getParentUuid() == null) { + failures.add(String.format("Non root level element (uuid=%s) has to have a parent element", + item.getUuid())); + failCounter++; + repairedItem.setParentUuid(UNBOUND_INVENTORY_UUID); + repairList.put(unboundItem.getUuid(), unboundItem.build()); + } + // check that parent exists in list and is tree-level -1 + else { + Optional parent = + list.stream().filter(e -> item.getParentUuid().equals(e.getUuid())).findFirst(); + if (parent.isEmpty()) { + failures.add(String.format("no parent found for uuid=%s with parent-uuid=%s", + item.getUuid(), item.getParentUuid())); + repairedItem.setParentUuid(UNBOUND_INVENTORY_UUID); + failCounter++; + } + } + } + // check for duplicated uui + Optional duplicate = list + .stream().filter(e -> !item.equals(e) && item.getUuid() != null + && item.getUuid().equals(e.getUuid()) && repairList.containsKey(e.getUuid())) + .findFirst(); + if (duplicate.isPresent()) { + failures.add(String.format("found duplicate uuid=%s", item.getUuid())); + failCounter++; + continue; + + } + if (failCounter > 0) { + repairList.put(repairedItem.getUuid(), repairedItem.build()); + } else { + repairList.put(item.getUuid(), item); + } + } + } + + if (failures.size() > 0) { + throw new DataInconsistencyException(new ArrayList<>(repairList.values()), + "inventory list is not consistent;\n" + String.join("\n", failures)); + } + } + + // -- Networkelement + + /** + * join base with parameters of toJoin (only non null values) + * + * @param base base object + * @param toJoin object with new property values + * @return new joined object + */ + @SuppressWarnings("unused") + private NetworkElementConnectionEntity joinNe(NetworkElementConnectionEntity base, + NetworkElementConnectionEntity toJoin) { + if (base == null) { + return toJoin; + } + NetworkElementConnectionBuilder builder = new NetworkElementConnectionBuilder(base); + if (toJoin != null) { + if (toJoin.isIsRequired() != null) { + builder.setIsRequired(toJoin.isIsRequired()); + } + if (toJoin.getCoreModelCapability() != null) { + builder.setCoreModelCapability(toJoin.getCoreModelCapability()); + } + if (toJoin.getDeviceType() != null) { + builder.setDeviceType(toJoin.getDeviceType()); + } + if (toJoin.getHost() != null) { + builder.setHost(toJoin.getHost()); + } + if (toJoin.getNodeDetails() != null) { + builder.setNodeDetails(toJoin.getNodeDetails()); + } + if (toJoin.getPassword() != null) { + builder.setPassword(toJoin.getPassword()); + } + if (toJoin.getPort() != null) { + builder.setPort(toJoin.getPort()); + } + if (toJoin.getStatus() != null) { + builder.setStatus(toJoin.getStatus()); + } + if (toJoin.getUsername() != null) { + builder.setUsername(toJoin.getUsername()); + } + } + return builder.build(); + } + + /** + * + * @param networkElementConnectionEntitiy to wirte to DB + * @param nodeId Id for this DB element + */ + @Override + public boolean updateNetworkConnectionDeviceType(NetworkElementConnectionEntity networkElementConnectionEntitiy, + String nodeId) { + return this.networkelementConnectionDB.update(networkElementConnectionEntitiy, nodeId) != null; + // NetworkElementConnectionEntity e = + // this.networkelementConnectionDB.read(nodeId); + // this.networkelementConnectionDB.write(this.joinNe(e, + // networkElementConnectionEntitiy), nodeId); + } + + /** + * Update after new mountpoint registration + * + * @param networkElementConnectionEntitiy data + * @param nodeId of device (mountpoint name) + */ + @Override + public boolean updateNetworkConnection22(NetworkElementConnectionEntity networkElementConnectionEntitiy, + String nodeId) { + LOG.info("update networkelement-connection for {} with data {}", nodeId, networkElementConnectionEntitiy); + return this.networkelementConnectionDB.updateOrCreate(networkElementConnectionEntitiy, nodeId, + Arrays.asList("is-required", "username", "password")) != null; + // NetworkElementConnectionEntity e = + // this.networkelementConnectionDB.read(nodeId); + // this.networkelementConnectionDB.write(this.joinNe(e, + // networkElementConnectionEntitiy), nodeId); + + } + + /* please do not remove */ + // public void cleanNetworkElementConnections() { + // this.networkelementConnectionDB.remove(QueryBuilders.matchQuery("is-required", + // false)); + // CreateNetworkElementConnectionInput x = new + // CreateNetworkElementConnectionInputBuilder().setStatus(ConnectionLogStatus.Disconnected).build(); + // this.networkelementConnectionDB.update(x,QueryBuilders.matchAllQuery()); + // } + + @Override + public void removeNetworkConnection(String nodeId) { + Boolean isRequired; + NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId); + if (e != null && (isRequired = e.isIsRequired()) != null) { + if (isRequired) { + LOG.debug("updating connection status for {} of required ne to disconnected", nodeId); + this.networkelementConnectionDB.update(new UpdateNetworkElementConnectionInputBuilder() + .setStatus(ConnectionLogStatus.Disconnected).build(), nodeId); + } else { + LOG.debug("remove networkelement-connection for {} entry because of non-required", nodeId); + this.networkelementConnectionDB.remove(nodeId); + } + } else { + LOG.warn("Unable to update connection-status. dbentry for {} not found in networkelement-connection", + nodeId); + } + } + + // -- Multiple areas + + @Override + public int doIndexClean(Date olderAreOutdated) { + + String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated); + int removed = 0; + + QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp); + removed += eventRWEventLogDevicemanager.remove(queryEventBase); + + QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp); + removed += eventRWFaultLogDB.remove(queryFaultLog); + return removed; + } + + @Override + public long getNumberOfOldObjects(Date olderAreOutdated) { + + String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated); + int numberOfElements = 0; + + QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp); + numberOfElements += eventRWEventLogDevicemanager.doReadAll(queryEventBase).getTotal(); + + QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp); + numberOfElements += eventRWFaultLogDB.doReadAll(queryFaultLog).getTotal(); + + return numberOfElements; + } + + // -- Helper + + /** + * Verify status of client + * + * @param event that is printed with message + * @return true if client is null + */ + private boolean assertIfClientNull(Object event) { + return assertIfClientNull("No DB, can not write: {}", event); + } + + private boolean assertIfClientNullForNodeName(Object object) { + return assertIfClientNull("No DB, can not handle node: {}", object); + } + + /** + * Verify status of client + * + * @param message to print including {} for object printout. + * @return true if client is null + */ + private boolean assertIfClientNull(String message, Object object) { + if (client == null) { + LOG.debug(message, object); + return true; + } + return false; + } + + // ### sub classes + + private static class EsEventBase { + /** + * Query to get older Elements + * + * @param netconfTimeStamp to identify older Elements + * @return QueryBuilder for older elements related to timestamp + */ + private static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) { + return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp); + } + } + + private static class EsFaultLogDevicemanager { + /** + * Get older Elements + * + * @param netconfTimeStamp to identify query elements older than this timestamp. + * @return QueryBuilder for related elements + */ + public static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) { + return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp); + } + } + + public static class EsFaultCurrent { + /** + * @param nodeName name of the node + * @return query builder + */ + public static QueryBuilder getQueryForOneNode(String nodeName) { + return QueryBuilders.matchQuery("node-id", nodeName); + } + + public static QueryBuilder getQueryForOneNodeAndObjectId(String nodeName, String objectId) { + BoolQueryBuilder bq = QueryBuilders.boolQuery(); + bq.must(QueryBuilders.matchQuery("node-id", nodeName)); + bq.must(QueryBuilders.matchQuery("object-id", objectId)); + return bq; + } + } + + @Override + public List getNetworkElementConnections() { + return this.networkelementConnectionDB.doReadAll().getHits(); + } + + @Override + public void doWritePerformanceData(List list) { + + list.forEach(elem -> { + GranularityPeriodType granularityPeriod = nnGetGranularityPeriodType(elem.getGranularityPeriod()); + // _id": "Sim12600/LP-MWPS-TTP-01/2017-07-04T15:15:00.0+00:00" + StringBuffer id = new StringBuffer(); + DateAndTime date = elem.getTimeStamp(); + id.append(elem.getNodeName()); + id.append("/"); + id.append(elem.getUuidInterface()); + id.append("/"); + id.append(date != null ? date.getValue() : "null"); + + switch (granularityPeriod) { + case Period15Min: + pmData15mDB.write(elem, id.toString()); + break; + case Period24Hours: + pmData24hDB.write(elem, id.toString()); + break; + case Unknown: + default: + LOG.debug("Unknown granularity {} id {}", granularityPeriod, id); + break; + } + }); + + } + + @NonNull + GranularityPeriodType nnGetGranularityPeriodType(@Nullable GranularityPeriodType granularityPeriod) { + return granularityPeriod != null ? granularityPeriod : GranularityPeriodType.Unknown; + } + + @Override + public HtDatabaseClient getRawClient() { + return this.client; + } + + @Override + public void writeGuiCutThroughData(Guicutthrough gcData, String nodeId) { + guiCutThroughDB.write(gcData, nodeId); + } + + @Override + public int clearGuiCutThroughEntriesOfNode(String nodeName) { + guiCutThroughDB.remove(nodeName); + return 0; + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseMaintenanceService.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseMaintenanceService.java new file mode 100644 index 000000000..c6161b54c --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/entity/HtDatabaseMaintenanceService.java @@ -0,0 +1,176 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity; + +import java.util.ArrayList; +import java.util.List; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.common.HtAssert; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsDataObjectReaderWriter2; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; +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.MaintenanceBuilder; +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.NetworkElementConnectionBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HtDatabaseMaintenanceService implements HtDatabaseMaintenance { + + private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseMaintenanceService.class); + + private final EsDataObjectReaderWriter2 maintenanceRW; + private final EsDataObjectReaderWriter2 requiredNeRW; + + public HtDatabaseMaintenanceService(@NonNull HtDatabaseClient client) throws ClassNotFoundException { + HtAssert.nonnull(client); + + // Create control structure + maintenanceRW = new EsDataObjectReaderWriter2<>(client, Entity.Maintenancemode, MaintenanceEntity.class, + MaintenanceBuilder.class, true).setEsIdAttributeName("_id"); + + requiredNeRW = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection, + NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class, true) + .setEsIdAttributeName("_id"); + + } + + /** + * Get existing object for mountpoint to manage maintenance mode + * + * @return Object with configuration + */ + @Override + @Nullable + public MaintenanceEntity getMaintenance(@Nullable String mountpointId) { + MaintenanceEntity deviceMaintenanceMode = null; + if (maintenanceRW != null && mountpointId != null) { + deviceMaintenanceMode = maintenanceRW.read(mountpointId); + } + return deviceMaintenanceMode; + } + + @Override + public MaintenanceEntity setMaintenance(MaintenanceEntity m) { + if (maintenanceRW != null) { + if (maintenanceRW.write(m, m.getNodeId()) == null) { + throw new IllegalArgumentException("Problem writing to database: " + m.getId()); + } + LOG.info("Wrote maintenance object {}", m.toString()); + } + return m; + } + + @Override + public List getAll() { + return maintenanceRW != null ? maintenanceRW.doReadAll().getHits() : new ArrayList<>(); + } + + @Override + public MaintenanceEntity createIfNotExists(String mountpointId) { + MaintenanceEntity deviceMaintenanceMode = null; + if (maintenanceRW != null) { + deviceMaintenanceMode = maintenanceRW.read(mountpointId); + if (deviceMaintenanceMode == null) { + LOG.debug("creating empty maintenance object in database"); + deviceMaintenanceMode = getDefaultMaintenance(mountpointId); + maintenanceRW.write(deviceMaintenanceMode, mountpointId); + } else { + LOG.debug("maintenance object already exists in database"); + } + } else { + LOG.warn("cannot create maintenance obj. db reader/writer is null"); + } + return deviceMaintenanceMode; + } + + @Override + public void deleteIfNotRequired(String mountPointNodeName) { + + if (!this.isRequireNe(mountPointNodeName)) { + if (maintenanceRW != null) { + LOG.debug("removing maintenance object in database for " + mountPointNodeName); + maintenanceRW.remove(mountPointNodeName); + } else { + LOG.warn("cannot create maintenance obj. db reader/writer is null"); + } + } + + + } + + /** + * Provide default maintenanceinformation for a device + * + * @param mountpointId nodeId of device + * @return default data + */ + static private MaintenanceEntity getDefaultMaintenance(String mountpointId) { + + DateAndTime now = NetconfTimeStampImpl.getConverter().getTimeStamp(); + + MaintenanceBuilder deviceMaintenanceModeBuilder = new MaintenanceBuilder(); + deviceMaintenanceModeBuilder.setNodeId(mountpointId).setId(mountpointId); + // Use time from mountpoint creation + deviceMaintenanceModeBuilder.setDescription(""); + // Use time from mountpoint creation + deviceMaintenanceModeBuilder.setStart(now); + deviceMaintenanceModeBuilder.setEnd(now); + deviceMaintenanceModeBuilder.setActive(false); + + // Reference to all + //consistent to UI input to null/not empty string + //deviceMaintenanceModeBuilder.setObjectIdRef(""); + //deviceMaintenanceModeBuilder.setProblem(""); + + return deviceMaintenanceModeBuilder.build(); + } + + // -- Private + /** + * Check in required ne if entry exists for mountpointNodeName + * + * @param mountPointNodeName + * @return + */ + @SuppressWarnings("null") + private boolean isRequireNe(String mountPointNodeName) { + NetworkElementConnectionEntity ne = null; + if (requiredNeRW != null) { + LOG.debug("searching for entry in required-networkelement for " + mountPointNodeName); + ne = requiredNeRW.read(mountPointNodeName); + } else { + LOG.warn("cannot read db. no db reader writer initialized"); + } + if (ne != null && ne.isIsRequired() != null) { + return ne.isIsRequired(); + } else { + return false; + } + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilter.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilter.java new file mode 100644 index 000000000..eef61e09c --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilter.java @@ -0,0 +1,436 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.common.YangHelper; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorPm; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; +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.SortOrder; +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.Pagination; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Sortorder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QueryByFilter { + + private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); + private static final List timestampValueNames = Arrays.asList("timestamp", "start", "end"); + + private static List emptySortOrderList = new ArrayList<>(); + private static List emptyFilterList = new ArrayList<>(); + + // Derived from input + private long page; + private long pageSize; + private long fromPage; + private List filterList; + private List sortOrder; + + /** + * Process input from RPC into Queries to database + * + * @param input Input from RPC, for test it could be null + */ + public QueryByFilter(EntityInput input) { + page = -1; + pageSize = -1; + if (input != null) { + @Nullable + Pagination pagination = input.getPagination(); + if (pagination != null) { + @Nullable + Uint64 pageOrNull = YangHelper2.getUint64(pagination.getPage()); + if (pageOrNull != null) { + page = pageOrNull.longValue(); + } + @Nullable + Uint32 pageSizeOrNull = YangHelper2.getUint32(pagination.getSize()); + if (pageSizeOrNull != null) { + pageSize = pageSizeOrNull.longValue(); + } + } + } + if (page < 0) + page = 1; + if (pageSize < 0) + pageSize = 1; + + fromPage = (page - 1) * pageSize; + if (fromPage < 0 || pageSize > 10000) + throw new IllegalArgumentException("mismatching input parameters. From:" + fromPage + " size:" + pageSize); + + filterList = YangHelper.getList(input.getFilter()); + if (filterList == null) + filterList = emptyFilterList; + sortOrder = YangHelper.getList(input.getSortorder()); + if (sortOrder == null) + sortOrder = emptySortOrderList; + + } + + public QueryBuilder getQueryBuilderByFilter() { + return getQueryBuilderByFilter(""); + } + + public QueryBuilder getQueryBuilderByFilter(String prefix) { + QueryBuilder queryBuilder = fromFilter(filterList, prefix).from(fromPage).size(pageSize); + setSortOrder(queryBuilder, sortOrder, prefix); + return queryBuilder; + } + + public SearchRequest getSearchRequestByFilter(String nodeKey, String uuidKey, String index, String dataType, + boolean doFullsizeRequest) { + Filter nodeFilter = getFilter(filterList, nodeKey); + if (nodeFilter != null) { + SearchRequest request = new SearchRequest(index, dataType); + request.setQuery(QueryBuilders.matchQuery(nodeKey, nodeFilter.getFiltervalue()) + .setFullsizeRequest(doFullsizeRequest).aggregations(uuidKey).size(0)); + return request; + } else { + String msg = "no nodename in filter found "; + LOG.debug(msg); + throw new IllegalArgumentException(msg); + } + } + + public SearchRequest getSearchRequestBySortOrder(String nodeKey, String uuidKey, String index, String dataType, + boolean doFullsizeRequest) { + Sortorder soNode = getSortOrder(sortOrder, nodeKey); + SearchRequest request = new SearchRequest(index, dataType); + QueryBuilder query = null; + if (soNode != null) { + query = QueryBuilders.matchAllQuery().setFullsizeRequest(doFullsizeRequest) + .aggregations(nodeKey, convert(soNode.getSortorder())).size(0); + } else { + query = QueryBuilders.matchAllQuery().setFullsizeRequest(doFullsizeRequest).aggregations(nodeKey).size(0); + } + request.setQuery(query); + return request; + } + + public long getPage() { + return page; + } + + public long getPageSize() { + return pageSize; + } + + public long getPageStartIndex() { + return fromPage; + } + + @Override + public String toString() { + return "QueryByFilter [page=" + page + ", pageSize=" + pageSize + ", fromPage=" + fromPage + ", filterList=" + + filterList + ", sortOrder=" + sortOrder + "]"; + } + + /* + * Private and static implementations + */ + private static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder, String prefix) { + if (sortorder != null && sortorder.size() > 0) { + for (Sortorder so : sortorder) { + query.sort(handlePrefix(prefix, so.getProperty()), convert(so.getSortorder())); + } + } + return query; + } + + private static org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder convert(SortOrder sortOrder) { + return sortOrder == SortOrder.Ascending + ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING + : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING; + }; + + private static Sortorder getSortOrder(@Nullable List list, String prop) { + if (list == null) { + return null; + } + for (Sortorder o : list) { + if (prop.equals(o.getProperty())) { + return o; + } + } + return null; + } + + private static Filter getFilter(@Nullable List list, String prop) { + if (list == null) { + return null; + } + for (Filter f : list) { + if (prop.equals(f.getProperty())) { + return f; + } + } + return null; + } + + private static String fillTimeStamp(String value) { + int idx = value.lastIndexOf("*"); + final String REPLACE = "0000-00-00T00:00:00.0Z"; + String s = value.substring(0, idx) + REPLACE.substring(idx); + if (Integer.parseInt(s.substring(5, 7)) == 0) { + s = s.substring(0, 5) + "01-" + s.substring(8); + } + if (Integer.parseInt(s.substring(8, 10)) == 0) { + s = s.substring(0, 8) + "01" + s.substring(10); + } + + return s; + } + + /** + * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: + * 2017-01-01T00:00:00Z, lt:2018-01-01T00:00:00Z + * + * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 + * + */ + private static @Nullable QueryBuilder fromTimestampSearchFilter(String property, String value) { + if (!value.endsWith("*")) { + return null; + } + int idx = value.lastIndexOf("*"); + String lowerEnd = fillTimeStamp(value); + String upperEnd = null; + NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); + Date dt = null; + try { + dt = converter.getDateFromNetconf(lowerEnd); + } catch (Exception e) { + + } + if (dt == null) { + return null; + } + // property.substring(0,idx)+REPLACE.substring(idx+1); + Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + c.setTime(dt); + int tmpvalue; + switch (idx) { + case 1: // (2*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 2: // (20*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 3: // (200*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 4: // (2000*) + case 5: // (2000-*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 6: // switch 10 months (2000-0* or 2000-1*) + tmpvalue = c.get(Calendar.MONTH); + if (tmpvalue < 9) { + c.set(Calendar.MONTH, 9); + } else { + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + c.set(Calendar.MONTH, 0); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + + break; + case 7: // switch one month (2018-01* or 2018-01-*) + case 8: + c.add(Calendar.MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 9: // (2018-01-0*) + tmpvalue = c.get(Calendar.DAY_OF_MONTH); + if (tmpvalue == 1) { + c.set(Calendar.DAY_OF_MONTH, 10); + } else if (tmpvalue == 10) { + c.set(Calendar.DAY_OF_MONTH, 20); + } else if (tmpvalue == 20) { + if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + c.set(Calendar.DAY_OF_MONTH, 30); + } + } else if (tmpvalue == 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + break; + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 10: // (2018-01-01*) + case 11: // (2018-01-01T*) + c.add(Calendar.DAY_OF_MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 12: // (2018-01-01T1*) + tmpvalue = c.get(Calendar.HOUR_OF_DAY); + if (tmpvalue == 20) { + c.set(Calendar.HOUR_OF_DAY, 0); + c.add(Calendar.DAY_OF_MONTH, 1); + } else { + c.add(Calendar.HOUR_OF_DAY, 10); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 13: // (2018-01-01T11*) + case 14: // (2018-01-01T11-*) + c.add(Calendar.HOUR_OF_DAY, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 15: // (2018-01-01T11-3*) + c.add(Calendar.MINUTE, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 16: // (2018-01-01T11-32*) + case 17: // (2018-01-01T11-32-*) + c.add(Calendar.MINUTE, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 18: // (2018-01-01T11-32-1*) + c.add(Calendar.SECOND, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 19: // (2018-01-01T11-32-11*) + case 20: // (2018-01-01T11-32-11.*) + c.add(Calendar.SECOND, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + + default: + break; + } + + if (upperEnd == null) { + return null; + } + return QueryBuilders.rangeQuery(property).gte(lowerEnd).lt(upperEnd); + + } + + private static List collectValues(Filter filter) { + List values = new ArrayList(); + if (filter.getFiltervalue() != null) { + values.add(filter.getFiltervalue()); + } + if (filter.getFiltervalues() != null) { + values.addAll(filter.getFiltervalues()); + } + return values; + } + + private static QueryBuilder fromFilter(@Nullable List filters, String prefix) { + if (filters == null || filters.size() == 0) { + return QueryBuilders.matchAllQuery(); + + } else if (filters.size() == 1) { + String property = filters.get(0).getProperty(); + List values = collectValues(filters.get(0)); + if ("id".equals(property)) { + property = "_id"; + } + if (values.size() == 1) { + return getSinglePropertyQuery(property, values.get(0), prefix); + } else { + BoolQueryBuilder bquery = new BoolQueryBuilder(); + for (String v : values) { + bquery.should(getSinglePropertyQuery(property, v, prefix)); + } + return bquery; + + } + } else { + BoolQueryBuilder query = new BoolQueryBuilder(); + for (Filter fi : filters) { + String p = fi.getProperty(); + List values = collectValues(fi); + if ("id".equals(p)) { + p = "_id"; + } + if (values.size() == 1) { + query.must(getSinglePropertyQuery(p, values.get(0), prefix)); + } else { + BoolQueryBuilder tmpQuery = QueryBuilders.boolQuery(); + for (String v : values) { + tmpQuery.should(getSinglePropertyQuery(p, v, prefix)); + } + query.must(tmpQuery); + tmpQuery = QueryBuilders.boolQuery(); + } + } + LOG.trace("Query result. {}", query.toJSON()); + return query; + } + } + + private static QueryBuilder getSinglePropertyQuery(String p, String v, String prefix) { + QueryBuilder query = null; + if (DbFilter.hasSearchParams(v)) { + if (p != null && timestampValueNames.contains(p.toLowerCase())) { + query = fromTimestampSearchFilter(p, v); + if (query == null) { + query = QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v)); + } + } else { + query = QueryBuilders.regex(handlePrefix(prefix, p), DbFilter.createDatabaseRegex(v)); + } + } else if (DbFilter.isComparisonValid(v)) { + query = DbFilter.getRangeQuery(handlePrefix(prefix, p), v); + if (query == null) { + query = QueryBuilders.matchQuery(handlePrefix(prefix, p), v); + } + } else { + query = QueryBuilders.matchQuery(handlePrefix(prefix, p), v); + } + return query; + } + + private static String handlePrefix(String prefix, String p) { + return (prefix != null ? prefix : "") + p; + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilterStatic.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilterStatic.java new file mode 100644 index 000000000..4e4c1a9c8 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryByFilterStatic.java @@ -0,0 +1,337 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import org.eclipse.jdt.annotation.Nullable; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorPm; +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.EntityInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SortOrder; +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.Sortorder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class QueryByFilterStatic { + + private static final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class); + private static final List timestampValueNames = Arrays.asList("timestamp", "start", "end"); + + private QueryByFilterStatic() { + //Hide + } + + static long getPage(EntityInput input) { + return getPage(input, 1); + } + + private static long getPage(EntityInput input, long defaultValue) { + return input.getPagination() != null ? input.getPagination().getPage().longValue() : defaultValue; + } + + static long getPageSize(EntityInput input) { + return getPageSize(input, 1); + } + + private static long getPageSize(EntityInput input, long defaultValue) { + return input.getPagination() != null ? input.getPagination().getSize().longValue() : defaultValue; + } + + + public static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder) { + return setSortOrder(query, sortorder, ""); + } + + private static QueryBuilder setSortOrder(QueryBuilder query, @Nullable List sortorder, String prefix) { + if (sortorder != null && sortorder.size() > 0) { + for (Sortorder so : sortorder) { + query.sort((prefix != null ? prefix : "") + so.getProperty(), + so.getSortorder() == SortOrder.Ascending + ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING + : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING); + } + } + return query; + + } + + + public static Sortorder getSortOrder(@Nullable List list, String prop) { + if (list == null) { + return null; + } + for (Sortorder o : list) { + if (prop.equals(o.getProperty())) { + return o; + } + } + return null; + } + + public static Filter getFilter(@Nullable List list, String prop) { + if (list == null) { + return null; + } + for (Filter f : list) { + if (prop.equals(f.getProperty())) { + return f; + } + } + return null; + } + + public static QueryBuilder fromFilter(@Nullable List filters) { + return fromFilter(filters, ""); + } + + private static String fillTimeStamp(String value) { + int idx = value.lastIndexOf("*"); + final String REPLACE = "0000-00-00T00:00:00.0Z"; + String s = value.substring(0, idx) + REPLACE.substring(idx); + if (Integer.parseInt(s.substring(5, 7)) == 0) { + s = s.substring(0, 5) + "01-" + s.substring(8); + } + if (Integer.parseInt(s.substring(8, 10)) == 0) { + s = s.substring(0, 8) + "01" + s.substring(10); + } + + return s; + } + + /** + * convert timestamp with ending placeholder in filter to elasticsearch filter e.g. 2017* => gte: + * 2017-01-01T00:00:00Z, lt:2018-01-01T00:00:00Z + * + * 201* => 2010-01... 2020 .. 2018-* => 2018-01... <=> 2019-01 + * + */ + private static @Nullable QueryBuilder fromTimestampSearchFilter(String property, String value) { + if (!value.endsWith("*")) { + return null; + } + int idx = value.lastIndexOf("*"); + String lowerEnd = fillTimeStamp(value); + String upperEnd = null; + NetconfTimeStamp converter = NetconfTimeStampImpl.getConverter(); + Date dt = null; + try { + dt = converter.getDateFromNetconf(lowerEnd); + } catch (Exception e) { + + } + if (dt == null) { + return null; + } + // property.substring(0,idx)+REPLACE.substring(idx+1); + Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + c.setTime(dt); + int tmpvalue; + switch (idx) { + case 1: // (2*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1000); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 2: // (20*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 100); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 3: // (200*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 4: // (2000*) + case 5: // (2000-*) + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 6: //switch 10 months (2000-0* or 2000-1*) + tmpvalue = c.get(Calendar.MONTH); + if (tmpvalue < 9) { + c.set(Calendar.MONTH, 9); + } else { + c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1); + c.set(Calendar.MONTH, 0); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + + break; + case 7: //switch one month (2018-01* or 2018-01-*) + case 8: + c.add(Calendar.MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 9: // (2018-01-0*) + tmpvalue = c.get(Calendar.DAY_OF_MONTH); + if (tmpvalue == 1) { + c.set(Calendar.DAY_OF_MONTH, 10); + } else if (tmpvalue == 10) { + c.set(Calendar.DAY_OF_MONTH, 20); + } else if (tmpvalue == 20) { + if (c.getActualMaximum(Calendar.DAY_OF_MONTH) < 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + c.set(Calendar.DAY_OF_MONTH, 30); + } + } else if (tmpvalue == 30) { + c.set(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MONTH, 1); + } else { + break; + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 10: // (2018-01-01*) + case 11: // (2018-01-01T*) + c.add(Calendar.DAY_OF_MONTH, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 12: // (2018-01-01T1*) + tmpvalue = c.get(Calendar.HOUR_OF_DAY); + if (tmpvalue == 20) { + c.set(Calendar.HOUR_OF_DAY, 0); + c.add(Calendar.DAY_OF_MONTH, 1); + } else { + c.add(Calendar.HOUR_OF_DAY, 10); + } + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 13: // (2018-01-01T11*) + case 14: // (2018-01-01T11-*) + c.add(Calendar.HOUR_OF_DAY, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 15: // (2018-01-01T11-3*) + c.add(Calendar.MINUTE, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 16: // (2018-01-01T11-32*) + case 17: // (2018-01-01T11-32-*) + c.add(Calendar.MINUTE, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 18: // (2018-01-01T11-32-1*) + c.add(Calendar.SECOND, 10); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + case 19: // (2018-01-01T11-32-11*) + case 20: // (2018-01-01T11-32-11.*) + c.add(Calendar.SECOND, 1); + upperEnd = converter.getTimeStampAsNetconfString(c.getTime()); + break; + + default: + break; + } + + if (upperEnd == null) { + return null; + } + return QueryBuilders.rangeQuery(property).gte(lowerEnd).lt(upperEnd); + + } + + private static QueryBuilder fromFilter(@Nullable List filters, String prefix) { + if (filters == null || filters.size() == 0) { + return QueryBuilders.matchAllQuery(); + + } else if (filters.size() == 1) { + QueryBuilder query; + String p = filters.get(0).getProperty(); + String v = filters.get(0).getFiltervalue(); + if ("id".equals(p)) { + p = "_id"; + } else { + // v=v.toLowerCase(); + } + if (DbFilter.hasSearchParams(v)) { + if (p != null && timestampValueNames.contains(p.toLowerCase())) { + query = fromTimestampSearchFilter(p, v); + if (query != null) { + return query; + } + } + return QueryBuilders.regex(p, DbFilter.createDatabaseRegex(v)); + + + } else if (DbFilter.isComparisonValid(v)) { + RangeQueryBuilder q = DbFilter.getRangeQuery((prefix != null ? prefix : "") + p, v); + if (q != null) { + return q; + } else { + return QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v); + } + } else { + return QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v); + } + } else { + BoolQueryBuilder query = new BoolQueryBuilder(); + QueryBuilder tmpQuery; + for (Filter fi : filters) { + String p = fi.getProperty(); + String v = fi.getFiltervalue(); + if ("id".equals(p)) { + p = "_id"; + } else { + // v=v.toLowerCase(); + } + if (DbFilter.hasSearchParams(v)) { + if (p != null && timestampValueNames.contains(p.toLowerCase())) { + tmpQuery = fromTimestampSearchFilter(p, v); + if (tmpQuery != null) { + query.must(tmpQuery); + } else { + query.must(QueryBuilders.regex((prefix != null ? prefix : "") + p, + DbFilter.createDatabaseRegex(v))); + } + } else { + query.must(QueryBuilders.regex((prefix != null ? prefix : "") + p, + DbFilter.createDatabaseRegex(v))); + } + } else if (DbFilter.isComparisonValid(v)) { + RangeQueryBuilder q = DbFilter.getRangeQuery((prefix != null ? prefix : "") + p, v); + if (q != null) { + query.must(q); + } else { + query.must(QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v)); + } + } else { + query.must(QueryBuilders.matchQuery((prefix != null ? prefix : "") + p, v)); + } + } + LOG.trace("Query result. {}", query.toJSON()); + return query; + } + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryResult.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryResult.java new file mode 100644 index 000000000..d4cf0eb53 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/data/rpctypehelper/QueryResult.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper; + +import java.math.BigInteger; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PaginationOutputG; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder; + +public class QueryResult { + + private SearchResult result; + private PaginationOutputG pagination; + + public QueryResult(long page, long pageSize, SearchResult result) { + this.result = result; + + PaginationBuilder x = new PaginationBuilder(); + x.setPage(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(page))); + x.setSize(YangHelper2.getLongOrUint32(pageSize)); + x.setTotal(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(result.getTotal()))); + pagination = x.build(); + } + + public QueryResult(QueryByFilter queryByFilter, SearchResult result) { + this.result = result; + + PaginationBuilder x = new PaginationBuilder(); + x.setPage(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(queryByFilter.getPage()))); + x.setSize(YangHelper2.getLongOrUint32(queryByFilter.getPageSize())); + x.setTotal(YangHelper2.getBigIntegerOrUint64(BigInteger.valueOf(result.getTotal()))); + pagination = x.build(); + } + + + public SearchResult getResult() { + return result; + } + + public PaginationOutputG getPagination() { + return pagination; + } + + @Override + public String toString() { + return "QueryResult [result=" + result + ", pagination=" + pagination + "]"; + } + + + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/ElasticSearchDataProvider.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/ElasticSearchDataProvider.java new file mode 100644 index 000000000..53b21e0ed --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/ElasticSearchDataProvider.java @@ -0,0 +1,520 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorPm; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorPm.Intervall; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorStatus; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.acessor.DataObjectAcessorWithId; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity.HtDatabaseEventsService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity.HtDatabaseMaintenanceService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryResult; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMediatorserver; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInput; +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.CreateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateNetworkElementConnectionOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.DeleteNetworkElementConnectionOutputBuilder; +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.EntityInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough; +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.MediatorServerEntity; +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.ReadConnectionlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadEventlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultcurrentListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadFaultlogListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadGuiCutThroughEntryOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadInventoryListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMaintenanceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadMediatorServerListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadNetworkElementConnectionListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata15mLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hDeviceListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadPmdata24hLtpListOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadStatusOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMaintenanceOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateMediatorServerOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.UpdateNetworkElementConnectionOutputBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ElasticSearchDataProvider implements DatabaseDataProvider { + + private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchDataProvider.class); + + private static final String EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE = "unable to write data to database"; + private static final String EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE = "unable to update data in database"; + private static final String EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE = "unable to remove data from database"; + + private final HtDatabaseClient dbClient; + private final DataObjectAcessorWithId eventRWFaultCurrent; + private final DataObjectAcessorWithId eventRWFaultLog; + private final DataObjectAcessorWithId mediatorserverRW; + private final DataObjectAcessorWithId maintenanceRW; + private final DataObjectAcessorWithId guicutthroughRW; + private final DataObjectAcessorWithId equipmentRW; + private final DataObjectAcessorWithId connnectionlogRW; + private final DataObjectAcessorWithId eventlogRW; + private final DataObjectAcessorWithId networkelementConnectionRW; + private final DataObjectAcessorPm pm15mRW; + private final DataObjectAcessorPm pm24hRW; + + private final DataObjectAcessorStatus readStatus; + private final HtDatabaseEventsService databaseService; + private final HtDatabaseMaintenanceService databaseMaintenanceService; + + private final HtDatabaseMediatorserver dbMediatorServerService = new HtDatabaseMediatorserver() { + + @Override + public List getAll() { + return new ArrayList<>(ElasticSearchDataProvider.this.mediatorserverRW.doReadAll().getHits()); + } + }; + + @Override + public HtDatabaseClient getRawClient() { + return this.dbClient; + } + + public ElasticSearchDataProvider(HostInfo[] hosts) throws Exception { + this(hosts, null, null, HtDatabaseClient.TRUSTALL_DEFAULT, false); + } + + public ElasticSearchDataProvider(EsConfig esConfig) throws Exception { + this(esConfig.getHosts(), esConfig.getBasicAuthUsername(), esConfig.getBasicAuthPassword(), + esConfig.trustAllCerts(), esConfig.doFullsizeRequests()); + } + + public ElasticSearchDataProvider(HostInfo[] hosts, String authUsername, String authPassword, boolean trustAllCerts, + boolean doFullsizeRequests) throws Exception { + super(); + LOG.info("Start {}", this.getClass().getName()); + + + this.dbClient = HtDatabaseClient.getClient(hosts, authUsername, authPassword, trustAllCerts); + this.mediatorserverRW = new DataObjectAcessorWithId<>(dbClient, Entity.MediatorServer, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.Data.class, + doFullsizeRequests); + this.mediatorserverRW.setWriteInterface(MediatorServerEntity.class); + + this.maintenanceRW = new DataObjectAcessorWithId<>(dbClient, Entity.Maintenancemode, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.Data.class, + doFullsizeRequests); + this.maintenanceRW.setWriteInterface(MaintenanceEntity.class); + + this.guicutthroughRW = new DataObjectAcessorWithId<>(dbClient, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity.Guicutthrough, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.Data.class, + doFullsizeRequests); + this.guicutthroughRW.setWriteInterface(Guicutthrough.class); + + this.equipmentRW = new DataObjectAcessorWithId<>(dbClient, Entity.Inventoryequipment, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.Data.class, + doFullsizeRequests); + + this.eventRWFaultCurrent = new DataObjectAcessorWithId<>(dbClient, Entity.Faultcurrent, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.Data.class, + doFullsizeRequests); + + this.eventRWFaultLog = new DataObjectAcessorWithId<>(dbClient, Entity.Faultlog, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.Data.class, + doFullsizeRequests); + + this.connnectionlogRW = new DataObjectAcessorWithId<>(dbClient, Entity.Connectionlog, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.Data.class, + doFullsizeRequests); + + this.eventlogRW = new DataObjectAcessorWithId<>(dbClient, Entity.Eventlog, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.Data.class, + doFullsizeRequests); + + this.networkelementConnectionRW = new DataObjectAcessorWithId<>(dbClient, Entity.NetworkelementConnection, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.Data.class, + doFullsizeRequests); + this.networkelementConnectionRW.setWriteInterface(NetworkElementConnectionEntity.class); + + this.pm15mRW = new DataObjectAcessorPm<>(dbClient, Intervall.PMDATA15M, Entity.Historicalperformance15min, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.Data.class, + doFullsizeRequests); + + this.pm24hRW = new DataObjectAcessorPm<>(dbClient, Intervall.PMDATA24H, Entity.Historicalperformance24h, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.Data.class, + doFullsizeRequests); + + this.readStatus = new DataObjectAcessorStatus(dbClient, Entity.Faultcurrent, doFullsizeRequests); + + this.databaseService = new HtDatabaseEventsService(dbClient, this); + this.databaseMaintenanceService = new HtDatabaseMaintenanceService(dbClient); + } + + /*------------------------- + * Provide access to model API + */ + + + @Override + public ReadFaultcurrentListOutputBuilder readFaultCurrentList(EntityInput input) { + + ReadFaultcurrentListOutputBuilder outputBuilder = new ReadFaultcurrentListOutputBuilder(); + + QueryResult result = + this.eventRWFaultCurrent.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultcurrent.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadFaultlogListOutputBuilder readFaultLogList(EntityInput input) { + ReadFaultlogListOutputBuilder outputBuilder = new ReadFaultlogListOutputBuilder(); + QueryResult result = + this.eventRWFaultLog.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.faultlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadMaintenanceListOutputBuilder readMaintenanceList(EntityInput input) { + ReadMaintenanceListOutputBuilder outputBuilder = new ReadMaintenanceListOutputBuilder(); + QueryResult result = + this.maintenanceRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.maintenance.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadMediatorServerListOutputBuilder readMediatorServerList(EntityInput input) { + + ReadMediatorServerListOutputBuilder outputBuilder = new ReadMediatorServerListOutputBuilder(); + QueryResult result = + this.mediatorserverRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.mediator.server.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadNetworkElementConnectionListOutputBuilder readNetworkElementConnectionList(EntityInput input) { + ReadNetworkElementConnectionListOutputBuilder outputBuilder = + new ReadNetworkElementConnectionListOutputBuilder(); + QueryResult result = + this.networkelementConnectionRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.network.element.connection.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadInventoryListOutputBuilder readInventoryList(EntityInput input) { + ReadInventoryListOutputBuilder outputBuilder = new ReadInventoryListOutputBuilder(); + QueryResult result = + this.equipmentRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.inventory.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadConnectionlogListOutputBuilder readConnectionlogList(EntityInput input) { + ReadConnectionlogListOutputBuilder outputBuilder = new ReadConnectionlogListOutputBuilder(); + QueryResult result = + this.connnectionlogRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.connectionlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadEventlogListOutputBuilder readEventlogList(EntityInput input) throws IOException { + ReadEventlogListOutputBuilder outputBuilder = new ReadEventlogListOutputBuilder(); + QueryResult result = + this.eventlogRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.eventlog.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata15mListOutputBuilder readPmdata15mList(EntityInput input) { + ReadPmdata15mListOutputBuilder outputBuilder = new ReadPmdata15mListOutputBuilder(); + QueryResult result = + this.pm15mRW.getData(input); + LOG.debug("Read data: readPmdata15mList: {}", result); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata24hListOutputBuilder readPmdata24hList(EntityInput input) { + ReadPmdata24hListOutputBuilder outputBuilder = new ReadPmdata24hListOutputBuilder(); + QueryResult result = + this.pm24hRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.list.output.PaginationBuilder( + result.getPagination()).build()); + return outputBuilder; + } + + @Override + public ReadPmdata15mLtpListOutputBuilder readPmdata15mLtpList(EntityInput input) throws IOException { + ReadPmdata15mLtpListOutputBuilder outputBuilder = new ReadPmdata15mLtpListOutputBuilder(); + QueryResult result = pm15mRW.getDataLtpList(input); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.ltp.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult().getHits()); + return outputBuilder; + } + + @Override + public ReadPmdata15mDeviceListOutputBuilder readPmdata15mDeviceList(EntityInput input) throws IOException { + ReadPmdata15mDeviceListOutputBuilder outputBuilder = new ReadPmdata15mDeviceListOutputBuilder(); + QueryResult result = pm15mRW.getDataDeviceList(input); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.device.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult().getHits()); + return outputBuilder; + } + + @Override + public ReadPmdata24hLtpListOutputBuilder readPmdata24hLtpList(EntityInput input) throws IOException { + + QueryResult result = pm24hRW.getDataLtpList(input); + + ReadPmdata24hLtpListOutputBuilder outputBuilder = new ReadPmdata24hLtpListOutputBuilder(); + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder(); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.ltp.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult().getHits()); + return outputBuilder; + } + + @Override + public ReadPmdata24hDeviceListOutputBuilder readPmdata24hDeviceList(EntityInput input) throws IOException { + + QueryResult result = pm24hRW.getDataDeviceList(input); + + ReadPmdata24hDeviceListOutputBuilder outputBuilder = new ReadPmdata24hDeviceListOutputBuilder(); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._24h.device.list.output.PaginationBuilder( + result.getPagination()).build()); + outputBuilder.setData(result.getResult().getHits()); + return outputBuilder; + } + + @Override + public ReadStatusOutputBuilder readStatus() throws IOException { + QueryResult result = + readStatus.getDataStatus(); + + ReadStatusOutputBuilder outputBuilder = new ReadStatusOutputBuilder(); + outputBuilder.setData(result.getResult().getHits()); + return outputBuilder; + } + + @Override + public boolean waitForYellowDatabaseStatus(long timeout, TimeUnit unit) { + return this.dbClient.waitForYellowStatus(unit.toMillis(timeout)); + } + + @Override + public CreateNetworkElementConnectionOutputBuilder createNetworkElementConnection( + NetworkElementConnectionEntity input) throws IOException { + String id = this.networkelementConnectionRW.update(input, input.getNodeId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateNetworkElementConnectionOutputBuilder builder = new CreateNetworkElementConnectionOutputBuilder(); + builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) + .setUsername(input.getUsername()).setPassword(input.getPassword()).setIsRequired(input.isIsRequired()) + .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); + return builder; + } + + @Override + public UpdateNetworkElementConnectionOutputBuilder updateNetworkElementConnection( + UpdateNetworkElementConnectionInput input) throws IOException { + String id = this.networkelementConnectionRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateNetworkElementConnectionOutputBuilder builder = new UpdateNetworkElementConnectionOutputBuilder(); + builder.setId(id).setNodeId(input.getNodeId()).setHost(input.getHost()).setPort(input.getPort()) + .setUsername(input.getUsername()).setPassword(input.getPassword()) + .setCoreModelCapability(input.getCoreModelCapability()).setDeviceType(input.getDeviceType()); + return builder; + } + + @Override + public DeleteNetworkElementConnectionOutputBuilder deleteNetworkElementConnection( + DeleteNetworkElementConnectionInput input) throws IOException { + boolean removed = this.networkelementConnectionRW.remove(input.getId()); + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteNetworkElementConnectionOutputBuilder(); + } + + @Override + public DeleteMediatorServerOutputBuilder deleteMediatorServer(DeleteMediatorServerInput input) throws IOException { + boolean removed = this.mediatorserverRW.remove(input.getId()); + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteMediatorServerOutputBuilder(); + } + + @Override + public DeleteMaintenanceOutputBuilder deleteMaintenance(DeleteMaintenanceInput input) throws IOException { + boolean removed = this.maintenanceRW.remove(input.getId()); + if (!removed) { + throw new IOException(EXCEPTION_UNABLE_TO_REMOVE_FROM_DATABASE); + } + return new DeleteMaintenanceOutputBuilder(); + } + + @Override + public UpdateMaintenanceOutputBuilder updateMaintenance(UpdateMaintenanceInput input) throws IOException { + if (input.getId() == null) { + throw new IOException("please give the id for updating entry"); + } + String id = this.maintenanceRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateMaintenanceOutputBuilder builder = new UpdateMaintenanceOutputBuilder(input).setId(id); + return builder; + } + + @Override + public UpdateMediatorServerOutputBuilder updateMediatorServer(UpdateMediatorServerInput input) throws IOException { + if (input.getId() == null) { + throw new IOException("please give the id for updating entry"); + } + String id = this.mediatorserverRW.update(input, input.getId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_UPDATE_IN_DATABASE); + } + UpdateMediatorServerOutputBuilder builder = new UpdateMediatorServerOutputBuilder(); + builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); + return builder; + } + + @Override + public CreateMaintenanceOutputBuilder createMaintenance(CreateMaintenanceInput input) throws IOException { + String id = this.maintenanceRW.write(input, input.getNodeId()); + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateMaintenanceOutputBuilder builder = new CreateMaintenanceOutputBuilder(input).setId(id); + return builder; + } + + @Override + public CreateMediatorServerOutputBuilder createMediatorServer(CreateMediatorServerInput input) throws IOException { + String id = this.mediatorserverRW.write(input, null); + + if (id == null) { + throw new IOException(EXCEPTION_UNABLE_TO_WRITE_IN_DATABASE); + } + CreateMediatorServerOutputBuilder builder = new CreateMediatorServerOutputBuilder(); + builder.setId(id).setName(input.getName()).setUrl(input.getUrl()); + return builder; + } + + @Override + public ReadGuiCutThroughEntryOutputBuilder readGuiCutThroughEntry(EntityInput input) { + ReadGuiCutThroughEntryOutputBuilder outputBuilder = new ReadGuiCutThroughEntryOutputBuilder(); + QueryResult result = + this.guicutthroughRW.getData(input); + outputBuilder.setData(result.getResult().getHits()); + outputBuilder.setPagination( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.gui.cut.through.entry.output.PaginationBuilder() + .build()); + return outputBuilder; + } + + @Override + public DataProvider getDataProvider() { + return this.databaseService; + } + + @Override + public HtDatabaseMaintenance getHtDatabaseMaintenance() { + return this.databaseMaintenanceService; + } + + @Override + public HtDatabaseMediatorserver getHtDatabaseMediatorServer() { + return dbMediatorServerService; + } + + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/HtUserdataManagerImpl.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/HtUserdataManagerImpl.java new file mode 100644 index 000000000..9dbdb136a --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/elasticsearch/impl/HtUserdataManagerImpl.java @@ -0,0 +1,150 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import org.json.JSONException; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HtUserdataManagerImpl implements HtUserdataManager { + + private static final Logger LOG = LoggerFactory.getLogger(HtUserdataManagerImpl.class); + + private static final String USERDATA_DEFAULTS_FILENAME = "etc/userdata-defaults.json"; + private static final JSONObject USERDATA_DEFAULTS_CONTENT = loadDefaults(); + + private static JSONObject loadDefaults() { + File f = new File(USERDATA_DEFAULTS_FILENAME); + String content; + JSONObject o = null; + if (f.exists()) { + try { + content = Files.readString(f.toPath()); + o = new JSONObject(content); + } catch (IOException e) { + LOG.warn("problem loading defaults: ", e); + } catch (JSONException e) { + LOG.warn("problem parsing defaults: ", e); + } + } + return o; + } + + + private final HtDatabaseClient dbClient; + + public HtUserdataManagerImpl(HtDatabaseClient rawClient) { + this.dbClient = rawClient; + } + + @Override + public String getUserdata(String username) { + SearchResult result = this.dbClient.doReadByQueryJsonData(Entity.Userdata.getName(), + QueryBuilders.matchQuery("_id", username)); + String json = result.getHits().size() > 0 ? result.getHits().get(0).getSourceAsString() : "{}"; + if (USERDATA_DEFAULTS_CONTENT != null) { + JSONObject merge = mergeData(new JSONObject(json), USERDATA_DEFAULTS_CONTENT); + json = merge.toString(); + } + return json; + } + + @Override + public String getUserdata(String username, String key) { + JSONObject o = new JSONObject(this.getUserdata(username)); + return o.has(key) ? o.get(key).toString() : "{}"; + } + + @Override + public boolean setUserdata(String username, String data) { + JSONObject o = new JSONObject(this.getUserdata(username)); + JSONObject merge = mergeData(o, new JSONObject(data)); + return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, merge.toString()) != null; + } + + @Override + public boolean setUserdata(String username, String key, String data) { + JSONObject o = new JSONObject(this.getUserdata(username)); + o = mergeData(o, key, new JSONObject(data)); + return this.dbClient.doWriteRaw(Entity.Userdata.getName(), username, o.toString()) != null; + } + + @Override + public boolean removeUserdata(String username) { + return this.dbClient.doRemove(Entity.Userdata.getName(), username); + } + + @Override + public boolean removeUserdata(String username, String key) { + JSONObject o = new JSONObject(this.getUserdata(username)); + if (o.has(key)) { + o.remove(key); + return this.setUserdata(username, o.toString()); + } + return true; + } + + private static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) { + if (!o.has(key)) { + o.put(key, subObject); + } else { + JSONObject tmp = new JSONObject(); + tmp.put(key, subObject); + o = mergeData(tmp, o); + } + return o; + } + + private static JSONObject mergeData(JSONObject source, JSONObject target) throws JSONException { + String[] keys = JSONObject.getNames(source); + if (keys == null) { + return target; + } + for (String key : keys) { + Object value = source.get(key); + if (!target.has(key)) { + // new value for "key": + target.put(key, value); + } else { + // existing value for "key" - recursively deep merge: + if (value instanceof JSONObject) { + JSONObject valueJson = (JSONObject) value; + mergeData(valueJson, target.getJSONObject(key)); + } else { + target.put(key, value); + } + } + } + return target; + } + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/DataTreeChildObject.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/DataTreeChildObject.java index 91d469700..05e6d2f13 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/DataTreeChildObject.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/DataTreeChildObject.java @@ -91,15 +91,6 @@ public class DataTreeChildObject { } } } else { - // if(this.children!=null) { - // for(DataTreeChildObject child:this.children.values()) { - // itemValue=(String) child.getProperty(childKey, null); - // if(itemValue!=null && itemValue.equals(itemValueToMatch)) { - // child.children.put(id, data); - // return true; - // } - // } - // } itemValue = this.getProperty(childKey, null); if (itemValue != null && itemValue.equals(itemValueToMatch)) { this.children.put(id, data); @@ -157,8 +148,8 @@ public class DataTreeChildObject { } } o.put("children", c); - // o.put("ownSeverity", null); - // o.put("childrenSeveritySummary", null); + //o.put("ownSeverity", null); + //o.put("childrenSeveritySummary", null); return o; } diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/MsServlet.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/MsServlet.java index 725256b17..103dec70d 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/MsServlet.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/MsServlet.java @@ -25,9 +25,9 @@ import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.onap.ccsdk.features.sdnr.wt.common.http.BaseServlet; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.MediatorServerDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.MediatorServerDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMediatorserver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -102,8 +102,8 @@ public class MsServlet extends BaseServlet { trustAll = trust; } - public void setDataProvider(MediatorServerDataProvider mediatorServerDataProvider) { - entryProvider = mediatorServerDataProvider; + public void setDataProvider(HtDatabaseMediatorserver entryProvider2) { + entryProvider = new MediatorServerDataProvider(entryProvider2); } @Override @@ -115,4 +115,11 @@ public class MsServlet extends BaseServlet { protected boolean isCorsEnabled() { return false; } + + public void triggerReloadSync() { + if(entryProvider!=null) { + entryProvider.triggerReloadSync(); + } + + } } diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/ReadyHttpServlet.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/ReadyHttpServlet.java deleted file mode 100644 index 284365021..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/ReadyHttpServlet.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.http; - -import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.apache.karaf.bundle.core.BundleInfo; -import org.apache.karaf.bundle.core.BundleService; -import org.apache.karaf.bundle.core.BundleState; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.MarkdownTable; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ReadyHttpServlet extends HttpServlet { - - /** - * - */ - private static final long serialVersionUID = 1L; - private static final Logger LOG = LoggerFactory.getLogger(ReadyHttpServlet.class); - private static boolean status; - - - private BundleService bundleService = null; - - public void setBundleService(BundleService bundleService) { - this.bundleService = bundleService; - } - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - if (isReady() && this.getBundleStatesReady()) { - resp.setStatus(HttpServletResponse.SC_OK); - } else { - - try { - resp.sendError(HttpServletResponse.SC_NOT_FOUND); - } catch (IOException | IllegalStateException e) { - LOG.warn("unable to write out 404 res not found: {}", e); - } - } - } - - private static boolean isReady() { - return status; - } - - public static void setStatus(boolean s) { - status = s; - LOG.info("status is set to ready: {}", status); - } - - private boolean getBundleStatesReady() { - Bundle thisbundle = FrameworkUtil.getBundle(this.getClass()); - BundleContext context = thisbundle ==null?null:thisbundle.getBundleContext(); - if (context == null) { - LOG.debug("no bundle context available"); - return true; - } - Bundle[] bundles = context.getBundles(); - if (bundles == null || bundles.length <= 0) { - LOG.debug("no bundles found"); - return true; - } - LOG.debug("found {} bundles", bundles.length); - MarkdownTable table = new MarkdownTable(); - table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"}); - int cntNotActive=0; - - for (Bundle bundle : bundles) { - if(this.bundleService!=null) { - BundleInfo info = this.bundleService.getInfo(bundle); - if(info.getState()==BundleState.Active ) { - continue; - } - if(info.getState()==BundleState.Resolved ) { - if(!this.isBundleImportant(bundle.getSymbolicName())) { - LOG.trace("ignore not important bundle {} with state {}",bundle.getSymbolicName(),info.getState()); - continue; - } - } - - LOG.trace("bundle {} is in state {}",bundle.getSymbolicName(),info.getState()); - } - else { - LOG.warn("bundle service is null"); - } - cntNotActive++; - } - - return cntNotActive==0; - } - - private boolean isBundleImportant(String symbolicName) { - symbolicName = symbolicName.toLowerCase(); - if(symbolicName.contains("mdsal")) { - return true; - } - if(symbolicName.contains("netconf")) { - return true; - } - if(symbolicName.contains("ccsdk")) { - return true; - } - if(symbolicName.contains("devicemanager")) { - return true; - } - if(symbolicName.contains("restconf")) { - return true; - } - - return false; - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/AboutHttpServlet.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/AboutHttpServlet.java index 90b5fd9af..64b5fab9f 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/AboutHttpServlet.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/AboutHttpServlet.java @@ -33,6 +33,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.http.HttpHeaders; //import org.apache.karaf.bundle.core.BundleInfo; //import org.apache.karaf.bundle.core.BundleService; import org.onap.ccsdk.features.sdnr.wt.common.Resources; @@ -71,22 +72,27 @@ public class AboutHttpServlet extends HttpServlet { private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}"; private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}"; private static final String README_FILE = "README.md"; + private static final String JSON_FILE = "README.json"; private static final String NO_DEVICEMANAGERS_RUNNING_MESSAGE = null; + private static final String MIMETYPE_JSON = "application/json"; + private static final String MIMETYPE_MARKDOWN = "text/markdown"; private final String groupId = this.getGroupIdOrDefault("org.onap.ccsdk.features.sdnr.wt"); private final String artifactId = "sdnr-wt-data-provider-provider"; - private final Map BUNDLESTATE_LUT; + private final Map BUNDLESTATE_LUT; private final Map data; private final String readmeContent; // private BundleService bundleService; + private String jsonContent; public AboutHttpServlet() { this.data = new HashMap<>(); this.collectStaticData(); - this.readmeContent = this.render(this.getResourceFileContent(README_FILE)); + this.readmeContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(README_FILE)); + this.jsonContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(JSON_FILE)); this.BUNDLESTATE_LUT = new HashMap<>(); this.BUNDLESTATE_LUT.put(Bundle.UNINSTALLED, "uninstalled"); this.BUNDLESTATE_LUT.put(Bundle.INSTALLED, "installed"); @@ -98,17 +104,17 @@ public class AboutHttpServlet extends HttpServlet { } protected String getGroupIdOrDefault(String def) { - String symbolicName = this.getManifestValue("Bundle-SymbolicName"); - if(symbolicName!=null) { - int idx = symbolicName.indexOf(this.artifactId); - if(idx>0) { - return symbolicName.substring(0, idx-1); - } - } - return def; - } - - // public void setBundleService(BundleService bundleService) { + String symbolicName = this.getManifestValue("Bundle-SymbolicName"); + if (symbolicName != null) { + int idx = symbolicName.indexOf(this.artifactId); + if (idx > 0) { + return symbolicName.substring(0, idx - 1); + } + } + return def; + } + + // public void setBundleService(BundleService bundleService) { // this.bundleService = bundleService; // } @@ -136,15 +142,16 @@ public class AboutHttpServlet extends HttpServlet { String uri = req.getRequestURI().substring(URI_PRE.length()); LOG.debug("request for {}", uri); if (uri.length() <= 0 || uri.equals("/")) { + ContentType ctype = this.detectContentType(req, ContentType.MARKDOWN); // collect data - this.collectData(); + this.collectData(ctype); // render readme - String content = this.render(); + String content = this.render(ctype); byte[] output = content != null ? content.getBytes() : new byte[0]; // output resp.setStatus(HttpServletResponse.SC_OK); resp.setContentLength(output.length); - resp.setContentType("text/plain"); + resp.setContentType(ctype.getMimeType()); ServletOutputStream os = null; try { os = resp.getOutputStream(); @@ -194,11 +201,11 @@ public class AboutHttpServlet extends HttpServlet { /** * collect dynamic data for about.md */ - private void collectData() { + private void collectData(ContentType ctype) { LOG.info("collecting dynamic data"); try { this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get()); - this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles()); + this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles(ctype)); } catch (Exception e) { LOG.warn("problem collecting system data: {}", e); } @@ -248,29 +255,6 @@ public class AboutHttpServlet extends HttpServlet { return null; } - /** - * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section - * - * @param key - * @return - */ - private String getPomProperty(String key) { - LOG.info("try to get pom property for {}", key); - URL url = Resources.getUrlForRessource(AboutHttpServlet.class, - METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml"); - if (url == null) { - return null; - } - PomFile pomfile; - try { - pomfile = new PomFile(url.openStream()); - return pomfile.getProperty(key); - } catch (Exception e) { - LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e); - } - return null; - } - /** * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml * @@ -293,35 +277,35 @@ public class AboutHttpServlet extends HttpServlet { return null; } - private String getDevicemanagerBundles() { + private String getDevicemanagerBundles(ContentType ctype) { Bundle thisbundle = FrameworkUtil.getBundle(this.getClass()); - BundleContext context = thisbundle ==null?null:thisbundle.getBundleContext(); + BundleContext context = thisbundle == null ? null : thisbundle.getBundleContext(); if (context == null) { LOG.debug("no bundle context available"); - return ""; + return ctype==ContentType.MARKDOWN?"":"[]"; } Bundle[] bundles = context.getBundles(); if (bundles == null || bundles.length <= 0) { LOG.debug("no bundles found"); - return NO_DEVICEMANAGERS_RUNNING_MESSAGE; + return ctype==ContentType.MARKDOWN?NO_DEVICEMANAGERS_RUNNING_MESSAGE:"[]"; } LOG.debug("found {} bundles", bundles.length); MarkdownTable table = new MarkdownTable(); - table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"}); + table.setHeader(new String[] {"Bundle-Id", "Version", "Symbolic-Name", "Status"}); String name; for (Bundle bundle : bundles) { name = bundle.getSymbolicName(); - if(!(name.contains("devicemanager") && name.contains("provider"))) { + if (!(name.contains("devicemanager") && name.contains("provider"))) { continue; } - if(name.equals("org.onap.ccsdk.features.sdnr.wt.sdnr-wt-devicemanager-core-provider")) { + if (name.equals("org.onap.ccsdk.features.sdnr.wt.sdnr-wt-devicemanager-provider")) { continue; } table.addRow(new String[] {String.valueOf(bundle.getBundleId()), bundle.getVersion().toString(), name, - BUNDLESTATE_LUT.getOrDefault(bundle.getState(),"unknown")}); + BUNDLESTATE_LUT.getOrDefault(bundle.getState(), "unknown")}); } - return table.toMarkDown(); + return ctype==ContentType.MARKDOWN?table.toMarkDown():table.toJson(); } /** @@ -382,10 +366,12 @@ public class AboutHttpServlet extends HttpServlet { /** * render this.readmeContent with this.data * + * @param ctype + * * @return */ - private String render() { - return this.render(null); + private String render(ContentType ctype) { + return this.render(ctype, null); } /** @@ -394,9 +380,9 @@ public class AboutHttpServlet extends HttpServlet { * @param content * @return */ - private String render(String content) { + private String render(ContentType ctype, String content) { if (content == null) { - content = this.readmeContent; + content = ctype==ContentType.MARKDOWN? this.readmeContent:this.jsonContent; } if (content == null) { return null; @@ -413,4 +399,30 @@ public class AboutHttpServlet extends HttpServlet { public void setClusterSize(String value) { this.data.put(PLACEHOLDER_CLUSTER_SIZE, value); } + + private ContentType detectContentType(HttpServletRequest req, ContentType def) { + String accept = req.getHeader(HttpHeaders.ACCEPT); + if (accept != null) { + if (accept.equals(MIMETYPE_JSON)) { + return ContentType.JSON; + } else if (accept.equals(MIMETYPE_MARKDOWN)) { + return ContentType.MARKDOWN; + } + } + return def; + } + + private enum ContentType { + MARKDOWN(MIMETYPE_MARKDOWN), JSON(MIMETYPE_JSON); + + private String mimeType; + + ContentType(String mimeType) { + this.mimeType = mimeType; + } + + String getMimeType() { + return this.mimeType; + } + } } diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/MarkdownTable.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/MarkdownTable.java index ac9cb44fe..1580a0fe3 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/MarkdownTable.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/MarkdownTable.java @@ -23,6 +23,8 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about; import java.util.ArrayList; import java.util.List; +import org.json.JSONArray; +import org.json.JSONObject; /** * @author Michael Dürre @@ -48,7 +50,7 @@ public class MarkdownTable { public String toMarkDown() { StringBuilder sb = new StringBuilder(); final int cols = - this.columns != null ? this.columns.length : !this.rows.isEmpty() ? this.rows.get(0).length : 0; + this.columns != null ? this.columns.length : this.rows.size() > 0 ? this.rows.get(0).length : 0; if (cols > 0) { sb.append("|"); for (int i = 0; i < cols; i++) { @@ -67,9 +69,23 @@ public class MarkdownTable { } sb.append("\n"); } - - } return sb.toString(); } + + public String toJson() { + JSONArray a = new JSONArray(); + final int cols = + this.columns != null ? this.columns.length : this.rows.size() > 0 ? this.rows.get(0).length : 0; + if (cols > 0) { + for (String[] row : this.rows) { + JSONObject o = new JSONObject(); + for (int i = 0; i < cols; i++) { + o.put(this.columns[i], row[i]); + } + a.put(o); + } + } + return a.toString(); + } } diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/ODLVersionLUT.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/ODLVersionLUT.java index 491775eb0..0e43cd251 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/ODLVersionLUT.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/ODLVersionLUT.java @@ -32,6 +32,9 @@ public class ODLVersionLUT { if (onapCCSDKVersion == null) { return def; } + if (onapCCSDKVersion.startsWith("2.2.")) { + return "ONAP Istanbul"; + } if (onapCCSDKVersion.startsWith("2.1.")) { return "ONAP Honolulu"; } @@ -60,6 +63,10 @@ public class ODLVersionLUT { } if(odlMdsalVersionLUT==null) { odlMdsalVersionLUT = new HashMap<>(); + odlMdsalVersionLUT.put("7.0.7","silicon-SR1 (0.14.1)"); + odlMdsalVersionLUT.put("7.0.6","silicon-SR0 (0.14.0)"); + odlMdsalVersionLUT.put("6.0.8","aluminium-SR3 (0.13.3)"); + //odlMdsalVersionLUT.put("6.0.7","aluminium-SR2 (0.13.2)"); odlMdsalVersionLUT.put("6.0.7","aluminium-SR1 (0.13.1)"); odlMdsalVersionLUT.put("6.0.4","aluminium-SR0 (0.13.0)"); odlMdsalVersionLUT.put("5.0.14","magnesium-SR2 (0.12.2)"); diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/yangschema/YangFileProvider.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/yangschema/YangFileProvider.java index 0d46ad098..c6758e240 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/yangschema/YangFileProvider.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/yangschema/YangFileProvider.java @@ -33,10 +33,8 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,22 +69,21 @@ public class YangFileProvider { } private List findYangFiles(String module) { + LOG.debug("try to find yang files for {}", module); List list = new ArrayList<>(); String[] files = this.mainSourcePath.toFile().list(yangFilenameFilter); YangFilename yangfile; - for (String file : files) { - files = this.mainSourcePath.toFile().list(yangFilenameFilter); - for (String fn : files) { - try { - yangfile = new YangFilename(this.mainSourcePath.resolve(fn).toString()); - if (yangfile.getModule().equals(module)) { - list.add(yangfile); - } - } catch (ParseException e) { - LOG.warn("unable to handle yangfile {}: {}", file, e); + for (String fn : files) { + try { + yangfile = new YangFilename(this.mainSourcePath.resolve(fn).toString()); + if (yangfile.getModule().equals(module)) { + list.add(yangfile); } + } catch (ParseException e) { + LOG.warn("unable to handle yangfile {}: {}", fn, e); } } + for (Path addPath : this.additionalSources) { files = addPath.toFile().list(yangFilenameFilter); for (String file : files) { @@ -105,7 +102,7 @@ public class YangFileProvider { /** * get yang file from source with specified version or least newer one if version is null then the latest one - * + * * @param module * @param version * @return @@ -140,7 +137,7 @@ public class YangFileProvider { /** * write filestream directly to output stream easier for http handling - * + * * @param module * @param version * @param outputStream diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderConfig.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderConfig.java new file mode 100644 index 000000000..5e8dc92fc --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderConfig.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl; + +import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; + +public class DataProviderConfig implements Configuration { + + private static final String PROPERTY_KEY_DBTYPE = "dbType"; + private static final Object DEFAULT_DBTYPE = "${SDNRDBTYPE}"; + private static final SdnrDbType DEFAULT_DBTYPE_VALUE = SdnrDbType.ELASTICSEARCH; + private final EsConfig esConfig; + private final SqlDBConfig maridadbConfig; + private ConfigurationFileRepresentation configuration; + + public DataProviderConfig(ConfigurationFileRepresentation configuration) { + this.configuration = configuration; + defaults(); + this.esConfig = new EsConfig(configuration); + this.maridadbConfig = new SqlDBConfig(configuration); + } + + public EsConfig getEsConfig() { + return this.esConfig; + } + + public SqlDBConfig getMariadbConfig() { + return this.maridadbConfig; + } + + @Override + public void defaults() { + + configuration.setPropertyIfNotAvailable(this.getSectionName(), PROPERTY_KEY_DBTYPE, DEFAULT_DBTYPE); + } + + @Override + public String getSectionName() { + return ConfigurationFileRepresentation.SECTIONNAME_ROOT; + } + + public SdnrDbType getDbType() { + String value = this.configuration.getProperty(this.getSectionName(), PROPERTY_KEY_DBTYPE); + if (value.isEmpty()) { + return DEFAULT_DBTYPE_VALUE; + } + return SdnrDbType.valueOf(value); + } + + +} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderImpl.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderImpl.java index 190c78904..e06a1a9a2 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderImpl.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderImpl.java @@ -25,7 +25,6 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClientException; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.MsServlet; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.ReadyHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.UserdataHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.AboutHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; @@ -36,6 +35,7 @@ import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.StatusChangedHandler.StatusKey; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; +import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +52,7 @@ public class DataProviderImpl implements IEntityDataProvider, AutoCloseable { private DataTreeHttpServlet treeServlet; private UserdataHttpServlet userdataServlet; private HtDatabaseClient dbClient; + private DataBroker dataBroker; // Blueprint 1 public DataProviderImpl() { @@ -77,12 +78,15 @@ public class DataProviderImpl implements IEntityDataProvider, AutoCloseable { public void setUserdataServlet(UserdataHttpServlet userdataServlet) { this.userdataServlet = userdataServlet; } + public void setDataBroker(DataBroker dataBroker) { + this.dataBroker = dataBroker; + } public void init() throws Exception { LOG.info("Session Initiated start {}", APPLICATION_NAME); try { // Start RPC Service - this.rpcApiService = new DataProviderServiceImpl(rpcProviderService, this.mediatorServerServlet); + this.rpcApiService = new DataProviderServiceImpl(rpcProviderService, this.mediatorServerServlet, this.dataBroker); this.treeServlet.setDatabaseClient(this.rpcApiService.getRawClient()); this.userdataServlet.setDatabaseClient(this.rpcApiService.getHtDatabaseUserManager()); LOG.info("Session Initiated end. Initialization done"); @@ -136,11 +140,6 @@ public class DataProviderImpl implements IEntityDataProvider, AutoCloseable { return NetconfTimeStampImpl.getConverter(); } - @Override - public void setReadyStatus(boolean status) { - ReadyHttpServlet.setStatus(status); - } - @Override public void setStatus(StatusKey key, String value) { if (this.aboutServlet != null) { diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderServiceImpl.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderServiceImpl.java index 9e7fc18bc..ad712847f 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderServiceImpl.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataProviderServiceImpl.java @@ -25,19 +25,31 @@ import com.google.common.util.concurrent.ListenableFuture; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNull; import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.HtUserdataManagerImpl; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.MediatorServerDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.HtUserdataManagerImpl; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.MsServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; +import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.RpcProviderService; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredential; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredentialKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMaintenanceOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerInput; @@ -92,9 +104,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.concepts.ObjectRegistration; import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,24 +118,30 @@ public class DataProviderServiceImpl implements DataProviderService, AutoCloseab private static final Logger LOG = LoggerFactory.getLogger(DataProviderServiceImpl.class); public static final String CONFIGURATIONFILE = "etc/dataprovider.properties"; private static final long DATABASE_TIMEOUT_MS = 120 * 1000L; + private static final @NonNull InstanceIdentifier KEYSTORE_IIF = InstanceIdentifier.create(Keystore.class); + private static final long DEFAULT_PAGESIZE = 20; + private static final long DEFAULT_PAGE = 1; private final ObjectRegistration<@NonNull DataProviderServiceImpl> rpcReg; - private final ElasticSearchDataProvider dataProvider; + private final DatabaseDataProvider dataProvider; private final ConfigurationFileRepresentation configuration; - private final EsConfig esConfig; - private final MediatorServerDataProvider mediatorServerDataProvider; + private final DataProviderConfig dbConfig; private final HtUserdataManager dbUserManager; - - DataProviderServiceImpl(final RpcProviderService rpcProviderService, MsServlet mediatorServerServlet) - throws Exception { + private final DataBroker dataBroker; + private final MsServlet mediatorServerServlet; + public DataProviderServiceImpl(final RpcProviderService rpcProviderService, MsServlet mediatorServerServlet, + DataBroker dataBroker) throws Exception { this.configuration = new ConfigurationFileRepresentation(CONFIGURATIONFILE); - this.esConfig = new EsConfig(configuration); - this.dataProvider = new ElasticSearchDataProvider(esConfig.getHosts(), esConfig.getBasicAuthUsername(), - esConfig.getBasicAuthPassword(), esConfig.trustAllCerts()); + this.dbConfig = new DataProviderConfig(configuration); + this.dataBroker = dataBroker; + this.mediatorServerServlet = mediatorServerServlet; + if (this.dbConfig.getDbType() == SdnrDbType.ELASTICSEARCH) { + this.dataProvider = new ElasticSearchDataProvider(this.dbConfig.getEsConfig()); + } else { + this.dataProvider = new SqlDBDataProvider(this.dbConfig.getMariadbConfig()); + } this.dataProvider.waitForYellowDatabaseStatus(DATABASE_TIMEOUT_MS, TimeUnit.MILLISECONDS); - this.mediatorServerDataProvider = new MediatorServerDataProvider(esConfig.getHosts(), - esConfig.getBasicAuthUsername(), esConfig.getBasicAuthPassword(),esConfig.trustAllCerts()); - mediatorServerServlet.setDataProvider(this.mediatorServerDataProvider); + mediatorServerServlet.setDataProvider(this.dataProvider.getHtDatabaseMediatorServer()); this.dbUserManager = new HtUserdataManagerImpl(this.dataProvider.getRawClient()); // Register ourselves as the REST API RPC implementation LOG.info("Register RPC Service " + DataProviderServiceImpl.class.getSimpleName()); @@ -128,7 +149,7 @@ public class DataProviderServiceImpl implements DataProviderService, AutoCloseab } private void sendResyncCallbackToApiGateway() { - mediatorServerDataProvider.triggerReloadSync(); + this.mediatorServerServlet.triggerReloadSync(); } /** @@ -153,7 +174,7 @@ public class DataProviderServiceImpl implements DataProviderService, AutoCloseab * @return configuration object */ public IEsConfig getEsConfig() { - return esConfig; + return dbConfig.getEsConfig(); } @@ -385,7 +406,6 @@ public class DataProviderServiceImpl implements DataProviderService, AutoCloseab read(() -> DataProviderServiceImpl.this.dataProvider.readGuiCutThroughEntry(input)); return result.buildFuture(); } - // -- private classes and functions private static String assembleExceptionMessage(Exception e) { StringWriter sw = new StringWriter(); @@ -420,4 +440,6 @@ public class DataProviderServiceImpl implements DataProviderService, AutoCloseab return this.dbUserManager; } + + } diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataTreeProviderImpl.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataTreeProviderImpl.java index b44906289..cb62e3326 100644 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataTreeProviderImpl.java +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/DataTreeProviderImpl.java @@ -70,12 +70,6 @@ public class DataTreeProviderImpl { if (nodeId != null) { bquery.must(QueryBuilders.matchQuery(nodeKey, nodeId)); } - // if (parentKey != null && parentValue != null) { - // bquery.must(QueryBuilders.matchQuery(parentKey, parentValue)); - // } - // if (childKey != null && childValue != null) { - // bquery.must(QueryBuilders.matchQuery(childKey, childValue)); - // } query = bquery; } @@ -134,7 +128,7 @@ public class DataTreeProviderImpl { public DataTreeObject readInventoryTree(List tree, String filter) throws IOException { //root nodes will be node-information -> below inventory - if (tree == null || tree.isEmpty()) { + if (tree == null || tree.size() <= 0) { return this.readInventoryTreeWithNode(filter); } //root node will be inventory on tree-level if sliced treePath @@ -155,7 +149,7 @@ public class DataTreeProviderImpl { throws IOException { DataTreeObject tree = new DataTreeObject(INVENTORY_PROPERTY_PARENTUUID, INVENTORY_PROPERTY_UUID); final String parentUuid = list.size() > 1 ? list.get(list.size() - 2) : null; - final String uuid = list.isEmpty() ? null : list.get(list.size() - 1); + final String uuid = list.size() > 0 ? list.get(list.size() - 1) : null; List matches = this.search(Entity.Inventoryequipment, filter, INVENTORY_PROPERTY_NODEID, nodeId, INVENTORY_PROPERTY_PARENTUUID, parentUuid, INVENTORY_PROPERTY_UUID, uuid, INVENTORY_PROPERTY_TREELEVEL); @@ -163,13 +157,13 @@ public class DataTreeProviderImpl { List others = this.search(Entity.Inventoryequipment, (String) null, INVENTORY_PROPERTY_NODEID, nodeId, null, null, null, null, INVENTORY_PROPERTY_TREELEVEL); if (matches.size() > 0) { - int treeLevelToStart = (list == null || list.isEmpty()) ? 0 : list.size() - 1; + int treeLevelToStart = (list == null || list.size() <= 0) ? 0 : list.size() - 1; //build tree JSONObject hitData; //fill root elems for (SearchHit hit : matches) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") == treeLevelToStart) { + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) == treeLevelToStart) { tree.put(hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), true) .setProperty(INVENTORY_PROPERTY_UUID, hitData.getString(INVENTORY_PROPERTY_UUID)) @@ -179,7 +173,7 @@ public class DataTreeProviderImpl { } for (SearchHit hit : others) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") == treeLevelToStart) { + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) == treeLevelToStart) { tree.putIfNotExists(hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), false) .setProperty(INVENTORY_PROPERTY_UUID, hitData.getString(INVENTORY_PROPERTY_UUID)) @@ -190,8 +184,8 @@ public class DataTreeProviderImpl { //fill child elems for (SearchHit hit : matches) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") > treeLevelToStart) { - tree.put(hitData.getLong("tree-level") - treeLevelToStart - 1, hit.getId(), + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) > treeLevelToStart) { + tree.put(hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) - treeLevelToStart - 1, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), true) .setProperty(INVENTORY_PROPERTY_UUID, hitData.getString(INVENTORY_PROPERTY_UUID)) .setProperty(INVENTORY_PROPERTY_PARENTUUID, @@ -200,8 +194,8 @@ public class DataTreeProviderImpl { } for (SearchHit hit : others) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") > treeLevelToStart) { - tree.putIfNotExists(hitData.getLong("tree-level") - treeLevelToStart - 1, hit.getId(), + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) > treeLevelToStart) { + tree.putIfNotExists(hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) - treeLevelToStart - 1, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), false) .setProperty(INVENTORY_PROPERTY_UUID, hitData.getString(INVENTORY_PROPERTY_UUID)) .setProperty(INVENTORY_PROPERTY_PARENTUUID, @@ -255,7 +249,7 @@ public class DataTreeProviderImpl { //fill root elems for (SearchHit hit : matches) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") == 0) { + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) == 0) { tree.put(0, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), true) .setProperty(INVENTORY_PROPERTY_UUID, hitData.getString(INVENTORY_PROPERTY_UUID)) @@ -266,7 +260,7 @@ public class DataTreeProviderImpl { if (others != null) { for (SearchHit hit : others) { hitData = hit.getSource(); - if (hitData.getLong("tree-level") == 0) { + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) == 0) { tree.putIfNotExists(0, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL), false) .setProperty(INVENTORY_PROPERTY_UUID, @@ -279,7 +273,7 @@ public class DataTreeProviderImpl { //fill child elements for (SearchHit hit : matches) { hitData = hit.getSource(); - long treeLevel = hitData.getLong("tree-level"); + long treeLevel = hitData.getLong(INVENTORY_PROPERTY_TREELEVEL); if (treeLevel > 0) { tree.put(treeLevel, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL_CHILD), true) @@ -291,8 +285,8 @@ public class DataTreeProviderImpl { if (others != null) { for (SearchHit hit : others) { hitData = hit.getSource(); - long treeLevel = hitData.getLong("tree-level"); - if (hitData.getLong("tree-level") > 0) { + long treeLevel = hitData.getLong(INVENTORY_PROPERTY_TREELEVEL); + if (hitData.getLong(INVENTORY_PROPERTY_TREELEVEL) > 0) { tree.putIfNotExists(treeLevel, hit.getId(), new DataTreeChildObject(hitData.getString(INVENTORY_PROPERTY_FOR_LABEL_CHILD), false) .setProperty(INVENTORY_PROPERTY_UUID, diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/EsConfig.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/EsConfig.java deleted file mode 100644 index 44751ffd0..000000000 --- a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/EsConfig.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; -import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; -import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EsConfig implements Configuration, IEsConfig { - - private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class); - - public static final String SECTION_MARKER_ES = "es"; - - private static final String PROPERTY_KEY_DBHOSTS = "esHosts"; - private static final String PROPERTY_KEY_TRUSTALLCERTS = "esTrustAllCerts"; - private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds"; - private static final String PROPERTY_KEY_CLUSTER = "esCluster"; - private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds"; - private static final String PROPERTY_KEY_NODE = "esNode"; - private static final String PROPERTY_KEY_AUTH_USERNAME = "esAuthUsername"; - private static final String PROPERTY_KEY_AUTH_PASSWORD = "esAuthPassword"; - - - private static String defaultHostinfo = "${SDNRDBURL}"; - private static final String DEFAULT_VALUE_CLUSTER = ""; - /** check db data in this interval [in seconds] 0 deactivated */ - private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0"; - /** keep data for this time [in seconds] 30 days */ - private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L); - private static final String DEFAULT_VALUE_NODE = "elasticsearchnode"; - private static final String DEFAULT_VALUE_DBUSERNAME = "${SDNRDBUSERNAME}"; - private static final String DEFAULT_VALUE_DBPASSWORD = "${SDNRDBPASSWORD}"; - private static final String DEFAULT_VALUE_TRUSTALLCERTS = "${SDNRDBTRUSTALLCERTS}"; - - private final ConfigurationFileRepresentation configuration; - - public EsConfig(ConfigurationFileRepresentation configuration) { - - this.configuration = configuration; - this.configuration.addSection(SECTION_MARKER_ES); - defaults(); - } - - /* - * Setter - */ - - public void setNode(String nodeName) { - configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName); - } - - /* - * Getter - */ - - public String getNode() { - return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE); - } - - public HostInfo[] getHosts() { - String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS); - return parseHosts(dbHosts); - } - - public void setHosts(HostInfo[] hosts) { - this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts)); - } - - @Override - public String getCluster() { - return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL); - } - - public void setCluster(String cluster) { - configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster); - } - - public boolean hasBasicAuthCredentials() { - return this.getBasicAuthUsername() != null && this.getBasicAuthPassword() != null - && !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty(); - } - - public String getBasicAuthUsername() { - return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME); - } - - public String getBasicAuthPassword() { - return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD); - } - - @Override - public long getArchiveCheckIntervalSeconds() { - return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L); - } - - public boolean trustAllCerts() { - return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS); - } - - public void setArchiveCheckIntervalSeconds(long seconds) { - configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds); - } - - @Override - public long getArchiveLifetimeSeconds() { - return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L); - } - - public void setArchiveLimit(long seconds) { - configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds); - } - - @Override - public String getSectionName() { - return SECTION_MARKER_ES; - } - - @Override - public synchronized void defaults() { - // Add default if not available - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, - DEFAULT_ARCHIVE_LIMIT_SEC); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, - DEFAULT_ARCHIVE_INTERVAL_SEC); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_VALUE_NODE); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME, - DEFAULT_VALUE_DBUSERNAME); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD, - DEFAULT_VALUE_DBPASSWORD); - configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS, - DEFAULT_VALUE_TRUSTALLCERTS); - - } - - @Override - public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) { - configuration.unregisterConfigChangedListener(archiveCleanService); - } - - @Override - public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) { - configuration.registerConfigChangedListener(archiveCleanService); - } - - /** @TODO Shift to own class **/ - private static String printHosts(HostInfo[] h) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < h.length; i++) { - sb.append(h[i].toUrl()); - if (i != h.length - 1) { - sb.append(","); - } - } - return sb.toString(); - } - - /** @TODO Shift to own class **/ - private static HostInfo[] parseHosts(String string) { - List infos = new ArrayList<>(); - String[] list = string.split(","); - if (list.length > 0) { - for (String item : list) { - try { - URL url = new URL(item); - infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol()))); - } catch (MalformedURLException e) { - LOG.warn("problem parsing url {} : {}", item, e.getMessage()); - } - } - } - HostInfo[] a = new HostInfo[infos.size()]; - return infos.toArray(a); - } - - @Override - public String toString() { - return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()=" - + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds() - + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()=" - + getSectionName() + "]"; - } - -} diff --git a/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/MediatorServerDataProvider.java b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/MediatorServerDataProvider.java new file mode 100644 index 000000000..7ae7a7d40 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/impl/MediatorServerDataProvider.java @@ -0,0 +1,92 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMediatorserver; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.MediatorServerEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MediatorServerDataProvider implements AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(MediatorServerDataProvider.class); + + private final HtDatabaseMediatorserver dbClient; + private final int REFRESH_INTERVAL = 60; + private final Map entries; + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private boolean isRunning; + + public MediatorServerDataProvider(HtDatabaseMediatorserver entryProvider) { + this.entries = new HashMap<>(); + this.dbClient = entryProvider; + this.scheduler.scheduleAtFixedRate(onTick, this.REFRESH_INTERVAL, this.REFRESH_INTERVAL, TimeUnit.SECONDS); + LOG.info("Start {}", this.getClass().getName()); + } + + + private final Runnable onTick = new Runnable() { + + @Override + public void run() { + isRunning = true; + runIt(); + isRunning = false; + } + + }; + + private void runIt() { + List result = this.dbClient.getAll(); + for (MediatorServerEntity item : result) { + MediatorServerDataProvider.this.entries.put(item.getId(), item); + } + } + + /** + * + * @param dbServerId + * @return url or null if not exists + */ + public String getHostUrl(String dbServerId) { + MediatorServerEntity info = this.entries.getOrDefault(dbServerId, null); + return info == null ? null : info.getUrl(); + } + + public boolean triggerReloadSync() { + if (!isRunning) { + runIt(); + } + return true; + } + + @Override + public void close() throws Exception { + this.scheduler.shutdown(); + } +} diff --git a/sdnr/wt/data-provider/provider/src/main/resources/about/README.json b/sdnr/wt/data-provider/provider/src/main/resources/about/README.json new file mode 100644 index 000000000..f39a4c233 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/main/resources/about/README.json @@ -0,0 +1,16 @@ +{ + "version-info":{ + "ONAP-release":"{release-name}", + "ONAP-release-version":"{release-version}", + "Opendaylight-release":"{odl-version}", + "ONAP-CCSDK-version":"{ccsdk-version}", + "Build-timestamp":"{build-time}", + "Yangtools-version":"{yangtools-version}", + "MD-SAL-version":"{mdsal-version}", + "SDN-R packages version":"{package-version} ({package-githash})", + "Cluster size":"{cluster-size}" + }, + "device-managers":{devicemanagers}, + "system-info":{} + +} \ No newline at end of file diff --git a/sdnr/wt/data-provider/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/sdnr/wt/data-provider/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml index 686af61d6..d5f8a991a 100644 --- a/sdnr/wt/data-provider/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml +++ b/sdnr/wt/data-provider/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml @@ -28,23 +28,10 @@ - - - - - - - - - - - + + - @@ -87,6 +74,7 @@ + 0); } diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestCRUDforDatabase.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestCRUDforDatabase.java index 5a8c549d8..c407859d5 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestCRUDforDatabase.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestCRUDforDatabase.java @@ -39,8 +39,9 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.HtUserdataManagerImpl; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.HtUserdataManagerImpl; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.UserdataHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; @@ -125,7 +126,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro public class TestCRUDforDatabase { - private static ElasticSearchDataProvider dbProvider; + private static DatabaseDataProvider dbProvider; private static HtDatabaseClient dbRawProvider; @BeforeClass diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfig.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfig.java index 9a96986c0..8c62ec9d5 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfig.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfig.java @@ -31,7 +31,7 @@ import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section; import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section.EnvGetter; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.EsConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfiguration.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfiguration.java index d06486550..dd285754a 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfiguration.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestConfiguration.java @@ -27,7 +27,7 @@ import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConfigurationException; import org.onap.ccsdk.features.sdnr.wt.common.util.ResourceFileLoader; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.EsConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsConfig; public class TestConfiguration { diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestEventService.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestEventService.java index b83034d90..dc5e049fd 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestEventService.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestEventService.java @@ -37,8 +37,9 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.entity.HtDatabaseEventsService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.entity.HtDatabaseEventsService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; @@ -70,7 +71,7 @@ import org.opendaylight.yangtools.yang.common.Uint32; * */ public class TestEventService { - private static ElasticSearchDataProvider dbProvider; + private static DatabaseDataProvider dbProvider; private static HtDatabaseClient dbRawProvider; private static HtDatabaseEventsService service = null; @@ -154,7 +155,7 @@ public class TestEventService { service.writeFaultLog(createFaultLog(NODEID2, OBJECTREFID2, "problem", SeverityType.Major, 1)); TestCRUDforDatabase.trySleep(100); now = new Date(); - int numOlds = service.getNumberOfOldObjects(now); + long numOlds = service.getNumberOfOldObjects(now); assertEquals(5, numOlds); TestCRUDforDatabase.trySleep(100); service.writeFaultLog(createFaultLog(NODEID, OBJECTREFID2, "problem", SeverityType.Major, 3)); @@ -205,11 +206,12 @@ public class TestEventService { clearDbEntity(Entity.NetworkelementConnection); List nes = service.getNetworkElementConnections(); assertEquals(0, nes.size()); - service.updateNetworkConnection22(createNeConnection(NODEID, NetworkElementDeviceType.Unknown), NODEID); - service.updateNetworkConnection22(createNeConnection(NODEID2, NetworkElementDeviceType.ORAN), NODEID2); + service.updateNetworkConnection22(createNeConnection(NODEID, NetworkElementDeviceType.Unknown, null), NODEID); + service.updateNetworkConnection22(createNeConnection(NODEID, NetworkElementDeviceType.Unknown, "old"), NODEID); + service.updateNetworkConnection22(createNeConnection(NODEID2, NetworkElementDeviceType.ORAN, "old"), NODEID2); nes = service.getNetworkElementConnections(); assertEquals(2, nes.size()); - service.updateNetworkConnectionDeviceType(createNeConnection(NODEID, NetworkElementDeviceType.Wireless), + service.updateNetworkConnectionDeviceType(createNeConnection(NODEID, NetworkElementDeviceType.Wireless,"old"), NODEID); nes = service.getNetworkElementConnections(); assertEquals(2, nes.size()); @@ -278,12 +280,13 @@ public class TestEventService { /** * @param devType + * @param mountMethod * @param nodename3 * @return */ - private static NetworkElementConnectionEntity createNeConnection(String nodeId, NetworkElementDeviceType devType) { + private static NetworkElementConnectionEntity createNeConnection(String nodeId, NetworkElementDeviceType devType, String mountMethod) { return new NetworkElementConnectionBuilder().setNodeId(nodeId).setHost("host") - .setPort(YangHelper2.getLongOrUint32(1234L)).setCoreModelCapability("123") + .setPort(YangHelper2.getLongOrUint32(1234L)).setCoreModelCapability("123")//.setMountMethod(mountMethod) .setStatus(ConnectionLogStatus.Connected).setDeviceType(devType).setIsRequired(true).build(); } diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestFilterConversion.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestFilterConversion.java index 68d367668..8a4e3e202 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestFilterConversion.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestFilterConversion.java @@ -29,7 +29,7 @@ import java.util.List; import org.junit.Test; import org.mockito.Mockito; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryByFilter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryByFilter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; 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.SortOrder; @@ -66,7 +66,7 @@ public class TestFilterConversion extends Mockito { List filters = Arrays.asList(new FilterBuilder().setProperty(PROPERTY).setFiltervalue("si?ba").build()); when(input.getFilter()).thenReturn(YangHelper2.getListOrMap(FilterKey.class, filters)); try { - new QueryByFilter(input).getSearchRequestByFilter("test1", "test2", "test3", "test4"); + new QueryByFilter(input).getSearchRequestByFilter("test1", "test2", "test3", "test4", false); fail(); } catch (IllegalArgumentException e) { // fails if type not correct diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestImplementation.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestImplementation.java index 16dfa0ec6..3c983e7a7 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestImplementation.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestImplementation.java @@ -81,7 +81,6 @@ public class TestImplementation { } impl.setStatus(StatusKey.CLUSTER_SIZE, "3"); - impl.setReadyStatus(true); try { impl.close(); } catch (Exception e) { diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestInventoryConsistency.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestInventoryConsistency.java index ed8eac949..ce1bd78fa 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestInventoryConsistency.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestInventoryConsistency.java @@ -28,10 +28,10 @@ import java.util.List; import java.util.concurrent.TimeUnit; import org.junit.BeforeClass; import org.junit.Test; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory; @@ -39,16 +39,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro public class TestInventoryConsistency { private static final String TEST1NODEID = "sim1"; - private static String resourceDirectoryPath = "/" + TestTree.class.getSimpleName() + "/"; - private static ElasticSearchDataProvider dbProvider; - private static HtDatabaseClient dbRawProvider; + private static DatabaseDataProvider dbProvider; @BeforeClass public static void init() throws Exception { HostInfo[] hosts = HostInfoForTest.get(); dbProvider = new ElasticSearchDataProvider(hosts); dbProvider.waitForYellowDatabaseStatus(30, TimeUnit.SECONDS); - dbRawProvider = HtDatabaseClient.getClient(hosts); } @Test diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMaintenanceServiceData.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMaintenanceServiceData.java index b9516b363..e85ed8cdd 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMaintenanceServiceData.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMaintenanceServiceData.java @@ -33,7 +33,8 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; @@ -45,7 +46,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro */ public class TestMaintenanceServiceData { - private static ElasticSearchDataProvider dbProvider; + private static DatabaseDataProvider dbProvider; private static HtDatabaseClient dbRawProvider; private static HtDatabaseMaintenance service = null; diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMediatorServerService.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMediatorServerService.java index 5b4bc3e0f..408f6a636 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMediatorServerService.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestMediatorServerService.java @@ -32,8 +32,9 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.MediatorServerDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.MediatorServerDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.CreateMediatorServerOutputBuilder; @@ -43,7 +44,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro * @author Michael Dürre */ public class TestMediatorServerService { - private static ElasticSearchDataProvider dbProvider; + private static DatabaseDataProvider dbProvider; private static HtDatabaseClient dbRawProvider; private static MediatorServerDataProvider service = null; @@ -54,7 +55,7 @@ public class TestMediatorServerService { dbProvider = new ElasticSearchDataProvider(hosts); dbProvider.waitForYellowDatabaseStatus(30, TimeUnit.SECONDS); dbRawProvider = HtDatabaseClient.getClient(hosts); - service = new MediatorServerDataProvider(hosts); + service = new MediatorServerDataProvider(dbProvider.getHtDatabaseMediatorServer()); } @Test diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTimestampFilter.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTimestampFilter.java index ad525b910..4a36262fd 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTimestampFilter.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTimestampFilter.java @@ -30,7 +30,7 @@ import org.json.JSONObject; import org.junit.Test; import org.mockito.Mockito; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.rpctypehelper.QueryByFilter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryByFilter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; 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.entity.input.Filter; diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTree.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTree.java index 28f7d49fe..29f35560e 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTree.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestTree.java @@ -35,7 +35,8 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest; import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet.EntityWithTree; import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeObject; @@ -46,7 +47,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.pro public class TestTree { private static String resourceDirectoryPath = "/" + TestTree.class.getSimpleName() + "/"; - private static ElasticSearchDataProvider dbProvider; + private static DatabaseDataProvider dbProvider; private static HtDatabaseClient dbRawProvider; @BeforeClass 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 fd397840e..3fcbdb84c 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 @@ -30,7 +30,7 @@ import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.HtUserdataManagerImpl; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.HtUserdataManagerImpl; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; public class TestUserdata { diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestYangGenSalMapping.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestYangGenSalMapping.java index 524659e99..611533ba1 100644 --- a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestYangGenSalMapping.java +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/TestYangGenSalMapping.java @@ -37,7 +37,7 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.EsDataObjectReaderWriter2; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.EsDataObjectReaderWriter2; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2; import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest; import org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools.DataProviderYangToolsMapper; diff --git a/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/util/MariaDBTestBase.java b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/util/MariaDBTestBase.java new file mode 100644 index 000000000..276bcf4a1 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/test/util/MariaDBTestBase.java @@ -0,0 +1,169 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util; + +import static org.junit.Assert.fail; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section.EnvGetter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper.UnableToMapClassException; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +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.MediatorServerEntity; +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.Pmdata15mEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata24hEntity; +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import ch.vorburger.mariadb4j.DBConfigurationBuilder; + +public class MariaDBTestBase { + + private final SqlDBDataProvider dbProvider; + private final DB db; + private static final Map envDefaultValues = initEnvDefaultValues(); + + public MariaDBTestBase() throws ManagedProcessException { + this(new Random().nextInt(1000) + 50000); + } + + private static Map initEnvDefaultValues() { + Map defaults = new HashMap<>(); + defaults.put("SDNRDBHOST", "localhost"); + defaults.put("SDNRDBDATABASE", "test"); + + return defaults; + } + + public MariaDBTestBase(String host, int port) { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBPORT", String.valueOf(port)); + envDefaultValues.put("SDNRDBHOST", host); + envDefaultValues.put("SDNRDBDATABASE", "sdnrdb"); + envDefaultValues.put("SDNRDBUSERNAME", "sdnrdb"); + envDefaultValues.put("SDNRDBPASSWORD", "sdnrdb"); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + SqlDBConfig config = new SqlDBConfig(new ConfigurationFileRepresentation("test.properties")); + this.db = null; + this.dbProvider = new SqlDBDataProvider(config); + testCreateTable(this.dbProvider.getDBService()); + } + + public MariaDBTestBase(int port) throws ManagedProcessException { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBPORT", String.valueOf(port)); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + SqlDBConfig config = new SqlDBConfig(new ConfigurationFileRepresentation("test.properties")); + //start db server + this.db = startDatabase(port); + //create db with name sdnrdb + this.dbProvider = new SqlDBDataProvider(config); + testCreateTable(this.dbProvider.getDBService()); + } + + public void close() throws ManagedProcessException { + if (db != null) { + this.db.stop(); + } + + } + + public SqlDBDataProvider getDbProvider() { + return dbProvider; + } + + public DB getDb() { + return db; + } + + private static DB startDatabase(int port) throws ManagedProcessException { + // Start MariaDB4j database + DBConfigurationBuilder dbconfig = DBConfigurationBuilder.newBuilder(); + dbconfig.setPort(port); // 0 => autom. detect free port + DB db = DB.newEmbeddedDB(dbconfig.build()); + db.start(); + return db; + } + public static void testCreateTable(SqlDBClient dbService) { + createTableOdl(dbService); + createTable(dbService, ConnectionlogEntity.class, Entity.Connectionlog, true); + createTable(dbService, EventlogEntity.class, Entity.Eventlog, true); + createTable(dbService, FaultcurrentEntity.class, Entity.Faultcurrent, false); + createTable(dbService, FaultlogEntity.class, Entity.Faultlog, true); + createTable(dbService, GuicutthroughEntity.class, Entity.Guicutthrough, true); + createTable(dbService, Pmdata15mEntity.class, Entity.Historicalperformance15min, false); + createTable(dbService, Pmdata24hEntity.class, Entity.Historicalperformance24h, false); + createTable(dbService, InventoryEntity.class, Entity.Inventoryequipment, false); + createTable(dbService, MaintenanceEntity.class, Entity.Maintenancemode, false); + createTable(dbService, MediatorServerEntity.class, Entity.MediatorServer, true); + createTable(dbService, NetworkElementConnectionEntity.class, Entity.NetworkelementConnection, false); + } + public static boolean createTableOdl(SqlDBClient dbService) { + String createStatement = null; + createStatement = SqlDBMapper.createTableOdl(); + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } + + public static boolean createTable(SqlDBClient dbService, Class cls, Entity entity, boolean autoIndex) { + String createStatement = null; + try { + createStatement = SqlDBMapper.createTable(cls, entity, "", autoIndex); + } catch (UnableToMapClassException e) { + fail(e.getMessage()); + } + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } +} diff --git a/sdnr/wt/data-provider/provider/src/test/resources/tlskeys/keys1.json b/sdnr/wt/data-provider/provider/src/test/resources/tlskeys/keys1.json new file mode 100644 index 000000000..30a4e0a45 --- /dev/null +++ b/sdnr/wt/data-provider/provider/src/test/resources/tlskeys/keys1.json @@ -0,0 +1,40 @@ +{ + "key-credential": [ + { + "key-id": "ODL_private_key_0", + "passphrase": "jKNzkicDKmVrpOehbo/Jtw==", + "private-key": "o9CI/v3k6c39t4THCi25Zs+2QZ3UlPv2Nax2TZCEjfA0kxbiZvfukYA5CrAQi5edqf8ZLBulD/ojSOO+c/AhR52tV59idN4krBd3JXfVbkXI1osZz2lPYnUfgGuZN/iBGYBYlWDe8BuwejK8e/8mEFL36oeKB9PG/f2NJFrMFv0LSG8TTjVxRyU5QRN64MN3d/yju0OAUdCfnJDyLr7VvsH9cP0essQ4uXByWeZIUYUPqSMIdGqGlAJSgYvVPrlKtEJyVmNVo3hG697/oouty/38nz93Jhiet7OA0fXlcAm+9lCfMJwedq4P+AUbCvrLrcAx7h4EYY7eW0irSQxiFreg+hcVxzmKjNDT/GwIaUnkL7fu7Y8PKuhBMUuq8hHcf2EqT/0WStX/QecRhnIy/tkNwboml/kHuTN7mJAcUqZ1r5/mEdn6YYUp6bwRbS9HkakPmjMOR3XuWU5ttBxBhUYOVpkDxITOA4NZr8YUD33PHWq10asPwJEVL8doEysONQHLcFnVluUgovSEaUnH2HicZGF/3JIhs3B6pnU/b3znNesXf3tCjIb2fvfajZPQMPhycDjeulDAHR64/9+6YN4/KUTWEV7nwljAYPlYddm/SFNf1xNgCqAkO6qoVHwcGH8BKB2cR0Rm2H3nBMsIXUABQ2FI+2wlCsnWm/UV/+uDeSKa7uASpArNbnvJM8lkbJC/ZEBzKYAeidOj2md6VNNWetBKaM0XsgnYd3iyr8Rp2QeGxGQz9v8ZdlLrx0J34mE/jsPNbh+ZIFyfEK8BoAWtGxQll5ranoNaBYIGX14ZuZDH63DVraI0p0U6DfKV26lJJoyBLfmMg5mKw7ov/aC81MEhI0lNFtJwpvtSEXwbdgCas24x/q+NxIso3Skdp67hXqVVQXFyc0Trb4Ci7Xgu3kxa8E0thKuKTjfFyDj5o/5/W1ncsh+noXfWCG4ENGWb0PYHKT2XLPbcE8B007zKXv10qFYq4w+JPg6ZYMdIlf9c+bCBJBEiUSd8oYMjXbdiVLKAzStBMvsQQpIKmkyv+07JPVRe3JlFeLoQrysZosJjlblIAiTygRu8fr4rLZl3YhGGE9BUfabBKemtuqS5SwCv0S+XCBq50nt4BsXqafJ7pkijORp4WJ/WXnIbzwIBPFpa8w6YhJpOqVHp2xMahoaEvzCcVpIhhZqoAjXw7hKyOPNAOmv+QXnCpFHidRnxOBpMNwHHHbBLWgTzD4KP0LIldfx10Njrq+i1VR+nf9kxt868ZP6a6MiMXITiuvi5L+I9KJpWZUhSOKAJspiLcWDMp4fDQdDa42vOqOhB5SAntmJap/V+rJEe9G6PqiZBOx7tDr8Q4PI7uCm1o5rWrHkgp/TFJWdrEfVoEcmOccRCWkypWM9GzDkB0BXlJiXLMflqiT4JumgvbB2kxnX45KP0u3dOY4MKc0M3143YaglUXegTDgv6EjyNDUfKCEro8F/zLjsWjVyGKz2kjsj6ZY+MrNb48cZKVmhK8lUfQ+b+zbLWzX6Yc+NRfsYJl8YX9Pb2rY+XCQrTFHaTOgb5RZ4QPTqgaO5TIs9PBez/sH1+T3IiVUP2RSh9E5b3OZ2k/Xrud4xgbuOXVtZXgI1y8wEiPxm4sVS5wLirlRhELmhy1d4yQYNcMPBAhhegPqy1yUCW3A2GSCaZCZuwwf5Lqj6wH3R8WS8pnFa2j+WpbEsZlymnsm/04jlHca2LXJ+4wCpqACOUgKsYc6sV7QDC/T7XoMcfhua89E6S5iHXBDkqTqk0Iy+Wedf37q7H7NgOheBUk651nEZhqOVWQMc8sVJZDyCqQ9Pwgt4ZiMy8rZIDwIviKeHsRN84SPUbOhVBPfnE1Q+9JZcFMBihf0lX9AHebT5aj3XfMQOCp9VrStcMUEzJXdlKTfPuDmalFtg+QpyvQyDz2c15YbMYptLgDj4Bpi7Zsvkn3/b3SsH4HZTxfRF/DMa1rzBWJQHAjd2ATLMX9T/zbwNXP5WiQiEvPF2pyyXfKNbkv6jJ9I3bcTX0vzXIMdlG+m9EPeROwPd0Uc/dnXxuagdZTeS7jR3q5CFixoiOYBhKDSWRT+TstKiOUvwELoqGiIuMCWMjh6PGY2pihpHGcjl5c1KoyxmpBQuCDDUw9wVopv916fOOKkPjXFV665CJrXc644xd" + }, + { + "key-id": "ODL_private_key_1", + "passphrase": "jKNzkicDKmVrpOehbo/Jtw==", + "private-key": "o9CI/v3k6c39t4THCi25Zs+2QZ3UlPv2Nax2TZCEjfA0kxbiZvfukYA5CrAQi5edqf8ZLBulD/ojSOO+c/AhR52tV59idN4krBd3JXfVbkXI1osZz2lPYnUfgGuZN/iBGYBYlWDe8BuwejK8e/8mEFL36oeKB9PG/f2NJFrMFv0LSG8TTjVxRyU5QRN64MN3d/yju0OAUdCfnJDyLr7VvsH9cP0essQ4uXByWeZIUYUPqSMIdGqGlAJSgYvVPrlKtEJyVmNVo3hG697/oouty/38nz93Jhiet7OA0fXlcAm+9lCfMJwedq4P+AUbCvrLrcAx7h4EYY7eW0irSQxiFreg+hcVxzmKjNDT/GwIaUnkL7fu7Y8PKuhBMUuq8hHcf2EqT/0WStX/QecRhnIy/tkNwboml/kHuTN7mJAcUqZ1r5/mEdn6YYUp6bwRbS9HkakPmjMOR3XuWU5ttBxBhUYOVpkDxITOA4NZr8YUD33PHWq10asPwJEVL8doEysONQHLcFnVluUgovSEaUnH2HicZGF/3JIhs3B6pnU/b3znNesXf3tCjIb2fvfajZPQMPhycDjeulDAHR64/9+6YN4/KUTWEV7nwljAYPlYddm/SFNf1xNgCqAkO6qoVHwcGH8BKB2cR0Rm2H3nBMsIXUABQ2FI+2wlCsnWm/UV/+uDeSKa7uASpArNbnvJM8lkbJC/ZEBzKYAeidOj2md6VNNWetBKaM0XsgnYd3iyr8Rp2QeGxGQz9v8ZdlLrx0J34mE/jsPNbh+ZIFyfEK8BoAWtGxQll5ranoNaBYIGX14ZuZDH63DVraI0p0U6DfKV26lJJoyBLfmMg5mKw7ov/aC81MEhI0lNFtJwpvtSEXwbdgCas24x/q+NxIso3Skdp67hXqVVQXFyc0Trb4Ci7Xgu3kxa8E0thKuKTjfFyDj5o/5/W1ncsh+noXfWCG4ENGWb0PYHKT2XLPbcE8B007zKXv10qFYq4w+JPg6ZYMdIlf9c+bCBJBEiUSd8oYMjXbdiVLKAzStBMvsQQpIKmkyv+07JPVRe3JlFeLoQrysZosJjlblIAiTygRu8fr4rLZl3YhGGE9BUfabBKemtuqS5SwCv0S+XCBq50nt4BsXqafJ7pkijORp4WJ/WXnIbzwIBPFpa8w6YhJpOqVHp2xMahoaEvzCcVpIhhZqoAjXw7hKyOPNAOmv+QXnCpFHidRnxOBpMNwHHHbBLWgTzD4KP0LIldfx10Njrq+i1VR+nf9kxt868ZP6a6MiMXITiuvi5L+I9KJpWZUhSOKAJspiLcWDMp4fDQdDa42vOqOhB5SAntmJap/V+rJEe9G6PqiZBOx7tDr8Q4PI7uCm1o5rWrHkgp/TFJWdrEfVoEcmOccRCWkypWM9GzDkB0BXlJiXLMflqiT4JumgvbB2kxnX45KP0u3dOY4MKc0M3143YaglUXegTDgv6EjyNDUfKCEro8F/zLjsWjVyGKz2kjsj6ZY+MrNb48cZKVmhK8lUfQ+b+zbLWzX6Yc+NRfsYJl8YX9Pb2rY+XCQrTFHaTOgb5RZ4QPTqgaO5TIs9PBez/sH1+T3IiVUP2RSh9E5b3OZ2k/Xrud4xgbuOXVtZXgI1y8wEiPxm4sVS5wLirlRhELmhy1d4yQYNcMPBAhhegPqy1yUCW3A2GSCaZCZuwwf5Lqj6wH3R8WS8pnFa2j+WpbEsZlymnsm/04jlHca2LXJ+4wCpqACOUgKsYc6sV7QDC/T7XoMcfhua89E6S5iHXBDkqTqk0Iy+Wedf37q7H7NgOheBUk651nEZhqOVWQMc8sVJZDyCqQ9Pwgt4ZiMy8rZIDwIviKeHsRN84SPUbOhVBPfnE1Q+9JZcFMBihf0lX9AHebT5aj3XfMQOCp9VrStcMUEzJXdlKTfPuDmalFtg+QpyvQyDz2c15YbMYptLgDj4Bpi7Zsvkn3/b3SsH4HZTxfRF/DMa1rzBWJQHAjd2ATLMX9T/zbwNXP5WiQiEvPF2pyyXfKNbkv6jJ9I3bcTX0vzXIMdlG+m9EPeROwPd0Uc/dnXxuagdZTeS7jR3q5CFixoiOYBhKDSWRT+TstKiOUvwELoqGiIuMCWMjh6PGY2pihpHGcjl5c1KoyxmpBQuCDDUw9wVopv916fOOKkPjXFV665CJrXc644xd" + }, + { + "key-id": "ODL_private_key_2", + "passphrase": "jKNzkicDKmVrpOehbo/Jtw==", + "private-key": "o9CI/v3k6c39t4THCi25Zs+2QZ3UlPv2Nax2TZCEjfA0kxbiZvfukYA5CrAQi5edqf8ZLBulD/ojSOO+c/AhR52tV59idN4krBd3JXfVbkXI1osZz2lPYnUfgGuZN/iBGYBYlWDe8BuwejK8e/8mEFL36oeKB9PG/f2NJFrMFv0LSG8TTjVxRyU5QRN64MN3d/yju0OAUdCfnJDyLr7VvsH9cP0essQ4uXByWeZIUYUPqSMIdGqGlAJSgYvVPrlKtEJyVmNVo3hG697/oouty/38nz93Jhiet7OA0fXlcAm+9lCfMJwedq4P+AUbCvrLrcAx7h4EYY7eW0irSQxiFreg+hcVxzmKjNDT/GwIaUnkL7fu7Y8PKuhBMUuq8hHcf2EqT/0WStX/QecRhnIy/tkNwboml/kHuTN7mJAcUqZ1r5/mEdn6YYUp6bwRbS9HkakPmjMOR3XuWU5ttBxBhUYOVpkDxITOA4NZr8YUD33PHWq10asPwJEVL8doEysONQHLcFnVluUgovSEaUnH2HicZGF/3JIhs3B6pnU/b3znNesXf3tCjIb2fvfajZPQMPhycDjeulDAHR64/9+6YN4/KUTWEV7nwljAYPlYddm/SFNf1xNgCqAkO6qoVHwcGH8BKB2cR0Rm2H3nBMsIXUABQ2FI+2wlCsnWm/UV/+uDeSKa7uASpArNbnvJM8lkbJC/ZEBzKYAeidOj2md6VNNWetBKaM0XsgnYd3iyr8Rp2QeGxGQz9v8ZdlLrx0J34mE/jsPNbh+ZIFyfEK8BoAWtGxQll5ranoNaBYIGX14ZuZDH63DVraI0p0U6DfKV26lJJoyBLfmMg5mKw7ov/aC81MEhI0lNFtJwpvtSEXwbdgCas24x/q+NxIso3Skdp67hXqVVQXFyc0Trb4Ci7Xgu3kxa8E0thKuKTjfFyDj5o/5/W1ncsh+noXfWCG4ENGWb0PYHKT2XLPbcE8B007zKXv10qFYq4w+JPg6ZYMdIlf9c+bCBJBEiUSd8oYMjXbdiVLKAzStBMvsQQpIKmkyv+07JPVRe3JlFeLoQrysZosJjlblIAiTygRu8fr4rLZl3YhGGE9BUfabBKemtuqS5SwCv0S+XCBq50nt4BsXqafJ7pkijORp4WJ/WXnIbzwIBPFpa8w6YhJpOqVHp2xMahoaEvzCcVpIhhZqoAjXw7hKyOPNAOmv+QXnCpFHidRnxOBpMNwHHHbBLWgTzD4KP0LIldfx10Njrq+i1VR+nf9kxt868ZP6a6MiMXITiuvi5L+I9KJpWZUhSOKAJspiLcWDMp4fDQdDa42vOqOhB5SAntmJap/V+rJEe9G6PqiZBOx7tDr8Q4PI7uCm1o5rWrHkgp/TFJWdrEfVoEcmOccRCWkypWM9GzDkB0BXlJiXLMflqiT4JumgvbB2kxnX45KP0u3dOY4MKc0M3143YaglUXegTDgv6EjyNDUfKCEro8F/zLjsWjVyGKz2kjsj6ZY+MrNb48cZKVmhK8lUfQ+b+zbLWzX6Yc+NRfsYJl8YX9Pb2rY+XCQrTFHaTOgb5RZ4QPTqgaO5TIs9PBez/sH1+T3IiVUP2RSh9E5b3OZ2k/Xrud4xgbuOXVtZXgI1y8wEiPxm4sVS5wLirlRhELmhy1d4yQYNcMPBAhhegPqy1yUCW3A2GSCaZCZuwwf5Lqj6wH3R8WS8pnFa2j+WpbEsZlymnsm/04jlHca2LXJ+4wCpqACOUgKsYc6sV7QDC/T7XoMcfhua89E6S5iHXBDkqTqk0Iy+Wedf37q7H7NgOheBUk651nEZhqOVWQMc8sVJZDyCqQ9Pwgt4ZiMy8rZIDwIviKeHsRN84SPUbOhVBPfnE1Q+9JZcFMBihf0lX9AHebT5aj3XfMQOCp9VrStcMUEzJXdlKTfPuDmalFtg+QpyvQyDz2c15YbMYptLgDj4Bpi7Zsvkn3/b3SsH4HZTxfRF/DMa1rzBWJQHAjd2ATLMX9T/zbwNXP5WiQiEvPF2pyyXfKNbkv6jJ9I3bcTX0vzXIMdlG+m9EPeROwPd0Uc/dnXxuagdZTeS7jR3q5CFixoiOYBhKDSWRT+TstKiOUvwELoqGiIuMCWMjh6PGY2pihpHGcjl5c1KoyxmpBQuCDDUw9wVopv916fOOKkPjXFV665CJrXc644xd" + }, + { + "key-id": "ODL_private_key_3", + "passphrase": "jKNzkicDKmVrpOehbo/Jtw==", + "private-key": "o9CI/v3k6c39t4THCi25Zs+2QZ3UlPv2Nax2TZCEjfA0kxbiZvfukYA5CrAQi5edqf8ZLBulD/ojSOO+c/AhR52tV59idN4krBd3JXfVbkXI1osZz2lPYnUfgGuZN/iBGYBYlWDe8BuwejK8e/8mEFL36oeKB9PG/f2NJFrMFv0LSG8TTjVxRyU5QRN64MN3d/yju0OAUdCfnJDyLr7VvsH9cP0essQ4uXByWeZIUYUPqSMIdGqGlAJSgYvVPrlKtEJyVmNVo3hG697/oouty/38nz93Jhiet7OA0fXlcAm+9lCfMJwedq4P+AUbCvrLrcAx7h4EYY7eW0irSQxiFreg+hcVxzmKjNDT/GwIaUnkL7fu7Y8PKuhBMUuq8hHcf2EqT/0WStX/QecRhnIy/tkNwboml/kHuTN7mJAcUqZ1r5/mEdn6YYUp6bwRbS9HkakPmjMOR3XuWU5ttBxBhUYOVpkDxITOA4NZr8YUD33PHWq10asPwJEVL8doEysONQHLcFnVluUgovSEaUnH2HicZGF/3JIhs3B6pnU/b3znNesXf3tCjIb2fvfajZPQMPhycDjeulDAHR64/9+6YN4/KUTWEV7nwljAYPlYddm/SFNf1xNgCqAkO6qoVHwcGH8BKB2cR0Rm2H3nBMsIXUABQ2FI+2wlCsnWm/UV/+uDeSKa7uASpArNbnvJM8lkbJC/ZEBzKYAeidOj2md6VNNWetBKaM0XsgnYd3iyr8Rp2QeGxGQz9v8ZdlLrx0J34mE/jsPNbh+ZIFyfEK8BoAWtGxQll5ranoNaBYIGX14ZuZDH63DVraI0p0U6DfKV26lJJoyBLfmMg5mKw7ov/aC81MEhI0lNFtJwpvtSEXwbdgCas24x/q+NxIso3Skdp67hXqVVQXFyc0Trb4Ci7Xgu3kxa8E0thKuKTjfFyDj5o/5/W1ncsh+noXfWCG4ENGWb0PYHKT2XLPbcE8B007zKXv10qFYq4w+JPg6ZYMdIlf9c+bCBJBEiUSd8oYMjXbdiVLKAzStBMvsQQpIKmkyv+07JPVRe3JlFeLoQrysZosJjlblIAiTygRu8fr4rLZl3YhGGE9BUfabBKemtuqS5SwCv0S+XCBq50nt4BsXqafJ7pkijORp4WJ/WXnIbzwIBPFpa8w6YhJpOqVHp2xMahoaEvzCcVpIhhZqoAjXw7hKyOPNAOmv+QXnCpFHidRnxOBpMNwHHHbBLWgTzD4KP0LIldfx10Njrq+i1VR+nf9kxt868ZP6a6MiMXITiuvi5L+I9KJpWZUhSOKAJspiLcWDMp4fDQdDa42vOqOhB5SAntmJap/V+rJEe9G6PqiZBOx7tDr8Q4PI7uCm1o5rWrHkgp/TFJWdrEfVoEcmOccRCWkypWM9GzDkB0BXlJiXLMflqiT4JumgvbB2kxnX45KP0u3dOY4MKc0M3143YaglUXegTDgv6EjyNDUfKCEro8F/zLjsWjVyGKz2kjsj6ZY+MrNb48cZKVmhK8lUfQ+b+zbLWzX6Yc+NRfsYJl8YX9Pb2rY+XCQrTFHaTOgb5RZ4QPTqgaO5TIs9PBez/sH1+T3IiVUP2RSh9E5b3OZ2k/Xrud4xgbuOXVtZXgI1y8wEiPxm4sVS5wLirlRhELmhy1d4yQYNcMPBAhhegPqy1yUCW3A2GSCaZCZuwwf5Lqj6wH3R8WS8pnFa2j+WpbEsZlymnsm/04jlHca2LXJ+4wCpqACOUgKsYc6sV7QDC/T7XoMcfhua89E6S5iHXBDkqTqk0Iy+Wedf37q7H7NgOheBUk651nEZhqOVWQMc8sVJZDyCqQ9Pwgt4ZiMy8rZIDwIviKeHsRN84SPUbOhVBPfnE1Q+9JZcFMBihf0lX9AHebT5aj3XfMQOCp9VrStcMUEzJXdlKTfPuDmalFtg+QpyvQyDz2c15YbMYptLgDj4Bpi7Zsvkn3/b3SsH4HZTxfRF/DMa1rzBWJQHAjd2ATLMX9T/zbwNXP5WiQiEvPF2pyyXfKNbkv6jJ9I3bcTX0vzXIMdlG+m9EPeROwPd0Uc/dnXxuagdZTeS7jR3q5CFixoiOYBhKDSWRT+TstKiOUvwELoqGiIuMCWMjh6PGY2pihpHGcjl5c1KoyxmpBQuCDDUw9wVopv916fOOKkPjXFV665CJrXc644xd" + } + ], + "trusted-certificate": [ + { + "name": "xNF_CA_certificate_0_0", + "certificate": "MIID7TCCAtWgAwIBAgIJAMtE1NGAR5KoMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD\nVQQGEwJDWjEWMBQGA1UECAwNU291dGggTW9yYXZpYTENMAsGA1UEBwwEQnJubzEP\nMA0GA1UECgwGQ0VTTkVUMQwwCgYDVQQLDANUTUMxEzARBgNVBAMMCmV4YW1wbGUg\nQ0ExIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVjYUBsb2NhbGhvc3QwHhcNMTQwNzI0\nMTQxOTAyWhcNMjQwNzIxMTQxOTAyWjCBjDELMAkGA1UEBhMCQ1oxFjAUBgNVBAgM\nDVNvdXRoIE1vcmF2aWExDTALBgNVBAcMBEJybm8xDzANBgNVBAoMBkNFU05FVDEM\nMAoGA1UECwwDVE1DMRMwEQYDVQQDDApleGFtcGxlIENBMSIwIAYJKoZIhvcNAQkB\nFhNleGFtcGxlY2FAbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\nCgKCAQEArD3TDHPAMT2Z84orK4lMlarbgooIUCcRZyLe+QM+8KY8Hn+mGaxPEOTS\nL3ywszqefB/Utm2hPKLHX684iRC14ID9WDGHxPjvoPArhgFhfV+qnPfxKTgxZC12\nuOj4u1V9y+SkTCocFbRfXVBGpojrBuDHXkDMDEWNvr8/52YCv7bGaiBwUHolcLCU\nbmtKILCG0RNJyTaJpXQdAeq5Z1SJotpbfYFFtAXB32hVoLug1dzl2tjG9sb1wq3Q\naDExcbC5w6P65qOkNoyym9ne6QlQagCqVDyFn3vcqkRaTjvZmxauCeUxXgJoXkyW\ncm0lM1KMHdoTArmchw2Dz0yHHSyDAQIDAQABo1AwTjAdBgNVHQ4EFgQUc1YQIqjZ\nsHVwlea0AB4N+ilNI2gwHwYDVR0jBBgwFoAUc1YQIqjZsHVwlea0AB4N+ilNI2gw\nDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEAI/1KH60qnw9Xs2RGfi0/\nIKf5EynXt4bQX8EIyVKwSkYKe04zZxYfLIl/Q2HOPYoFmm3daj5ddr0ZS1i4p4fT\nUhstjsYWvXs3W/HhVmFUslakkn3PrswhP77fCk6eEJLxdfyJ1C7Uudq2m1isZbKi\nh+XF0mG1LxJaDMocSz4eAya7M5brwjy8DoOmA1TnLQFCVcpn+sCr7VC4wE/JqxyV\nhBCk/MuGqqM3B1j90bGFZ112ZOecyE0EDSr6IbiRBtmeNbEwOFjKXhNLYdxpBZ9D\n8A/368OckZkCrVLGuJNxK9UwCVTe8IhotHUqU9EqFDmxdV8oIdU/OzUwwNPA/Bd/\n9g==" + } + ], + "private-key": [ + { + "name": "ODL_private_key_0", + "data": "MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68\nSfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt\n6jAWZDzVfopwpJPAzRPxACDftIqFGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4\nVDUHSNVbglc+u4UbEzNIFXMdEFsJZpkynOmSiTsIATqIhb+2srkVgLwhfkC2qkuH\nQwAHdubuB07ObM2z01UhyEdDvEYGHwtYAGDBL2TAcsI0oGeVkRyuOkV0QY0UN7UE\nFI1yTYw+xZ42HgFx3uGwApCImxhbj69GBYWFqwIDAQABAoIBAQCZN9kR8DGu6V7y\nt0Ax68asL8O5B/OKaHWKQ9LqpVrXmikZJOxkbzoGldow/CIFoU+q+Zbwu9aDa65a\n0wiP7Hoa4Py3q5XNNUrOQDyU/OYC7cI0I83WS0lJ2zOJGYj8wKae5Z81IeQFKGHK\n4lsy1OGPAvPRGh7RjUUgRavA2MCwe07rWRuDb/OJFe4Oh56UMEjwMiNBtMNtncog\nj1vr/qgRJdf9tf0zlJmLvUJ9+HSFFV9I/97LJyFhb95gAfHkjdVroLVgT3Cho+4P\nWtZaKCIGD0OwfOG2nLV4leXvRUk62/LMlB8NI9+JF7Xm+HCKbaWHNWC7mvWSLV58\nZl4AbUWRAoGBANyJ6SFHFRHSPDY026SsdMzXR0eUxBAK7G70oSBKKhY+O1j0ocLE\njI2krHJBhHbLlnvJVyMUaCUOTS5m0uDw9hgSsAqeSL3hL38kxVZw+KNG9Ouno1Fl\nKnE/xXHlPQyeGs/P8nAMzHZxQtEsQdQayJEhK2XXHTsy7Q3MxDisfVJ1AoGBANfD\n34gB+OMx6pwj7zk3qWbYXSX8xjCZMR0ciko+h4xeMP2N8B0oyoqC+v1ABMAtJ3wG\nsGZd0hV9gwM7OUM3SEwkn6oeg1GemWLcn4rlSmTnZc4aeVwrEWlnSNFX3s4g9l4u\nk8Ugu4MVJYqH8HuDQ5Ggl6/QAwPzMSEdCW0O+jOfAoGAIBRbegC5+t6m7Yegz4Ja\ndxV1g98K6f58x+MDsQu4tYWV4mmrQgaPH2dtwizvlMwmdpkh+LNWNtWuumowkJHc\nakIFo3XExQIFg6wYnGtQb4e5xrGa2xMpKlIJaXjb+YLiCYqJDG2ALFZrTrvuU2kV\n9a5qfqTc1qigvNolTM0iaaUCgYApmrZWhnLUdEKV2wP813PNxfioI4afxlpHD8LG\nsCn48gymR6E+Lihn7vuwq5B+8fYEH1ISWxLwW+RQUjIneNhy/jjfV8TgjyFqg7or\n0Sy4KjpiNI6kLBXOakELRNNMkeSPopGR2E7v5rr3bGD9oAD+aqX1G7oJH/KgPPYd\nVl7+ZwKBgQDcHyWYrimjyUgKaQD2GmoO9wdcJYQ59ke9K+OuGlp4ti5arsi7N1tP\nB4f09aeELM2ASIuk8Q/Mx0jQFnm8lzRFXdewgvdPoZW/7VufM9O7dGPOc41cm2Dh\nyrTcXx/VmUBb+/fnXVEgCv7gylp/wtdTGHQBQJHR81jFBz0lnLj+gg==", + "certificate-chain": [ + "MIIECTCCAvGgAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBjDELMAkGA1UEBhMCQ1ox\nFjAUBgNVBAgMDVNvdXRoIE1vcmF2aWExDTALBgNVBAcMBEJybm8xDzANBgNVBAoM\nBkNFU05FVDEMMAoGA1UECwwDVE1DMRMwEQYDVQQDDApleGFtcGxlIENBMSIwIAYJ\nKoZIhvcNAQkBFhNleGFtcGxlY2FAbG9jYWxob3N0MB4XDTE1MDczMDA3MjcxOFoX\nDTM1MDcyNTA3MjcxOFowgYUxCzAJBgNVBAYTAkNaMRYwFAYDVQQIDA1Tb3V0aCBN\nb3JhdmlhMQ8wDQYDVQQKDAZDRVNORVQxDDAKBgNVBAsMA1RNQzEXMBUGA1UEAwwO\nZXhhbXBsZSBjbGllbnQxJjAkBgkqhkiG9w0BCQEWF2V4YW1wbGVjbGllbnRAbG9j\nYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAueCQaNQWoNmF\nK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68SfFNaY06zZl8QB9W02nr5kWeeMY0\nVA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt6jAWZDzVfopwpJPAzRPxACDftIqF\nGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4VDUHSNVbglc+u4UbEzNIFXMdEFsJ\nZpkynOmSiTsIATqIhb+2srkVgLwhfkC2qkuHQwAHdubuB07ObM2z01UhyEdDvEYG\nHwtYAGDBL2TAcsI0oGeVkRyuOkV0QY0UN7UEFI1yTYw+xZ42HgFx3uGwApCImxhb\nj69GBYWFqwIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVu\nU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUXGpLeLnh2cSDARAV\nA7KrBxGYpo8wHwYDVR0jBBgwFoAUc1YQIqjZsHVwlea0AB4N+ilNI2gwDQYJKoZI\nhvcNAQELBQADggEBAJPV3RTXFRtNyOU4rjPpYeBAIAFp2aqGc4t2J1c7oPp/1n+l\nZvjnwtlJpZHxMM783e2ryDQ6dkvXDf8kpwKlg3U3mkJ3xKkDdWrM4QwghXdCN519\naa9qmu0zdFL+jUAaWlQ5tsceOrvbusCcbMqiFGk/QfpHqPv52SVWbYyUx7IX7DE+\nUjgsLHycfV/tlcx4ZE6soTzl9VdgSL/zmzG3rjsr58J80rXckLgBhvijgBlIAJvW\nfC7D0vaouvBInSFXymdPVoUDZ30cdGLf+hI/i/TfsEMOinLrXVdkSGNo6FXAHKSv\nXeB9oFKSzhQ7OPyRyqvEPycUSw/qD6FVr80oDDc=", + "MIID7TCCAtWgAwIBAgIJAMtE1NGAR5KoMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD\nVQQGEwJDWjEWMBQGA1UECAwNU291dGggTW9yYXZpYTENMAsGA1UEBwwEQnJubzEP\nMA0GA1UECgwGQ0VTTkVUMQwwCgYDVQQLDANUTUMxEzARBgNVBAMMCmV4YW1wbGUg\nQ0ExIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVjYUBsb2NhbGhvc3QwHhcNMTQwNzI0\nMTQxOTAyWhcNMjQwNzIxMTQxOTAyWjCBjDELMAkGA1UEBhMCQ1oxFjAUBgNVBAgM\nDVNvdXRoIE1vcmF2aWExDTALBgNVBAcMBEJybm8xDzANBgNVBAoMBkNFU05FVDEM\nMAoGA1UECwwDVE1DMRMwEQYDVQQDDApleGFtcGxlIENBMSIwIAYJKoZIhvcNAQkB\nFhNleGFtcGxlY2FAbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\nCgKCAQEArD3TDHPAMT2Z84orK4lMlarbgooIUCcRZyLe+QM+8KY8Hn+mGaxPEOTS\nL3ywszqefB/Utm2hPKLHX684iRC14ID9WDGHxPjvoPArhgFhfV+qnPfxKTgxZC12\nuOj4u1V9y+SkTCocFbRfXVBGpojrBuDHXkDMDEWNvr8/52YCv7bGaiBwUHolcLCU\nbmtKILCG0RNJyTaJpXQdAeq5Z1SJotpbfYFFtAXB32hVoLug1dzl2tjG9sb1wq3Q\naDExcbC5w6P65qOkNoyym9ne6QlQagCqVDyFn3vcqkRaTjvZmxauCeUxXgJoXkyW\ncm0lM1KMHdoTArmchw2Dz0yHHSyDAQIDAQABo1AwTjAdBgNVHQ4EFgQUc1YQIqjZ\nsHVwlea0AB4N+ilNI2gwHwYDVR0jBBgwFoAUc1YQIqjZsHVwlea0AB4N+ilNI2gw\nDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEAI/1KH60qnw9Xs2RGfi0/\nIKf5EynXt4bQX8EIyVKwSkYKe04zZxYfLIl/Q2HOPYoFmm3daj5ddr0ZS1i4p4fT\nUhstjsYWvXs3W/HhVmFUslakkn3PrswhP77fCk6eEJLxdfyJ1C7Uudq2m1isZbKi\nh+XF0mG1LxJaDMocSz4eAya7M5brwjy8DoOmA1TnLQFCVcpn+sCr7VC4wE/JqxyV\nhBCk/MuGqqM3B1j90bGFZ112ZOecyE0EDSr6IbiRBtmeNbEwOFjKXhNLYdxpBZ9D\n8A/368OckZkCrVLGuJNxK9UwCVTe8IhotHUqU9EqFDmxdV8oIdU/OzUwwNPA/Bd/\n9g==\n" + ] + } + ] +} \ No newline at end of file diff --git a/sdnr/wt/data-provider/setup/pom.xml b/sdnr/wt/data-provider/setup/pom.xml index d5a64a3e5..97b739c2c 100644 --- a/sdnr/wt/data-provider/setup/pom.xml +++ b/sdnr/wt/data-provider/setup/pom.xml @@ -57,6 +57,11 @@ mockito-core test + + ch.vorburger.mariaDB4j + mariaDB4j + test + commons-cli commons-cli @@ -74,11 +79,25 @@ sdnr-wt-common ${project.version} + + ${project.groupId} + sdnr-wt-yang-utils + ${project.version} + ${project.groupId} sdnr-wt-data-provider-model ${project.version} + + ${project.groupId} + sdnr-wt-data-provider-dblib + ${project.version} + + + org.opendaylight.mdsal.binding.model.ietf + rfc6991-ietf-yang-types + @@ -133,7 +152,7 @@ - false + false org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.Program diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderImpl.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderImpl.java index 7d2adc5ad..466a3e290 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderImpl.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderImpl.java @@ -21,127 +21,33 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import org.json.JSONObject; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; -import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateAliasRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteAliasRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AcknowledgedResponse; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetInfoResponse; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListAliasesResponse; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListIndicesResponse; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentData; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataContainer; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ReleaseGroup; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.database.ElasticsearchDataMigrationProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.database.MariaDbDataMigrationProvider; public class DataMigrationProviderImpl implements DataMigrationProviderService { + private final DataMigrationProviderService dbProvider; - private static final Logger LOG = LoggerFactory.getLogger(DataMigrationProviderImpl.class); - - private final HtDatabaseClient dbClient; - - public DataMigrationProviderImpl(HostInfo[] hosts, String username, String password, boolean trustAll, + public DataMigrationProviderImpl(SdnrDbType type, String url, String username, String password, boolean trustAll, long timeoutms) throws Exception { - - dbClient = HtDatabaseClient.getClient(hosts, username, password, trustAll, timeoutms); + if (type == SdnrDbType.ELASTICSEARCH) { + dbProvider = new ElasticsearchDataMigrationProvider(url, username, password, trustAll, timeoutms); + } else { + dbProvider = new MariaDbDataMigrationProvider(url, username, password, trustAll, timeoutms); + } } @Override public DataMigrationReport importData(String filename, boolean dryrun) throws Exception { - return this.importData(filename, dryrun, Release.CURRENT_RELEASE); + return this.dbProvider.importData(filename, dryrun, Release.CURRENT_RELEASE); } + @Override public DataMigrationReport importData(String filename, boolean dryrun, Release forRelease) throws Exception { - DataMigrationReport report = new DataMigrationReport(); - File file = new File(filename); - if (!file.exists()) { - if (dryrun) { - report.error("file %s not found", filename); - return report; - } - throw new FileNotFoundException(filename); - } - DataContainer container = null; - try { - container = DataContainer.load(file); - } catch (Exception e) { - if (dryrun) { - report.error("problem loading file %s: %s", filename, e.getMessage()); - return report; - } - throw new Exception("problem loading file " + filename, e); - } - ReleaseInformation ri = ReleaseInformation.getInstance(forRelease); - SearchHitConverter converter; - Set components = ri.getComponents(); - //for all db components of dest architecture - for (ComponentName component : components) { - //convert to ComponentData for current release with existing ComponentData of the container - converter = SearchHitConverter.Factory.getInstance(container.getRelease(), forRelease, component); - if (converter == null) { - continue; - } - ComponentData data = converter.convert(container); - if (data != null) { - String indexName = ri.getAlias(component); - String dataTypeName = ri.getDataType(component); - if (dryrun) { - report.log("write %d entries into %s/%s", data.size(), indexName, dataTypeName); - } else { - LOG.debug("write {} entries into {}/{}", data.size(), indexName, dataTypeName); - } - for (SearchHit item : data) { - if (!dryrun) { - String id = this.dbClient.doWriteRaw(indexName, dataTypeName, item.getId(), - item.getSourceAsString(), true); - if (!item.getId().equals(id)) { - LOG.warn("entry for {} with original id {} was written with another id {}", - component.getValue(), item.getId(), id); - } - } - } - } else { - if (dryrun) { - report.error("unable to convert data for " + component.getValue() + " from version " - + container.getRelease().getValue() + " to " + forRelease.getValue() + "\n"); - } else { - LOG.warn("unable to convert data for {} from version {} to {}", component.getValue(), - container.getRelease().getValue(), forRelease.getValue()); - } - } - } - LOG.info("import of {} completed", filename); - if (dryrun) { - report.log("import of %s completed", filename); - } - report.setCompleted(true); - return report; + return this.dbProvider.importData(filename, dryrun, forRelease); } @@ -151,49 +57,7 @@ public class DataMigrationProviderImpl implements DataMigrationProviderService { */ @Override public DataMigrationReport exportData(String filename) { - DataMigrationReport report = new DataMigrationReport(); - - DataContainer container = new DataContainer(); - - filename = this.checkFilenameForWrite(filename); - LOG.info("output will be written to {}", filename); - //autodetect version - Release dbRelease = this.autoDetectRelease(); - if (dbRelease == null) { - report.error("unbable to detect db release. is database initialized?"); - return report; - } - ReleaseInformation ri = ReleaseInformation.getInstance(dbRelease); - boolean componentsSucceeded = true; - for (ComponentName c : ri.getComponents()) { - ComponentData data = new ComponentData(c); - SearchResult result = this.dbClient.doReadAllJsonData(ri.getAlias(c), ri.getDataType(c), false); - data.addAll(result.getHits()); - container.addComponent(c, data); - } - try { - Files.write(new File(filename).toPath(), Arrays.asList(container.toJSON()), StandardCharsets.UTF_8); - report.setCompleted(componentsSucceeded); - } catch (IOException e) { - LOG.warn("problem writing data to {}: {}", filename, e); - } - return report; - } - - private String checkFilenameForWrite(String filename) { - File f = new File(filename); - if (!f.exists()) { - return filename; - } - return this.checkFilenameForWrite(filename, 0); - } - - private String checkFilenameForWrite(String filename, int apdx) { - File f = new File(String.format("$s.$d", filename, apdx)); - if (!f.exists()) { - return filename; - } - return this.checkFilenameForWrite(filename, apdx + 1); + return this.dbProvider.exportData(filename); } @Override @@ -202,226 +66,20 @@ public class DataMigrationProviderImpl implements DataMigrationProviderService { } + @Override public Release autoDetectRelease() { - EsVersion dbVersion = this.readActualVersion(); - AliasesEntryList aliases = this.readAliases(); - IndicesEntryList indices = this.readIndices(); - if (indices == null) { - return null; - } - List foundReleases = new ArrayList<>(); - //if there are active aliases reduce indices to the active ones - if (aliases != null && aliases.size() > 0) { - indices = indices.subList(aliases.getLinkedIndices()); - } - for (Release r : Release.values()) { - if (r.isDbInRange(dbVersion)) { - ReleaseInformation ri = ReleaseInformation.getInstance(r); - if (ri != null && ri.containsIndices(indices)) { - foundReleases.add(r); - } - } - } - if (foundReleases.size() == 1) { - return foundReleases.get(0); - } - LOG.error("detect {} releases: {}. unable to detect for which one to do sth.", foundReleases.size(), - foundReleases); - return null; - } - - private EsVersion readActualVersion() { - try { - GetInfoResponse response = this.dbClient.getInfo(); - return response.getVersion(); - } catch (Exception e) { - LOG.warn(e.getMessage()); - } - return null; - } - - private AliasesEntryList readAliases() { - AliasesEntryList entries = null; - try { - ListAliasesResponse response = this.dbClient.getAliases(); - entries = response.getEntries(); - } catch (ParseException | IOException e) { - LOG.error(e.getMessage()); - } - return entries; - } - - private IndicesEntryList readIndices() { - IndicesEntryList entries = null; - try { - ListIndicesResponse response = this.dbClient.getIndices(); - entries = response.getEntries(); - } catch (ParseException | IOException e) { - LOG.error(e.getMessage()); - } - return entries; + return this.dbProvider.autoDetectRelease(); } - @Override public boolean initDatabase(Release release, int numShards, int numReplicas, String dbPrefix, boolean forceRecreate, long timeoutms) { - if (timeoutms > 0) { - this.dbClient.waitForYellowStatus(timeoutms); - } - EsVersion dbVersion = this.readActualVersion(); - if (dbVersion == null) { - return false; - } - LOG.info("detected database version {}", dbVersion); - if (release == null) { - release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion); - if (release == null) { - LOG.warn("unable to autodetect release for this database version for release {}", - ReleaseGroup.CURRENT_RELEASE.name()); - return false; - } - LOG.info("autodetect release {}", release); - } - if (!release.isDbInRange(dbVersion)) { - LOG.warn("db version {} maybe not compatible with release {}", dbVersion, release); - return false; - } - if (forceRecreate) { - this.clearDatabase(release, dbPrefix, 0); - } - ReleaseInformation ri = ReleaseInformation.getInstance(release); - AliasesEntryList aliases = this.readAliases(); - IndicesEntryList indices = this.readIndices(); - if (aliases == null || indices == null) { - return false; - } - AcknowledgedResponse response = null; - if (!ri.runPreInitCommands(this.dbClient)) { - return false; - } - for (ComponentName component : ri.getComponents()) { - try { - if (ri.hasOwnDbIndex(component)) { - //check if index already exists - String indexName = ri.getIndex(component, dbPrefix); - String aliasName = ri.getAlias(component, dbPrefix); - if (indices.findByIndex(indexName) == null) { - LOG.info("creating index for {}", component); - CreateIndexRequest request = new CreateIndexRequest(ri.getIndex(component, dbPrefix)); - request.mappings(new JSONObject(ri.getDatabaseMapping(component))); - request.settings(new JSONObject(ri.getDatabaseSettings(component, numShards, numReplicas))); - response = this.dbClient.createIndex(request); - LOG.info(response.isAcknowledged() ? "succeeded" : "failed"); - } else { - LOG.info("index {} for {} already exists", indexName, component); - } - //check if alias already exists - if (aliases.findByAlias(aliasName) == null) { - LOG.info("creating alias for {}", component); - response = this.dbClient.createAlias(new CreateAliasRequest(indexName, aliasName)); - LOG.info(response.isAcknowledged() ? "succeeded" : "failed"); - } else { - LOG.info("alias {} for index {} for {} already exists", aliasName, indexName, component); - } - } - } catch (IOException e) { - LOG.error(e.getMessage()); - return false; - } - } - if (!ri.runPostInitCommands(this.dbClient)) { - return false; - } - return true; + return this.dbProvider.initDatabase(release, numShards, numReplicas, dbPrefix, forceRecreate, timeoutms); } @Override public boolean clearDatabase(Release release, String dbPrefix, long timeoutms) { - - if (timeoutms > 0) { - this.dbClient.waitForYellowStatus(timeoutms); - } - //check aliases - AliasesEntryList entries = this.readAliases(); - IndicesEntryList entries2 = this.readIndices(); - if (entries == null) { - return false; - } - if (release == null) { - EsVersion dbVersion = this.readActualVersion(); - if (dbVersion == null) { - return false; - } - LOG.info("detected database version {}", dbVersion); - release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion); - if (release == null) { - LOG.warn("unable to autodetect release for this database version for release {}", - ReleaseGroup.CURRENT_RELEASE.name()); - return false; - } - LOG.info("autodetect release {}", release); - } - ReleaseInformation ri = ReleaseInformation.getInstance(release); - AcknowledgedResponse response; - if (entries.size() <= 0) { - LOG.info("no aliases to clear"); - } else { - //check for every component of release if alias exists - for (ComponentName component : ri.getComponents()) { - String aliasToDelete = ri.getAlias(component, dbPrefix); - AliasesEntry entryToDelete = entries.findByAlias(aliasToDelete); - if (entryToDelete != null) { - try { - LOG.info("deleting alias {} for index {}", entryToDelete.getAlias(), entryToDelete.getIndex()); - response = this.dbClient.deleteAlias( - new DeleteAliasRequest(entryToDelete.getIndex(), entryToDelete.getAlias())); - LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); - } catch (IOException e) { - LOG.error(e.getMessage()); - return false; - } - } - else { - //try to find malformed typed index with alias name - IndicesEntry entry2ToDelete = entries2.findByIndex(aliasToDelete); - if (entry2ToDelete != null) { - try { - LOG.info("deleting index {}", entry2ToDelete.getName()); - response = this.dbClient.deleteIndex(new DeleteIndexRequest(entry2ToDelete.getName())); - LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); - } catch (IOException e) { - LOG.error(e.getMessage()); - return false; - } - } - } - } - } - if (entries2 == null) { - return false; - } - if (entries2.size() <= 0) { - LOG.info("no indices to clear"); - } else { - //check for every component of release if index exists - for (ComponentName component : ri.getComponents()) { - String indexToDelete = ri.getIndex(component, dbPrefix); - IndicesEntry entryToDelete = entries2.findByIndex(indexToDelete); - if (entryToDelete != null) { - try { - LOG.info("deleting index {}", entryToDelete.getName()); - response = this.dbClient.deleteIndex(new DeleteIndexRequest(entryToDelete.getName())); - LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); - } catch (IOException e) { - LOG.error(e.getMessage()); - return false; - } - } - } - } - - return true; + return this.dbProvider.clearDatabase(release, dbPrefix, timeoutms); } /** @@ -429,34 +87,6 @@ public class DataMigrationProviderImpl implements DataMigrationProviderService { * @return */ public boolean clearCompleteDatabase(long timeoutms) { - if (timeoutms > 0) { - this.dbClient.waitForYellowStatus(timeoutms); - } - //check aliases and indices - AliasesEntryList aliases = this.readAliases(); - IndicesEntryList indices = this.readIndices(); - if (aliases == null || indices == null) { - return false; - } - for (AliasesEntry alias : aliases) { - try { - LOG.info("deleting alias {} for index {}", alias.getAlias(), alias.getIndex()); - this.dbClient.deleteAlias(new DeleteAliasRequest(alias.getIndex(), alias.getAlias())); - } catch (IOException e) { - LOG.error("problem deleting alias {}: {}", alias.getAlias(), e); - return false; - } - } - for (IndicesEntry index : indices) { - try { - LOG.info("deleting index {}", index.getName()); - this.dbClient.deleteIndex(new DeleteIndexRequest(index.getName())); - } catch (IOException e) { - LOG.error("problem deleting index {}: {}", index.getName(), e); - return false; - } - } - return true; + return this.clearCompleteDatabase(timeoutms); } - } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderService.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderService.java index 0f1fc6c73..53ab32636 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderService.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/DataMigrationProviderService.java @@ -22,7 +22,6 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; import java.io.FileNotFoundException; - import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; @@ -31,7 +30,7 @@ public interface DataMigrationProviderService { /** * import data from file and write these to database - * + * * @param filename source * @param dryrun only loading file and check consistency, not pushing into database * @return report @@ -40,19 +39,22 @@ public interface DataMigrationProviderService { */ DataMigrationReport importData(String filename, boolean dryrun) throws FileNotFoundException, Exception; + DataMigrationReport importData(String filename, boolean dryrun, Release forRelease) throws Exception; /** * export current data to file - * + * * @param filename */ DataMigrationReport exportData(String filename); /** - * + * * @return */ Release getCurrentVersion(); + Release autoDetectRelease(); + /** * @param release * @param numShards @@ -67,8 +69,9 @@ public interface DataMigrationProviderService { /** * clean up the database all data will be removed complete structure will be destroyed - * + * * @return */ boolean clearDatabase(Release release, String dbPrefix, long timeoutms); + } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/Program.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/Program.java index 54204f170..56973f459 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/Program.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/Program.java @@ -37,7 +37,7 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.RollingFileAppender; -import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MavenDatabasePluginInitFile; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; @@ -77,7 +77,8 @@ public class Program { private static final int DEFAULT_SHARDS = 5; private static final int DEFAULT_REPLICAS = 1; private static final int DEFAULT_DATABASEWAIT_SECONDS = 30; - private static final String DEFAULT_DBURL = "http://sdnrdb:9200"; + private static final String DEFAULT_DBURL_ELASTICSEARCH = "http://sdnrdb:9200"; + private static final String DEFAULT_DBURL_MARIADB = "jdbc:mysql://sdnrdb:3306/sdnrdb"; private static final String DEFAULT_DBPREFIX = ""; private static final boolean DEFAULT_TRUSTINSECURESSL = false; @@ -103,6 +104,8 @@ public class Program { private static final String OPTION_DATABASE_LONG = "dburl"; private static final String OPTION_COMMAND_SHORT = "c"; private static final String OPTION_COMMAND_LONG = "cmd"; + private static final String OPTION_DATABASETYPE_SHORT = "dbt"; + private static final String OPTION_DATABASETYPE_LONG = "db-type"; private static final String OPTION_DATABASEUSER_SHORT = "dbu"; private static final String OPTION_DATABASEUSER_LONG = "db-username"; private static final String OPTION_DATABASEPASSWORD_SHORT = "dbp"; @@ -141,6 +144,9 @@ public class Program { if (def instanceof Release) { return cmd.hasOption(option) ? (T) Release.getValueBySuffix(cmd.getOptionValue(option)) : def; } + if (def instanceof SdnrDbType) { + return cmd.hasOption(option) ? (T) SdnrDbType.valueOf(cmd.getOptionValue(option).toUpperCase()) : def; + } if (cmd.hasOption(option) && cmd.getOptionValue(option) != null) { if (option.equals(OPTION_VERSION_SHORT)) { String v = cmd.getOptionValue(option); @@ -303,7 +309,7 @@ public class Program { if (filename == null) { throw new Exception("please add output file parameter"); } - DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] {HostInfo.parse(options.getUrl())}, + DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(), options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs()); DataMigrationReport report = service.importData(filename, false); LOG.info(report); @@ -319,7 +325,7 @@ public class Program { if (filename == null) { throw new Exception("please add output file parameter"); } - DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] {HostInfo.parse(options.getUrl())}, + DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(), options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs()); DataMigrationReport report = service.exportData(filename); LOG.info(report); @@ -342,7 +348,7 @@ public class Program { Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, (Release) null); DatabaseOptions options = new DatabaseOptions(cmd); String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX); - DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] {HostInfo.parse(options.getUrl())}, + DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(), options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs()); if (!service.clearDatabase(r, dbPrefix, options.getTimeoutMs())) { throw new Exception("failed to init database"); @@ -352,7 +358,7 @@ public class Program { private static void cmd_clear_db_complete(CommandLine cmd) throws Exception { DatabaseOptions options = new DatabaseOptions(cmd); - DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] {HostInfo.parse(options.getUrl())}, + DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(), options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs()); if (!service.clearCompleteDatabase(options.getTimeoutMs())) { throw new Exception("failed to init database"); @@ -366,7 +372,7 @@ public class Program { int numReplicas = getOptionOrDefault(cmd, OPTION_REPLICAS_SHORT, DEFAULT_REPLICAS); DatabaseOptions options = new DatabaseOptions(cmd); String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX); - DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] {HostInfo.parse(options.getUrl())}, + DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(),options.getUrl(), options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs()); boolean forceRecreate = cmd.hasOption(OPTION_FORCE_RECREATE_SHORT); if (!service.initDatabase(r, numShards, numReplicas, dbPrefix, forceRecreate, options.getTimeoutMs())) { @@ -380,6 +386,8 @@ public class Program { Options result = new Options(); result.addOption(createOption(OPTION_COMMAND_SHORT, OPTION_COMMAND_LONG, true, "command to execute", false)); result.addOption(createOption(OPTION_DATABASE_SHORT, OPTION_DATABASE_LONG, true, "database url", false)); + result.addOption(createOption(OPTION_DATABASETYPE_SHORT, OPTION_DATABASETYPE_LONG, true, + "database type (elasticsearch|mariadb)", false)); result.addOption(createOption(OPTION_DATABASEUSER_SHORT, OPTION_DATABASEUSER_LONG, true, "database basic auth username", false)); result.addOption(createOption(OPTION_DATABASEPASSWORD_SHORT, OPTION_DATABASEPASSWORD_LONG, true, @@ -432,10 +440,14 @@ public class Program { private final String password; private final boolean trustAll; private final long timeoutMs; + private final SdnrDbType type; public String getUrl() { return this.url; } + public SdnrDbType getType() { + return this.type; + } public String getUsername() { return this.username; } @@ -448,8 +460,11 @@ public class Program { public long getTimeoutMs() { return this.timeoutMs; } + public DatabaseOptions(CommandLine cmd) throws ParseException { - this.url = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL); + this.type = getOptionOrDefault(cmd, OPTION_DATABASETYPE_LONG, SdnrDbType.ELASTICSEARCH); + this.url = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, + this.type == SdnrDbType.ELASTICSEARCH ? DEFAULT_DBURL_ELASTICSEARCH : DEFAULT_DBURL_MARIADB); this.username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null); this.password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null); this.trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, DEFAULT_TRUSTINSECURESSL); diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/ReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/ReleaseInformation.java index 8362b0030..6e5bdc522 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/ReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/ReleaseInformation.java @@ -21,13 +21,17 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; +import java.util.HashMap; import java.util.Map; import java.util.Set; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MariaDBTableInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto.ElAltoReleaseInformation; @@ -42,15 +46,26 @@ public abstract class ReleaseInformation { // variables private final Release release; private final Map dbMap; + private Map mariadbMap; // end of variables // constructors public ReleaseInformation(Release r, Map dbMap) { + this(r, dbMap, new HashMap<>()); + } + + public ReleaseInformation(Release r, Map dbMap, + Map mariadbMap) { this.release = r; this.dbMap = dbMap; + this.mariadbMap = mariadbMap; } // end of constructors + protected Release getReleas() { + return this.release; + } + /** * get database alias for component * @@ -120,6 +135,18 @@ public abstract class ReleaseInformation { return dbMap.get(name) == null ? null : dbMap.get(name).getSettings(shards, replicas); } + public String getDatabaseMapping(ComponentName name, SdnrDbType dbType) { + switch (dbType) { + case ELASTICSEARCH: + return this.getDatabaseMapping(name); + case MARIADB: + return mariadbMap.get(name) == null ? null + : mariadbMap.get(name).getMapping(this.release.getDBSuffix()); + default: + return null; + } + } + /** * get converter for component data * @@ -172,7 +199,7 @@ public abstract class ReleaseInformation { * @param indices * @return true if components of this release are covered by the given indices */ - protected boolean containsIndices(IndicesEntryList indices) { + public boolean containsIndices(IndicesEntryList indices) { if (this.dbMap.size() <= 0) { return false; @@ -191,13 +218,19 @@ public abstract class ReleaseInformation { * @param dbClient * @return if succeeded or not */ - protected abstract boolean runPreInitCommands(HtDatabaseClient dbClient); + public abstract boolean runPreInitCommands(HtDatabaseClient dbClient); + + public abstract boolean runPreInitCommands(SqlDBClient dbClient); /** * * @param dbClient * @return if succeeded or not */ - protected abstract boolean runPostInitCommands(HtDatabaseClient dbClient); + public abstract boolean runPostInitCommands(HtDatabaseClient dbClient); + + public abstract boolean runPostInitCommands(SqlDBClient dbClient); + + } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/MariaDBTableInfo.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/MariaDBTableInfo.java new file mode 100644 index 000000000..956790942 --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/MariaDBTableInfo.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data; + +/** + * @author Michael Dürre + * + */ +public class MariaDBTableInfo { + public final String alias; + protected final String mapping; + + public MariaDBTableInfo(String alias, String mapping) { + this.alias = alias; + this.mapping = mapping; + } + + public String getMapping(String dbSuffix) { + return this.mapping.replaceAll("%s", dbSuffix); + } + + +} diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/Release.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/Release.java index 119479b97..22338e89a 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/Release.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/Release.java @@ -22,34 +22,51 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data; import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; + public enum Release { - EL_ALTO("el alto", "_v1", new EsVersion(2, 2, 0), new EsVersion(2, 2, 0)), - FRANKFURT_R1("frankfurt-R1", "-v2", new EsVersion(6, 4, 3), new EsVersion(6, 8, 6)), - FRANKFURT_R2("frankfurt-R2", "-v3", new EsVersion(7, 0, 1), new EsVersion(7, 6, 1)), - GUILIN_R1("guilin-R1", "-v4", new EsVersion(7,1,1), new EsVersion(7,6,1)), - HONOLULU_R1("honolulu-R1", "-v5", new EsVersion(7,1,1), new EsVersion(8,0,0), false), - ISTANBUL_R1("istanbul-R1", "-v6", new EsVersion(7,1,1), new EsVersion(8,0,0), false); + EL_ALTO("el alto", "_v1", new DatabaseVersion(2, 2, 0), new DatabaseVersion(2, 2, 0)), + FRANKFURT_R1("frankfurt-R1", "-v2", new DatabaseVersion(6, 4, 3), new DatabaseVersion(6, 8, 6)), + FRANKFURT_R2("frankfurt-R2", "-v3", new DatabaseVersion(7, 0, 1), new DatabaseVersion(7, 6, 1)), + GUILIN_R1("guilin-R1", "-v4", new DatabaseVersion(7,1,1), new DatabaseVersion(7,6,1)), + HONOLULU_R1("honolulu-R1", "-v5", new DatabaseVersion(7,1,1), new DatabaseVersion(8,0,0), false), + ISTANBUL_R1("istanbul-R1", "-v6", new DatabaseVersion(7,1,1), new DatabaseVersion(8,0,0), false, + new DatabaseVersion(10,2,7), new DatabaseVersion(10,6,0), false); public static final Release CURRENT_RELEASE = Release.ISTANBUL_R1; private final String value; private final String dbSuffix; - private final EsVersion minDbVersion; - private final EsVersion maxDbVersion; + private final DatabaseVersion minDbVersion; + private final DatabaseVersion maxDbVersion; + private final DatabaseVersion minMariaDbVersion; + private final DatabaseVersion maxMariaDbVersion; private final boolean includeEndVersion; + private final boolean mariaDbIncludeEndVersion; + + private Release(String s, String dbsuffix, DatabaseVersion minDbVersion, DatabaseVersion maxDbVersion) { + this(s, dbsuffix, minDbVersion, maxDbVersion, true, null, null, false); + } + + private Release(String s, String dbsuffix, DatabaseVersion minDbVersion, DatabaseVersion maxDbVersion, + boolean includeEnd) { + this(s, dbsuffix, minDbVersion, maxDbVersion, includeEnd, null, null, false); + } - private Release(String s, String dbsuffix, EsVersion minDbVersion, EsVersion maxDbVersion) { - this(s, dbsuffix, minDbVersion, maxDbVersion, true); - } - private Release(String s, String dbsuffix, EsVersion minDbVersion, EsVersion maxDbVersion, boolean includeEnd) { + private Release(String s, String dbsuffix, DatabaseVersion minDbVersion, DatabaseVersion maxDbVersion, + boolean includeEnd, DatabaseVersion minMariaDbVersion, DatabaseVersion maxMariaDbVersion, + boolean mariaDbIncludeEnd) { this.value = s; this.dbSuffix = dbsuffix; this.minDbVersion = minDbVersion; this.maxDbVersion = maxDbVersion; this.includeEndVersion = includeEnd; + this.minMariaDbVersion = minMariaDbVersion; + this.maxMariaDbVersion = maxMariaDbVersion; + this.mariaDbIncludeEndVersion = mariaDbIncludeEnd; } @Override @@ -94,16 +111,24 @@ public enum Release { return this.dbSuffix; } - public EsVersion getDBVersion() { + public DatabaseVersion getDBVersion() { return this.minDbVersion; } - public boolean isDbInRange(EsVersion dbVersion) { - if(this.includeEndVersion) { - return dbVersion.isNewerOrEqualThan(minDbVersion) && dbVersion.isOlderOrEqualThan(maxDbVersion); - } - else { - return dbVersion.isNewerOrEqualThan(minDbVersion) && dbVersion.isOlderThan(maxDbVersion); + public boolean isDbInRange(DatabaseVersion dbVersion, SdnrDbType type) { + if (type == SdnrDbType.ELASTICSEARCH) { + if (this.includeEndVersion) { + return dbVersion.isNewerOrEqualThan(minDbVersion) && dbVersion.isOlderOrEqualThan(maxDbVersion); + } else { + return dbVersion.isNewerOrEqualThan(minDbVersion) && dbVersion.isOlderThan(maxDbVersion); + } + } else { + if (this.mariaDbIncludeEndVersion) { + return dbVersion.isNewerOrEqualThan(minMariaDbVersion) + && dbVersion.isOlderOrEqualThan(maxMariaDbVersion); + } else { + return dbVersion.isNewerOrEqualThan(minMariaDbVersion) && dbVersion.isOlderThan(maxMariaDbVersion); + } } } } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/ReleaseGroup.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/ReleaseGroup.java index 664dce677..58e8d2055 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/ReleaseGroup.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/data/ReleaseGroup.java @@ -23,7 +23,9 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data; import java.util.ArrayList; import java.util.List; -import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; + /** * @author Michael Dürre @@ -51,10 +53,10 @@ public enum ReleaseGroup { * @param dbVersion * @return */ - public Release getLatestCompatibleRelease(EsVersion dbVersion) { + public Release getLatestCompatibleRelease(DatabaseVersion dbVersion, SdnrDbType type) { Release match = null; for (Release r : this.releases) { - if (r.isDbInRange(dbVersion)) { + if (r.isDbInRange(dbVersion, type)) { match = r; } } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/ElasticsearchDataMigrationProvider.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/ElasticsearchDataMigrationProvider.java new file mode 100644 index 000000000..3cfa8c17f --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/ElasticsearchDataMigrationProvider.java @@ -0,0 +1,465 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.database; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateAliasRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteAliasRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AcknowledgedResponse; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetInfoResponse; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListAliasesResponse; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListIndicesResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.DataMigrationProviderService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentData; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataContainer; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ReleaseGroup; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ElasticsearchDataMigrationProvider implements DataMigrationProviderService { + + + private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchDataMigrationProvider.class); + private final HtDatabaseClient dbClient; + + public ElasticsearchDataMigrationProvider(String url, String username, String password, boolean trustAll, + long timeoutms) throws Exception { + dbClient = HtDatabaseClient.getClient(new HostInfo[] {HostInfo.parse(url)}, username, password, trustAll, + true, timeoutms); + } + + @Override + public DataMigrationReport importData(String filename, boolean dryrun) throws Exception { + return this.importData(filename, dryrun, Release.CURRENT_RELEASE); + } + + @Override + public DataMigrationReport importData(String filename, boolean dryrun, Release forRelease) throws Exception { + DataMigrationReport report = new DataMigrationReport(); + File file = new File(filename); + if (!file.exists()) { + if (dryrun) { + report.error("file %s not found", filename); + return report; + } + throw new FileNotFoundException(filename); + } + DataContainer container = null; + try { + container = DataContainer.load(file); + } catch (Exception e) { + if (dryrun) { + report.error("problem loading file %s: %s", filename, e.getMessage()); + return report; + } + throw new Exception("problem loading file " + filename, e); + } + ReleaseInformation ri = ReleaseInformation.getInstance(forRelease); + SearchHitConverter converter; + Set components = ri.getComponents(); + //for all db components of dest architecture + for (ComponentName component : components) { + //convert to ComponentData for current release with existing ComponentData of the container + converter = SearchHitConverter.Factory.getInstance(container.getRelease(), forRelease, component); + if (converter == null) { + continue; + } + ComponentData data = converter.convert(container); + if (data != null) { + String indexName = ri.getAlias(component); + String dataTypeName = ri.getDataType(component); + if (dryrun) { + report.log("write %d entries into %s/%s", data.size(), indexName, dataTypeName); + } else { + LOG.debug("write {} entries into {}/{}", data.size(), indexName, dataTypeName); + } + for (SearchHit item : data) { + if (!dryrun) { + String id = this.dbClient.doWriteRaw(indexName, dataTypeName, item.getId(), + item.getSourceAsString(), true); + if (!item.getId().equals(id)) { + LOG.warn("entry for {} with original id {} was written with another id {}", + component.getValue(), item.getId(), id); + } + } + } + } else { + if (dryrun) { + report.error("unable to convert data for " + component.getValue() + " from version " + + container.getRelease().getValue() + " to " + forRelease.getValue() + "\n"); + } else { + LOG.warn("unable to convert data for {} from version {} to {}", component.getValue(), + container.getRelease().getValue(), forRelease.getValue()); + } + } + } + LOG.info("import of {} completed", filename); + if (dryrun) { + report.log("import of %s completed", filename); + } + report.setCompleted(true); + return report; + } + + + /** + * export data if file exists .1 (.n) will be created + * + */ + @Override + public DataMigrationReport exportData(String filename) { + DataMigrationReport report = new DataMigrationReport(); + + DataContainer container = new DataContainer(); + + filename = this.checkFilenameForWrite(filename); + LOG.info("output will be written to {}", filename); + //autodetect version + Release dbRelease = this.autoDetectRelease(); + if (dbRelease == null) { + report.error("unbable to detect db release. is database initialized?"); + return report; + } + ReleaseInformation ri = ReleaseInformation.getInstance(dbRelease); + boolean componentsSucceeded = true; + for (ComponentName c : ri.getComponents()) { + ComponentData data = new ComponentData(c); + SearchResult result = this.dbClient.doReadAllJsonData(ri.getAlias(c), ri.getDataType(c), false); + data.addAll(result.getHits()); + container.addComponent(c, data); + } + try { + Files.write(new File(filename).toPath(), Arrays.asList(container.toJSON()), StandardCharsets.UTF_8); + report.setCompleted(componentsSucceeded); + } catch (IOException e) { + LOG.warn("problem writing data to {}: {}", filename, e); + } + return report; + } + + private String checkFilenameForWrite(String filename) { + File f = new File(filename); + if (!f.exists()) { + return filename; + } + return this.checkFilenameForWrite(filename, 0); + } + + private String checkFilenameForWrite(String filename, int apdx) { + File f = new File(String.format("$s.$d", filename, apdx)); + if (!f.exists()) { + return filename; + } + return this.checkFilenameForWrite(filename, apdx + 1); + } + + @Override + public Release getCurrentVersion() { + return Release.CURRENT_RELEASE; + } + + + @Override + public Release autoDetectRelease() { + DatabaseVersion dbVersion = this.readActualVersion(); + AliasesEntryList aliases = this.readAliases(); + IndicesEntryList indices = this.readIndices(); + if (indices == null) { + return null; + } + List foundReleases = new ArrayList<>(); + //if there are active aliases reduce indices to the active ones + if (aliases != null && aliases.size() > 0) { + indices = indices.subList(aliases.getLinkedIndices()); + } + for (Release r : Release.values()) { + if (r.isDbInRange(dbVersion, SdnrDbType.ELASTICSEARCH)) { + ReleaseInformation ri = ReleaseInformation.getInstance(r); + if (ri != null && ri.containsIndices(indices)) { + foundReleases.add(r); + } + } + } + if (foundReleases.size() == 1) { + return foundReleases.get(0); + } + LOG.error("detect {} releases: {}. unable to detect for which one to do sth.", foundReleases.size(), + foundReleases); + return null; + } + + private DatabaseVersion readActualVersion() { + try { + GetInfoResponse response = this.dbClient.getInfo(); + return response.getVersion(); + } catch (Exception e) { + LOG.warn(e.getMessage()); + } + return null; + } + + private AliasesEntryList readAliases() { + AliasesEntryList entries = null; + try { + ListAliasesResponse response = this.dbClient.getAliases(); + entries = response.getEntries(); + } catch (ParseException | IOException e) { + LOG.error(e.getMessage()); + } + return entries; + } + + private IndicesEntryList readIndices() { + IndicesEntryList entries = null; + try { + ListIndicesResponse response = this.dbClient.getIndices(); + entries = response.getEntries(); + } catch (ParseException | IOException e) { + LOG.error(e.getMessage()); + } + return entries; + } + + + @Override + public boolean initDatabase(Release release, int numShards, int numReplicas, String dbPrefix, boolean forceRecreate, + long timeoutms) { + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + DatabaseVersion dbVersion = this.readActualVersion(); + if (dbVersion == null) { + return false; + } + LOG.info("detected database version {}", dbVersion); + if (release == null) { + release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.ELASTICSEARCH); + if (release == null) { + LOG.warn("unable to autodetect release for this database version for release {}", + ReleaseGroup.CURRENT_RELEASE.name()); + return false; + } + LOG.info("autodetect release {}", release); + } + if (!release.isDbInRange(dbVersion, SdnrDbType.ELASTICSEARCH)) { + LOG.warn("db version {} maybe not compatible with release {}", dbVersion, release); + return false; + } + if (forceRecreate) { + this.clearDatabase(release, dbPrefix, 0); + } + ReleaseInformation ri = ReleaseInformation.getInstance(release); + AliasesEntryList aliases = this.readAliases(); + IndicesEntryList indices = this.readIndices(); + if (aliases == null || indices == null) { + return false; + } + AcknowledgedResponse response = null; + if (!ri.runPreInitCommands(this.dbClient)) { + return false; + } + for (ComponentName component : ri.getComponents()) { + try { + if (ri.hasOwnDbIndex(component)) { + //check if index already exists + String indexName = ri.getIndex(component, dbPrefix); + String aliasName = ri.getAlias(component, dbPrefix); + if (indices.findByIndex(indexName) == null) { + LOG.info("creating index for {}", component); + CreateIndexRequest request = new CreateIndexRequest(ri.getIndex(component, dbPrefix)); + request.mappings(new JSONObject(ri.getDatabaseMapping(component))); + request.settings(new JSONObject(ri.getDatabaseSettings(component, numShards, numReplicas))); + response = this.dbClient.createIndex(request); + LOG.info(response.isAcknowledged() ? "succeeded" : "failed"); + } else { + LOG.info("index {} for {} already exists", indexName, component); + } + //check if alias already exists + if (aliases.findByAlias(aliasName) == null) { + LOG.info("creating alias for {}", component); + response = this.dbClient.createAlias(new CreateAliasRequest(indexName, aliasName)); + LOG.info(response.isAcknowledged() ? "succeeded" : "failed"); + } else { + LOG.info("alias {} for index {} for {} already exists", aliasName, indexName, component); + } + } + } catch (IOException e) { + LOG.error(e.getMessage()); + return false; + } + } + if (!ri.runPostInitCommands(this.dbClient)) { + return false; + } + return true; + } + + @Override + public boolean clearDatabase(Release release, String dbPrefix, long timeoutms) { + + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + //check aliases + AliasesEntryList entries = this.readAliases(); + IndicesEntryList entries2 = this.readIndices(); + if (entries == null) { + return false; + } + if (release == null) { + DatabaseVersion dbVersion = this.readActualVersion(); + if (dbVersion == null) { + return false; + } + LOG.info("detected database version {}", dbVersion); + release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.ELASTICSEARCH); + if (release == null) { + LOG.warn("unable to autodetect release for this database version for release {}", + ReleaseGroup.CURRENT_RELEASE.name()); + return false; + } + LOG.info("autodetect release {}", release); + } + ReleaseInformation ri = ReleaseInformation.getInstance(release); + AcknowledgedResponse response; + if (entries.size() <= 0) { + LOG.info("no aliases to clear"); + } else { + //check for every component of release if alias exists + for (ComponentName component : ri.getComponents()) { + String aliasToDelete = ri.getAlias(component, dbPrefix); + AliasesEntry entryToDelete = entries.findByAlias(aliasToDelete); + if (entryToDelete != null) { + try { + LOG.info("deleting alias {} for index {}", entryToDelete.getAlias(), entryToDelete.getIndex()); + response = this.dbClient.deleteAlias( + new DeleteAliasRequest(entryToDelete.getIndex(), entryToDelete.getAlias())); + LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); + } catch (IOException e) { + LOG.error(e.getMessage()); + return false; + } + } else { + //try to find malformed typed index with alias name + IndicesEntry entry2ToDelete = entries2.findByIndex(aliasToDelete); + if (entry2ToDelete != null) { + try { + LOG.info("deleting index {}", entry2ToDelete.getName()); + response = this.dbClient.deleteIndex(new DeleteIndexRequest(entry2ToDelete.getName())); + LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); + } catch (IOException e) { + LOG.error(e.getMessage()); + return false; + } + } + } + } + } + if (entries2 == null) { + return false; + } + if (entries2.size() <= 0) { + LOG.info("no indices to clear"); + } else { + //check for every component of release if index exists + for (ComponentName component : ri.getComponents()) { + String indexToDelete = ri.getIndex(component, dbPrefix); + IndicesEntry entryToDelete = entries2.findByIndex(indexToDelete); + if (entryToDelete != null) { + try { + LOG.info("deleting index {}", entryToDelete.getName()); + response = this.dbClient.deleteIndex(new DeleteIndexRequest(entryToDelete.getName())); + LOG.info(response.isResponseSucceeded() ? "succeeded" : "failed"); + } catch (IOException e) { + LOG.error(e.getMessage()); + return false; + } + } + } + } + + return true; + } + + /** + * @param timeoutms + * @return + */ + public boolean clearCompleteDatabase(long timeoutms) { + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + //check aliases and indices + AliasesEntryList aliases = this.readAliases(); + IndicesEntryList indices = this.readIndices(); + if (aliases == null || indices == null) { + return false; + } + for (AliasesEntry alias : aliases) { + try { + LOG.info("deleting alias {} for index {}", alias.getAlias(), alias.getIndex()); + this.dbClient.deleteAlias(new DeleteAliasRequest(alias.getIndex(), alias.getAlias())); + } catch (IOException e) { + LOG.error("problem deleting alias {}: {}", alias.getAlias(), e); + return false; + } + } + for (IndicesEntry index : indices) { + try { + LOG.info("deleting index {}", index.getName()); + this.dbClient.deleteIndex(new DeleteIndexRequest(index.getName())); + } catch (IOException e) { + LOG.error("problem deleting index {}: {}", index.getName(), e); + return false; + } + } + return true; + } + +} diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/MariaDbDataMigrationProvider.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/MariaDbDataMigrationProvider.java new file mode 100644 index 000000000..d46fc4e2e --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/MariaDbDataMigrationProvider.java @@ -0,0 +1,433 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.database; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.sql.SQLException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.DataMigrationProviderService; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentData; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataContainer; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ReleaseGroup; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MariaDbDataMigrationProvider implements DataMigrationProviderService { + + + private static final Logger LOG = LoggerFactory.getLogger(MariaDbDataMigrationProvider.class); + private static final SdnrDbType DBTYPE = SdnrDbType.MARIADB; + private final SqlDBClient dbClient; + + public MariaDbDataMigrationProvider(String url, String username, String password, boolean trustAll, + long timeoutms) throws Exception { + dbClient = new SqlDBClient(url, username, password); + } + + @Override + public DataMigrationReport importData(String filename, boolean dryrun) throws Exception { + return this.importData(filename, dryrun, Release.CURRENT_RELEASE); + } + + @Override + public DataMigrationReport importData(String filename, boolean dryrun, Release forRelease) throws Exception { + DataMigrationReport report = new DataMigrationReport(); + File file = new File(filename); + if (!file.exists()) { + if (dryrun) { + report.error("file %s not found", filename); + return report; + } + throw new FileNotFoundException(filename); + } + DataContainer container = null; + try { + container = DataContainer.load(file); + } catch (Exception e) { + if (dryrun) { + report.error("problem loading file %s: %s", filename, e.getMessage()); + return report; + } + throw new Exception("problem loading file " + filename, e); + } + ReleaseInformation ri = ReleaseInformation.getInstance(forRelease); + SearchHitConverter converter; + Set components = ri.getComponents(); + //for all db components of dest architecture + for (ComponentName component : components) { + //convert to ComponentData for current release with existing ComponentData of the container + converter = SearchHitConverter.Factory.getInstance(container.getRelease(), forRelease, component); + if (converter == null) { + continue; + } + ComponentData data = converter.convert(container); + if (data != null) { + String indexName = ri.getAlias(component); + String dataTypeName = ri.getDataType(component); + if (dryrun) { + report.log("write %d entries into %s/%s", data.size(), indexName, dataTypeName); + } else { + LOG.debug("write {} entries into {}/{}", data.size(), indexName, dataTypeName); + } + for (SearchHit item : data) { + if (!dryrun) { + String id = null;//this.dbClient.doWriteRaw(indexName, dataTypeName, item.getId(), item.getSourceAsString(), true); + if (!item.getId().equals(id)) { + LOG.warn("entry for {} with original id {} was written with another id {}", + component.getValue(), item.getId(), id); + } + } + } + } else { + if (dryrun) { + report.error("unable to convert data for " + component.getValue() + " from version " + + container.getRelease().getValue() + " to " + forRelease.getValue() + "\n"); + } else { + LOG.warn("unable to convert data for {} from version {} to {}", component.getValue(), + container.getRelease().getValue(), forRelease.getValue()); + } + } + } + LOG.info("import of {} completed", filename); + if (dryrun) { + report.log("import of %s completed", filename); + } + report.setCompleted(true); + return report; + } + + + /** + * export data if file exists .1 (.n) will be created + * + */ + @Override + public DataMigrationReport exportData(String filename) { + DataMigrationReport report = new DataMigrationReport(); + + DataContainer container = new DataContainer(); + + filename = this.checkFilenameForWrite(filename); + LOG.info("output will be written to {}", filename); + //autodetect version + Release dbRelease = this.autoDetectRelease(); + if (dbRelease == null) { + report.error("unbable to detect db release. is database initialized?"); + return report; + } + ReleaseInformation ri = ReleaseInformation.getInstance(dbRelease); + boolean componentsSucceeded = true; + for (ComponentName c : ri.getComponents()) { + ComponentData data = new ComponentData(c); + SearchResult result = null;//this.dbClient.doReadAllJsonData(ri.getAlias(c), ri.getDataType(c), false); + data.addAll(result.getHits()); + container.addComponent(c, data); + } + try { + Files.write(new File(filename).toPath(), Arrays.asList(container.toJSON()), StandardCharsets.UTF_8); + report.setCompleted(componentsSucceeded); + } catch (IOException e) { + LOG.warn("problem writing data to {}: {}", filename, e); + } + return report; + } + + private String checkFilenameForWrite(String filename) { + File f = new File(filename); + if (!f.exists()) { + return filename; + } + return this.checkFilenameForWrite(filename, 0); + } + + private String checkFilenameForWrite(String filename, int apdx) { + File f = new File(String.format("$s.$d", filename, apdx)); + if (!f.exists()) { + return filename; + } + return this.checkFilenameForWrite(filename, apdx + 1); + } + + @Override + public Release getCurrentVersion() { + return Release.CURRENT_RELEASE; + } + + + @Override + public Release autoDetectRelease() { + DatabaseVersion dbVersion; + try { + dbVersion = this.dbClient.readActualVersion(); + } catch (SQLException | ParseException e) { + LOG.error("unable to detect db version", e); + return null; + } + AliasesEntryList aliases = this.dbClient.readViews(); + IndicesEntryList indices = this.dbClient.readTables(); + if (indices == null) { + return null; + } + List foundReleases = new ArrayList<>(); + //if there are active aliases reduce indices to the active ones + if (aliases != null && aliases.size() > 0) { + indices = indices.subList(aliases.getLinkedIndices()); + } + for (Release r : Release.values()) { + if (r.isDbInRange(dbVersion, SdnrDbType.MARIADB)) { + ReleaseInformation ri = ReleaseInformation.getInstance(r); + if (ri != null && ri.containsIndices(indices)) { + foundReleases.add(r); + } + } + } + if (foundReleases.size() == 1) { + return foundReleases.get(0); + } + LOG.error("detect {} releases: {}. unable to detect for which one to do sth.", foundReleases.size(), + foundReleases); + return null; + } + + @Override + public boolean initDatabase(Release release, int numShards, int numReplicas, String dbPrefix, boolean forceRecreate, + long timeoutms) { + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + DatabaseVersion dbVersion; + try { + dbVersion = this.dbClient.readActualVersion(); + } catch (SQLException | ParseException e1) { + LOG.error("unable to detect db version", e1); + return false; + } + if (dbVersion == null) { + return false; + } + LOG.info("detected database version {}", dbVersion); + if (release == null) { + release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.MARIADB); + if (release == null) { + LOG.warn("unable to autodetect release for this database version for release {}", + ReleaseGroup.CURRENT_RELEASE.name()); + return false; + } + LOG.info("autodetect release {}", release); + } + if (!release.isDbInRange(dbVersion, SdnrDbType.MARIADB)) { + LOG.warn("db version {} maybe not compatible with release {}", dbVersion, release); + return false; + } + if (forceRecreate) { + this.clearDatabase(release, dbPrefix, 0); + } + ReleaseInformation ri = ReleaseInformation.getInstance(release); + AliasesEntryList views = this.dbClient.readViews(); + IndicesEntryList indices = this.dbClient.readTables(); + if (views == null || indices == null) { + return false; + } + boolean response = false; + if (!ri.runPreInitCommands(this.dbClient)) { + return false; + } + for (ComponentName component : ri.getComponents()) { + try { + if (ri.hasOwnDbIndex(component)) { + //check if index already exists + String tableName = ri.getIndex(component, dbPrefix); + String viewName = ri.getAlias(component, dbPrefix); + if (indices.findByIndex(tableName) == null) { + LOG.info("creating index for {}", component); + response = this.dbClient.createTable(ri.getIndex(component, dbPrefix), ri.getDatabaseMapping(component, DBTYPE)); + LOG.info(response ? "succeeded" : "failed"); + } else { + LOG.info("index {} for {} already exists", tableName, component); + } + //check if alias already exists + if (views.findByAlias(viewName) == null) { + LOG.info("creating alias for {}", component); + response = this.dbClient.createView(tableName, viewName); + LOG.info(response ? "succeeded" : "failed"); + } else { + LOG.info("view {} for table {} for {} already exists", viewName, tableName, component); + } + } + } catch (SQLException e) { + LOG.error(e.getMessage()); + return false; + } + } + if (!ri.runPostInitCommands(this.dbClient)) { + return false; + } + return true; + } + + @Override + public boolean clearDatabase(Release release, String dbPrefix, long timeoutms) { + + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + //check aliases + AliasesEntryList entries = this.dbClient.readViews(); + IndicesEntryList entries2 = this.dbClient.readTables(); + if (entries == null) { + return false; + } + if (release == null) { + DatabaseVersion dbVersion; + try { + dbVersion = this.dbClient.readActualVersion(); + } catch (SQLException | ParseException e) { + LOG.error("unable to detect db version", e); + return false; + } + LOG.info("detected database version {}", dbVersion); + release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.MARIADB); + if (release == null) { + LOG.warn("unable to autodetect release for this database version for release {}", + ReleaseGroup.CURRENT_RELEASE.name()); + return false; + } + LOG.info("autodetect release {}", release); + } + ReleaseInformation ri = ReleaseInformation.getInstance(release); + boolean response; + if (entries.size() <= 0) { + LOG.info("no aliases to clear"); + } else { + //check for every component of release if alias exists + for (ComponentName component : ri.getComponents()) { + String aliasToDelete = ri.getAlias(component, dbPrefix); + AliasesEntry entryToDelete = entries.findByAlias(aliasToDelete); + if (entryToDelete != null) { + try { + LOG.info("deleting alias {} for index {}", entryToDelete.getAlias(), entryToDelete.getIndex()); + response = this.dbClient.deleteView(entryToDelete.getAlias()); + LOG.info(response ? "succeeded" : "failed"); + } catch (SQLException e) { + LOG.error(e.getMessage()); + return false; + } + } else { + //try to find malformed typed index with alias name + IndicesEntry entry2ToDelete = entries2.findByIndex(aliasToDelete); + if (entry2ToDelete != null) { + try { + LOG.info("deleting index {}", entry2ToDelete.getName()); + response = this.dbClient.deleteTable(entry2ToDelete.getName()); + LOG.info(response ? "succeeded" : "failed"); + } catch (SQLException e) { + LOG.error(e.getMessage()); + return false; + } + } + } + } + } + if (entries2 == null) { + return false; + } + if (entries2.size() <= 0) { + LOG.info("no indices to clear"); + } else { + //check for every component of release if index exists + for (ComponentName component : ri.getComponents()) { + String indexToDelete = ri.getIndex(component, dbPrefix); + IndicesEntry entryToDelete = entries2.findByIndex(indexToDelete); + if (entryToDelete != null) { + try { + LOG.info("deleting index {}", entryToDelete.getName()); + response = this.dbClient.deleteTable(entryToDelete.getName()); + LOG.info(response ? "succeeded" : "failed"); + } catch (SQLException e) { + LOG.error(e.getMessage()); + return false; + } + } + } + } + + return true; + } + + /** + * @param timeoutms + * @return + */ + public boolean clearCompleteDatabase(long timeoutms) { + if (timeoutms > 0) { + this.dbClient.waitForYellowStatus(timeoutms); + } + //check aliases and indices + AliasesEntryList aliases = this.dbClient.readViews(); + IndicesEntryList indices = this.dbClient.readTables(); + if (aliases == null || indices == null) { + return false; + } + for (AliasesEntry alias : aliases) { + try { + LOG.info("deleting alias {} for index {}", alias.getAlias(), alias.getIndex()); + this.dbClient.deleteView(alias.getAlias()); + } catch (SQLException e) { + LOG.error("problem deleting alias {}: {}", alias.getAlias(), e); + return false; + } + } + for (IndicesEntry index : indices) { + try { + LOG.info("deleting index {}", index.getName()); + this.dbClient.deleteTable(index.getName()); + } catch (SQLException e) { + LOG.error("problem deleting index {}: {}", index.getName(), e); + return false; + } + } + return true; + } + +} diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/elalto/ElAltoReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/elalto/ElAltoReleaseInformation.java index 739afaf0e..4165080b5 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/elalto/ElAltoReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/elalto/ElAltoReleaseInformation.java @@ -23,13 +23,14 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto; import java.util.HashMap; import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; public class ElAltoReleaseInformation extends ReleaseInformation { @@ -96,13 +97,22 @@ public class ElAltoReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { + public boolean runPreInitCommands(HtDatabaseClient dbClient) { return true; } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(HtDatabaseClient dbClient) { return true; } + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + return false; + } + + @Override + public boolean runPostInitCommands(SqlDBClient dbClient) { + return false; + } } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformation.java index 7b8c40bca..83ad6c4e7 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformation.java @@ -24,16 +24,17 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; -import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; public class FrankfurtReleaseInformation extends ReleaseInformation { @@ -82,7 +83,7 @@ public class FrankfurtReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { + public boolean runPreInitCommands(HtDatabaseClient dbClient) { ClusterSettingsResponse response = null; try { @@ -94,8 +95,17 @@ public class FrankfurtReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(HtDatabaseClient dbClient) { return true; } + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + return false; + } + + @Override + public boolean runPostInitCommands(SqlDBClient dbClient) { + return false; + } } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformationR2.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformationR2.java index f972777b9..8882a8b68 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformationR2.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/frankfurt/FrankfurtReleaseInformationR2.java @@ -27,6 +27,7 @@ import java.util.Map; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; @@ -59,10 +60,10 @@ public class FrankfurtReleaseInformationR2 extends ReleaseInformation { "{\"date\": {\"type\": \"keyword\"},\"model-identifier\": {\"type\": \"keyword\"},\"manufacturer-identifier\": {\"type\": \"keyword\"},\"type-name\": {\"type\": \"keyword\"},\"description\": {\"type\": \"keyword\"},\"uuid\": {\"type\": \"keyword\"},\"version\": {\"type\": \"keyword\"},\"parent-uuid\": {\"type\": \"keyword\"},\"contained-holder\": {\"type\": \"keyword\"},\"node-id\": {\"type\": \"keyword\"},\"tree-level\": {\"type\": \"long\"},\"part-type-id\": {\"type\": \"keyword\"},\"serial\": {\"type\": \"keyword\"}}")); map.put(ComponentName.HISTORICAL_PERFORMANCE_15M, new DatabaseInfo7("historicalperformance15min", "historicalperformance15min", - "{\"node-name\":{\"type\": \"keyword\"},\"timestamp\":{\"type\": \"date\"},\"suspect-interval-flag\":{\"type\":\"boolean\"},\"scanner-id\":{\"type\": \"keyword\"},\"uuid-interface\":{\"type\": \"keyword\"},\"layer-protocol-name\":{\"type\": \"keyword\"},\"granularity-period\":{\"type\": \"keyword\"},\"radio-signal-id\":{\"type\": \"keyword\"}}")); + "{\"node-name\":{\"type\": \"keyword\"},\"time-stamp\":{\"type\": \"date\"},\"suspect-interval-flag\":{\"type\":\"boolean\"},\"scanner-id\":{\"type\": \"keyword\"},\"uuid-interface\":{\"type\": \"keyword\"},\"layer-protocol-name\":{\"type\": \"keyword\"},\"granularity-period\":{\"type\": \"keyword\"},\"radio-signal-id\":{\"type\": \"keyword\"}}")); map.put(ComponentName.HISTORICAL_PERFORMANCE_24H, new DatabaseInfo7("historicalperformance24h", "historicalperformance24h", - "{\"node-name\":{\"type\": \"keyword\"},\"timestamp\":{\"type\": \"date\"},\"suspect-interval-flag\":{\"type\":\"boolean\"},\"scanner-id\":{\"type\": \"keyword\"},\"uuid-interface\":{\"type\": \"keyword\"},\"layer-protocol-name\":{\"type\": \"keyword\"},\"granularity-period\":{\"type\": \"keyword\"},\"radio-signal-id\":{\"type\": \"keyword\"}}")); + "{\"node-name\":{\"type\": \"keyword\"},\"time-stamp\":{\"type\": \"date\"},\"suspect-interval-flag\":{\"type\":\"boolean\"},\"scanner-id\":{\"type\": \"keyword\"},\"uuid-interface\":{\"type\": \"keyword\"},\"layer-protocol-name\":{\"type\": \"keyword\"},\"granularity-period\":{\"type\": \"keyword\"},\"radio-signal-id\":{\"type\": \"keyword\"}}")); map.put(ComponentName.REQUIRED_NETWORKELEMENT, new DatabaseInfo7("networkelement-connection", "networkelement-connection", "{\"node-id\": {\"type\": \"keyword\"},\"host\": {\"type\": \"keyword\"},\"port\": {\"type\": \"long\"},\"username\": {\"type\": \"keyword\"},\"password\": {\"type\": \"keyword\"},\"core-model-capability\": {\"type\": \"keyword\"},\"device-type\": {\"type\": \"keyword\"},\"is-required\": {\"type\": \"boolean\"},\"status\": {\"type\": \"keyword\"}}")); @@ -83,7 +84,7 @@ public class FrankfurtReleaseInformationR2 extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { + public boolean runPreInitCommands(HtDatabaseClient dbClient) { ClusterSettingsResponse response = null; try { @@ -95,8 +96,18 @@ public class FrankfurtReleaseInformationR2 extends ReleaseInformation { } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(HtDatabaseClient dbClient) { return true; } + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + return false; + } + + @Override + public boolean runPostInitCommands(SqlDBClient dbClient) { + return false; + } + } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/guilin/GuilinReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/guilin/GuilinReleaseInformation.java index e057d82f2..5efaf9331 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/guilin/GuilinReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/guilin/GuilinReleaseInformation.java @@ -25,6 +25,7 @@ import java.io.IOException; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter; @@ -51,7 +52,7 @@ public class GuilinReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { + public boolean runPreInitCommands(HtDatabaseClient dbClient) { ClusterSettingsResponse response = null; try { response = dbClient.setupClusterSettings(new ClusterSettingsRequest(false).maxCompilationsPerMinute(400)); @@ -62,8 +63,18 @@ public class GuilinReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(HtDatabaseClient dbClient) { return true; } + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + return false; + } + + @Override + public boolean runPostInitCommands(SqlDBClient dbClient) { + return false; + } + } diff --git a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/honolulu/HonoluluReleaseInformation.java b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/honolulu/HonoluluReleaseInformation.java index 41f8175db..148c004a6 100644 --- a/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/honolulu/HonoluluReleaseInformation.java +++ b/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/honolulu/HonoluluReleaseInformation.java @@ -26,6 +26,7 @@ import java.util.Map; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; @@ -61,7 +62,7 @@ public class HonoluluReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { + public boolean runPreInitCommands(HtDatabaseClient dbClient) { ClusterSettingsResponse response = null; try { response = dbClient.setupClusterSettings(new ClusterSettingsRequest(false).maxCompilationsPerMinute(400)); @@ -72,8 +73,18 @@ public class HonoluluReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(HtDatabaseClient dbClient) { return true; } + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + return false; + } + + @Override + public boolean runPostInitCommands(SqlDBClient dbClient) { + return false; + } + } 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 67200bb2f..0dcd3ae3c 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 @@ -21,33 +21,154 @@ */ package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.istanbul; -import java.io.IOException; +import java.util.HashMap; import java.util.Map; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; -import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; -import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo7; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MariaDBTableInfo; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.honolulu.HonoluluReleaseInformation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity; public class IstanbulReleaseInformation extends ReleaseInformation { - private final Logger LOG = LoggerFactory.getLogger(IstanbulReleaseInformation.class); + private static final String TIMEZONE_TYPE_FORMAT = + "CHAR(6) DEFAULT NULL CHECK (`%s` regexp '^[+-]\\\\d\\\\d:\\\\d\\\\d$')"; + private static final String TABLENAME_CONTROLLER_FORMAT = "controller%s"; + private static final String TABLEMAPPING_CONTROLLER = + "`id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,`desc` VARCHAR(255) CHARACTER SET utf8 ,primary key(id)"; + private static final String TABLEMAPPING_CONNECTIONLOG_FORMAT = "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + "`timestamp` DATETIME(3) ,\n" + + "`status` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_EVENTLOG_FORMAT = "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`source-type` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`object-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`timestamp` DATETIME(3) ,\n" + "`timestamp-tz` " + String.format(TIMEZONE_TYPE_FORMAT, "timestamp-tz") + + " ,\n" + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`counter` INTEGER ,\n" + + "`attribute-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`new-value` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_FAULTCURRENT_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`object-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`timestamp` DATETIME(3) ,\n" + "`timestamp-tz` " + + String.format(TIMEZONE_TYPE_FORMAT, "timestamp-tz") + " ,\n" + + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`counter` INTEGER ,\n" + + "`severity` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`problem` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_FAULTLOG_FORMAT = "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`source-type` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`object-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`timestamp` DATETIME(3) ,\n" + "`timestamp-tz` " + String.format(TIMEZONE_TYPE_FORMAT, "timestamp-tz") + + " ,\n" + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`counter` INTEGER ,\n" + + "`severity` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`problem` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_GUICUTTHROUGH_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`name` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`weburi` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_HISTORICALPM15M_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + "`performance-data` JSON ,\n" + + "`granularity-period` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`time-stamp` DATETIME(3) ,\n" + + "`time-stamp-tz` " + String.format(TIMEZONE_TYPE_FORMAT, "time-stamp-tz") + " ,\n" + + "`node-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`uuid-interface` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`layer-protocol-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`radio-signal-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`suspect-interval-flag` BOOLEAN ,\n" + + "`scanner-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_HISTORICALPM24H_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + "`performance-data` JSON ,\n" + + "`granularity-period` VARCHAR(100) CHARACTER SET utf8 ,\n" + "`time-stamp` DATETIME(3) ,\n" + + "`time-stamp-tz` " + String.format(TIMEZONE_TYPE_FORMAT, "time-stamp-tz") + " ,\n" + + "`node-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`uuid-interface` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`layer-protocol-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`radio-signal-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`suspect-interval-flag` BOOLEAN ,\n" + + "`scanner-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_INVENTORY_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`version` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`type-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`date` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`description` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`uuid` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`part-type-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`model-identifier` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`manufacturer-identifier` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`tree-level` BIGINT ,\n" + + "`parent-uuid` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`contained-holder` JSON ,\n" + + "`serial` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`manufacturer-name` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_MAINTENANCE_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + "`active` BOOLEAN ,\n" + + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`description` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`problem` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`start` DATETIME(3) ,\n" + "`start-tz` " + + String.format(TIMEZONE_TYPE_FORMAT, "start-tz") + " ,\n" + "`end` DATETIME(3) ,\n" + "`end-tz` " + + String.format(TIMEZONE_TYPE_FORMAT, "end-tz") + " ,\n" + + "`object-id-ref` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_MEDIATORSERVER_FORMAT = + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`name` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`url` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_NETWORKELEMENT_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + + "`password` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`host` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`port` BIGINT ,\n" + "`status` VARCHAR(100) CHARACTER SET utf8 ,\n" + + "`node-id` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`username` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`is-required` BOOLEAN ,\n" + "`core-model-capability` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "`device-type` VARCHAR(100) CHARACTER SET utf8 ,\n" + + "`device-function` VARCHAR(512) CHARACTER SET utf8 ,\n" + "`node-details` JSON ,\n" + + "`tls-key` VARCHAR(255) CHARACTER SET utf8 ,\n" + "`mount-method` VARCHAR(255) CHARACTER SET utf8 ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + private static final String TABLEMAPPING_USERDATA_FORMAT = "`id` VARCHAR(255) CHARACTER SET utf8 NOT NULL,\n" + + "`controller-id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,\n" + "`value` JSON ,\n" + + "primary key(id),foreign key(`controller-id`) references `controller%s`(id)"; + + public IstanbulReleaseInformation() { - super(Release.ISTANBUL_R1,createDBMap()); + super(Release.ISTANBUL_R1, createDBMap(), createMariaDBMap(Release.ISTANBUL_R1.getDBSuffix())); + } + private static Map createMariaDBMap(String suffix) { + Map map = new HashMap<>(); + map.put(ComponentName.CONNECTIONLOG, + new MariaDBTableInfo(Entity.Connectionlog.getName(), TABLEMAPPING_CONNECTIONLOG_FORMAT)); + map.put(ComponentName.EVENTLOG, new MariaDBTableInfo(Entity.Eventlog.getName(), TABLEMAPPING_EVENTLOG_FORMAT)); + map.put(ComponentName.FAULTCURRENT, + new MariaDBTableInfo(Entity.Faultcurrent.getName(), TABLEMAPPING_FAULTCURRENT_FORMAT)); + map.put(ComponentName.FAULTLOG, new MariaDBTableInfo(Entity.Faultlog.getName(), TABLEMAPPING_FAULTLOG_FORMAT)); + map.put(ComponentName.GUICUTTHROUGH, + new MariaDBTableInfo(Entity.Guicutthrough.getName(), TABLEMAPPING_GUICUTTHROUGH_FORMAT)); + map.put(ComponentName.HISTORICAL_PERFORMANCE_15M, + new MariaDBTableInfo(Entity.Historicalperformance15min.getName(), TABLEMAPPING_HISTORICALPM15M_FORMAT)); + map.put(ComponentName.HISTORICAL_PERFORMANCE_24H, + new MariaDBTableInfo(Entity.Historicalperformance24h.getName(), TABLEMAPPING_HISTORICALPM24H_FORMAT)); + map.put(ComponentName.INVENTORY, + new MariaDBTableInfo(Entity.Inventoryequipment.getName(), TABLEMAPPING_INVENTORY_FORMAT)); + map.put(ComponentName.MAINTENANCE, + new MariaDBTableInfo(Entity.Maintenancemode.getName(), TABLEMAPPING_MAINTENANCE_FORMAT)); + map.put(ComponentName.MEDIATOR_SERVER, + new MariaDBTableInfo(Entity.MediatorServer.getName(), TABLEMAPPING_MEDIATORSERVER_FORMAT)); + map.put(ComponentName.REQUIRED_NETWORKELEMENT, + new MariaDBTableInfo(Entity.NetworkelementConnection.getName(), TABLEMAPPING_NETWORKELEMENT_FORMAT)); + map.put(ComponentName.USERDATA, new MariaDBTableInfo(Entity.Userdata.getName(), TABLEMAPPING_USERDATA_FORMAT)); + return map; } private static Map createDBMap() { - Map map= HonoluluReleaseInformation.createDBMap(); + Map map = HonoluluReleaseInformation.createDBMap(); map.put(ComponentName.USERDATA, new DatabaseInfo7("userdata", "userdata", "{}")); + map.put(ComponentName.REQUIRED_NETWORKELEMENT, new DatabaseInfo7("networkelement-connection", + "networkelement-connection", + "{\"node-id\": {\"type\": \"keyword\"},\"host\": {\"type\": \"keyword\"},\"port\": " + + "{\"type\": \"long\"},\"username\": {\"type\": \"keyword\"},\"password\": {\"type\": \"keyword\"}," + + "\"core-model-capability\": {\"type\": \"keyword\"},\"device-type\": {\"type\": \"keyword\"}," + + "\"device-function\": {\"type\": \"keyword\"},\"is-required\": {\"type\": \"boolean\"}," + + "\"status\": {\"type\": \"keyword\"},\"tls-key\": {\"type\": \"keyword\"}," + + "\"mount-method\": {\"type\":\"keyword\"}}")); return map; } @@ -60,18 +181,24 @@ public class IstanbulReleaseInformation extends ReleaseInformation { } @Override - protected boolean runPreInitCommands(HtDatabaseClient dbClient) { - ClusterSettingsResponse response = null; - try { - response = dbClient.setupClusterSettings(new ClusterSettingsRequest(false).maxCompilationsPerMinute(400)); - } catch (IOException e) { - LOG.warn("problem setting up cluster: {}", e); - } - return response == null ? false : response.isAcknowledged(); + public boolean runPreInitCommands(HtDatabaseClient dbClient) { + return true; + } + + @Override + public boolean runPostInitCommands(HtDatabaseClient dbClient) { + return true; + } + + @Override + public boolean runPreInitCommands(SqlDBClient dbClient) { + boolean success = dbClient.createTable( + String.format(TABLENAME_CONTROLLER_FORMAT, this.getReleas().getDBSuffix()), TABLEMAPPING_CONTROLLER); + return success; } @Override - protected boolean runPostInitCommands(HtDatabaseClient dbClient) { + public boolean runPostInitCommands(SqlDBClient dbClient) { return true; } diff --git a/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/MariaDBTestBase.java b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/MariaDBTestBase.java new file mode 100644 index 000000000..264f1203c --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/MariaDBTestBase.java @@ -0,0 +1,187 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; + +import static org.junit.Assert.fail; +import java.io.File; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section.EnvGetter; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper.UnableToMapClassException; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +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.MediatorServerEntity; +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.Pmdata15mEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata24hEntity; +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import ch.vorburger.mariadb4j.DBConfigurationBuilder; + +public class MariaDBTestBase { + + private final SqlDBDataProvider dbProvider; + private final DB db; + private static final Map envDefaultValues = initEnvDefaultValues(); + private static final String SDNRDBDATABASETEST="test"; + private static final String TESTPROPFILE = "test.properties"; + + public MariaDBTestBase() throws ManagedProcessException { + this(new Random().nextInt(1000) + 50000); + } + private static String dbUrl(String host, int port, String dbName) { + return String.format("jdbc:mysql://%s:%d/%s", host,port,dbName); + } + private static Map initEnvDefaultValues() { + Map defaults = new HashMap<>(); + defaults.put("SDNRDBURL", dbUrl("localhost",3306,SDNRDBDATABASETEST)); + defaults.put("SDNRDBDATABASE", "test"); + + return defaults; + } + public MariaDBTestBase(String host, int port) { + this(host, port, SDNRDBDATABASETEST ); + } + public MariaDBTestBase(String host, int port, String dbName) { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBURL", dbUrl(host,port,dbName)); + envDefaultValues.put("SDNRDBUSERNAME", "sdnrdb"); + envDefaultValues.put("SDNRDBPASSWORD", "sdnrdb"); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + SqlDBConfig config = new SqlDBConfig(new ConfigurationFileRepresentation(TESTPROPFILE)); + config.setControllerId("test123"); + this.db = null; + this.dbProvider = new SqlDBDataProvider(config, false); + //testCreateTable(this.dbProvider.getDBService()); + } + + public MariaDBTestBase(int port) throws ManagedProcessException { + EnvGetter env = Section.getEnvGetter(); + envDefaultValues.put("SDNRDBURL",dbUrl("localhost",port,SDNRDBDATABASETEST)); + Section.setEnvGetter((envname) -> { + return envDefaultValues.getOrDefault(envname, env.getenv(envname)); + }); + SqlDBConfig config = new SqlDBConfig(new ConfigurationFileRepresentation(TESTPROPFILE)); + //start db server + this.db = startDatabase(port); + //create db with name sdnrdb + this.dbProvider = new SqlDBDataProvider(config, false); + //testCreateTable(this.dbProvider.getDBService()); + } + + public void close() throws ManagedProcessException { + if (db != null) { + this.db.stop(); + } + File f = new File(TESTPROPFILE); + if(f.exists()) { + f.delete(); + } + } + + public SqlDBDataProvider getDbProvider() { + return dbProvider; + } + + public DB getDb() { + return db; + } + + private static DB startDatabase(int port) throws ManagedProcessException { + // Start MariaDB4j database + DBConfigurationBuilder dbconfig = DBConfigurationBuilder.newBuilder(); + dbconfig.setPort(port); // 0 => autom. detect free port + DB db = DB.newEmbeddedDB(dbconfig.build()); + db.start(); + return db; + } + public static void testCreateTable(SqlDBClient dbService) { + createTableOdl(dbService); + createTable(dbService, ConnectionlogEntity.class, Entity.Connectionlog, true); + createTable(dbService, EventlogEntity.class, Entity.Eventlog, true); + createTable(dbService, FaultcurrentEntity.class, Entity.Faultcurrent, false); + createTable(dbService, FaultlogEntity.class, Entity.Faultlog, true); + createTable(dbService, GuicutthroughEntity.class, Entity.Guicutthrough, true); + createTable(dbService, Pmdata15mEntity.class, Entity.Historicalperformance15min, false); + createTable(dbService, Pmdata24hEntity.class, Entity.Historicalperformance24h, false); + createTable(dbService, InventoryEntity.class, Entity.Inventoryequipment, false); + createTable(dbService, MaintenanceEntity.class, Entity.Maintenancemode, false); + createTable(dbService, MediatorServerEntity.class, Entity.MediatorServer, true); + createTable(dbService, NetworkElementConnectionEntity.class, Entity.NetworkelementConnection, false); + } + public static boolean createTableOdl(SqlDBClient dbService) { + String createStatement = null; + createStatement = SqlDBMapper.createTableOdl(); + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } + + public static boolean createTable(SqlDBClient dbService, Class cls, Entity entity, boolean autoIndex) { + String createStatement = null; + try { + createStatement = SqlDBMapper.createTable(cls, entity, "", autoIndex); + } catch (UnableToMapClassException e) { + fail(e.getMessage()); + } + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } + public String getDBUrl() { + return envDefaultValues.get("SDNRDBURL"); + } + public String getDBUsername() { + return envDefaultValues.getOrDefault("SDNRDBUSERNAME",""); + } + public String getDBPassword() { + return envDefaultValues.getOrDefault("SDNRDBPASSWORD",""); + } +} diff --git a/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBIstanbul.java b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBIstanbul.java new file mode 100644 index 000000000..072ce2f6b --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBIstanbul.java @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.sql.SQLException; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; +import ch.vorburger.exec.ManagedProcessException; + +public class TestMariaDBIstanbul { + + private static final String MARIADB_USERNAME = "sdnrdb"; + private static final String MARIADB_PASSWORD = "sdnrdb"; + // private static final String MARIADB_HOST = "10.20.11.159"; + private static final String MARIADB_HOST = "sdnrdb"; + private static final int MARIADB_PORT = 3306; + private static final String MARIADB_DATABASENAME = "sdnrdb"; + + private static SqlDBClient dbService; + + private static MariaDBTestBase testBase; + private static SqlDBDataProvider dbProvider; + + @BeforeClass + public static void init() throws Exception { + + //testBase = new MariaDBTestBase(MARIADB_HOST,MARIADB_PORT, MARIADB_DATABASENAME); + testBase = new MariaDBTestBase(); + dbProvider = testBase.getDbProvider(); + dbProvider.waitForDatabaseReady(30, TimeUnit.SECONDS); + // dbProvider.setControllerId(); + + } + + @AfterClass + public static void close() { + try { + testBase.close(); + } catch (ManagedProcessException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreate() { + DataMigrationProviderImpl provider = null; + try { + provider = new DataMigrationProviderImpl(SdnrDbType.MARIADB, testBase.getDBUrl(), testBase.getDBUsername(), + testBase.getDBPassword(), true, 30000); + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + boolean success = provider.initDatabase(Release.ISTANBUL_R1, 1, 1, "", false, 1000); + assertTrue("init database failed",success); + try { + dbProvider.setControllerId(); + } catch (SQLException e) { + fail(e.getMessage()); + } + + } +} diff --git a/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBMapper.java b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBMapper.java new file mode 100644 index 000000000..f39f9b088 --- /dev/null +++ b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMariaDBMapper.java @@ -0,0 +1,228 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup; + +import static org.junit.Assert.fail; +import com.fasterxml.jackson.core.JsonProcessingException; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.SqlDBDataProvider; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper.UnableToMapClassException; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.InsertQuery; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryEntity; +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.MediatorServerEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnection; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder; +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.NetworkElementDeviceType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata15mEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Pmdata24hEntity; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.network.element.connection.entity.NodeDetails; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.network.element.connection.entity.NodeDetailsBuilder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.common.Uint32; +import ch.vorburger.exec.ManagedProcessException; + +public class TestMariaDBMapper { + + + private static final String MARIADB_USERNAME = "sdnrdb"; + private static final String MARIADB_PASSWORD = "sdnrdb"; + // private static final String MARIADB_HOST = "10.20.11.159"; + private static final String MARIADB_HOST = "sdnrdb"; + private static final int MARIADB_PORT = 3306; + private static final String MARIADB_DATABASENAME = "sdnrdb"; + + // private static DbLibService dbService; + + private static MariaDBTestBase testBase; + private static SqlDBDataProvider dbProvider; + + @BeforeClass + public static void init() throws Exception { + + testBase = new MariaDBTestBase(); + dbProvider = testBase.getDbProvider(); + dbProvider.waitForDatabaseReady(30, TimeUnit.SECONDS); + // dbProvider.setControllerId(); + + } + + @AfterClass + public static void close() { + try { + testBase.close(); + } catch (ManagedProcessException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateSdnrDBTables() { + createTables(dbProvider.getDBService()); + } + + public static void createTables(SqlDBClient dbService) { + createTableOdl(dbService); + createTable(dbService, ConnectionlogEntity.class, Entity.Connectionlog, true); + createTable(dbService, EventlogEntity.class, Entity.Eventlog, true); + createTable(dbService, FaultcurrentEntity.class, Entity.Faultcurrent, false); + createTable(dbService, FaultlogEntity.class, Entity.Faultlog, true); + createTable(dbService, GuicutthroughEntity.class, Entity.Guicutthrough, true); + createTable(dbService, Pmdata15mEntity.class, Entity.Historicalperformance15min, false); + createTable(dbService, Pmdata24hEntity.class, Entity.Historicalperformance24h, false); + createTable(dbService, InventoryEntity.class, Entity.Inventoryequipment, false); + createTable(dbService, MaintenanceEntity.class, Entity.Maintenancemode, false); + createTable(dbService, MediatorServerEntity.class, Entity.MediatorServer, true); + createTable(dbService, NetworkElementConnectionEntity.class, Entity.NetworkelementConnection, false); + } + + //@Test + public void testInsert() { + NetworkElementConnectionBuilder builder = new NetworkElementConnectionBuilder(); + builder.setId("ROADM-A"); + builder.setNodeId("ROADM-A"); + builder.setCoreModelCapability("2017-03-12"); + builder.setDeviceType(NetworkElementDeviceType.OROADM); + builder.setHost("10.20.30.40"); + builder.setIsRequired(true); + NodeDetails nodeDetails = new NodeDetailsBuilder().setAvailableCapabilities(Arrays.asList( + "urn:ietf:params:netconf:capability:with-defaults:1.0?basic-mode=explicit&also-supported=report-all," + + "report-all-tagged,trim,explicit", + "urn:ietf:params:netconf:capability:validate:1.1", "urn:ietf:params:netconf:capability:url:1.0")) + .build(); + builder.setNodeDetails(nodeDetails); + builder.setPassword("password"); + builder.setPort(Uint32.valueOf(50000)); + builder.setStatus(ConnectionLogStatus.Connected); + builder.setUsername("admin"); + try { + writeEntry(builder.build(), Entity.NetworkelementConnection); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + //@Test + public void testRead() { + List con = + readEntry(Entity.NetworkelementConnection, NetworkElementConnection.class, "ROADM-A"); + System.out.println(con); + } + + private List readEntry(Entity entity, Class clazz, String id) { + final SelectQuery selectStatement = new SelectQuery(entity.getName()); + System.out.println(selectStatement); + try { + return SqlDBMapper.read(dbProvider.getDBService().read(selectStatement.toSql()), clazz); + + } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return null; + } + + private boolean writeEntry(T data, Entity entity) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, JsonProcessingException { + + final InsertQuery insertStatement = new InsertQuery(entity, data, dbProvider.getControllerId()); + System.out.println(insertStatement); + try { + return dbProvider.getDBService().write(insertStatement.toSql()); + + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + + return false; + } + + private static boolean createTableOdl(SqlDBClient dbService) { + String createStatement = null; + createStatement = SqlDBMapper.createTableOdl(); + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } + + private static boolean createTable(SqlDBClient dbService, Class cls, Entity entity, boolean autoIndex) { + String createStatement = null; + try { + createStatement = SqlDBMapper.createTable(cls, entity, "", autoIndex); + } catch (UnableToMapClassException e) { + fail(e.getMessage()); + } + System.out.println(createStatement); + try { + return dbService.write(createStatement); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + return false; + } + + private static Properties getConfig() { + Properties config = new Properties(); + config.setProperty("org.onap.ccsdk.sli.dbtype", "jdbc"); + config.setProperty("org.onap.ccsdk.sli.jdbc.hosts", MARIADB_HOST); + config.setProperty("org.onap.ccsdk.sli.jdbc.url", + String.format("jdbc:mysql://dbhost:%d/%s", MARIADB_PORT, MARIADB_DATABASENAME)); + config.setProperty("org.onap.ccsdk.sli.jdbc.driver", "org.mariadb.jdbc.Driver"); + config.setProperty("org.onap.ccsdk.sli.jdbc.database", MARIADB_DATABASENAME); + config.setProperty("org.onap.ccsdk.sli.jdbc.user", MARIADB_USERNAME); + config.setProperty("org.onap.ccsdk.sli.jdbc.password", MARIADB_PASSWORD); + return config; + } +} diff --git a/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMigrationProvider.java b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMigrationProvider.java index 311582a08..412c8b857 100644 --- a/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMigrationProvider.java +++ b/sdnr/wt/data-provider/setup/src/test/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/TestMigrationProvider.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport; import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release; @@ -41,8 +42,9 @@ public class TestMigrationProvider { @Test public void testCreateImport() throws Exception { - DataMigrationProviderImpl provider = new DataMigrationProviderImpl(hosts, null, null, true, 5000); - DataMigrationReport report=null; + DataMigrationProviderImpl provider = + new DataMigrationProviderImpl(SdnrDbType.ELASTICSEARCH, hosts[0].toUrl(), null, null, true, 5000); + DataMigrationReport report = null; try { diff --git a/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java b/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java index 0b69830c7..ed452cb7e 100644 --- a/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java +++ b/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java @@ -188,8 +188,6 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa LOG.info("Session Initiated start {}", APPLICATION_NAME); - this.iEntityDataProvider.setReadyStatus(false); - this.dataProvider = iEntityDataProvider.getDataProvider(); // Get configuration @@ -255,8 +253,6 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa this.devicemanagerInitializationOk = true; LOG.info("Session Initiated end. Initialization done {}", devicemanagerInitializationOk); - this.iEntityDataProvider.setReadyStatus(true); - } @Override diff --git a/sdnr/wt/devicemanager-core/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevicemanager.java b/sdnr/wt/devicemanager-core/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevicemanager.java index ed7f56f4b..06b825934 100644 --- a/sdnr/wt/devicemanager-core/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevicemanager.java +++ b/sdnr/wt/devicemanager-core/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevicemanager.java @@ -93,7 +93,6 @@ public class TestDevicemanager extends Mockito { WebsocketManagerService websocketmanagerService = mock(WebsocketManagerService.class); IEntityDataProvider iEntityDataProvider = mock(IEntityDataProvider.class); - doNothing().when(iEntityDataProvider).setReadyStatus(isA(Boolean.class)); DataProvider dataProvider = mock(DataProvider.class); when(iEntityDataProvider.getDataProvider()).thenReturn(dataProvider); -- cgit 1.2.3-korg