Merge "DG changes for the closed loop and async support in MDONS"
[sdnc/oam.git] / data-migrator / src / main / java / org / onap / sdnc / oam / datamigrator / DataMigrationInternal.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SDNC
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
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.onap.sdnc.oam.datamigrator;
21
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;
31
32 import java.lang.reflect.Modifier;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37
38 public class DataMigrationInternal {
39
40     private final Logger log;
41
42     public DataMigrationInternal(Logger log) {
43         this.log = log;
44     }
45
46     private void logAndPrint(String msg) {
47         System.out.println(msg);
48         log.info(msg);
49     }
50
51     public void run(String[] args){
52         CommandLineArgs cArgs = new CommandLineArgs();
53         JCommander jCommander = new JCommander(cArgs, args);
54         jCommander.setProgramName(DataMigration.class.getSimpleName());
55
56         if (cArgs.help) {
57             jCommander.usage();
58             return;
59         }
60
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());
64         }
65         if(cArgs.excludeClasses.size() > 0){
66             migratorList = migratorList.stream().filter(aClass -> !cArgs.excludeClasses.contains(aClass.getSimpleName())).collect(Collectors.toSet());
67         }
68
69         if(migratorList.size()>0) {
70             logAndPrint("Total number of available migrations: " + migratorList.size());
71             if(cArgs.list) {
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() );
76                     }else {
77                         logAndPrint(migrator.getSimpleName());
78                     }
79                 }
80             }else {
81                 Operation operation;
82                 try {
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.");
87                     return;
88                 }
89                 boolean success = true;
90                 MigratorConfiguration config;
91                 if(!Strings.isStringEmpty(cArgs.config)){
92                     config = new MigratorConfiguration(cArgs.config);
93                 }else {
94                     logAndPrint("No external configuration provided. Initializing Default configuration.");
95                     config = new MigratorConfiguration();
96                 }
97                 for (Class<? extends Migrator> migratorClass : migratorList) {
98                     logAndPrint("Started executing migrator: "+ migratorClass.getSimpleName());
99                     try {
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);
106                         success=false;
107                     }
108                     logAndPrint("Completed execution for migrator "+ migratorClass.getSimpleName() +" with status: " + success);
109                 }
110                 if(success){
111                     logAndPrint(operation.name()+ " operation completed Successfully.");
112                 }else{
113                     logAndPrint("Error during "+ operation.name() +" operation. Check logs for details.");
114                 }
115             }
116         }else{
117             logAndPrint("No migrations available.");
118         }
119     }
120
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());
124     }
125
126     class CommandLineArgs {
127
128         @Parameter(names = "--h", help = true)
129         public boolean help;
130
131         @Parameter(names = "-o", description = "Operation to be performed. Default is Migrate. Supported operations: Migrate , Backup , Restore.")
132         public String operation = "Migrate";
133
134         @Parameter(names = "-c", description = "Configuration File path / directory")
135         public String config;
136
137         @Parameter(names = "-m", description = "Names of migration scripts to run")
138         public List<String> scripts = new ArrayList<>();
139
140         @Parameter(names = "-l", description = "List the available of migrations")
141         public boolean list = false;
142
143         @Parameter(names = "-e", description = "Exclude list of migrator classes")
144         public List<String> excludeClasses = new ArrayList<>();
145     }
146
147
148 }