2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup;
24 import java.util.Arrays;
25 import java.util.List;
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;
41 * @author Michael Dürre
44 public class Program {
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";
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";
80 private static Options options = init();
82 private static Log LOG = null;
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;
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));
94 return (T) cmd.getParsedOptionValue(option);
100 private static void initLog(boolean silent, String logfile) {
101 initLog(silent, logfile, Level.INFO);
104 private static void initLog(boolean silent, String logfile, Level loglvl) {
105 Logger.getRootLogger().getLoggerRepository().resetConfiguration();
106 LOG = LogFactory.getLog(Program.class);
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);
117 if (logfile != null) {
118 RollingFileAppender fa = new RollingFileAppender();
119 fa.setName("FileLogger");
121 fa.setLayout(new PatternLayout("%d %-5p [%c] %m%n"));
122 fa.setThreshold(loglvl);
123 fa.setMaximumFileSize(10000000);
125 fa.setMaxBackupIndex(5);
126 fa.activateOptions();
127 // add appender to any Logger (here is root)
128 Logger.getRootLogger().addAppender(fa);
130 // repeat with all other desired appenders
133 public static void main(String[] args) {
134 CommandLineParser parser = new DefaultParser();
135 HelpFormatter formatter = new HelpFormatter();
136 CommandLine cmd = null;
138 cmd = parser.parse(options, args);
139 } catch (ParseException e) {
140 System.out.println(e.getMessage());
141 printHelp(formatter);
145 printHelp(formatter);
149 initLog(getOptionOrDefault(cmd, OPTION_SILENT_SHORT, false), null,getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false)?Level.DEBUG:Level.INFO);
150 } catch (ParseException e2) {
153 switch (cmd.getOptionValue("c")) {
157 } catch (Exception e1) {
164 } catch (Exception e1) {
168 case CMD_CREATE_PLUGIN_INIT_FILE:
170 String of = getOptionOrDefault(cmd, "of", null);
172 throw new Exception("please add the parameter output-file");
174 MavenDatabasePluginInitFile.create(Release.CURRENT_RELEASE, of);
175 } catch (Exception e) {
182 } catch (Exception e1) {
189 } catch (Exception e) {
193 case CMD_LIST_VERSION:
197 printHelp(formatter);
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]));
217 private static void cmd_listversion() {
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()) {
224 System.out.println(String.format(format, r.getValue(),
225 r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
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");
243 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
244 username, password,trustAll);
245 DataMigrationReport report = service.importData(filename,false);
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");
262 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
263 username, password,trustAll);
264 DataMigrationReport report = service.exportData(filename);
271 private static void exit(Exception e) {
273 LOG.error("Error during execution: {}", e);
275 System.err.println(e);
282 * @throws ParseException
283 * @throws java.text.ParseException
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);
299 * @throws ParseException
300 * @throws java.text.ParseException
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);
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));
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));
351 private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
353 Option o = new Option(opt, longOpt, hasArg, description);
354 o.setRequired(required);