aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/Program.java
blob: d06ffe1ee3d672f01577c4a30024f7d86f5b22bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.cli.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MavenDatabasePluginInitFile;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;

/**
 * @author Michael Dürre
 *
 */
public class Program {

	// constants
    private static final String CMD_INITDB = "init";
    private static final String CMD_CLEAR_DB = "delete";
    private static final String CMD_CLEAR_DB_COMPLETE = "clear";
    private static final String CMD_CREATE_PLUGIN_INIT_FILE = "pluginfile";
    private static final String CMD_IMPORT = "import";
    private static final String CMD_EXPORT = "export";
    private static final String CMD_LIST_VERSION = "list";

    private static final String CMD_INITDB_DESCRIPTION = "initialize databse indices and aliases";
    private static final String CMD_CLEAR_DB_DESCRIPTION = "clear database indices and aliases";
    private static final String CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION = "create maven plugin file";
    private static final String CMD_IMPORT_DESCRIPTION = "import data into database";
    private static final String CMD_EXPORT_DESCRIPTION = "export data from database";
    private static final String CMD_LIST_VERSION_DESCRIPTION = "list release versions";

    private static final List<String[]> commands = Arrays.asList(new String[] { CMD_INITDB, CMD_INITDB_DESCRIPTION },
            new String[] { CMD_CLEAR_DB, CMD_CLEAR_DB_DESCRIPTION },
            new String[] { CMD_CREATE_PLUGIN_INIT_FILE, CMD_CREATE_PLUGIN_INIT_FILE_DESCRIPTION },
            new String[] { CMD_IMPORT, CMD_IMPORT_DESCRIPTION }, new String[] { CMD_EXPORT, CMD_EXPORT_DESCRIPTION },
            new String[] { CMD_LIST_VERSION, CMD_LIST_VERSION_DESCRIPTION });
    private static final String APPLICATION_NAME = "SDNR DataMigrationTool";
    private static final int DEFAULT_SHARDS = 5;
    private static final int DEFAULT_REPLICAS = 1;
    private static final String DEFAULT_DBURL = "http://sdnrdb:9200";
    private static final String DEFAULT_DBPREFIX = "";
    private static final String OPTION_FORCE_RECREATE_SHORT = "f";
    private static final String OPTION_SILENT_SHORT = "n";
    private static final String OPTION_SILENT = "silent";
    private static final String OPTION_VERSION_SHORT = "v";
    private static final String OPTION_SHARDS_SHORT = "s";
    private static final String OPTION_REPLICAS_SHORT = "r";
    private static final String OPTION_OUTPUTFILE_SHORT = "of";
    private static final String OPTION_INPUTFILE_SHORT = "if";
    private static final String OPTION_DEBUG_SHORT = "x";
    private static final String OPTION_TRUSTINSECURESSL_SHORT = "k";
    private static final String OPTION_DATABASE_SHORT = "db";
    private static final String OPTION_COMMAND_SHORT = "c";
	private static final String OPTION_DATABASEUSER_SHORT = "dbu";
	private static final String OPTION_DATABASEPASSWORD_SHORT = "dbp";
	private static final String OPTION_DATABASEPREFIX_SHORT = "p";
	private static final String OPTION_DATABASEWAIT_SHORT = "w";
	private static final String OPTION_HELP_SHORT = "h";
    // end of constants
	
	// variables
    private static Options options = init();
    private static Log LOG = null;
    // end of variables
    
    // public methods
    public static void main(String[] args) {
        System.exit( main2(args) );
    }
    // end of public methods
    
    // private methods
    @SuppressWarnings("unchecked")
    private static <T> T getOptionOrDefault(CommandLine cmd, String option, T def) throws ParseException {
        if (def instanceof Boolean) {
            return cmd.hasOption(option) ? (T) Boolean.TRUE : def;
        }
        if (def instanceof Integer) {
            return cmd.hasOption(option) ? (T) Integer.valueOf(cmd.getOptionValue(option)) : def;
        }
        if (def instanceof Long) {
            return cmd.hasOption(option) ? (T) Long.valueOf(cmd.getOptionValue(option)) : def;
        }
        if (cmd.hasOption(option) && cmd.getOptionValue(option) != null) {
            if (option.equals(OPTION_VERSION_SHORT)) {
                String v = cmd.getOptionValue(option);
                return (T) Release.getValueBySuffix(v.startsWith("-") ? v : "-" + v);
            } else {
                return (T) cmd.getParsedOptionValue(option);
            }
        }
        return def;
    }

    private static void initLog(boolean silent, String logfile, Level loglvl) {
        Logger.getRootLogger().getLoggerRepository().resetConfiguration();
        LOG = LogFactory.getLog(Program.class);
        if (!silent) {
            ConsoleAppender console = new ConsoleAppender(); // create appender
            // configure the appender
            String PATTERN = "%d [%p|%C{1}] %m%n";
            console.setLayout(new PatternLayout(PATTERN));
            console.setThreshold(loglvl);
            console.activateOptions();
            // add appender to any Logger (here is root)
            Logger.getRootLogger().addAppender(console);
        }
        if (logfile != null) {
            RollingFileAppender fa = new RollingFileAppender();
            fa.setName("FileLogger");
            fa.setFile(logfile);
            fa.setLayout(new PatternLayout("%d %-5p [%c] %m%n"));
            fa.setThreshold(loglvl);
            fa.setMaximumFileSize(10000000);
            fa.setAppend(true);
            fa.setMaxBackupIndex(5);
            fa.activateOptions();
            // add appender to any Logger (here is root)
            Logger.getRootLogger().addAppender(fa);
        }
        // repeat with all other desired appenders
    }

    private static int main2(String[] args) {

        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter = new HelpFormatter();
        CommandLine cmd = null;
       
        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            printHelp(formatter);
            return 1;
        }
        if (cmd == null) {
            printHelp(formatter);
            return 1;
        }
        try {
            initLog(getOptionOrDefault(cmd, OPTION_SILENT_SHORT, false), null,
                    getOptionOrDefault(cmd, OPTION_DEBUG_SHORT, false) ? Level.DEBUG : Level.INFO);
        } catch (ParseException e2) {

        }
        try {
			if(getOptionOrDefault(cmd, OPTION_HELP_SHORT, false)) {
			    printHelp(formatter);
			    return 0;
			}
		} catch (ParseException e2) {
			return exit(e2);
		}
        final String command = cmd.getOptionValue(OPTION_COMMAND_SHORT);
        if(command==null) {
        	printHelp(formatter);
            return 1;
        }
        switch (command) {
        case CMD_INITDB:
            try {
                cmd_init_db(cmd);
            } catch (Exception e1) {
                return exit(e1);
            }
            break;
        case CMD_CLEAR_DB:
            try {
                cmd_clear_db(cmd);
            } catch (Exception e1) {
                return exit(e1);
            }
            break;
        case CMD_CLEAR_DB_COMPLETE:
        	try {
                cmd_clear_db_complete(cmd);
            } catch (Exception e1) {
                return exit(e1);
            }
            break;
        case CMD_CREATE_PLUGIN_INIT_FILE:
            try {
                String of = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
                if (of == null) {
                    throw new Exception("please add the parameter output-file");
                }
                MavenDatabasePluginInitFile.create(Release.CURRENT_RELEASE, of);
            } catch (Exception e) {
                return exit(e);
            }
            break;
        case CMD_IMPORT:
            try {
                cmd_dbimport(cmd);
            } catch (Exception e1) {
                return exit(e1);
            }
            break;
        case CMD_EXPORT:
            try {
                cmd_dbexport(cmd);
            } catch (Exception e) {
                return exit(e);
            }
            break;
        case CMD_LIST_VERSION:
            cmd_listversion();
            break;

        default:
            printHelp(formatter);
            return 1;
        }
        return 0;
    }

    private static void printHelp(HelpFormatter formatter) {
        formatter.printHelp(APPLICATION_NAME, options);
        System.out.println("\nCommands:");
        for (String[] c : commands) {
            System.out.println(String.format("%10s\t%s", c[0], c[1]));
        }
    }

    private static void cmd_listversion() {

        System.out.println("Database Releases:");
        final String format = "%15s\t%8s";
        System.out.println(String.format(format, "Name", "Version"));
        for (Release r : Release.values()) {

            System.out.println(String.format(format, r.getValue(),
                    r.getDBSuffix() != null && r.getDBSuffix().length() > 1 ? r.getDBSuffix().substring(1) : ""));
        }

    }

   private static void cmd_dbimport(CommandLine cmd) throws Exception {
        String dbUrl = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL);
        String username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
        String password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null);
        String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
        boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
        if (filename == null) {
            throw new Exception("please add output file parameter");
        }
        long timeoutms = getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, 30)*1000;
        DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
                username, password, trustAll, timeoutms);
        DataMigrationReport report = service.importData(filename, false);
        LOG.info(report);
        if(!report.completed()) {
            throw new Exception("db import seems to be not executed completed");
        }
    }

    private static void cmd_dbexport(CommandLine cmd) throws Exception {
        String dbUrl = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL);
        String username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
        String password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null);
        String filename = getOptionOrDefault(cmd, OPTION_OUTPUTFILE_SHORT, null);
        boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
        if (filename == null) {
            throw new Exception("please add output file parameter");
        }
        long timeoutms = getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, 30)*1000;
        DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
                username, password, trustAll, timeoutms);
        DataMigrationReport report = service.exportData(filename);
        LOG.info(report);
        if(!report.completed()) {
            throw new Exception("db export seems to be not executed completed");
        }

    }

    private static int exit(Exception e) {
        if (LOG != null) {
            LOG.error("Error during execution: {}", e);
        } else {
            System.err.println(e);
        }
        return 1;
    }

    private static void cmd_clear_db(CommandLine cmd) throws Exception {
        Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
        String dbUrl = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL);
        String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX);
        String username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
        String password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null);
        boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
        long timeoutms = getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, 30)*1000;
        DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
                username, password, trustAll, timeoutms);
        if (!service.clearDatabase(r, dbPrefix,timeoutms)) {
            throw new Exception("failed to init database");
        }
    }
    private static void cmd_clear_db_complete(CommandLine cmd) throws Exception {
        String dbUrl = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL);
        String username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
        String password = getOptionOrDefault(cmd, OPTION_DATABASEPASSWORD_SHORT, null);
        boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
        long timeoutms = getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, 30)*1000;
        DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
                username, password, trustAll, timeoutms);
        if (!service.clearCompleteDatabase(timeoutms)) {
        	throw new Exception("failed to init database");
        }
    }

    private static void cmd_init_db(CommandLine cmd) throws Exception {
        Release r = getOptionOrDefault(cmd, OPTION_VERSION_SHORT, Release.CURRENT_RELEASE);
        int numShards = getOptionOrDefault(cmd, OPTION_SHARDS_SHORT, DEFAULT_SHARDS);
        int numReplicas = getOptionOrDefault(cmd, OPTION_REPLICAS_SHORT, DEFAULT_REPLICAS);
        String dbUrl = getOptionOrDefault(cmd, OPTION_DATABASE_SHORT, DEFAULT_DBURL);
        String dbPrefix = getOptionOrDefault(cmd, OPTION_DATABASEPREFIX_SHORT, DEFAULT_DBPREFIX);
        String username = getOptionOrDefault(cmd, OPTION_DATABASEUSER_SHORT, null);
        String password = getOptionOrDefault(cmd,OPTION_DATABASEPASSWORD_SHORT, null);
        boolean trustAll = getOptionOrDefault(cmd, OPTION_TRUSTINSECURESSL_SHORT, false);
        long timeoutms = getOptionOrDefault(cmd, OPTION_DATABASEWAIT_SHORT, 30)*1000;
        DataMigrationProviderImpl service = new DataMigrationProviderImpl(new HostInfo[] { HostInfo.parse(dbUrl) },
                username, password, trustAll, timeoutms);
        boolean forceRecreate = cmd.hasOption(OPTION_FORCE_RECREATE_SHORT);
        if (!service.initDatabase(r, numShards, numReplicas, dbPrefix, forceRecreate,timeoutms)) {
            throw new Exception("failed to init database");
        }

    }

    private static Options init() {
        Options result = new Options();
        result.addOption(createOption(OPTION_COMMAND_SHORT, "cmd", true, "command to execute", false));
        result.addOption(createOption(OPTION_DATABASE_SHORT, "dburl", true, "database url", false));
        result.addOption(createOption(OPTION_DATABASEUSER_SHORT, "db-username", true, "database basic auth username", false));
        result.addOption(createOption(OPTION_DATABASEPASSWORD_SHORT, "db-password", true, "database basic auth password", false));
        result.addOption(createOption(OPTION_REPLICAS_SHORT, "replicas", true, "amount of replicas", false));
        result.addOption(createOption(OPTION_SHARDS_SHORT, "shards", true, "amount of shards", false));
        result.addOption(createOption(OPTION_DATABASEPREFIX_SHORT, "prefix", true, "prefix for db indices", false));
        result.addOption(createOption(OPTION_VERSION_SHORT, "version", true, "version", false));
        result.addOption(createOption(OPTION_DEBUG_SHORT, "verbose", false, "verbose mode", false));
        result.addOption(createOption(OPTION_TRUSTINSECURESSL_SHORT, "trust-insecure", false,
                "trust insecure ssl certs", false));
        result.addOption(createOption(OPTION_DATABASEWAIT_SHORT, "wait", true, "wait for yellow status with timeout in seconds", false));
        result.addOption(
                createOption(OPTION_FORCE_RECREATE_SHORT, "force-recreate", false, "delete if sth exists", false));
        result.addOption(createOption(OPTION_SILENT_SHORT, OPTION_SILENT, false, "prevent console output", false));
        result.addOption(createOption(OPTION_OUTPUTFILE_SHORT, "output-file", true, "file to write into", false));
        result.addOption(createOption(OPTION_INPUTFILE_SHORT, "input-file", true, "file to read from", false));
        result.addOption(createOption(OPTION_HELP_SHORT,"help",false,"show help",false));
        return result;
    }

    /**
     * @param opt
     * @param longOpt
     * @param hasArg
     * @param description
     * @param required
     * @return
     */
    private static Option createOption(String opt, String longOpt, boolean hasArg, String description,
            boolean required) {
        Option o = new Option(opt, longOpt, hasArg, description);
        o.setRequired(required);
        return o;
    }
    // end of private methods
}