From fdfb070d8a5139963fa02e59814855b8b19576a3 Mon Sep 17 00:00:00 2001 From: "aravind.est" Date: Fri, 16 Aug 2024 12:50:52 +0100 Subject: Add database migration configuration Database migration added. Issue-ID: CCSDK-4033 Change-Id: Ib7dc38826a4547a62bcd4839bf212758955b3d38 Signed-off-by: aravind.est --- a1-policy-management/config/application.yaml | 16 ++++++ a1-policy-management/pom.xml | 4 ++ .../a1policymanagementservice/BeanFactory.java | 27 ---------- .../DatabaseIndependentBeanFactory.java | 57 ++++++++++++++++++++++ .../database/DatabaseDependentBeanFactory.java | 57 ++++++++++++++++++++++ .../database/ExcludeDatabaseAutoConfiguration.java | 32 ------------ .../db/migration/V1__create_base_schema.sql | 35 +++++++++++++ 7 files changed, 169 insertions(+), 59 deletions(-) create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/DatabaseIndependentBeanFactory.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/DatabaseDependentBeanFactory.java delete mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/ExcludeDatabaseAutoConfiguration.java create mode 100644 a1-policy-management/src/main/resources/db/migration/V1__create_base_schema.sql (limited to 'a1-policy-management') diff --git a/a1-policy-management/config/application.yaml b/a1-policy-management/config/application.yaml index 3ef68023..9dcfc338 100644 --- a/a1-policy-management/config/application.yaml +++ b/a1-policy-management/config/application.yaml @@ -3,6 +3,7 @@ # ONAP : ccsdk oran # ================================================================================ # Copyright (C) 2020-2023 Nordix Foundation. All rights reserved. +# Copyright (C) 2024 OpenInfra Foundation Europe. 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. @@ -29,9 +30,21 @@ spring: aop: auto: false r2dbc: + # Configuration of the postgres database to be used by the application. + # These values can be passed via configmap/secret/env variable based on the installation. url: "r2dbc:postgresql://127.0.0.1:5432/a1pms" username: a1pms password: mypwd + flyway: + # Configuration of the postgres database to be used for database migration. + # This is where the flyway maintains the information about the sql files loaded. + # These values can be passed via configmap/secret/env variable based on the installation. + # By default, Flyway uses location classpath:db/migration to load the sql files. + # This can be overridden using "flyway.locations" to have a different location. + url: "jdbc:postgresql://127.0.0.1:5432/a1pms" + user: a1pms + password: mypwd + baseline-on-migrate: true management: tracing: propagation: @@ -111,6 +124,9 @@ app: accessKeyId: minio secretAccessKey: miniostorage bucket: + # Postgres database usage is enabled using the below parameter. + # If this is enabled, the application will use postgres database for storage. + # This overrides the s3(s3.bucket) or file store(vardata-directory) configuration if enabled. database-enabled: false otel: sdk: diff --git a/a1-policy-management/pom.xml b/a1-policy-management/pom.xml index 0bea5fd5..1455bad1 100644 --- a/a1-policy-management/pom.xml +++ b/a1-policy-management/pom.xml @@ -107,6 +107,10 @@ r2dbc-postgresql runtime + + org.flywaydb + flyway-core + javax.servlet diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/BeanFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/BeanFactory.java index 0f077a13..4d1fa331 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/BeanFactory.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/BeanFactory.java @@ -21,15 +21,11 @@ package org.onap.ccsdk.oran.a1policymanagementservice; - import org.apache.catalina.connector.Connector; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; @@ -37,7 +33,6 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; @Configuration public class BeanFactory { @@ -55,28 +50,6 @@ public class BeanFactory { return new Rics(); } - @Bean - @DependsOn("springContextProvider") - public Services getServices(@Autowired ApplicationConfig applicationConfig) { - Services services = new Services(applicationConfig); - services.restoreFromDatabase().subscribe(); - return services; - } - - @Bean - @DependsOn("springContextProvider") - public PolicyTypes getPolicyTypes(@Autowired ApplicationConfig applicationConfig) { - PolicyTypes types = new PolicyTypes(applicationConfig); - types.restoreFromDatabase().blockLast(); - return types; - } - - @Bean - @DependsOn("springContextProvider") - public Policies getPolicies(@Autowired ApplicationConfig applicationConfig) { - return new Policies(applicationConfig); - } - @Bean public A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig applicationConfig, @Autowired SecurityContext securityContext) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/DatabaseIndependentBeanFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/DatabaseIndependentBeanFactory.java new file mode 100644 index 00000000..305499d0 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/DatabaseIndependentBeanFactory.java @@ -0,0 +1,57 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 OpenInfra Foundation Europe. 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.oran.a1policymanagementservice; + +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; +import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(prefix = "app", name = "database-enabled", havingValue = "false") +@EnableAutoConfiguration(exclude = { R2dbcAutoConfiguration.class, FlywayAutoConfiguration.class }) +public class DatabaseIndependentBeanFactory { + @Bean + public Services getServices(@Autowired ApplicationConfig applicationConfig) { + Services services = new Services(applicationConfig); + services.restoreFromDatabase().subscribe(); + return services; + } + + @Bean + public PolicyTypes getPolicyTypes(@Autowired ApplicationConfig applicationConfig) { + PolicyTypes types = new PolicyTypes(applicationConfig); + types.restoreFromDatabase().blockLast(); + return types; + } + + @Bean + public Policies getPolicies(@Autowired ApplicationConfig applicationConfig) { + return new Policies(applicationConfig); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/DatabaseDependentBeanFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/DatabaseDependentBeanFactory.java new file mode 100644 index 00000000..f463ecd0 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/DatabaseDependentBeanFactory.java @@ -0,0 +1,57 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 OpenInfra Foundation Europe. 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.oran.a1policymanagementservice.database; + +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; + +@Configuration +@ConditionalOnProperty(prefix = "app", name = "database-enabled", havingValue = "true") +public class DatabaseDependentBeanFactory { + @Bean + @DependsOn({ "springContextProvider", "flywayInitializer" }) + public Services getServices(@Autowired ApplicationConfig applicationConfig) { + Services services = new Services(applicationConfig); + services.restoreFromDatabase().subscribe(); + return services; + } + + @Bean + @DependsOn({ "springContextProvider", "flywayInitializer" }) + public PolicyTypes getPolicyTypes(@Autowired ApplicationConfig applicationConfig) { + PolicyTypes types = new PolicyTypes(applicationConfig); + types.restoreFromDatabase().blockLast(); + return types; + } + + @Bean + @DependsOn({ "springContextProvider", "flywayInitializer" }) + public Policies getPolicies(@Autowired ApplicationConfig applicationConfig) { + return new Policies(applicationConfig); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/ExcludeDatabaseAutoConfiguration.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/ExcludeDatabaseAutoConfiguration.java deleted file mode 100644 index 799a8cb2..00000000 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/ExcludeDatabaseAutoConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. 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.oran.a1policymanagementservice.database; - -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ConditionalOnProperty(prefix = "app", name = "database-enabled", havingValue = "false") -@EnableAutoConfiguration(exclude = R2dbcAutoConfiguration.class) -public class ExcludeDatabaseAutoConfiguration { -} diff --git a/a1-policy-management/src/main/resources/db/migration/V1__create_base_schema.sql b/a1-policy-management/src/main/resources/db/migration/V1__create_base_schema.sql new file mode 100644 index 00000000..a6d49989 --- /dev/null +++ b/a1-policy-management/src/main/resources/db/migration/V1__create_base_schema.sql @@ -0,0 +1,35 @@ +-- ============LICENSE_START======================================================= +-- Copyright (C) 2024 OpenInfra Foundation Europe +-- ================================================================================ +-- 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. +-- +-- SPDX-License-Identifier: Apache-2.0 +-- ============LICENSE_END========================================================= + +CREATE TABLE IF NOT EXISTS policies ( + id varchar NOT NULL, + payload varchar NOT NULL, + CONSTRAINT policies_pk PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS policy_types ( + id varchar NOT NULL, + payload varchar NOT NULL, + CONSTRAINT policy_types_pk PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS services ( + id varchar NOT NULL, + payload varchar NOT NULL, + CONSTRAINT services_pk PRIMARY KEY (id) +); \ No newline at end of file -- cgit 1.2.3-korg