2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.tools.commands;
23 import static org.openecomp.core.tools.commands.Command.COMMAND_OPTION;
24 import static org.openecomp.core.tools.util.Utils.printMessage;
26 import java.util.EnumMap;
28 import java.util.Optional;
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.DefaultParser;
31 import org.apache.commons.cli.Option;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
35 import org.openecomp.core.tools.importinfo.ImportDataCommand;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
39 public class CommandsHolder {
41 private static final Logger LOGGER = LoggerFactory.getLogger(CommandsHolder.class);
42 private static final Options OPTIONS = new Options();
43 private static final Map<CommandName, Command> COMMANDS = new EnumMap<>(CommandName.class);
47 Option.builder(COMMAND_OPTION).hasArg().argName("command").desc("command name, mandatory").build());
51 private CommandsHolder() {
54 private static void registerCommands() {
55 new SetHealingFlag().register();
56 new ExportDataCommand().register();
57 new ImportDataCommand().register();
58 new HealAll().register();
59 new PopulateUserPermissions().register();
60 new AddContributorCommand().register();
61 new CleanUserDataCommand().register();
62 new DeletePublicVersionCommand().register();
63 new SetHealingFlagByItemVersionCommand().register();
66 public static Optional<Command> getCommand(String[] args) {
67 CommandLine cmd = parseArgs(args);
68 return cmd == null || !cmd.hasOption(COMMAND_OPTION) || cmd.getOptionValue(COMMAND_OPTION) == null
70 : getCommandName(cmd.getOptionValue(COMMAND_OPTION)).map(COMMANDS::get);
73 public static void printUsages() {
74 COMMANDS.values().forEach(Command::printUsage);
77 private static Optional<CommandName> getCommandName(String commandName) {
79 return Optional.of(CommandName.valueOf(commandName));
80 } catch (IllegalArgumentException iae) {
81 printMessage(LOGGER, String.format("message: %s is illegal command.", commandName));
82 return Optional.empty();
86 private static CommandLine parseArgs(String[] args) {
88 return new DefaultParser().parse(OPTIONS, args, true);
89 } catch (ParseException e) {
90 LOGGER.error("Error parsing arguments", e);
95 static void addCommand(Command command) {
96 CommandName commandName = command.getCommandName();
97 if (COMMANDS.containsKey(commandName)) {
98 throw new IllegalArgumentException(
99 String.format("Command with the name %s was already registered", commandName));
101 COMMANDS.put(commandName, command);