add data migration tool
[ccsdk/features.git] / sdnr / wt / data-provider / setup / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / setup / Program.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup;
23
24 import java.util.Arrays;
25 import java.util.List;
26
27 import org.apache.commons.cli.*;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.log4j.ConsoleAppender;
31 import org.apache.log4j.Level;
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.PatternLayout;
34 import org.apache.log4j.RollingFileAppender;
35 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MavenDatabasePluginInitFile;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
39
40 /**
41  * @author Michael Dürre
42  *
43  */
44 public class Program {
45
46         private static final String CMD_INITDB = "init";
47         private static final String CMD_CLEAR_DB = "delete";
48         private static final String CMD_CREATE_PLUGIN_INIT_FILE = "pluginfile";
49         private static final String CMD_IMPORT = "import";
50         private static final String CMD_EXPORT = "export";
51         private static final String CMD_LIST_VERSION = "list";
52         private static final String CMD_INITDB_DESCRIPTION = "initialize databse indices and aliases";
53         private static final String CMD_CLEAR_DB_DESCRIPTION = "clear database indices and aliases";
54         private static final String CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION = "create maven plugin file";
55         private static final String CMD_IMPORT_DESCRIPTION = "import data into database";
56         private static final String CMD_EXPORT_DESCRIPTION = "export data from database";
57         private static final String CMD_LIST_VERSION_DESCRIPTION = "list release versions";
58
59         private static final List<String[]> commands = Arrays.asList(new String[] { CMD_INITDB, CMD_INITDB_DESCRIPTION },
60                         new String[] { CMD_CLEAR_DB, CMD_CLEAR_DB_DESCRIPTION },
61                         new String[] { CMD_CREATE_PLUGIN_INIT_FILE, CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION },
62                         new String[] { CMD_IMPORT, CMD_IMPORT_DESCRIPTION }, new String[] { CMD_EXPORT, CMD_EXPORT_DESCRIPTION },
63                         new String[] { CMD_LIST_VERSION, CMD_LIST_VERSION_DESCRIPTION });
64         private static final String APPLICATION_NAME = "SDNR DataMigrationTool";
65         private static final int DEFAULT_SHARDS = 5;
66         private static final int DEFAULT_REPLICAS = 1;
67         private static final String DEFAULT_DBURL = "http://sdnrdb:9200";
68         private static final String DEFAULT_DBPREFIX = "";
69         private static final String OPTION_FORCE_RECREATE_SHORT = "f";
70         private static final String OPTION_SILENT_SHORT = "n";
71         private static final String OPTION_SILENT = "silent";
72         private static final String OPTION_VERSION_SHORT = "v";
73         private static final String OPTION_SHARDS_SHORT = "s";
74         private static final String OPTION_REPLICAS_SHORT = "r";
75         private static final String OPTION_OUTPUTFILE_SHORT = "of";
76         private static final String OPTION_INPUTFILE_SHORT = "if";
77         private static final String OPTION_DEBUG_SHORT = "x";
78         private static final String OPTION_TRUSTINSECURESSL_SHORT = "k";
79
80         private static Options options = init();
81
82         private static Log LOG = null;
83
84         @SuppressWarnings("unchecked")
85         private static <T> T getOptionOrDefault(CommandLine cmd, String option, T def) throws ParseException {
86                 if (def instanceof Boolean) {
87                         return cmd.hasOption(option) ? (T) Boolean.TRUE : def;
88                 }
89                 if (cmd.hasOption(option) && cmd.getOptionValue(option) != null) {
90                         if (option.equals(OPTION_VERSION_SHORT)) {
91                                 String v = cmd.getOptionValue(option);
92                                 return (T) Release.getValueBySuffix(v.startsWith("-") ? v : ("-" + v));
93                         } else {
94                                 return (T) cmd.getParsedOptionValue(option);
95                         }
96                 }
97                 return def;
98         }
99
100         private static void initLog(boolean silent, String logfile) {
101                 initLog(silent, logfile, Level.INFO);
102         }
103
104         private static void initLog(boolean silent, String logfile, Level loglvl) {
105                 Logger.getRootLogger().getLoggerRepository().resetConfiguration();
106                 LOG = LogFactory.getLog(Program.class);
107                 if (!silent) {
108                         ConsoleAppender console = new ConsoleAppender(); // create appender
109                         // configure the appender
110                         String PATTERN = "%d [%p|%C{1}] %m%n";
111                         console.setLayout(new PatternLayout(PATTERN));
112                         console.setThreshold(loglvl);
113                         console.activateOptions();
114                         // add appender to any Logger (here is root)
115                         Logger.getRootLogger().addAppender(console);
116                 }
117                 if (logfile != null) {
118                         RollingFileAppender fa = new RollingFileAppender();
119                         fa.setName("FileLogger");
120                         fa.setFile(logfile);
121                         fa.setLayout(new PatternLayout("%d %-5p [%c] %m%n"));
122                         fa.setThreshold(loglvl);
123                         fa.setMaximumFileSize(10000000);
124                         fa.setAppend(true);
125                         fa.setMaxBackupIndex(5);
126                         fa.activateOptions();
127                         // add appender to any Logger (here is root)
128                         Logger.getRootLogger().addAppender(fa);
129                 }
130                 // repeat with all other desired appenders
131         }
132
133         public static void main(String[] args) {
134                 CommandLineParser parser = new DefaultParser();
135                 HelpFormatter formatter = new HelpFormatter();
136                 CommandLine cmd = null;
137                 try {
138                         cmd = parser.parse(options, args);
139                 } catch (ParseException e) {
140                         System.out.println(e.getMessage());
141                         printHelp(formatter);
142                         System.exit(1);
143                 }
144                 if (cmd == null) {
145                         printHelp(formatter);
146                         System.exit(1);
147                 }
148                 try {
149                         initLog(getOptionOrDefault(cmd, OPTION_SILENT_SHORT, false), null,getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false)?Level.DEBUG:Level.INFO);
150                 } catch (ParseException e2) {
151
152                 }
153                 switch (cmd.getOptionValue("c")) {
154                 case CMD_INITDB:
155                         try {
156                                 cmd_init_db(cmd);
157                         } catch (Exception e1) {
158                                 exit(e1);
159                         }
160                         break;
161                 case CMD_CLEAR_DB:
162                         try {
163                                 cmd_clear_db(cmd);
164                         } catch (Exception e1) {
165                                 exit(e1);
166                         }
167                         break;
168                 case CMD_CREATE_PLUGIN_INIT_FILE:
169                         try {
170                                 String of = getOptionOrDefault(cmd, "of", null);
171                                 if (of == null) {
172                                         throw new Exception("please add the parameter output-file");
173                                 }
174                                 MavenDatabasePluginInitFile.create(Release.CURRENT_RELEASE, of);
175                         } catch (Exception e) {
176                                 exit(e);
177                         }
178                         break;
179                 case CMD_IMPORT:
180                         try {
181                                 cmd_dbimport(cmd);
182                         } catch (Exception e1) {
183                                 exit(e1);
184                         }
185                         break;
186                 case CMD_EXPORT:
187                         try {
188                                 cmd_dbexport(cmd);
189                         } catch (Exception e) {
190                                 exit(e);
191                         }
192                         break;
193                 case CMD_LIST_VERSION:
194                         cmd_listversion();
195                         break;
196                 default:
197                         printHelp(formatter);
198                         break;
199                 }
200                 System.exit(0);
201         }
202
203         /**
204          * @param formatter
205          */
206         private static void printHelp(HelpFormatter formatter) {
207                 formatter.printHelp(APPLICATION_NAME, options);
208                 System.out.println("\nCommands:");
209                 for (String[] c : commands) {
210                         System.out.println(String.format("%10s\t%s", c[0], c[1]));
211                 }
212         }
213
214         /**
215          * 
216          */
217         private static void cmd_listversion() {
218
219                 System.out.println("Database Releases:");
220                 final String format = "%15s\t%8s";
221                 System.out.println(String.format(format, "Name", "Version"));
222                 for (Release r : Release.values()) {
223
224                         System.out.println(String.format(format, r.getValue(),
225                                         r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
226                 }
227
228         }
229
230         /**
231          * @throws Exception 
232          * 
233          */
234         private static void cmd_dbimport(CommandLine cmd) throws Exception {
235                 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
236                 String username = getOptionOrDefault(cmd, "dbu", null);
237                 String password = getOptionOrDefault(cmd, "dbp", null);
238                 String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
239                 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
240                 if (filename == null) {
241                         throw new Exception("please add output file parameter");
242                 }
243                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
244                                 username, password,trustAll);
245                 DataMigrationReport report = service.importData(filename,false);
246                 LOG.info(report);
247         }
248
249         /**
250          * @throws Exception 
251          * 
252          */
253         private static void cmd_dbexport(CommandLine cmd) throws Exception {
254                 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
255                 String username = getOptionOrDefault(cmd, "dbu", null);
256                 String password = getOptionOrDefault(cmd, "dbp", null);
257                 String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
258                 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
259                 if (filename == null) {
260                         throw new Exception("please add output file parameter");
261                 }
262                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
263                                 username, password,trustAll);
264                 DataMigrationReport report = service.exportData(filename);
265                 LOG.info(report);
266         }
267
268         /**
269          * @param e
270          */
271         private static void exit(Exception e) {
272                 if (LOG != null) {
273                         LOG.error("Error during execution: {}", e);
274                 } else {
275                         System.err.println(e);
276                 }
277                 System.exit(1);
278         }
279
280         /**
281          * @param cmd
282          * @throws ParseException 
283          * @throws java.text.ParseException 
284          */
285         private static void cmd_clear_db(CommandLine cmd) throws ParseException, java.text.ParseException {
286                 Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
287                 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
288                 String dbPrefix = getOptionOrDefault(cmd, "p", DEFAULT_DBPREFIX);
289                 String username = getOptionOrDefault(cmd, "dbu", null);
290                 String password = getOptionOrDefault(cmd, "dbp", null);
291                 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
292                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
293                                 username, password,trustAll);
294                 service.clearDatabase(r, dbPrefix);
295         }
296
297         /**
298          * @param cmd
299          * @throws ParseException 
300          * @throws java.text.ParseException 
301          */
302         private static void cmd_init_db(CommandLine cmd) throws ParseException, java.text.ParseException {
303                 Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
304                 int numShards = getOptionOrDefault(cmd, OPTION_SHARDS_SHORT, DEFAULT_SHARDS);
305                 int numReplicas = getOptionOrDefault(cmd, OPTION_REPLICAS_SHORT, DEFAULT_REPLICAS);
306                 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
307                 String dbPrefix = getOptionOrDefault(cmd, "p", DEFAULT_DBPREFIX);
308                 String username = getOptionOrDefault(cmd, "dbu", null);
309                 String password = getOptionOrDefault(cmd, "dbp", null);
310                 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
311                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
312                                 username, password,trustAll);
313                 boolean forceRecreate = cmd.hasOption(OPTION_FORCE_RECREATE_SHORT);
314                 service.initDatabase(r, numShards, numReplicas, dbPrefix, forceRecreate);
315
316         }
317
318         /**
319          * @return
320          */
321         private static Options init() {
322                 Options options = new Options();
323                 options.addOption(createOption("c", "cmd", true, "command to execute", true));
324                 options.addOption(createOption("db", "dburl", true, "database url", false));
325                 options.addOption(createOption("dbu", "db-username", true, "database basic auth username", false));
326                 options.addOption(createOption("dbp", "db-password", true, "database basic auth password", false));
327                 options.addOption(createOption(OPTION_REPLICAS_SHORT, "replicas", true, "amount of replicas", false));
328                 options.addOption(createOption(OPTION_SHARDS_SHORT, "shards", true, "amount of shards", false));
329                 options.addOption(createOption("p", "prefix", true, "prefix for db indices", false));
330                 options.addOption(createOption(OPTION_VERSION_SHORT, "version", true, "version", false));
331                 options.addOption(createOption(OPTION_DEBUG_SHORT, "verbose", false, "verbose mode", false));
332                 options.addOption(createOption(OPTION_TRUSTINSECURESSL_SHORT, "trust-insecure", false, "trust insecure ssl certs", false));
333                 options.addOption(createOption("w", "wait", true, "wait delay for yellow status", false));
334                 options.addOption(
335                                 createOption(OPTION_FORCE_RECREATE_SHORT, "force-recreate", false, "delete if sth exists", false));
336                 options.addOption(createOption(OPTION_SILENT_SHORT, OPTION_SILENT, false, "prevent console output", false));
337                 options.addOption(createOption(OPTION_OUTPUTFILE_SHORT, "output-file", true, "file to write into", false));
338                 options.addOption(createOption(OPTION_INPUTFILE_SHORT, "input-file", true, "file to read from", false));
339
340                 return options;
341         }
342
343         /**
344          * @param opt 
345          * @param longOpt 
346          * @param hasArg 
347          * @param description 
348          * @param required 
349          * @return
350          */
351         private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
352                         boolean required) {
353                 Option o = new Option(opt, longOpt, hasArg, description);
354                 o.setRequired(required);
355                 return o;
356         }
357 }