dc237b5ccc45c0f56ae788fbfbeaf48bf99adf29
[sdc.git] /
1 package org.openecomp.core.tools.commands;
2
3 import static org.openecomp.core.tools.commands.Command.COMMAND_OPTION;
4 import static org.openecomp.core.tools.util.Utils.printMessage;
5
6 import java.util.EnumMap;
7 import java.util.Map;
8 import java.util.Optional;
9 import org.apache.commons.cli.CommandLine;
10 import org.apache.commons.cli.DefaultParser;
11 import org.apache.commons.cli.Option;
12 import org.apache.commons.cli.Options;
13 import org.apache.commons.cli.ParseException;
14 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
15 import org.openecomp.core.tools.importinfo.ImportDataCommand;
16 import org.openecomp.sdc.logging.api.Logger;
17 import org.openecomp.sdc.logging.api.LoggerFactory;
18
19 public class CommandsHolder {
20
21     private static final Logger LOGGER = LoggerFactory.getLogger(CommandsHolder.class);
22     private static final Options OPTIONS = new Options();
23     private static final Map<CommandName, Command> COMMANDS = new EnumMap<>(CommandName.class);
24
25     static {
26         OPTIONS.addOption(
27                 Option.builder(COMMAND_OPTION).hasArg().argName("command").desc("command name, mandatory").build());
28         registerCommands();
29     }
30
31     private static void registerCommands() {
32         new SetHealingFlag().register();
33         new ExportDataCommand().register();
34         new ImportDataCommand().register();
35         new HealAll().register();
36         new PopulateUserPermissions().register();
37         new AddContributorCommand().register();
38         new CleanUserDataCommand().register();
39         new DeletePublicVersionCommand().register();
40     }
41
42     private CommandsHolder() {
43     }
44
45     public static Optional<Command> getCommand(String[] args) {
46         CommandLine cmd = parseArgs(args);
47         return cmd == null || !cmd.hasOption(COMMAND_OPTION) || cmd.getOptionValue(COMMAND_OPTION) == null
48                        ? Optional.empty()
49                        : getCommandName(cmd.getOptionValue(COMMAND_OPTION)).map(COMMANDS::get);
50     }
51
52     public static void printUsages() {
53         COMMANDS.values().forEach(Command::printUsage);
54     }
55
56     private static Optional<CommandName> getCommandName(String commandName) {
57         try {
58             return Optional.of(CommandName.valueOf(commandName));
59         } catch (IllegalArgumentException iae) {
60             printMessage(LOGGER, String.format("message: %s is illegal command.", commandName));
61             return Optional.empty();
62         }
63     }
64
65     private static CommandLine parseArgs(String[] args) {
66         try {
67             return new DefaultParser().parse(OPTIONS, args, true);
68         } catch (ParseException e) {
69             LOGGER.error("Error parsing arguments", e);
70             return null;
71         }
72     }
73
74     static void addCommand(Command command) {
75         CommandName commandName = command.getCommandName();
76         if (COMMANDS.containsKey(commandName)) {
77             throw new IllegalArgumentException(
78                     String.format("Command with the name %s was already registered", commandName));
79         }
80         COMMANDS.put(commandName, command);
81     }
82 }