23a64f2523d926e00fc5841ed7c3bcd71fe178b7
[ccsdk/features.git] /
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,
150                                         getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false) ? Level.DEBUG : Level.INFO);
151                 } catch (ParseException e2) {
152
153                 }
154                 switch (cmd.getOptionValue("c")) {
155                 case CMD_INITDB:
156                         try {
157                                 cmd_init_db(cmd);
158                         } catch (Exception e1) {
159                                 exit(e1);
160                         }
161                         break;
162                 case CMD_CLEAR_DB:
163                         try {
164                                 cmd_clear_db(cmd);
165                         } catch (Exception e1) {
166                                 exit(e1);
167                         }
168                         break;
169                 case CMD_CREATE_PLUGIN_INIT_FILE:
170                         try {
171                                 String of = getOptionOrDefault(cmd, "of", null);
172                                 if (of == null) {
173                                         throw new Exception("please add the parameter output-file");
174                                 }
175                                 MavenDatabasePluginInitFile.create(Release.CURRENT_RELEASE, of);
176                         } catch (Exception e) {
177                                 exit(e);
178                         }
179                         break;
180                 case CMD_IMPORT:
181                         try {
182                                 cmd_dbimport(cmd);
183                         } catch (Exception e1) {
184                                 exit(e1);
185                         }
186                         break;
187                 case CMD_EXPORT:
188                         try {
189                                 cmd_dbexport(cmd);
190                         } catch (Exception e) {
191                                 exit(e);
192                         }
193                         break;
194                 case CMD_LIST_VERSION:
195                         cmd_listversion();
196                         break;
197                 default:
198                         printHelp(formatter);
199                         break;
200                 }
201                 System.exit(0);
202         }
203
204         /**
205          * @param formatter
206          */
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]));
212                 }
213         }
214
215         /**
216          * 
217          */
218         private static void cmd_listversion() {
219
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()) {
224
225                         System.out.println(String.format(format, r.getValue(),
226                                         r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
227                 }
228
229         }
230
231         /**
232          * @throws Exception 
233          * 
234          */
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");
243                 }
244                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
245                                 username, password, trustAll);
246                 DataMigrationReport report = service.importData(filename, false);
247                 LOG.info(report);
248                 if(!report.completed()) {
249                         throw new Exception("db import seems to be not executed completed");
250                 }
251         }
252
253         /**
254          * @throws Exception 
255          * 
256          */
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");
265                 }
266                 DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
267                                 username, password, trustAll);
268                 DataMigrationReport report = service.exportData(filename);
269                 LOG.info(report);
270                 if(!report.completed()) {
271                         throw new Exception("db export seems to be not executed completed");
272                 }
273
274         }
275
276         /**
277          * @param e
278          */
279         private static void exit(Exception e) {
280                 if (LOG != null) {
281                         LOG.error("Error during execution: {}", e);
282                 } else {
283                         System.err.println(e);
284                 }
285                 System.exit(1);
286         }
287
288         /**
289          * @param cmd
290          * @throws java.text.ParseException 
291          * @throws Exception 
292          */
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");
304                 }
305         }
306
307         /**
308          * @param cmd
309          * @throws java.text.ParseException 
310          * @throws Exception 
311          */
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");
326                 }
327
328         }
329
330         /**
331          * @return
332          */
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));
347                 options.addOption(
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));
352
353                 return options;
354         }
355
356         /**
357          * @param opt 
358          * @param longOpt 
359          * @param hasArg 
360          * @param description 
361          * @param required 
362          * @return
363          */
364         private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
365                         boolean required) {
366                 Option o = new Option(opt, longOpt, hasArg, description);
367                 o.setRequired(required);
368                 return o;
369         }
370 }