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.pdp;
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.apache.log4j.chainsaw.Main;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
39 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorRunTimeException;
42 * This class reads and handles command line parameters for the pdp simulator.
44 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
46 public class PdpSimulatorCommandLineArguments {
48 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
49 private static final int HELP_LINE_LENGTH = 120;
51 private final Options options;
52 private String configurationFilePath = null;
53 private String propertyFilePath = null;
56 * Construct the options for the CLI editor.
58 public PdpSimulatorCommandLineArguments() {
60 options = new Options();
61 options.addOption(Option.builder("h")
63 .desc("outputs the usage of this command")
67 options.addOption(Option.builder("v")
69 .desc("outputs the version of pdp simulator")
73 options.addOption(Option.builder("c")
74 .longOpt("config-file")
75 .desc("the full path to the configuration file to use, "
76 + "the configuration file must be a Json file containing the pdp simulator parameters")
78 .argName("CONFIG_FILE")
82 options.addOption(Option.builder("p")
83 .longOpt("property-file")
84 .desc("the full path to the topic property file to use, "
85 + "the property file contains the pdp simulator topic properties")
95 * Construct the options for the CLI editor and parse in the given arguments.
97 * @param args The command line arguments
99 public PdpSimulatorCommandLineArguments(final String[] args) {
100 // Set up the options with the default constructor
103 // Parse the arguments
106 } catch (final PdpSimulatorException e) {
107 throw new PdpSimulatorRunTimeException("parse error on pdp simulator parameters", e);
112 * Parse the command line options.
114 * @param args The command line arguments
115 * @return a string with a message for help and version, or null if there is no message
116 * @throws PdpSimulatorException on command argument errors
118 public String parse(final String[] args) throws PdpSimulatorException {
119 // Clear all our arguments
120 setConfigurationFilePath(null);
121 setPropertyFilePath(null);
123 CommandLine commandLine = null;
125 commandLine = new DefaultParser().parse(options, args);
126 } catch (final ParseException e) {
127 throw new PdpSimulatorException("invalid command line arguments specified : " + e.getMessage());
130 // Arguments left over after Commons CLI does its stuff
131 final String[] remainingArgs = commandLine.getArgs();
133 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 0) {
134 throw new PdpSimulatorException("too many command line arguments specified : " + Arrays.toString(args));
137 if (remainingArgs.length == 1) {
138 configurationFilePath = remainingArgs[0];
141 if (commandLine.hasOption('h')) {
142 return help(Main.class.getCanonicalName());
145 if (commandLine.hasOption('v')) {
149 if (commandLine.hasOption('c')) {
150 setConfigurationFilePath(commandLine.getOptionValue('c'));
153 if (commandLine.hasOption('p')) {
154 setPropertyFilePath(commandLine.getOptionValue('p'));
161 * Validate the command line options.
163 * @throws PdpSimulatorException on command argument validation errors
165 public void validate() throws PdpSimulatorException {
166 validateReadableFile("pdp simulator configuration", configurationFilePath);
167 validateReadableFile("pdp simulator properties", propertyFilePath);
171 * Print version information for pdp simulator.
173 * @return the version string
175 public String version() {
176 return ResourceUtils.getResourceAsString("src/main/resources/version.txt");
180 * Print help information for pdp simulator.
182 * @param mainClassName the main class name
183 * @return the help string
185 public String help(final String mainClassName) {
186 final HelpFormatter helpFormatter = new HelpFormatter();
187 final StringWriter stringWriter = new StringWriter();
188 final PrintWriter printWriter = new PrintWriter(stringWriter);
190 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
193 return stringWriter.toString();
197 * Gets the configuration file path.
199 * @return the configuration file path
201 public String getConfigurationFilePath() {
202 return configurationFilePath;
206 * Gets the full expanded configuration file path.
208 * @return the configuration file path
210 public String getFullConfigurationFilePath() {
211 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
215 * Sets the configuration file path.
217 * @param configurationFilePath the configuration file path
219 public void setConfigurationFilePath(final String configurationFilePath) {
220 this.configurationFilePath = configurationFilePath;
225 * Check set configuration file path.
227 * @return true, if check set configuration file path
229 public boolean checkSetConfigurationFilePath() {
230 return configurationFilePath != null && configurationFilePath.length() > 0;
234 * Gets the property file path.
236 * @return the property file path
238 public String getPropertyFilePath() {
239 return propertyFilePath;
243 * Gets the full expanded property file path.
245 * @return the property file path
247 public String getFullPropertyFilePath() {
248 return ResourceUtils.getFilePath4Resource(getPropertyFilePath());
252 * Sets the property file path.
254 * @param propertyFilePath the property file path
256 public void setPropertyFilePath(final String propertyFilePath) {
257 this.propertyFilePath = propertyFilePath;
262 * Check set property file path.
264 * @return true, if check set property file path
266 public boolean checkSetPropertyFilePath() {
267 return propertyFilePath != null && propertyFilePath.length() > 0;
271 * Validate readable file.
273 * @param fileTag the file tag
274 * @param fileName the file name
275 * @throws PdpSimulatorException on the file name passed as a parameter
277 private void validateReadableFile(final String fileTag, final String fileName) throws PdpSimulatorException {
278 if (fileName == null || fileName.length() == 0) {
279 throw new PdpSimulatorException(fileTag + " file was not specified as an argument");
282 // The file name refers to a resource on the local file system
283 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
284 if (fileUrl == null) {
285 throw new PdpSimulatorException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
288 final File theFile = new File(fileUrl.getPath());
289 if (!theFile.exists()) {
290 throw new PdpSimulatorException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
292 if (!theFile.isFile()) {
293 throw new PdpSimulatorException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
295 if (!theFile.canRead()) {
296 throw new PdpSimulatorException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");