2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
23 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
27 import org.flywaydb.core.api.MigrationVersion;
28 import org.flywaydb.core.api.migration.MigrationChecksumProvider;
29 import org.flywaydb.core.api.migration.MigrationInfoProvider;
30 import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
31 import org.onap.so.db.catalog.beans.CloudIdentity;
32 import org.onap.so.db.catalog.beans.CloudSite;
33 import org.onap.so.db.catalog.beans.CloudifyManager;
34 import org.onap.so.logger.MsoLogger;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.sql.Connection;
40 import java.sql.PreparedStatement;
41 import java.sql.ResultSet;
42 import java.sql.SQLException;
43 import java.sql.Statement;
44 import java.util.Collection;
47 * 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.
49 @JsonIgnoreProperties(ignoreUnknown = true)
50 public class R__CloudConfigMigration implements JdbcMigration , MigrationInfoProvider, MigrationChecksumProvider {
51 public static final String FLYWAY = "FLYWAY";
53 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, R__CloudConfigMigration.class);
54 @JsonProperty("cloud_config")
55 private CloudConfig cloudConfig;
58 public void migrate(Connection connection) throws Exception {
59 LOGGER.debug("Starting migration for CloudConfig");
61 CloudConfig cloudConfig = null;
63 // Try the override file
64 String configLocation = System.getProperty("spring.config.location");
65 if (configLocation != null) {
66 try (InputStream stream = new FileInputStream(configLocation)) {
67 cloudConfig = loadCloudConfig(stream);
71 if (cloudConfig == null) {
72 LOGGER.debug("No CloudConfig defined in " + configLocation);
74 // Try the application.yaml file
75 try (InputStream stream = R__CloudConfigMigration.class.getResourceAsStream(getApplicationYamlName())) {
76 cloudConfig = loadCloudConfig(stream);
79 if (cloudConfig == null) {
80 LOGGER.debug("No CloudConfig defined in " + getApplicationYamlName());
84 if(cloudConfig != null){
85 migrateCloudIdentity(cloudConfig.getIdentityServices().values(), connection);
86 migrateCloudSite(cloudConfig.getCloudSites().values(), connection);
87 migrateCloudifyManagers(cloudConfig.getCloudifyManagers().values(), connection);
91 public CloudConfig getCloudConfig() {
95 public void setCloudConfig(CloudConfig cloudConfig) {
96 this.cloudConfig = cloudConfig;
99 private CloudConfig loadCloudConfig(InputStream stream) throws IOException {
100 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
101 R__CloudConfigMigration cloudConfigMigration =
102 mapper.readValue(stream, R__CloudConfigMigration.class);
103 CloudConfig cloudConfig = cloudConfigMigration.getCloudConfig();
105 if(cloudConfig != null){
106 cloudConfig.populateId();
112 private String getApplicationYamlName() {
113 String profile = System.getProperty("spring.profiles.active") == null ? "" : "-" + System.getProperty("spring.profiles.active");
114 return "/application" + profile + ".yaml";
117 private void migrateCloudIdentity(Collection<CloudIdentity> entities, Connection connection) throws SQLException {
118 LOGGER.debug("Starting migration for CloudConfig-->IdentityService");
119 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`) " +
120 "VALUES (?,?,?,?,?,?,?,?,?,?);";
122 try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
123 for (CloudIdentity cloudIdentity : entities) {
124 try (ResultSet rows = stmt.executeQuery("Select count(1) from identity_services where id='" + cloudIdentity.getId() + "'")) {
126 while (rows.next()) {
127 count = rows.getInt(1);
130 ps.setString(1, cloudIdentity.getId());
131 ps.setString(2, cloudIdentity.getIdentityUrl());
132 ps.setString(3, cloudIdentity.getMsoId());
133 ps.setString(4, cloudIdentity.getMsoPass());
134 ps.setString(5, cloudIdentity.getAdminTenant());
135 ps.setString(6, cloudIdentity.getMemberRole());
136 ps.setBoolean(7, cloudIdentity.getTenantMetadata());
137 ps.setString(8, cloudIdentity.getIdentityServerType() != null ? cloudIdentity.getIdentityServerType().name() : null);
138 ps.setString(9, cloudIdentity.getIdentityAuthenticationType() != null ? cloudIdentity.getIdentityAuthenticationType().name() : null);
139 ps.setString(10, FLYWAY);
147 private void migrateCloudSite(Collection<CloudSite> entities, Connection connection) throws SQLException {
148 LOGGER.debug("Starting migration for CloudConfig-->CloudSite");
149 String insert = "INSERT INTO `cloud_sites` (`ID`, `REGION_ID`, `IDENTITY_SERVICE_ID`, `CLOUD_VERSION`, `CLLI`, `CLOUDIFY_ID`, `PLATFORM`, `ORCHESTRATOR`, `LAST_UPDATED_BY`) " +
150 "VALUES (?,?,?,?,?,?,?,?,?);";
152 try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
153 for (CloudSite cloudSite : entities) {
154 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloud_sites where id='" + cloudSite.getId() + "'")) {
156 while (rows.next()) {
157 count = rows.getInt(1);
160 ps.setString(1, cloudSite.getId());
161 ps.setString(2, cloudSite.getRegionId());
162 ps.setString(3, cloudSite.getIdentityServiceId());
163 ps.setString(4, cloudSite.getCloudVersion());
164 ps.setString(5, cloudSite.getClli());
165 ps.setString(6, cloudSite.getCloudifyId());
166 ps.setString(7, cloudSite.getPlatform());
167 ps.setString(8, cloudSite.getOrchestrator());
168 ps.setString(9, FLYWAY);
176 private void migrateCloudifyManagers(Collection<CloudifyManager> entities, Connection connection) throws SQLException {
177 String insert = "INSERT INTO `cloudify_managers` (`ID`, `CLOUDIFY_URL`, `USERNAME`, `PASSWORD`, `VERSION`, `LAST_UPDATED_BY`)" +
178 " VALUES (?,?,?,?,?,?);";
180 try (Statement stmt = connection.createStatement();PreparedStatement ps = connection.prepareStatement(insert)) {
181 for (CloudifyManager cloudifyManager : entities) {
182 try (ResultSet rows = stmt.executeQuery("Select count(1) from cloudify_managers where id='" + cloudifyManager.getId() + "'")) {
184 while (rows.next()) {
185 count = rows.getInt(1);
188 ps.setString(1, cloudifyManager.getId());
189 ps.setString(2, cloudifyManager.getCloudifyUrl());
190 ps.setString(3, cloudifyManager.getUsername());
191 ps.setString(4, cloudifyManager.getPassword());
192 ps.setString(5, cloudifyManager.getVersion());
193 ps.setString(6, FLYWAY);
201 public MigrationVersion getVersion() {
205 public String getDescription() {
206 return "R_CloudConfigMigration";
209 public Integer getChecksum() {
210 return Math.toIntExact(System.currentTimeMillis() / 1000);