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.execution;
23 import org.openecomp.sdc.asdctool.migration.core.MigrationException;
24 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
25 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
27 import org.springframework.util.StopWatch;
30 public class MigrationExecutorImpl implements MigrationExecutor {
32 private static final Logger LOGGER = Logger.getLogger(MigrationExecutorImpl.class);
35 public MigrationExecutionResult execute(IMigrationStage migration) throws MigrationException {
37 LOGGER.info("starting migration {}. description: {}. version {}", migration.getClass().getName(), migration.description(), migration.getVersion().toString());
38 StopWatch stopWatch = new StopWatch();
40 MigrationResult migrationResult = migration.migrate();
42 double executionTime = stopWatch.getTotalTimeSeconds();
43 return logAndCreateExecutionResult(migration, migrationResult, executionTime);
44 } catch (RuntimeException e) {
45 LOGGER.error("migration {} has failed!", migration.description(), e);
46 throw new MigrationException("migration %s failed!!!", e);
51 private MigrationExecutionResult logAndCreateExecutionResult(IMigrationStage migration, MigrationResult migrationResult, double executionTime) {
52 LOGGER.info("finished migration {}. with version {}. migration status: {}, migration message: {}, execution time: {}", migration.getClass().getName(), migration.getVersion().toString(), migrationResult.getMigrationStatus().name(), migrationResult.getMsg(), executionTime);
53 return createMigrationTask(migration, migrationResult, executionTime);
56 private MigrationExecutionResult createMigrationTask(IMigrationStage migration, MigrationResult migrationResult, double totalTimeSeconds) {
57 MigrationExecutionResult migrationExecutionResult = new MigrationExecutionResult();
58 migrationExecutionResult.setExecutionTime(totalTimeSeconds);
59 migrationExecutionResult.setMigrationStatus(migrationResult.getMigrationStatus());
60 migrationExecutionResult.setMsg(migrationResult.getMsg());
61 migrationExecutionResult.setTaskName(migration.getClass().getName());
62 migrationExecutionResult.setVersion(migration.getVersion());
63 migrationExecutionResult.setDescription(migration.description());
64 return migrationExecutionResult;