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 // Recurring string constants
46 private static final String FILE_PREAMBLE = " file \"";
47 private static final int HELP_LINE_LENGTH = 120;
49 // Apache Commons CLI options
50 private final Options options;
52 // The command line options
53 private String modelFilePath = null;
54 private String configurationFilePath = null;
57 * Construct the options for the CLI editor.
59 public ApexCommandLineArguments() {
61 options = new Options();
62 options.addOption(Option.builder("h")
64 .desc("outputs the usage of this command")
68 options.addOption(Option.builder("v")
70 .desc("outputs the version of Apex")
74 options.addOption(Option.builder("c")
75 .longOpt("config-file")
76 .desc("the full path to the configuration file to use, the configuration file must be a Json file "
77 + "containing the Apex configuration parameters")
79 .argName("CONFIG_FILE")
83 options.addOption(Option.builder("m").longOpt("model-file")
84 .desc("the full path to the model file to use, if set it overrides the model file set in the "
85 + "configuration file").hasArg().argName("MODEL_FILE")
87 .type(String.class).build());
92 * Construct the options for the CLI editor and parse in the given arguments.
94 * @param args The command line arguments
96 public ApexCommandLineArguments(final String[] args) {
97 // Set up the options with the default constructor
100 // Parse the arguments
103 } catch (final ApexException e) {
104 throw new ApexRuntimeException("parse error on Apex parameters");
109 * Parse the command line options.
111 * @param args The command line arguments
112 * @return a string with a message for help and version, or null if there is no message
113 * @throws ApexException on command argument errors
115 public String parse(final String[] args) throws ApexException {
116 // Clear all our arguments
117 setConfigurationFilePath(null);
118 setModelFilePath(null);
120 CommandLine commandLine = null;
122 commandLine = new DefaultParser().parse(options, args);
123 } catch (final ParseException e) {
124 throw new ApexException("invalid command line arguments specified : " + e.getMessage());
127 // Arguments left over after Commons CLI does its stuff
128 final String[] remainingArgs = commandLine.getArgs();
130 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 1) {
131 throw new ApexException("too many command line arguments specified : " + Arrays.toString(args));
134 if (remainingArgs.length == 1) {
135 configurationFilePath = remainingArgs[0];
138 if (commandLine.hasOption('h')) {
139 return help(ApexMain.class.getCanonicalName());
142 if (commandLine.hasOption('v')) {
146 if (commandLine.hasOption('c')) {
147 setConfigurationFilePath(commandLine.getOptionValue('c'));
150 if (commandLine.hasOption('m')) {
151 setModelFilePath(commandLine.getOptionValue('m'));
158 * Validate the command line options.
160 * @throws ApexException on command argument validation errors
162 public void validate() throws ApexException {
163 validateReadableFile("Apex configuration", configurationFilePath);
165 if (checkSetModelFilePath()) {
166 validateReadableFile("Apex model", modelFilePath);
171 * Print version information for Apex.
173 * @return the version string
175 public String version() {
176 return ResourceUtils.getResourceAsString("version.txt");
180 * Print help information for Apex.
182 * @param mainClassName the main class name
183 * @return the help string
185 public String help(final String mainClassName) {
186 final StringWriter stringWriter = new StringWriter();
187 final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
189 new HelpFormatter().printHelp(stringPrintWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options",
192 return stringWriter.toString();
196 * Gets the model file path.
198 * @return the model file path
200 public String getModelFilePath() {
201 return ResourceUtils.getFilePath4Resource(modelFilePath);
205 * Sets the model file path.
207 * @param modelFilePath the model file path
209 public void setModelFilePath(final String modelFilePath) {
210 this.modelFilePath = modelFilePath;
214 * Check set model file path.
216 * @return true, if check set model file path
218 public boolean checkSetModelFilePath() {
219 return modelFilePath != null && modelFilePath.length() > 0;
223 * Gets the configuration file path.
225 * @return the configuration file path
227 public String getConfigurationFilePath() {
228 return configurationFilePath;
232 * Gets the full expanded configuration file path.
234 * @return the configuration file path
236 public String getFullConfigurationFilePath() {
237 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
241 * Sets the configuration file path.
243 * @param configurationFilePath the configuration file path
245 public void setConfigurationFilePath(final String configurationFilePath) {
246 this.configurationFilePath = configurationFilePath;
251 * Check set configuration file path.
253 * @return true, if check set configuration file path
255 public boolean checkSetConfigurationFilePath() {
256 return configurationFilePath != null && configurationFilePath.length() > 0;
260 * Validate readable file.
262 * @param fileTag the file tag
263 * @param fileName the file name
264 * @throws ApexException the apex exception
266 private void validateReadableFile(final String fileTag, final String fileName) throws ApexException {
267 if (fileName == null || fileName.length() == 0) {
268 throw new ApexException(fileTag + " file was not specified as an argument");
271 // The file name can refer to a resource on the local file system or on the class path
272 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
273 if (fileUrl == null) {
274 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" does not exist");
277 final File theFile = new File(fileUrl.getPath());
278 if (!theFile.exists()) {
279 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" does not exist");
281 if (!theFile.isFile()) {
282 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" is not a normal file");
284 if (!theFile.canRead()) {
285 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" is ureadable");