c20acaf967f5176010a602ace2729bbaf73b5d4b
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / db / migration / R__CloudConfigMigration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package db.migration;
24
25 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
29 import org.flywaydb.core.api.MigrationVersion;
30 import org.flywaydb.core.api.migration.MigrationChecksumProvider;
31 import org.flywaydb.core.api.migration.MigrationInfoProvider;
32 import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
33 import org.onap.so.db.catalog.beans.CloudIdentity;
34 import org.onap.so.db.catalog.beans.CloudSite;
35 import org.onap.so.db.catalog.beans.CloudifyManager;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import java.io.FileInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.nio.file.Paths;
43 import java.sql.Connection;
44 import java.sql.PreparedStatement;
45 import java.sql.ResultSet;
46 import java.sql.SQLException;
47 import java.sql.Statement;
48 import java.util.Collection;
49
50 /**
51  * 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.
52  */
53 @JsonIgnoreProperties(ignoreUnknown = true)
54 public class R__CloudConfigMigration implements JdbcMigration , MigrationInfoProvider, MigrationChecksumProvider {
55     public static final String FLYWAY = "FLYWAY";
56
57     private static final Logger logger = LoggerFactory.getLogger(R__CloudConfigMigration.class);
58     @JsonProperty("cloud_config")
59     private CloudConfig cloudConfig;
60     
61     @Override
62     public boolean isUndo(){
63         return false;
64     }
65
66     @Override
67     public void migrate(Connection connection) throws Exception {
68         logger.debug("Starting migration for CloudConfig");
69         
70         CloudConfig cloudConfig = null;
71         
72         String tableQuery = "SELECT * FROM identity_services";
73         int totalRetries = 20;
74         boolean tableExists = false;
75         int count = 1;
76         while(!tableExists && count != totalRetries) {
77                 try(Statement stmt = connection.createStatement();) {
78                 stmt.executeQuery(tableQuery);
79                 tableExists = true;
80             } catch (SQLException e) {
81                 count++;
82                 // Wait 5 mintues
83                 Thread.sleep(300000);
84             }
85         }
86         
87         // Try the override file
88         String configLocation = System.getProperty("spring.config.additional-location");
89         if (configLocation != null) {
90             try (InputStream stream = new FileInputStream(Paths.get(configLocation).normalize().toString())) {
91                 cloudConfig = loadCloudConfig(stream);
92             }catch(Exception e){
93                 logger.warn("Error Loading override.yaml", e);
94             } 
95         }
96         
97         if (cloudConfig == null) {
98             logger.debug("No CloudConfig defined in {}", configLocation);
99
100                 // Try the application.yaml file
101             try (InputStream stream = R__CloudConfigMigration.class.getResourceAsStream(getApplicationYamlName())) {
102                 cloudConfig = loadCloudConfig(stream);
103             }
104
105             if (cloudConfig == null) {
106                 logger.debug("No CloudConfig defined in {}", getApplicationYamlName());
107             }
108         }
109  
110         if(cloudConfig != null){
111             migrateCloudIdentity(cloudConfig.getIdentityServices().values(), connection);
112             migrateCloudSite(cloudConfig.getCloudSites().values(), connection);
113             migrateCloudifyManagers(cloudConfig.getCloudifyManagers().values(), connection);
114         }
115     }
116
117     public CloudConfig getCloudConfig() {
118         return cloudConfig;
119     }
120
121     public void setCloudConfig(CloudConfig cloudConfig) {
122         this.cloudConfig = cloudConfig;
123     }
124
125     private CloudConfig loadCloudConfig(InputStream stream) throws IOException  {
126         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
127         R__CloudConfigMigration cloudConfigMigration =
128                         mapper.readValue(stream, R__CloudConfigMigration.class);
129         CloudConfig cloudConfig = cloudConfigMigration.getCloudConfig();
130
131         if(cloudConfig != null){
132                 cloudConfig.populateId();
133         }
134
135         return cloudConfig;
136     }
137
138     private String getApplicationYamlName() {
139         String profile = System.getProperty("spring.profiles.active") == null ? "" : "-" + System.getProperty("spring.profiles.active");
140         return "/application" + profile + ".yaml";
141     }
142
143     private void migrateCloudIdentity(Collection<CloudIdentity> entities, Connection connection) throws SQLException  {
144         logger.debug("Starting migration for CloudConfig-->IdentityService");
145         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`) " +
146                 "VALUES (?,?,?,?,?,?,?,?,?,?);";
147
148         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
149             for (CloudIdentity cloudIdentity : entities) {
150                 try (ResultSet rows = stmt.executeQuery("Select count(1) from identity_services where id='" + cloudIdentity.getId() + "'")) {
151                     int count = 0;
152                     while (rows.next()) {
153                         count = rows.getInt(1);
154                     }
155                     if (count == 0) {
156                         ps.setString(1, cloudIdentity.getId());
157                         ps.setString(2, cloudIdentity.getIdentityUrl());
158                         ps.setString(3, cloudIdentity.getMsoId());
159                         ps.setString(4, cloudIdentity.getMsoPass());
160                         ps.setString(5, cloudIdentity.getAdminTenant());
161                         ps.setString(6, cloudIdentity.getMemberRole());
162                         ps.setBoolean(7, cloudIdentity.getTenantMetadata());
163                         ps.setString(8, cloudIdentity.getIdentityServerType() != null ? cloudIdentity.getIdentityServerType().name() : null);
164                         ps.setString(9, cloudIdentity.getIdentityAuthenticationType() != null ? cloudIdentity.getIdentityAuthenticationType().name() : null);
165                         ps.setString(10, FLYWAY);
166                         ps.executeUpdate();
167                     }
168                 }
169             }
170         }
171     }
172
173     private void migrateCloudSite(Collection<CloudSite> entities, Connection connection) throws SQLException  {
174         logger.debug("Starting migration for CloudConfig-->CloudSite");
175         String insert = "INSERT INTO `cloud_sites` (`ID`, `REGION_ID`, `IDENTITY_SERVICE_ID`, `CLOUD_VERSION`, `CLLI`, `CLOUDIFY_ID`, `PLATFORM`, `ORCHESTRATOR`, `LAST_UPDATED_BY`) " +
176                 "VALUES (?,?,?,?,?,?,?,?,?);";
177
178         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
179             for (CloudSite cloudSite : entities) {
180                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloud_sites where id='" + cloudSite.getId() + "'")) {
181                     int count = 0;
182                     while (rows.next()) {
183                         count = rows.getInt(1);
184                     }
185                     if (count == 0) {
186                         ps.setString(1, cloudSite.getId());
187                         ps.setString(2, cloudSite.getRegionId());
188                         ps.setString(3, cloudSite.getIdentityServiceId());
189                         ps.setString(4, cloudSite.getCloudVersion());
190                         ps.setString(5, cloudSite.getClli());
191                         ps.setString(6, cloudSite.getCloudifyId());
192                         ps.setString(7, cloudSite.getPlatform());
193                         ps.setString(8, cloudSite.getOrchestrator());
194                         ps.setString(9, FLYWAY);
195                         ps.executeUpdate();
196                     }
197                 }
198             }
199         }
200     }
201
202     private void migrateCloudifyManagers(Collection<CloudifyManager> entities, Connection connection) throws SQLException  {
203         String insert = "INSERT INTO `cloudify_managers` (`ID`, `CLOUDIFY_URL`, `USERNAME`, `PASSWORD`, `VERSION`, `LAST_UPDATED_BY`)" +
204                 " VALUES (?,?,?,?,?,?);";
205
206         try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
207             for (CloudifyManager cloudifyManager : entities) {
208                 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloudify_managers where id='" + cloudifyManager.getId() + "'")) {
209                     int count = 0;
210                     while (rows.next()) {
211                         count = rows.getInt(1);
212                     }
213                     if (count == 0) {
214                         ps.setString(1, cloudifyManager.getId());
215                         ps.setString(2, cloudifyManager.getCloudifyUrl());
216                         ps.setString(3, cloudifyManager.getUsername());
217                         ps.setString(4, cloudifyManager.getPassword());
218                         ps.setString(5, cloudifyManager.getVersion());
219                         ps.setString(6, FLYWAY);
220                         ps.executeUpdate();
221                     }
222                 }
223             }
224         }
225     }
226
227     public MigrationVersion getVersion() {
228         return null;
229     }
230
231     public String getDescription() {
232         return "R_CloudConfigMigration";
233     }
234
235     public Integer getChecksum() {
236         return Math.toIntExact(System.currentTimeMillis() / 1000);
237     }
238 }
239