fd2ec179dc6cd686c73585f07c0898c2f24e6a36
[so.git] / adapters / mso-openstack-adapters / src / main / java / db / migration / R__CloudConfigMigration.java
1 package db.migration;
2
3 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4 import com.fasterxml.jackson.annotation.JsonProperty;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
7 import org.flywaydb.core.api.MigrationVersion;
8 import org.flywaydb.core.api.migration.MigrationChecksumProvider;
9 import org.flywaydb.core.api.migration.MigrationInfoProvider;
10 import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
11 import org.onap.so.db.catalog.beans.CloudIdentity;
12 import org.onap.so.db.catalog.beans.CloudSite;
13 import org.onap.so.db.catalog.beans.CloudifyManager;
14 import org.onap.so.logger.MsoLogger;
15
16 import java.sql.Connection;
17 import java.sql.PreparedStatement;
18 import java.sql.ResultSet;
19 import java.sql.Statement;
20 import java.util.Collection;
21
22 /**
23  * Performs migration using JDBC Connection from the cloud config provided in the environment (application-{profile}.yaml) and persist data (when not already present) to the catalod database.
24  */
25 @JsonIgnoreProperties(ignoreUnknown = true)
26 public class R__CloudConfigMigration implements JdbcMigration , MigrationInfoProvider, MigrationChecksumProvider {
27     public static final String FLYWAY = "FLYWAY";
28
29     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, R__CloudConfigMigration.class);
30     @JsonProperty("cloud_config")
31     private CloudConfig cloudConfig;
32
33     @Override
34     public void migrate(Connection connection) throws Exception {
35         LOGGER.debug("Starting migration for CloudConfig");
36         CloudConfig cloudConfig = loadCloudConfig();
37         if(cloudConfig == null){
38             LOGGER.debug("No CloudConfig defined in :"+getApplicationYamlName()+" exiting.");
39         }else{
40             migrateCloudIdentity(cloudConfig.getIdentityServices().values(), connection);
41             migrateCloudSite(cloudConfig.getCloudSites().values(), connection);
42             migrateCloudifyManagers(cloudConfig.getCloudifyManagers().values(), connection);
43         }
44     }
45
46     public CloudConfig getCloudConfig() {
47         return cloudConfig;
48     }
49
50     public void setCloudConfig(CloudConfig cloudConfig) {
51         this.cloudConfig = cloudConfig;
52     }
53
54     private CloudConfig loadCloudConfig() throws Exception {
55         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
56         R__CloudConfigMigration cloudConfigMigration = mapper.readValue(R__CloudConfigMigration.class
57                 .getResourceAsStream(getApplicationYamlName()), R__CloudConfigMigration.class);
58         CloudConfig cloudConfig = cloudConfigMigration.getCloudConfig();
59         if(cloudConfig != null){
60             cloudConfig.populateId();
61         }
62
63         return cloudConfig;
64     }
65
66     private String getApplicationYamlName() {
67         String profile = System.getProperty("spring.profiles.active") == null ? "" : "-" + System.getProperty("spring.profiles.active");
68         return "/application" + profile + ".yaml";
69     }
70
71     private void migrateCloudIdentity(Collection<CloudIdentity> entities, Connection connection) throws Exception {
72         LOGGER.debug("Starting migration for CloudConfig-->IdentityService");
73         String insert = "INSERT INTO `identity_services` (`ID`, `IDENTITY_URL`, `MSO_ID`, `MSO_PASS`, `ADMIN_TENANT`, `MEMBER_ROLE`, `TENANT_METADATA`, `IDENTITY_SERVER_TYPE`, `IDENTITY_AUTHENTICATION_TYPE`, `LAST_UPDATED_BY`) " +
74                 "VALUES (?,?,?,?,?,?,?,?,?,?);";
75         PreparedStatement ps = connection.prepareStatement(insert);
76         try (Statement stmt = connection.createStatement()) {
77             for (CloudIdentity cloudIdentity : entities) {
78                 try (ResultSet rows = stmt.executeQuery("Select count(1) from identity_services where id='" + cloudIdentity.getId() + "'")) {
79                     int count = 0;
80                     while (rows.next()) {
81                         count = rows.getInt(1);
82                     }
83                     if (count == 0) {
84                         ps.setString(1, cloudIdentity.getId());
85                         ps.setString(2, cloudIdentity.getIdentityUrl());
86                         ps.setString(3, cloudIdentity.getMsoId());
87                         ps.setString(4, cloudIdentity.getMsoPass());
88                         ps.setString(5, cloudIdentity.getAdminTenant());
89                         ps.setString(6, cloudIdentity.getMemberRole());
90                         ps.setBoolean(7, cloudIdentity.getTenantMetadata());
91                         ps.setString(8, cloudIdentity.getIdentityServerType() != null ? cloudIdentity.getIdentityServerType().name() : null);
92                         ps.setString(9, cloudIdentity.getIdentityAuthenticationType() != null ? cloudIdentity.getIdentityAuthenticationType().name() : null);
93                         ps.setString(10, FLYWAY);
94                         ps.executeUpdate();
95                     }
96                 }
97             }
98         }
99     }
100
101     private void migrateCloudSite(Collection<CloudSite> entities, Connection connection) throws Exception {
102         LOGGER.debug("Starting migration for CloudConfig-->CloudSite");
103         String insert = "INSERT INTO `cloud_sites` (`ID`, `REGION_ID`, `IDENTITY_SERVICE_ID`, `CLOUD_VERSION`, `CLLI`, `CLOUDIFY_ID`, `PLATFORM`, `ORCHESTRATOR`, `LAST_UPDATED_BY`) " +
104                 "VALUES (?,?,?,?,?,?,?,?,?);";
105         PreparedStatement ps = connection.prepareStatement(insert);
106         try (Statement stmt = connection.createStatement()) {
107             for (CloudSite cloudSite : entities) {
108                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloud_sites where id='" + cloudSite.getId() + "'")) {
109                     int count = 0;
110                     while (rows.next()) {
111                         count = rows.getInt(1);
112                     }
113                     if (count == 0) {
114                         ps.setString(1, cloudSite.getId());
115                         ps.setString(2, cloudSite.getRegionId());
116                         ps.setString(3, cloudSite.getIdentityServiceId());
117                         ps.setString(4, cloudSite.getCloudVersion());
118                         ps.setString(5, cloudSite.getClli());
119                         ps.setString(6, cloudSite.getCloudifyId());
120                         ps.setString(7, cloudSite.getPlatform());
121                         ps.setString(8, cloudSite.getOrchestrator());
122                         ps.setString(9, FLYWAY);
123                         ps.executeUpdate();
124                     }
125                 }
126             }
127         }
128     }
129
130     private void migrateCloudifyManagers(Collection<CloudifyManager> entities, Connection connection) throws Exception {
131         String insert = "INSERT INTO `cloudify_managers` (`ID`, `CLOUDIFY_URL`, `USERNAME`, `PASSWORD`, `VERSION`, `LAST_UPDATED_BY`)" +
132                 " VALUES (?,?,?,?,?,?);";
133         PreparedStatement ps = connection.prepareStatement(insert);
134         try (Statement stmt = connection.createStatement()) {
135             for (CloudifyManager cloudifyManager : entities) {
136                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloudify_managers where id='" + cloudifyManager.getId() + "'")) {
137                     int count = 0;
138                     while (rows.next()) {
139                         count = rows.getInt(1);
140                     }
141                     if (count == 0) {
142                         ps.setString(1, cloudifyManager.getId());
143                         ps.setString(2, cloudifyManager.getCloudifyUrl());
144                         ps.setString(3, cloudifyManager.getUsername());
145                         ps.setString(4, cloudifyManager.getPassword());
146                         ps.setString(5, cloudifyManager.getVersion());
147                         ps.setString(6, FLYWAY);
148                         ps.executeUpdate();
149                     }
150                 }
151             }
152         }
153     }
154
155     public MigrationVersion getVersion() {
156         return null;
157     }
158
159     public String getDescription() {
160         return "R_CloudConfigMigration";
161     }
162
163     public Integer getChecksum() {
164         return Math.toIntExact(System.currentTimeMillis() / 1000);
165     }
166 }
167