2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 AMDOCS
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=========================================================
20 package org.onap.sdnc.oam.datamigrator;
22 import com.beust.jcommander.JCommander;
23 import com.beust.jcommander.Parameter;
24 import com.beust.jcommander.Strings;
25 import org.onap.sdnc.oam.datamigrator.common.Description;
26 import org.onap.sdnc.oam.datamigrator.common.MigratorConfiguration;
27 import org.onap.sdnc.oam.datamigrator.common.Operation;
28 import org.onap.sdnc.oam.datamigrator.migrators.Migrator;
29 import org.reflections.Reflections;
30 import org.slf4j.Logger;
32 import java.lang.reflect.Modifier;
33 import java.util.ArrayList;
34 import java.util.List;
36 import java.util.stream.Collectors;
38 public class DataMigrationInternal {
40 private final Logger log;
42 public DataMigrationInternal(Logger log) {
46 private void logAndPrint(String msg) {
47 System.out.println(msg);
51 public void run(String[] args){
52 CommandLineArgs cArgs = new CommandLineArgs();
53 JCommander jCommander = new JCommander(cArgs, args);
54 jCommander.setProgramName(DataMigration.class.getSimpleName());
61 Set<Class<? extends Migrator>> migratorList = getMigratorList();
62 if(cArgs.scripts.size() > 0){
63 migratorList = migratorList.stream().filter(aClass -> cArgs.scripts.contains(aClass.getSimpleName())).collect(Collectors.toSet());
65 if(cArgs.excludeClasses.size() > 0){
66 migratorList = migratorList.stream().filter(aClass -> !cArgs.excludeClasses.contains(aClass.getSimpleName())).collect(Collectors.toSet());
69 if(migratorList.size()>0) {
70 logAndPrint("Total number of available migrations: " + migratorList.size());
72 logAndPrint("List of available migrations:");
73 for (Class<? extends Migrator> migrator : migratorList) {
74 if(migrator.getAnnotation(Description.class) != null && !migrator.getAnnotation(Description.class).value().isEmpty()) {
75 logAndPrint(migrator.getSimpleName()+ ": " + migrator.getAnnotation(Description.class).value() );
77 logAndPrint(migrator.getSimpleName());
83 operation = Operation.valueOf(cArgs.operation.toUpperCase());
84 logAndPrint("Starting operation: " + operation.name());
85 }catch (IllegalArgumentException e) {
86 logAndPrint("Invalid operation: " + cArgs.operation +". Supported operations are: Migrate, Backup, Restore.");
89 boolean success = true;
90 MigratorConfiguration config;
91 if(!Strings.isStringEmpty(cArgs.config)){
92 config = new MigratorConfiguration(cArgs.config);
94 logAndPrint("No external configuration provided. Initializing Default configuration.");
95 config = new MigratorConfiguration();
97 for (Class<? extends Migrator> migratorClass : migratorList) {
98 logAndPrint("Started executing migrator: "+ migratorClass.getSimpleName());
100 Migrator migrator = migratorClass.newInstance();
101 migrator.init(config);
102 migrator.run(operation);
103 success = success && migrator.isSuccess();
104 } catch (InstantiationException | IllegalAccessException e) {
105 logAndPrint("Error instantiating migrator: " + migratorClass);
108 logAndPrint("Completed execution for migrator "+ migratorClass.getSimpleName() +" with status: " + success);
111 logAndPrint(operation.name()+ " operation completed Successfully.");
113 logAndPrint("Error during "+ operation.name() +" operation. Check logs for details.");
117 logAndPrint("No migrations available.");
121 private Set<Class<? extends Migrator>> getMigratorList() {
122 Reflections reflections = new Reflections("org.onap.sdnc.oam.datamigrator.migrators");
123 return reflections.getSubTypesOf(Migrator.class).stream().filter(aClass -> !Modifier.isAbstract(aClass.getModifiers())).collect(Collectors.toSet());
126 class CommandLineArgs {
128 @Parameter(names = "--h", help = true)
131 @Parameter(names = "-o", description = "Operation to be performed. Default is Migrate. Supported operations: Migrate , Backup , Restore.")
132 public String operation = "Migrate";
134 @Parameter(names = "-c", description = "Configuration File path / directory")
135 public String config;
137 @Parameter(names = "-m", description = "Names of migration scripts to run")
138 public List<String> scripts = new ArrayList<>();
140 @Parameter(names = "-l", description = "List the available of migrations")
141 public boolean list = false;
143 @Parameter(names = "-e", description = "Exclude list of migrator classes")
144 public List<String> excludeClasses = new ArrayList<>();