Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / commands / CommandsHolder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.tools.commands;
22
23 import static org.openecomp.core.tools.commands.Command.COMMAND_OPTION;
24 import static org.openecomp.core.tools.util.Utils.printMessage;
25
26 import java.util.EnumMap;
27 import java.util.Map;
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;
38
39 public class CommandsHolder {
40
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);
44
45     static {
46         OPTIONS.addOption(
47                 Option.builder(COMMAND_OPTION).hasArg().argName("command").desc("command name, mandatory").build());
48         registerCommands();
49     }
50     
51     private CommandsHolder() {
52     }
53
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();
64     }
65
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
69                        ? Optional.empty()
70                        : getCommandName(cmd.getOptionValue(COMMAND_OPTION)).map(COMMANDS::get);
71     }
72
73     public static void printUsages() {
74         COMMANDS.values().forEach(Command::printUsage);
75     }
76
77     private static Optional<CommandName> getCommandName(String commandName) {
78         try {
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();
83         }
84     }
85
86     private static CommandLine parseArgs(String[] args) {
87         try {
88             return new DefaultParser().parse(OPTIONS, args, true);
89         } catch (ParseException e) {
90             LOGGER.error("Error parsing arguments", e);
91             return null;
92         }
93     }
94
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));
100         }
101         COMMANDS.put(commandName, command);
102     }
103 }