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.core;
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;
32 import java.util.List;
34 public class SdcMigrationTool {
36 private static final Logger LOGGER = Logger.getLogger(SdcMigrationTool.class);
38 private MigrationResolver migrationsResolver;
40 private SdcRepoService sdcRepoService;
42 public SdcMigrationTool(MigrationResolver migrationsResolver, SdcRepoService sdcRepoService) {
43 this.migrationsResolver = migrationsResolver;
44 this.sdcRepoService = sdcRepoService;
47 public SdcMigrationTool() {
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) {
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());
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);
72 private boolean migrationHasFailed(MigrationExecutionResult migrationResult) {
73 return migrationResult.getMigrationStatus().equals(MigrationResult.MigrationStatus.FAILED);
76 private void handleEnforceMigrationFlag(boolean enforceAll) {
78 LOGGER.info("enforcing migration for current version");
79 sdcRepoService.clearTasksForCurrentMajor();