Release version 1.13.7
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / core / SdcMigrationTool.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.core;
21
22 import java.util.List;
23 import org.openecomp.sdc.asdctool.migration.core.execution.MigrationExecutionResult;
24 import org.openecomp.sdc.asdctool.migration.core.execution.MigrationExecutorImpl;
25 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
26 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage.AspectMigrationEnum;
27 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
28 import org.openecomp.sdc.asdctool.migration.resolver.MigrationResolver;
29 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31
32 public class SdcMigrationTool {
33
34     private static final Logger LOGGER = Logger.getLogger(SdcMigrationTool.class);
35     private MigrationResolver migrationsResolver;
36     private SdcRepoService sdcRepoService;
37
38     public SdcMigrationTool(MigrationResolver migrationsResolver, SdcRepoService sdcRepoService) {
39         this.migrationsResolver = migrationsResolver;
40         this.sdcRepoService = sdcRepoService;
41     }
42
43     public SdcMigrationTool() {
44     }
45
46     public boolean migrate(boolean enforceAll) {
47         LOGGER.info("starting migration process");
48         handleEnforceMigrationFlag(enforceAll);
49         List<IMigrationStage> migrations = migrationsResolver.resolveMigrations();
50         LOGGER.info("there are {} migrations task to execute", migrations.size());
51         for (IMigrationStage migration : migrations) {
52             try {
53                 MigrationExecutionResult executionResult = new MigrationExecutorImpl().execute(migration);
54                 if (migrationHasFailed(executionResult)) {
55                     LOGGER.error("migration {} with version {} has failed. error msg: {}", migration.getClass().getName(),
56                         migration.getVersion().toString(), executionResult.getMsg());
57                     return false;
58                 }
59                 if (migration.getAspectMigration() == AspectMigrationEnum.MIGRATION) {
60                     sdcRepoService.createMigrationTask(executionResult.toMigrationTaskEntry());
61                 }
62             } catch (RuntimeException e) {
63                 LOGGER.error("migration {} with version {} has failed. error msg: {}", migration.getClass().getName(),
64                     migration.getVersion().toString(), e);
65                 return false;
66             }
67         }
68         return true;
69     }
70
71     private boolean migrationHasFailed(MigrationExecutionResult migrationResult) {
72         return migrationResult.getMigrationStatus().equals(MigrationResult.MigrationStatus.FAILED);
73     }
74
75     private void handleEnforceMigrationFlag(boolean enforceAll) {
76         if (enforceAll) {
77             LOGGER.info("enforcing migration for current version");
78             sdcRepoService.clearTasksForCurrentMajor();
79         }
80     }
81 }