Release version 1.13.7
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / resolver / SpringBeansMigrationResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.asdctool.migration.resolver;
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
27 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
28 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
29 import org.openecomp.sdc.asdctool.migration.core.task.PostMigration;
30 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
31
32 public class SpringBeansMigrationResolver implements MigrationResolver {
33
34     private List<Migration> migrations = new ArrayList<>();
35     private List<PostMigration> postMigrations = new ArrayList<>();
36     private SdcRepoService sdcRepoService;
37
38     public SpringBeansMigrationResolver(List<Migration> migrations, List<PostMigration> postMigrations, SdcRepoService sdcRepoService) {
39         this.migrations = migrations;
40         this.postMigrations = postMigrations;
41         this.sdcRepoService = sdcRepoService;
42     }
43
44     @Override
45     public List<IMigrationStage> resolveMigrations() {
46         migrations.sort(Comparator.comparing(Migration::getVersion));
47         List<IMigrationStage> allTasks = resolveNonExecutedMigrations();
48         allTasks.addAll(postMigrations);
49         return allTasks;
50     }
51
52     //package private for testing
53     void setMigrations(List<Migration> migrations) {
54         this.migrations = migrations;
55     }
56
57     //package private for testing
58     void setPostMigrations(List<PostMigration> postMigrations) {
59         this.postMigrations = postMigrations;
60     }
61
62     private List<IMigrationStage> resolveNonExecutedMigrations() {
63         DBVersion latestDBVersion = sdcRepoService.getLatestDBVersion();
64         return migrations.stream().filter(mig -> isMigrationVersionGreaterThanLatestVersion(latestDBVersion, mig)).collect(Collectors.toList());
65     }
66
67     private boolean isMigrationVersionGreaterThanLatestVersion(DBVersion latestDBVersion, Migration mig) {
68         return mig.getVersion().compareTo(latestDBVersion) > 0;
69     }
70 }