2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.service.engine.main;
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.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
37 import org.onap.policy.apex.model.utilities.ResourceUtils;
40 * This class reads and handles command line parameters for the Apex main program.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class ApexCommandLineArguments {
45 private static final int HELP_LINE_LENGTH = 120;
47 // Apache Commons CLI options
48 private final Options options;
50 // The command line options
51 private String modelFilePath = null;
52 private String configurationFilePath = null;
55 * Construct the options for the CLI editor.
57 public ApexCommandLineArguments() {
59 options = new Options();
60 options.addOption(Option.builder("h")
62 .desc("outputs the usage of this command")
66 options.addOption(Option.builder("v")
68 .desc("outputs the version of Apex")
72 options.addOption(Option.builder("c")
73 .longOpt("config-file")
74 .desc("the full path to the configuration file to use, the configuration file must be a Json file containing the Apex configuration parameters")
76 .argName("CONFIG_FILE")
80 options.addOption(Option.builder("m").longOpt("model-file")
81 .desc("the full path to the model file to use, if set it overrides the model file set in the configuration file").hasArg().argName("MODEL_FILE")
83 .type(String.class).build());
88 * Construct the options for the CLI editor and parse in the given arguments.
90 * @param args The command line arguments
92 public ApexCommandLineArguments(final String[] args) {
93 // Set up the options with the default constructor
96 // Parse the arguments
99 } catch (final ApexException e) {
100 throw new ApexRuntimeException("parse error on Apex parameters");
105 * Parse the command line options.
107 * @param args The command line arguments
108 * @return a string with a message for help and version, or null if there is no message
109 * @throws ApexException on command argument errors
111 public String parse(final String[] args) throws ApexException {
112 // Clear all our arguments
113 setConfigurationFilePath(null);
114 setModelFilePath(null);
116 CommandLine commandLine = null;
118 commandLine = new DefaultParser().parse(options, args);
119 } catch (final ParseException e) {
120 throw new ApexException("invalid command line arguments specified : " + e.getMessage());
123 // Arguments left over after Commons CLI does its stuff
124 final String[] remainingArgs = commandLine.getArgs();
126 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 1) {
127 throw new ApexException("too many command line arguments specified : " + Arrays.toString(args));
130 if (remainingArgs.length == 1) {
131 configurationFilePath = remainingArgs[0];
134 if (commandLine.hasOption('h')) {
135 return help(ApexMain.class.getCanonicalName());
138 if (commandLine.hasOption('v')) {
142 if (commandLine.hasOption('c')) {
143 setConfigurationFilePath(commandLine.getOptionValue('c'));
146 if (commandLine.hasOption('m')) {
147 setModelFilePath(commandLine.getOptionValue('m'));
154 * Validate the command line options.
156 * @throws ApexException on command argument validation errors
158 public void validate() throws ApexException {
159 validateReadableFile("Apex configuration", configurationFilePath);
161 if (checkSetModelFilePath()) {
162 validateReadableFile("Apex model", modelFilePath);
167 * Print version information for Apex.
169 * @return the version string
171 public String version() {
172 return ResourceUtils.getResourceAsString("version.txt");
176 * Print help information for Apex.
178 * @param mainClassName the main class name
179 * @return the help string
181 public String help(final String mainClassName) {
182 final HelpFormatter helpFormatter = new HelpFormatter();
183 final StringWriter stringWriter = new StringWriter();
184 final PrintWriter stringPW = new PrintWriter(stringWriter);
186 helpFormatter.printHelp(stringPW, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, 0,
189 return stringWriter.toString();
193 * Gets the model file path.
195 * @return the model file path
197 public String getModelFilePath() {
198 return ResourceUtils.getFilePath4Resource(modelFilePath);
202 * Sets the model file path.
204 * @param modelFilePath the model file path
206 public void setModelFilePath(final String modelFilePath) {
207 this.modelFilePath = modelFilePath;
211 * Check set model file path.
213 * @return true, if check set model file path
215 public boolean checkSetModelFilePath() {
216 return modelFilePath != null && modelFilePath.length() > 0;
220 * Gets the configuration file path.
222 * @return the configuration file path
224 public String getConfigurationFilePath() {
225 return configurationFilePath;
229 * Gets the full expanded configuration file path.
231 * @return the configuration file path
233 public String getFullConfigurationFilePath() {
234 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
238 * Sets the configuration file path.
240 * @param configurationFilePath the configuration file path
242 public void setConfigurationFilePath(final String configurationFilePath) {
243 this.configurationFilePath = configurationFilePath;
248 * Check set configuration file path.
250 * @return true, if check set configuration file path
252 public boolean checkSetConfigurationFilePath() {
253 return configurationFilePath != null && configurationFilePath.length() > 0;
257 * Validate readable file.
259 * @param fileTag the file tag
260 * @param fileName the file name
261 * @throws ApexException the apex exception
263 private void validateReadableFile(final String fileTag, final String fileName) throws ApexException {
264 if (fileName == null || fileName.length() == 0) {
265 throw new ApexException(fileTag + " file was not specified as an argument");
268 // The file name can refer to a resource on the local file system or on the class path
269 final URL fileURL = ResourceUtils.getURL4Resource(fileName);
270 if (fileURL == null) {
271 throw new ApexException(fileTag + " file \"" + fileName + "\" does not exist");
274 final File theFile = new File(fileURL.getPath());
275 if (!theFile.exists()) {
276 throw new ApexException(fileTag + " file \"" + fileName + "\" does not exist");
278 if (!theFile.isFile()) {
279 throw new ApexException(fileTag + " file \"" + fileName + "\" is not a normal file");
281 if (!theFile.canRead()) {
282 throw new ApexException(fileTag + " file \"" + fileName + "\" is ureadable");