fix some sdnr sonar bugs
[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 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.CommandLineParser;
28 import org.apache.commons.cli.DefaultParser;
29 import org.apache.commons.cli.HelpFormatter;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.log4j.ConsoleAppender;
36 import org.apache.log4j.Level;
37 import org.apache.log4j.Logger;
38 import org.apache.log4j.PatternLayout;
39 import org.apache.log4j.RollingFileAppender;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MavenDatabasePluginInitFile;
43 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
44
45 /**
46  * @author Michael Dürre
47  *
48  */
49 public class Program {
50
51     // constants
52     private static final String CMD_INITDB = "init";
53     private static final String CMD_CLEAR_DB = "delete";
54     private static final String CMD_CLEAR_DB_COMPLETE = "clear";
55     private static final String CMD_CREATE_PLUGIN_INIT_FILE = "pluginfile";
56     private static final String CMD_IMPORT = "import";
57     private static final String CMD_EXPORT = "export";
58     private static final String CMD_LIST_VERSION = "list";
59
60     private static final String CMD_INITDB_DESCRIPTION = "initialize databse indices and aliases";
61     private static final String CMD_CLEAR_DB_DESCRIPTION = "delete database indices and aliases for current release";
62     private static final String CMD_CLEAR_DB_COMPLETE_DESCRIPTION = "delete all database indices and aliases";
63
64     private static final String CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION = "create maven plugin file";
65     private static final String CMD_IMPORT_DESCRIPTION = "import data into database";
66     private static final String CMD_EXPORT_DESCRIPTION = "export data from database";
67     private static final String CMD_LIST_VERSION_DESCRIPTION = "list release versions";
68
69     private static final List<String[]> commands = Arrays.asList(new String[] {CMD_INITDB, CMD_INITDB_DESCRIPTION},
70             new String[] {CMD_CLEAR_DB, CMD_CLEAR_DB_DESCRIPTION},
71             new String[] {CMD_CLEAR_DB_COMPLETE, CMD_CLEAR_DB_COMPLETE_DESCRIPTION},
72             new String[] {CMD_CREATE_PLUGIN_INIT_FILE, CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION},
73             new String[] {CMD_IMPORT, CMD_IMPORT_DESCRIPTION}, new String[] {CMD_EXPORT, CMD_EXPORT_DESCRIPTION},
74             new String[] {CMD_LIST_VERSION, CMD_LIST_VERSION_DESCRIPTION});
75     private static final String APPLICATION_NAME = "SDNR DataMigrationTool";
76
77     private static final int DEFAULT_SHARDS = 5;
78     private static final int DEFAULT_REPLICAS = 1;
79     private static final int DEFAULT_DATABASEWAIT_SECONDS = 30;
80     private static final String DEFAULT_DBURL_ELASTICSEARCH = "http://sdnrdb:9200";
81     private static final String DEFAULT_DBURL_MARIADB = "jdbc:mysql://sdnrdb:3306/sdnrdb";
82     private static final String DEFAULT_DBPREFIX = "";
83     private static final boolean DEFAULT_TRUSTINSECURESSL = false;
84
85     private static final String OPTION_FORCE_RECREATE_SHORT = "f";
86     private static final String OPTION_FORCE_RECREATE_LONG = "force-recreate";
87     private static final String OPTION_SILENT_SHORT = "n";
88     private static final String OPTION_SILENT = "silent";
89     private static final String OPTION_VERSION_SHORT = "v";
90     private static final String OPTION_VERSION_LONG = "version";
91     private static final String OPTION_SHARDS_SHORT = "s";
92     private static final String OPTION_SHARDS_LONG = "shards";
93     private static final String OPTION_REPLICAS_SHORT = "r";
94     private static final String OPTION_REPLICAS_LONG = "replicas";
95     private static final String OPTION_OUTPUTFILE_SHORT = "of";
96     private static final String OPTION_OUTPUTFILE_LONG = "output-file";
97     private static final String OPTION_INPUTFILE_SHORT = "if";
98     private static final String OPTION_INPUTFILE_LONG = "input-file";
99     private static final String OPTION_DEBUG_SHORT = "x";
100     private static final String OPTION_DEBUG_LONG = "verbose";
101     private static final String OPTION_TRUSTINSECURESSL_SHORT = "k";
102     private static final String OPTION_TRUSTINSECURESSL_LONG = "trust-insecure";
103     private static final String OPTION_DATABASE_SHORT = "db";
104     private static final String OPTION_DATABASE_LONG = "dburl";
105     private static final String OPTION_COMMAND_SHORT = "c";
106     private static final String OPTION_COMMAND_LONG = "cmd";
107     private static final String OPTION_DATABASETYPE_SHORT = "dbt";
108     private static final String OPTION_DATABASETYPE_LONG = "db-type";
109     private static final String OPTION_DATABASEUSER_SHORT = "dbu";
110     private static final String OPTION_DATABASEUSER_LONG = "db-username";
111     private static final String OPTION_DATABASEPASSWORD_SHORT = "dbp";
112     private static final String OPTION_DATABASEPASSWORD_LONG = "db-password";
113     private static final String OPTION_DATABASEPREFIX_SHORT = "p";
114     private static final String OPTION_DATABASEPREFIX_LONG = "prefix";
115     private static final String OPTION_DATABASEWAIT_SHORT = "w";
116     private static final String OPTION_DATABASEWAIT_LONG = "wait";
117     private static final String OPTION_HELP_SHORT = "h";
118     private static final String OPTION_HELP_LONG = "help";
119     // end of constants
120
121     // variables
122     private static Options options = init();
123     private static Log LOG = null;
124     // end of variables
125
126     // public methods
127     public static void main(String[] args) {
128         System.exit(main2(args));
129     }
130     // end of public methods
131
132     // private methods
133     @SuppressWarnings("unchecked")
134     private static <T> T getOptionOrDefault(CommandLine cmd, String option, T def) throws ParseException {
135         if (def instanceof Boolean) {
136             return cmd.hasOption(option) ? (T) Boolean.TRUE : def;
137         }
138         if (def instanceof Integer) {
139             return cmd.hasOption(option) ? (T) Integer.valueOf(cmd.getOptionValue(option)) : def;
140         }
141         if (def instanceof Long) {
142             return cmd.hasOption(option) ? (T) Long.valueOf(cmd.getOptionValue(option)) : def;
143         }
144         if (def instanceof Release) {
145             return cmd.hasOption(option) ? (T) Release.getValueBySuffix(cmd.getOptionValue(option)) : def;
146         }
147         if (def instanceof SdnrDbType) {
148             return cmd.hasOption(option) ? (T) SdnrDbType.valueOf(cmd.getOptionValue(option).toUpperCase()) : def;
149         }
150         if (cmd.hasOption(option) && cmd.getOptionValue(option) != null) {
151             if (option.equals(OPTION_VERSION_SHORT)) {
152                 String v = cmd.getOptionValue(option);
153                 return (T) Release.getValueBySuffix(v.startsWith("-") ? v : "-" + v);
154             } else {
155                 return (T) cmd.getParsedOptionValue(option);
156             }
157         }
158         return def;
159     }
160
161     private static void initLog(boolean silent, String logfile, Level loglvl) {
162         Logger.getRootLogger().getLoggerRepository().resetConfiguration();
163         LOG = LogFactory.getLog(Program.class);
164         if (!silent) {
165             ConsoleAppender console = new ConsoleAppender(); // create appender
166             // configure the appender
167             String PATTERN = "%d [%p|%C{1}] %m%n";
168             console.setLayout(new PatternLayout(PATTERN));
169             console.setThreshold(loglvl);
170             console.activateOptions();
171             // add appender to any Logger (here is root)
172             Logger.getRootLogger().addAppender(console);
173         }
174         if (logfile != null) {
175             RollingFileAppender fa = new RollingFileAppender();
176             fa.setName("FileLogger");
177             fa.setFile(logfile);
178             fa.setLayout(new PatternLayout("%d %-5p [%c] %m%n"));
179             fa.setThreshold(loglvl);
180             fa.setMaximumFileSize(10000000);
181             fa.setAppend(true);
182             fa.setMaxBackupIndex(5);
183             fa.activateOptions();
184             // add appender to any Logger (here is root)
185             Logger.getRootLogger().addAppender(fa);
186         }
187         // repeat with all other desired appenders
188     }
189
190     private static int main2(String[] args) {
191
192         CommandLineParser parser = new DefaultParser();
193         HelpFormatter formatter = new HelpFormatter();
194         CommandLine cmd = null;
195
196         try {
197             cmd = parser.parse(options, args);
198         } catch (ParseException e) {
199             System.out.println(e.getMessage());
200             printHelp(formatter);
201             return 1;
202         }
203         if (cmd == null) {
204             printHelp(formatter);
205             return 1;
206         }
207         try {
208             initLog(getOptionOrDefault(cmd, OPTION_SILENT_SHORT, false), null,
209                     getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false) ? Level.DEBUG : Level.INFO);
210         } catch (ParseException e2) {
211
212         }
213         try {
214             if (getOptionOrDefault(cmd, OPTION_HELP_SHORT, false)) {
215                 printHelp(formatter);
216                 return 0;
217             }
218         } catch (ParseException e2) {
219             return exit(e2);
220         }
221         final String command = cmd.getOptionValue(OPTION_COMMAND_SHORT);
222         if (command == null) {
223             printHelp(formatter);
224             return 1;
225         }
226         switch (command) {
227             case CMD_INITDB:
228                 try {
229                     cmd_init_db(cmd);
230                 } catch (Exception e1) {
231                     return exit(e1);
232                 }
233                 break;
234             case CMD_CLEAR_DB:
235                 try {
236                     cmd_clear_db(cmd);
237                 } catch (Exception e1) {
238                     return exit(e1);
239                 }
240                 break;
241             case CMD_CLEAR_DB_COMPLETE:
242                 try {
243                     cmd_clear_db_complete(cmd);
244                 } catch (Exception e1) {
245                     return exit(e1);
246                 }
247                 break;
248             case CMD_CREATE_PLUGIN_INIT_FILE:
249                 try {
250                     String of = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
251                     if (of == null) {
252                         throw new Exception("please add the parameter output-file");
253                     }
254                     MavenDatabasePluginInitFile
255                             .create(getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE), of);
256                 } catch (Exception e) {
257                     return exit(e);
258                 }
259                 break;
260             case CMD_IMPORT:
261                 try {
262                     cmd_dbimport(cmd);
263                 } catch (Exception e1) {
264                     return exit(e1);
265                 }
266                 break;
267             case CMD_EXPORT:
268                 try {
269                     cmd_dbexport(cmd);
270                 } catch (Exception e) {
271                     return exit(e);
272                 }
273                 break;
274             case CMD_LIST_VERSION:
275                 cmd_listversion();
276                 break;
277
278             default:
279                 printHelp(formatter);
280                 return 1;
281         }
282         return 0;
283     }
284
285     private static void printHelp(HelpFormatter formatter) {
286         formatter.printHelp(APPLICATION_NAME, options);
287         System.out.println("\nCommands:");
288         for (String[] c : commands) {
289             System.out.println(String.format("%10s\t%s", c[0], c[1]));
290         }
291     }
292
293     private static void cmd_listversion() {
294
295         System.out.println("Database Releases:");
296         final String format = "%15s\t%8s";
297         System.out.println(String.format(format, "Name", "Version"));
298         for (Release r : Release.values()) {
299
300             System.out.println(String.format(format, r.getValue(),
301                     r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
302         }
303
304     }
305
306     private static void cmd_dbimport(CommandLine cmd) throws Exception {
307         DatabaseOptions options = new DatabaseOptions(cmd);
308         String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
309         if (filename == null) {
310             throw new Exception("please add output file parameter");
311         }
312         DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(),
313                 options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs());
314         DataMigrationReport report = service.importData(filename, false);
315         LOG.info(report);
316         if (!report.completed()) {
317             throw new Exception("db import seems to be not executed completed");
318         }
319         LOG.info("database import completed successfully");
320     }
321
322     private static void cmd_dbexport(CommandLine cmd) throws Exception {
323         DatabaseOptions options = new DatabaseOptions(cmd);
324         String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
325         if (filename == null) {
326             throw new Exception("please add output file parameter");
327         }
328         DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(),
329                 options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs());
330         DataMigrationReport report = service.exportData(filename);
331         LOG.info(report);
332         if (!report.completed()) {
333             throw new Exception("db export seems to be not executed completed");
334         }
335         LOG.info("database export completed successfully");
336     }
337
338     private static int exit(Exception e) {
339         if (LOG != null) {
340             LOG.error("Error during execution: {}", e);
341         } else {
342             System.err.println(e);
343         }
344         return 1;
345     }
346
347     private static void cmd_clear_db(CommandLine cmd) throws Exception {
348         Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, (Release) null);
349         DatabaseOptions options = new DatabaseOptions(cmd);
350         String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX);
351         DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(),
352                 options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs());
353         if (!service.clearDatabase(r, dbPrefix, options.getTimeoutMs())) {
354             throw new Exception("failed to init database");
355         }
356         LOG.info("database clear completed successfully");
357     }
358
359     private static void cmd_clear_db_complete(CommandLine cmd) throws Exception {
360         DatabaseOptions options = new DatabaseOptions(cmd);
361         DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(), options.getUrl(),
362                 options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs());
363         if (!service.clearCompleteDatabase(options.getTimeoutMs())) {
364             throw new Exception("failed to init database");
365         }
366         LOG.info("database complete clear completed successfully");
367     }
368
369     private static void cmd_init_db(CommandLine cmd) throws Exception {
370         Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, (Release) null);
371         int numShards = getOptionOrDefault(cmd, OPTION_SHARDS_SHORT, DEFAULT_SHARDS);
372         int numReplicas = getOptionOrDefault(cmd, OPTION_REPLICAS_SHORT, DEFAULT_REPLICAS);
373         DatabaseOptions options = new DatabaseOptions(cmd);
374         String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX);
375         DataMigrationProviderImpl service = new DataMigrationProviderImpl(options.getType(),options.getUrl(),
376                 options.getUsername(), options.getPassword(), options.doTrustAll(), options.getTimeoutMs());
377         boolean forceRecreate = cmd.hasOption(OPTION_FORCE_RECREATE_SHORT);
378         if (!service.initDatabase(r, numShards, numReplicas, dbPrefix, forceRecreate, options.getTimeoutMs())) {
379             throw new Exception("failed to init database");
380         }
381         LOG.info("database init completed successfully");
382
383     }
384
385     private static Options init() {
386         Options result = new Options();
387         result.addOption(createOption(OPTION_COMMAND_SHORT, OPTION_COMMAND_LONG, true, "command to execute", false));
388         result.addOption(createOption(OPTION_DATABASE_SHORT, OPTION_DATABASE_LONG, true, "database url", false));
389         result.addOption(createOption(OPTION_DATABASETYPE_SHORT, OPTION_DATABASETYPE_LONG, true,
390                 "database type (elasticsearch|mariadb)", false));
391         result.addOption(createOption(OPTION_DATABASEUSER_SHORT, OPTION_DATABASEUSER_LONG, true,
392                 "database basic auth username", false));
393         result.addOption(createOption(OPTION_DATABASEPASSWORD_SHORT, OPTION_DATABASEPASSWORD_LONG, true,
394                 "database basic auth password", false));
395         result.addOption(createOption(OPTION_REPLICAS_SHORT, OPTION_REPLICAS_LONG, true, "amount of replicas", false));
396         result.addOption(createOption(OPTION_SHARDS_SHORT, OPTION_SHARDS_LONG, true, "amount of shards", false));
397         result.addOption(createOption(OPTION_DATABASEPREFIX_SHORT, OPTION_DATABASEPREFIX_LONG, true,
398                 "prefix for db indices", false));
399         result.addOption(createOption(OPTION_VERSION_SHORT, OPTION_VERSION_LONG, true, "version", false));
400         result.addOption(createOption(OPTION_DEBUG_SHORT, OPTION_DEBUG_LONG, false, "verbose mode", false));
401         result.addOption(createOption(OPTION_TRUSTINSECURESSL_SHORT, OPTION_TRUSTINSECURESSL_LONG, false,
402                 "trust insecure ssl certs", false));
403         result.addOption(createOption(OPTION_DATABASEWAIT_SHORT, OPTION_DATABASEWAIT_LONG, true,
404                 "wait for yellow status with timeout in seconds", false));
405         result.addOption(createOption(OPTION_FORCE_RECREATE_SHORT, OPTION_FORCE_RECREATE_LONG, false,
406                 "delete if sth exists", false));
407         result.addOption(createOption(OPTION_SILENT_SHORT, OPTION_SILENT, false, "prevent console output", false));
408         result.addOption(
409                 createOption(OPTION_OUTPUTFILE_SHORT, OPTION_OUTPUTFILE_LONG, true, "file to write into", false));
410         result.addOption(createOption(OPTION_INPUTFILE_SHORT, OPTION_INPUTFILE_LONG, true, "file to read from", false));
411         result.addOption(createOption(OPTION_HELP_SHORT, OPTION_HELP_LONG, false, "show help", false));
412         return result;
413     }
414
415     private static long getTimeoutOptionMillis(CommandLine cmd) throws ParseException {
416         return getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, DEFAULT_DATABASEWAIT_SECONDS) * 1000L;
417     }
418
419     /**
420      * create option for argparse lib
421      *
422      * @param opt short option string
423      * @param longOpt long option string
424      * @param hasArg flag if has a parameter after option tag
425      * @param description description for help output
426      * @param required flag if is required for program
427      * @return option object for argparse lib
428      */
429     private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
430             boolean required) {
431         Option o = new Option(opt, longOpt, hasArg, description);
432         o.setRequired(required);
433         return o;
434     }
435     // end of private methods
436
437     private static class DatabaseOptions{
438         private final String url;
439         private final String username;
440         private final String password;
441         private final boolean trustAll;
442         private final long timeoutMs;
443         private final SdnrDbType type;
444
445         public String getUrl() {
446             return this.url;
447         }
448         public SdnrDbType getType() {
449             return this.type;
450         }
451         public String getUsername() {
452             return this.username;
453         }
454         public String getPassword() {
455             return this.password;
456         }
457         public boolean doTrustAll() {
458             return this.trustAll;
459         }
460         public long getTimeoutMs() {
461             return this.timeoutMs;
462         }
463
464         public DatabaseOptions(CommandLine cmd) throws ParseException {
465             this.type = getOptionOrDefault(cmd, OPTION_DATABASETYPE_LONG, SdnrDbType.ELASTICSEARCH);
466             this.url = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT,
467                     this.type == SdnrDbType.ELASTICSEARCH ? DEFAULT_DBURL_ELASTICSEARCH : DEFAULT_DBURL_MARIADB);
468             this.username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
469             this.password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null);
470             this.trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, DEFAULT_TRUSTINSECURESSL);
471             this.timeoutMs = getTimeoutOptionMillis(cmd);
472         }
473     }
474 }