2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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=========================================================
21 package org.openecomp.sdc.asdctool.migration.resolver;
24 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
25 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
26 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
27 import org.openecomp.sdc.asdctool.migration.core.task.PostMigration;
28 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
30 import java.util.ArrayList;
31 import java.util.Comparator;
32 import java.util.List;
33 import java.util.stream.Collectors;
35 public class SpringBeansMigrationResolver implements MigrationResolver {
37 private List<Migration> migrations = new ArrayList<>();
38 private List<PostMigration> postMigrations = new ArrayList<>();
40 private SdcRepoService sdcRepoService;
42 public SpringBeansMigrationResolver(List<Migration> migrations, List<PostMigration> postMigrations, SdcRepoService sdcRepoService) {
43 this.migrations = migrations;
44 this.postMigrations = postMigrations;
45 this.sdcRepoService = sdcRepoService;
49 public List<IMigrationStage> resolveMigrations() {
50 migrations.sort(Comparator.comparing(Migration::getVersion));
51 List<IMigrationStage> allTasks = resolveNonExecutedMigrations();
52 allTasks.addAll(postMigrations);
56 //package private for testing
57 void setMigrations(List<Migration> migrations) {
58 this.migrations = migrations;
61 //package private for testing
62 void setPostMigrations(List<PostMigration> postMigrations) {
63 this.postMigrations = postMigrations;
66 private List<IMigrationStage> resolveNonExecutedMigrations() {
67 DBVersion latestDBVersion = sdcRepoService.getLatestDBVersion();
68 return migrations.stream()
69 .filter(mig -> isMigrationVersionGreaterThanLatestVersion(latestDBVersion, mig))
70 .collect(Collectors.toList());
73 private boolean isMigrationVersionGreaterThanLatestVersion(DBVersion latestDBVersion, Migration mig) {
74 return mig.getVersion().compareTo(latestDBVersion) > 0;