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,
150 getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false) ? Level.DEBUG : Level.INFO);
151 } catch (ParseException e2) {
154 switch (cmd.getOptionValue("c")) {
158 } catch (Exception e1) {
165 } catch (Exception e1) {
169 case CMD_CREATE_PLUGIN_INIT_FILE:
171 String of = getOptionOrDefault(cmd, "of", null);
173 throw new Exception("please add the parameter output-file");
175 MavenDatabasePluginInitFile.create(Release.CURRENT_RELEASE, of);
176 } catch (Exception e) {
183 } catch (Exception e1) {
190 } catch (Exception e) {
194 case CMD_LIST_VERSION:
198 printHelp(formatter);
207 private static void printHelp(HelpFormatter formatter) {
208 formatter.printHelp(APPLICATION_NAME, options);
209 System.out.println("\nCommands:");
210 for (String[] c : commands) {
211 System.out.println(String.format("%10s\t%s", c[0], c[1]));
218 private static void cmd_listversion() {
220 System.out.println("Database Releases:");
221 final String format = "%15s\t%8s";
222 System.out.println(String.format(format, "Name", "Version"));
223 for (Release r : Release.values()) {
225 System.out.println(String.format(format, r.getValue(),
226 r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
235 private static void cmd_dbimport(CommandLine cmd) throws Exception {
236 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
237 String username = getOptionOrDefault(cmd, "dbu", null);
238 String password = getOptionOrDefault(cmd, "dbp", null);
239 String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
240 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
241 if (filename == null) {
242 throw new Exception("please add output file parameter");
244 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
245 username, password, trustAll);
246 DataMigrationReport report = service.importData(filename, false);
248 if(!report.completed()) {
249 throw new Exception("db import seems to be not executed completed");
257 private static void cmd_dbexport(CommandLine cmd) throws Exception {
258 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
259 String username = getOptionOrDefault(cmd, "dbu", null);
260 String password = getOptionOrDefault(cmd, "dbp", null);
261 String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
262 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
263 if (filename == null) {
264 throw new Exception("please add output file parameter");
266 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
267 username, password, trustAll);
268 DataMigrationReport report = service.exportData(filename);
270 if(!report.completed()) {
271 throw new Exception("db export seems to be not executed completed");
279 private static void exit(Exception e) {
281 LOG.error("Error during execution: {}", e);
283 System.err.println(e);
290 * @throws java.text.ParseException
293 private static void cmd_clear_db(CommandLine cmd) throws Exception {
294 Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
295 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
296 String dbPrefix = getOptionOrDefault(cmd, "p", DEFAULT_DBPREFIX);
297 String username = getOptionOrDefault(cmd, "dbu", null);
298 String password = getOptionOrDefault(cmd, "dbp", null);
299 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
300 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
301 username, password, trustAll);
302 if (!service.clearDatabase(r, dbPrefix)) {
303 throw new Exception("failed to init database");
309 * @throws java.text.ParseException
312 private static void cmd_init_db(CommandLine cmd) throws Exception {
313 Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
314 int numShards = getOptionOrDefault(cmd, OPTION_SHARDS_SHORT, DEFAULT_SHARDS);
315 int numReplicas = getOptionOrDefault(cmd, OPTION_REPLICAS_SHORT, DEFAULT_REPLICAS);
316 String dbUrl = getOptionOrDefault(cmd, "db", DEFAULT_DBURL);
317 String dbPrefix = getOptionOrDefault(cmd, "p", DEFAULT_DBPREFIX);
318 String username = getOptionOrDefault(cmd, "dbu", null);
319 String password = getOptionOrDefault(cmd, "dbp", null);
320 boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
321 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
322 username, password, trustAll);
323 boolean forceRecreate = cmd.hasOption(OPTION_FORCE_RECREATE_SHORT);
324 if (!service.initDatabase(r, numShards, numReplicas, dbPrefix, forceRecreate)) {
325 throw new Exception("failed to init database");
333 private static Options init() {
334 Options options = new Options();
335 options.addOption(createOption("c", "cmd", true, "command to execute", true));
336 options.addOption(createOption("db", "dburl", true, "database url", false));
337 options.addOption(createOption("dbu", "db-username", true, "database basic auth username", false));
338 options.addOption(createOption("dbp", "db-password", true, "database basic auth password", false));
339 options.addOption(createOption(OPTION_REPLICAS_SHORT, "replicas", true, "amount of replicas", false));
340 options.addOption(createOption(OPTION_SHARDS_SHORT, "shards", true, "amount of shards", false));
341 options.addOption(createOption("p", "prefix", true, "prefix for db indices", false));
342 options.addOption(createOption(OPTION_VERSION_SHORT, "version", true, "version", false));
343 options.addOption(createOption(OPTION_DEBUG_SHORT, "verbose", false, "verbose mode", false));
344 options.addOption(createOption(OPTION_TRUSTINSECURESSL_SHORT, "trust-insecure", false,
345 "trust insecure ssl certs", false));
346 options.addOption(createOption("w", "wait", true, "wait delay for yellow status", false));
348 createOption(OPTION_FORCE_RECREATE_SHORT, "force-recreate", false, "delete if sth exists", false));
349 options.addOption(createOption(OPTION_SILENT_SHORT, OPTION_SILENT, false, "prevent console output", false));
350 options.addOption(createOption(OPTION_OUTPUTFILE_SHORT, "output-file", true, "file to write into", false));
351 options.addOption(createOption(OPTION_INPUTFILE_SHORT, "input-file", true, "file to read from", false));
364 private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
366 Option o = new Option(opt, longOpt, hasArg, description);
367 o.setRequired(required);