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;
38 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
41 * This class reads and handles command line parameters for the Apex main program.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class ApexCommandLineArguments {
46 // A system property holding the root directory for relative paths in the configuration file
47 public static final String RELATIVE_FILE_ROOT = "APEX_RELATIVE_FILE_ROOT";
49 // Recurring string constants
50 private static final String FILE_PREAMBLE = " file \"";
51 private static final int HELP_LINE_LENGTH = 120;
53 // Apache Commons CLI options
54 private final Options options;
56 // The command line options
57 private String modelFilePath = null;
58 private String configurationFilePath = null;
59 private String relativeFileRoot = null;
62 * Construct the options for the CLI editor.
64 public ApexCommandLineArguments() {
66 options = new Options();
67 options.addOption(Option.builder("h")
69 .desc("outputs the usage of this command")
73 options.addOption(Option.builder("v")
75 .desc("outputs the version of Apex")
79 options.addOption(Option.builder("c")
80 .longOpt("config-file")
81 .desc("the full path to the configuration file to use, "
82 + "the configuration file must be a Json file "
83 + "containing the Apex configuration parameters")
85 .argName("CONFIG_FILE")
89 options.addOption(Option.builder("rfr")
90 .longOpt("relative-file-root")
91 .desc("the root file path for relative file paths specified in the Apex configuration file, "
92 + "defaults to the current directory from where Apex is executed")
94 .argName(RELATIVE_FILE_ROOT)
98 options.addOption(Option.builder("m").longOpt("model-file")
99 .desc("the full path to the model file to use, if set it overrides the model file set in the "
100 + "configuration file").hasArg().argName("MODEL_FILE")
102 .type(String.class).build());
107 * Construct the options for the CLI editor and parse in the given arguments.
109 * @param args The command line arguments
111 public ApexCommandLineArguments(final String[] args) {
112 // Set up the options with the default constructor
115 // Parse the arguments
118 } catch (final ApexException e) {
119 throw new ApexRuntimeException("parse error on Apex parameters", e);
124 * Parse the command line options.
126 * @param args The command line arguments
127 * @return a string with a message for help and version, or null if there is no message
128 * @throws ApexException on command argument errors
130 public String parse(final String[] args) throws ApexException {
131 // Clear all our arguments
132 setConfigurationFilePath(null);
133 setModelFilePath(null);
135 CommandLine commandLine = null;
137 commandLine = new DefaultParser().parse(options, args);
138 } catch (final ParseException e) {
139 throw new ApexException("invalid command line arguments specified : " + e.getMessage());
142 // Arguments left over after Commons CLI does its stuff
143 final String[] remainingArgs = commandLine.getArgs();
145 if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 1) {
146 throw new ApexException("too many command line arguments specified : " + Arrays.toString(args));
149 if (remainingArgs.length == 1) {
150 configurationFilePath = remainingArgs[0];
153 if (commandLine.hasOption('h')) {
154 return help(ApexMain.class.getName());
157 if (commandLine.hasOption('v')) {
161 if (commandLine.hasOption('c')) {
162 setConfigurationFilePath(commandLine.getOptionValue('c'));
165 if (commandLine.hasOption("rfr")) {
166 setRelativeFileRoot(commandLine.getOptionValue("rfr"));
168 setRelativeFileRoot(null);
171 if (commandLine.hasOption('m')) {
172 setModelFilePath(commandLine.getOptionValue('m'));
179 * Validate the command line options.
181 * @throws ApexException on command argument validation errors
183 public void validate() throws ApexException {
184 validateReadableFile("Apex configuration", configurationFilePath);
186 if (checkSetModelFilePath()) {
187 validateReadableFile("Apex model", modelFilePath);
190 validateRelativeFileRoot();
194 * Print version information for Apex.
196 * @return the version string
198 public String version() {
199 return ResourceUtils.getResourceAsString("version.txt");
203 * Print help information for Apex.
205 * @param mainClassName the main class name
206 * @return the help string
208 public String help(final String mainClassName) {
209 final StringWriter stringWriter = new StringWriter();
210 final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
212 new HelpFormatter().printHelp(stringPrintWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options",
215 return stringWriter.toString();
219 * Gets the model file path.
221 * @return the model file path
223 public String getModelFilePath() {
224 return ResourceUtils.getFilePath4Resource(modelFilePath);
228 * Sets the model file path.
230 * @param modelFilePath the model file path
232 public void setModelFilePath(final String modelFilePath) {
233 this.modelFilePath = modelFilePath;
237 * Check set model file path.
239 * @return true, if check set model file path
241 public boolean checkSetModelFilePath() {
242 return modelFilePath != null && modelFilePath.length() > 0;
246 * Gets the configuration file path.
248 * @return the configuration file path
250 public String getConfigurationFilePath() {
251 return configurationFilePath;
255 * Gets the root file path for relative file paths in the configuration file.
257 * @return the root file path
259 public String getRelativeFileRoot() {
260 return relativeFileRoot;
264 * Gets the full expanded configuration file path.
266 * @return the configuration file path
268 public String getFullConfigurationFilePath() {
269 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
273 * Sets the configuration file path.
275 * @param configurationFilePath the configuration file path
277 public void setConfigurationFilePath(final String configurationFilePath) {
278 this.configurationFilePath = configurationFilePath;
283 * Sets the root file path for relative file paths in the configuration file.
285 * @param relativeFileRoot the configuration file path
287 public void setRelativeFileRoot(final String relativeFileRoot) {
288 String relativeFileRootValue = relativeFileRoot;
290 if (!ParameterValidationUtils.validateStringParameter(relativeFileRoot)) {
291 relativeFileRootValue = System.getProperty(RELATIVE_FILE_ROOT);
294 if (!ParameterValidationUtils.validateStringParameter(relativeFileRootValue)) {
295 relativeFileRootValue = System.getProperty("user.dir");
297 else if (!(new File(relativeFileRootValue).isAbsolute())) {
298 relativeFileRootValue = System.getProperty("user.dir") + File.separator + relativeFileRootValue;
301 this.relativeFileRoot = relativeFileRootValue;
302 System.setProperty(RELATIVE_FILE_ROOT, relativeFileRootValue);
306 * Check set configuration file path.
308 * @return true, if check set configuration file path
310 public boolean checkSetConfigurationFilePath() {
311 return configurationFilePath != null && configurationFilePath.length() > 0;
315 * Validate readable file.
317 * @param fileTag the file tag
318 * @param fileName the file name
319 * @throws ApexException the apex exception
321 private void validateReadableFile(final String fileTag, final String fileName) throws ApexException {
322 if (fileName == null || fileName.length() == 0) {
323 throw new ApexException(fileTag + " file was not specified as an argument");
326 // The file name can refer to a resource on the local file system or on the class path
327 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
328 if (fileUrl == null) {
329 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" does not exist");
332 final File theFile = new File(fileUrl.getPath());
333 if (!theFile.exists()) {
334 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" does not exist");
336 if (!theFile.isFile()) {
337 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" is not a normal file");
339 if (!theFile.canRead()) {
340 throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" is ureadable");
345 * Validate the relative file root.
347 private void validateRelativeFileRoot() throws ApexException {
348 File relativeFileRootPath = new File(relativeFileRoot);
349 if (!relativeFileRootPath.isDirectory()) {
350 throw new ApexException(
351 "relative file root \"" + relativeFileRoot + "\" does not exist or is not a directory");
354 if (!relativeFileRootPath.canWrite()) {
355 throw new ApexException(
356 "relative file root \"" + relativeFileRoot + "\" does not exist or is not a directory");