2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.dmaap.startstop;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import java.util.Arrays;
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.DefaultParser;
31 import org.apache.commons.cli.HelpFormatter;
32 import org.apache.commons.cli.Option;
33 import org.apache.commons.cli.Options;
34 import org.apache.commons.cli.ParseException;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.sim.dmaap.DmaapSimException;
37 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
41 * This class reads and handles command line parameters for the DMaaP simulator service.
43 public class DmaapSimCommandLineArguments {
44 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
45 private static final int HELP_LINE_LENGTH = 120;
47 private final Options options;
48 private String configurationFilePath = null;
51 * Construct the options for the CLI editor.
53 public DmaapSimCommandLineArguments() {
55 options = new Options();
56 options.addOption(Option.builder("h")
58 .desc("outputs the usage of this command")
62 options.addOption(Option.builder("v")
64 .desc("outputs the version of DMaaP simulator")
68 options.addOption(Option.builder("c")
69 .longOpt("config-file")
70 .desc("the full path to the configuration file to use, "
71 + "the configuration file must be a Json file containing the DMaaP simulator parameters")
73 .argName("CONFIG_FILE")
81 * Construct the options for the CLI editor and parse in the given arguments.
83 * @param args The command line arguments
85 public DmaapSimCommandLineArguments(final String[] args) {
86 // Set up the options with the default constructor
89 // Parse the arguments
92 } catch (final DmaapSimException e) {
93 throw new DmaapSimRuntimeException("parse error on DMaaP simulator parameters", e);
98 * Parse the command line options.
100 * @param args The command line arguments
101 * @return a string with a message for help and version, or null if there is no message
102 * @throws DmaapSimException on command argument errors
104 public String parse(final String[] args) throws DmaapSimException {
105 // Clear all our arguments
106 setConfigurationFilePath(null);
108 CommandLine commandLine = null;
110 commandLine = new DefaultParser().parse(options, args);
111 } catch (final ParseException e) {
112 throw new DmaapSimException("invalid command line arguments specified : " + e.getMessage());
115 // Arguments left over after Commons CLI does its stuff
116 final String[] remainingArgs = commandLine.getArgs();
118 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 0) {
119 throw new DmaapSimException("too many command line arguments specified : " + Arrays.toString(args));
122 if (remainingArgs.length == 1) {
123 configurationFilePath = remainingArgs[0];
126 if (commandLine.hasOption('h')) {
127 return help(Main.class.getCanonicalName());
130 if (commandLine.hasOption('v')) {
134 if (commandLine.hasOption('c')) {
135 setConfigurationFilePath(commandLine.getOptionValue('c'));
142 * Validate the command line options.
144 * @throws DmaapSimException on command argument validation errors
146 public void validate() throws DmaapSimException {
147 validateReadableFile("DMaaP simulator configuration", configurationFilePath);
151 * Print version information for DMaaP simulator.
153 * @return the version string
155 public String version() {
156 return ResourceUtils.getResourceAsString("version.txt");
160 * Print help information for DMaaP simulator.
162 * @param mainClassName the main class name
163 * @return the help string
165 public String help(final String mainClassName) {
166 final HelpFormatter helpFormatter = new HelpFormatter();
167 final StringWriter stringWriter = new StringWriter();
168 final PrintWriter printWriter = new PrintWriter(stringWriter);
170 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
173 return stringWriter.toString();
177 * Gets the configuration file path.
179 * @return the configuration file path
181 public String getConfigurationFilePath() {
182 return configurationFilePath;
186 * Gets the full expanded configuration file path.
188 * @return the configuration file path
190 public String getFullConfigurationFilePath() {
191 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
195 * Sets the configuration file path.
197 * @param configurationFilePath the configuration file path
199 public void setConfigurationFilePath(final String configurationFilePath) {
200 this.configurationFilePath = configurationFilePath;
205 * Check set configuration file path.
207 * @return true, if check set configuration file path
209 public boolean checkSetConfigurationFilePath() {
210 return configurationFilePath != null && configurationFilePath.length() > 0;
214 * Validate readable file.
216 * @param fileTag the file tag
217 * @param fileName the file name
218 * @throws DmaapSimException on the file name passed as a parameter
220 private void validateReadableFile(final String fileTag, final String fileName) throws DmaapSimException {
221 if (fileName == null || fileName.length() == 0) {
222 throw new DmaapSimException(fileTag + " file was not specified as an argument");
225 // The file name refers to a resource on the local file system
226 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
227 if (fileUrl == null) {
228 throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
231 final File theFile = new File(fileUrl.getPath());
232 if (!theFile.exists()) {
233 throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
235 if (!theFile.isFile()) {
236 throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
238 if (!theFile.canRead()) {
239 throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");