2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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;
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;
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.
53 @JsonIgnoreProperties(ignoreUnknown = true)
54 public class R__CloudConfigMigration implements JdbcMigration , MigrationInfoProvider, MigrationChecksumProvider {
55 public static final String FLYWAY = "FLYWAY";
57 private static final Logger logger = LoggerFactory.getLogger(R__CloudConfigMigration.class);
58 @JsonProperty("cloud_config")
59 private CloudConfig cloudConfig;
62 public boolean isUndo(){
67 public void migrate(Connection connection) throws Exception {
68 logger.debug("Starting migration for CloudConfig");
70 CloudConfig cloudConfig = null;
72 String tableQuery = "SELECT * FROM identity_services";
73 int totalRetries = 20;
74 boolean tableExists = false;
76 while(!tableExists && count != totalRetries) {
77 try(Statement stmt = connection.createStatement();) {
78 stmt.executeQuery(tableQuery);
80 } catch (SQLException e) {
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);
93 logger.warn("Error Loading override.yaml", e);
97 if (cloudConfig == null) {
98 logger.debug("No CloudConfig defined in {}", configLocation);
100 // Try the application.yaml file
101 try (InputStream stream = R__CloudConfigMigration.class.getResourceAsStream(getApplicationYamlName())) {
102 cloudConfig = loadCloudConfig(stream);
105 if (cloudConfig == null) {
106 logger.debug("No CloudConfig defined in {}", getApplicationYamlName());
110 if(cloudConfig != null){
111 migrateCloudIdentity(cloudConfig.getIdentityServices().values(), connection);
112 migrateCloudSite(cloudConfig.getCloudSites().values(), connection);
113 migrateCloudifyManagers(cloudConfig.getCloudifyManagers().values(), connection);
117 public CloudConfig getCloudConfig() {
121 public void setCloudConfig(CloudConfig cloudConfig) {
122 this.cloudConfig = cloudConfig;
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();
131 if(cloudConfig != null){
132 cloudConfig.populateId();
138 private String getApplicationYamlName() {
139 String profile = System.getProperty("spring.profiles.active") == null ? "" : "-" + System.getProperty("spring.profiles.active");
140 return "/application" + profile + ".yaml";
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 (?,?,?,?,?,?,?,?,?,?);";
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() + "'")) {
152 while (rows.next()) {
153 count = rows.getInt(1);
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);
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 (?,?,?,?,?,?,?,?,?);";
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() + "'")) {
182 while (rows.next()) {
183 count = rows.getInt(1);
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);
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 (?,?,?,?,?,?);";
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() + "'")) {
210 while (rows.next()) {
211 count = rows.getInt(1);
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);
227 public MigrationVersion getVersion() {
231 public String getDescription() {
232 return "R_CloudConfigMigration";
235 public Integer getChecksum() {
236 return Math.toIntExact(System.currentTimeMillis() / 1000);