Add database migration configuration 79/138779/3
authoraravind.est <aravindhan.a@est.tech>
Fri, 16 Aug 2024 11:50:52 +0000 (12:50 +0100)
committeraravind.est <aravindhan.a@est.tech>
Mon, 19 Aug 2024 12:53:29 +0000 (13:53 +0100)
Database migration added.

Issue-ID: CCSDK-4033
Change-Id: Ib7dc38826a4547a62bcd4839bf212758955b3d38
Signed-off-by: aravind.est <aravindhan.a@est.tech>
a1-policy-management/config/application.yaml
a1-policy-management/pom.xml
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/BeanFactory.java
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/DatabaseIndependentBeanFactory.java [moved from a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/ExcludeDatabaseAutoConfiguration.java with 50% similarity]
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/database/DatabaseDependentBeanFactory.java [new file with mode: 0644]
a1-policy-management/src/main/resources/db/migration/V1__create_base_schema.sql [new file with mode: 0644]

index 3ef6802..9dcfc33 100644 (file)
@@ -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:
index 0bea5fd..1455bad 100644 (file)
             <artifactId>r2dbc-postgresql</artifactId>
             <scope>runtime</scope>
         </dependency>
+        <dependency>
+            <groupId>org.flywaydb</groupId>
+            <artifactId>flyway-core</artifactId>
+        </dependency>
         <dependency>
             <!-- May be possible to remove this later when ccsdk parent bom stabilizes -->
             <groupId>javax.servlet</groupId>
index 0f077a1..4d1fa33 100644 (file)
 
 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) {
  * ========================LICENSE_END===================================
  */
 
-package org.onap.ccsdk.oran.a1policymanagementservice.database;
+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)
-public class ExcludeDatabaseAutoConfiguration {
+@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 (file)
index 0000000..f463ecd
--- /dev/null
@@ -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/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 (file)
index 0000000..a6d4998
--- /dev/null
@@ -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