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.common.utils.resources.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 "
75 + "containing the Apex configuration parameters")
77 .argName("CONFIG_FILE")
81 options.addOption(Option.builder("m").longOpt("model-file")
82 .desc("the full path to the model file to use, if set it overrides the model file set in the "
83 + "configuration file").hasArg().argName("MODEL_FILE")
85 .type(String.class).build());
90 * Construct the options for the CLI editor and parse in the given arguments.
92 * @param args The command line arguments
94 public ApexCommandLineArguments(final String[] args) {
95 // Set up the options with the default constructor
98 // Parse the arguments
101 } catch (final ApexException e) {
102 throw new ApexRuntimeException("parse error on Apex parameters");
107 * Parse the command line options.
109 * @param args The command line arguments
110 * @return a string with a message for help and version, or null if there is no message
111 * @throws ApexException on command argument errors
113 public String parse(final String[] args) throws ApexException {
114 // Clear all our arguments
115 setConfigurationFilePath(null);
116 setModelFilePath(null);
118 CommandLine commandLine = null;
120 commandLine = new DefaultParser().parse(options, args);
121 } catch (final ParseException e) {
122 throw new ApexException("invalid command line arguments specified : " + e.getMessage());
125 // Arguments left over after Commons CLI does its stuff
126 final String[] remainingArgs = commandLine.getArgs();
128 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 1) {
129 throw new ApexException("too many command line arguments specified : " + Arrays.toString(args));
132 if (remainingArgs.length == 1) {
133 configurationFilePath = remainingArgs[0];
136 if (commandLine.hasOption('h')) {
137 return help(ApexMain.class.getCanonicalName());
140 if (commandLine.hasOption('v')) {
144 if (commandLine.hasOption('c')) {
145 setConfigurationFilePath(commandLine.getOptionValue('c'));
148 if (commandLine.hasOption('m')) {
149 setModelFilePath(commandLine.getOptionValue('m'));
156 * Validate the command line options.
158 * @throws ApexException on command argument validation errors
160 public void validate() throws ApexException {
161 validateReadableFile("Apex configuration", configurationFilePath);
163 if (checkSetModelFilePath()) {
164 validateReadableFile("Apex model", modelFilePath);
169 * Print version information for Apex.
171 * @return the version string
173 public String version() {
174 return ResourceUtils.getResourceAsString("version.txt");
178 * Print help information for Apex.
180 * @param mainClassName the main class name
181 * @return the help string
183 public String help(final String mainClassName) {
184 final HelpFormatter helpFormatter = new HelpFormatter();
185 final StringWriter stringWriter = new StringWriter();
186 final PrintWriter stringPW = new PrintWriter(stringWriter);
188 helpFormatter.printHelp(stringPW, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, 0,
191 return stringWriter.toString();
195 * Gets the model file path.
197 * @return the model file path
199 public String getModelFilePath() {
200 return ResourceUtils.getFilePath4Resource(modelFilePath);
204 * Sets the model file path.
206 * @param modelFilePath the model file path
208 public void setModelFilePath(final String modelFilePath) {
209 this.modelFilePath = modelFilePath;
213 * Check set model file path.
215 * @return true, if check set model file path
217 public boolean checkSetModelFilePath() {
218 return modelFilePath != null && modelFilePath.length() > 0;
222 * Gets the configuration file path.
224 * @return the configuration file path
226 public String getConfigurationFilePath() {
227 return configurationFilePath;
231 * Gets the full expanded configuration file path.
233 * @return the configuration file path
235 public String getFullConfigurationFilePath() {
236 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
240 * Sets the configuration file path.
242 * @param configurationFilePath the configuration file path
244 public void setConfigurationFilePath(final String configurationFilePath) {
245 this.configurationFilePath = configurationFilePath;
250 * Check set configuration file path.
252 * @return true, if check set configuration file path
254 public boolean checkSetConfigurationFilePath() {
255 return configurationFilePath != null && configurationFilePath.length() > 0;
259 * Validate readable file.
261 * @param fileTag the file tag
262 * @param fileName the file name
263 * @throws ApexException the apex exception
265 private void validateReadableFile(final String fileTag, final String fileName) throws ApexException {
266 if (fileName == null || fileName.length() == 0) {
267 throw new ApexException(fileTag + " file was not specified as an argument");
270 // The file name can refer to a resource on the local file system or on the class path
271 final URL fileURL = ResourceUtils.getUrl4Resource(fileName);
272 if (fileURL == null) {
273 throw new ApexException(fileTag + " file \"" + fileName + "\" does not exist");
276 final File theFile = new File(fileURL.getPath());
277 if (!theFile.exists()) {
278 throw new ApexException(fileTag + " file \"" + fileName + "\" does not exist");
280 if (!theFile.isFile()) {
281 throw new ApexException(fileTag + " file \"" + fileName + "\" is not a normal file");
283 if (!theFile.canRead()) {
284 throw new ApexException(fileTag + " file \"" + fileName + "\" is ureadable");