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