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