b6cc0487eaa6f8a70d0c067ca35d881c314a176b
[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     }
39
40     private CommandsHolder() {
41     }
42
43     public static Optional<Command> getCommand(String[] args) {
44         CommandLine cmd = parseArgs(args);
45         return cmd == null || !cmd.hasOption(COMMAND_OPTION) || cmd.getOptionValue(COMMAND_OPTION) == null
46                        ? Optional.empty()
47                        : getCommandName(cmd.getOptionValue(COMMAND_OPTION)).map(COMMANDS::get);
48     }
49
50     public static void printUsages() {
51         COMMANDS.values().forEach(Command::printUsage);
52     }
53
54     private static Optional<CommandName> getCommandName(String commandName) {
55         try {
56             return Optional.of(CommandName.valueOf(commandName));
57         } catch (IllegalArgumentException iae) {
58             printMessage(LOGGER, String.format("message: %s is illegal command.", commandName));
59             return Optional.empty();
60         }
61     }
62
63     private static CommandLine parseArgs(String[] args) {
64         try {
65             return new DefaultParser().parse(OPTIONS, args, true);
66         } catch (ParseException e) {
67             LOGGER.error("Error parsing arguments", e);
68             return null;
69         }
70     }
71
72     static void addCommand(Command command) {
73         CommandName commandName = command.getCommandName();
74         if (COMMANDS.containsKey(commandName)) {
75             throw new IllegalArgumentException(
76                     String.format("Command with the name %s was already registered", commandName));
77         }
78         COMMANDS.put(commandName, command);
79     }
80 }