ee89edbda86923132e9964c659b837b7424bc1dc
[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.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.sql.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.Collection;
25
26 /**
27  * 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.
28  */
29 @JsonIgnoreProperties(ignoreUnknown = true)
30 public class R__CloudConfigMigration implements JdbcMigration , MigrationInfoProvider, MigrationChecksumProvider {
31     public static final String FLYWAY = "FLYWAY";
32
33     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, R__CloudConfigMigration.class);
34     @JsonProperty("cloud_config")
35     private CloudConfig cloudConfig;
36
37     @Override
38     public void migrate(Connection connection) throws Exception {
39         LOGGER.debug("Starting migration for CloudConfig");
40         
41         CloudConfig cloudConfig = null;
42
43         // Try the override file
44         String configLocation = System.getProperty("spring.config.location");
45         if (configLocation != null) {
46             try (InputStream stream = new FileInputStream(configLocation)) {
47                 cloudConfig = loadCloudConfig(stream);
48             }
49         }
50         
51         if (cloudConfig == null) {
52                 LOGGER.debug("No CloudConfig defined in " + configLocation);
53
54                 // Try the application.yaml file
55             try (InputStream stream = R__CloudConfigMigration.class.getResourceAsStream(getApplicationYamlName())) {
56                 cloudConfig = loadCloudConfig(stream);
57             }
58
59             if (cloudConfig == null) {
60                 LOGGER.debug("No CloudConfig defined in " + getApplicationYamlName());
61             }
62         }
63  
64         if(cloudConfig != null){
65             migrateCloudIdentity(cloudConfig.getIdentityServices().values(), connection);
66             migrateCloudSite(cloudConfig.getCloudSites().values(), connection);
67             migrateCloudifyManagers(cloudConfig.getCloudifyManagers().values(), connection);
68         }
69     }
70
71     public CloudConfig getCloudConfig() {
72         return cloudConfig;
73     }
74
75     public void setCloudConfig(CloudConfig cloudConfig) {
76         this.cloudConfig = cloudConfig;
77     }
78
79     private CloudConfig loadCloudConfig(InputStream stream) throws IOException  {
80         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
81         R__CloudConfigMigration cloudConfigMigration =
82                         mapper.readValue(stream, R__CloudConfigMigration.class);
83         CloudConfig cloudConfig = cloudConfigMigration.getCloudConfig();
84
85         if(cloudConfig != null){
86                 cloudConfig.populateId();
87         }
88
89         return cloudConfig;
90     }
91
92     private String getApplicationYamlName() {
93         String profile = System.getProperty("spring.profiles.active") == null ? "" : "-" + System.getProperty("spring.profiles.active");
94         return "/application" + profile + ".yaml";
95     }
96
97     private void migrateCloudIdentity(Collection<CloudIdentity> entities, Connection connection) throws SQLException  {
98         LOGGER.debug("Starting migration for CloudConfig-->IdentityService");
99         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`) " +
100                 "VALUES (?,?,?,?,?,?,?,?,?,?);";
101
102         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
103             for (CloudIdentity cloudIdentity : entities) {
104                 try (ResultSet rows = stmt.executeQuery("Select count(1) from identity_services where id='" + cloudIdentity.getId() + "'")) {
105                     int count = 0;
106                     while (rows.next()) {
107                         count = rows.getInt(1);
108                     }
109                     if (count == 0) {
110                         ps.setString(1, cloudIdentity.getId());
111                         ps.setString(2, cloudIdentity.getIdentityUrl());
112                         ps.setString(3, cloudIdentity.getMsoId());
113                         ps.setString(4, cloudIdentity.getMsoPass());
114                         ps.setString(5, cloudIdentity.getAdminTenant());
115                         ps.setString(6, cloudIdentity.getMemberRole());
116                         ps.setBoolean(7, cloudIdentity.getTenantMetadata());
117                         ps.setString(8, cloudIdentity.getIdentityServerType() != null ? cloudIdentity.getIdentityServerType().name() : null);
118                         ps.setString(9, cloudIdentity.getIdentityAuthenticationType() != null ? cloudIdentity.getIdentityAuthenticationType().name() : null);
119                         ps.setString(10, FLYWAY);
120                         ps.executeUpdate();
121                     }
122                 }
123             }
124         }
125     }
126
127     private void migrateCloudSite(Collection<CloudSite> entities, Connection connection) throws SQLException  {
128         LOGGER.debug("Starting migration for CloudConfig-->CloudSite");
129         String insert = "INSERT INTO `cloud_sites` (`ID`, `REGION_ID`, `IDENTITY_SERVICE_ID`, `CLOUD_VERSION`, `CLLI`, `CLOUDIFY_ID`, `PLATFORM`, `ORCHESTRATOR`, `LAST_UPDATED_BY`) " +
130                 "VALUES (?,?,?,?,?,?,?,?,?);";
131
132         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
133             for (CloudSite cloudSite : entities) {
134                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloud_sites where id='" + cloudSite.getId() + "'")) {
135                     int count = 0;
136                     while (rows.next()) {
137                         count = rows.getInt(1);
138                     }
139                     if (count == 0) {
140                         ps.setString(1, cloudSite.getId());
141                         ps.setString(2, cloudSite.getRegionId());
142                         ps.setString(3, cloudSite.getIdentityServiceId());
143                         ps.setString(4, cloudSite.getCloudVersion());
144                         ps.setString(5, cloudSite.getClli());
145                         ps.setString(6, cloudSite.getCloudifyId());
146                         ps.setString(7, cloudSite.getPlatform());
147                         ps.setString(8, cloudSite.getOrchestrator());
148                         ps.setString(9, FLYWAY);
149                         ps.executeUpdate();
150                     }
151                 }
152             }
153         }
154     }
155
156     private void migrateCloudifyManagers(Collection<CloudifyManager> entities, Connection connection) throws SQLException  {
157         String insert = "INSERT INTO `cloudify_managers` (`ID`, `CLOUDIFY_URL`, `USERNAME`, `PASSWORD`, `VERSION`, `LAST_UPDATED_BY`)" +
158                 " VALUES (?,?,?,?,?,?);";
159
160         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
161             for (CloudifyManager cloudifyManager : entities) {
162                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloudify_managers where id='" + cloudifyManager.getId() + "'")) {
163                     int count = 0;
164                     while (rows.next()) {
165                         count = rows.getInt(1);
166                     }
167                     if (count == 0) {
168                         ps.setString(1, cloudifyManager.getId());
169                         ps.setString(2, cloudifyManager.getCloudifyUrl());
170                         ps.setString(3, cloudifyManager.getUsername());
171                         ps.setString(4, cloudifyManager.getPassword());
172                         ps.setString(5, cloudifyManager.getVersion());
173                         ps.setString(6, FLYWAY);
174                         ps.executeUpdate();
175                     }
176                 }
177             }
178         }
179     }
180
181     public MigrationVersion getVersion() {
182         return null;
183     }
184
185     public String getDescription() {
186         return "R_CloudConfigMigration";
187     }
188
189     public Integer getChecksum() {
190         return Math.toIntExact(System.currentTimeMillis() / 1000);
191     }
192 }
193