Added oparent to sdc main
[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
21 package org.openecomp.sdc.asdctool.migration.resolver;
22
23
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;
29
30 import java.util.ArrayList;
31 import java.util.Comparator;
32 import java.util.List;
33 import java.util.stream.Collectors;
34
35 public class SpringBeansMigrationResolver implements MigrationResolver {
36
37     private List<Migration> migrations = new ArrayList<>();
38     private List<PostMigration> postMigrations = new ArrayList<>();
39     
40     private SdcRepoService sdcRepoService;
41
42     public SpringBeansMigrationResolver(List<Migration> migrations, List<PostMigration> postMigrations, SdcRepoService sdcRepoService) {
43         this.migrations = migrations;
44         this.postMigrations = postMigrations;
45         this.sdcRepoService = sdcRepoService;
46     }
47
48     @Override
49     public List<IMigrationStage> resolveMigrations() {
50         migrations.sort(Comparator.comparing(Migration::getVersion));
51         List<IMigrationStage> allTasks = resolveNonExecutedMigrations();
52         allTasks.addAll(postMigrations);
53         return allTasks;
54     }
55
56     //package private for testing
57     void setMigrations(List<Migration> migrations) {
58         this.migrations = migrations;
59     }
60     
61     //package private for testing
62     void setPostMigrations(List<PostMigration> postMigrations) {
63         this.postMigrations = postMigrations;
64     }
65
66     private List<IMigrationStage> resolveNonExecutedMigrations() {
67         DBVersion latestDBVersion = sdcRepoService.getLatestDBVersion();
68         return migrations.stream()
69                 .filter(mig -> isMigrationVersionGreaterThanLatestVersion(latestDBVersion, mig))
70                 .collect(Collectors.toList());
71     }
72
73     private boolean isMigrationVersionGreaterThanLatestVersion(DBVersion latestDBVersion, Migration mig) {
74         return mig.getVersion().compareTo(latestDBVersion) > 0;
75     }
76 }