2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modification Copyright (C) 2020-2021 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.main;
25 import java.util.Arrays;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.DefaultParser;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
35 import org.onap.policy.common.utils.cmd.CommandLineArgumentsHandler;
36 import org.onap.policy.common.utils.cmd.CommandLineException;
37 import org.onap.policy.common.utils.resources.MessageConstants;
38 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
41 * This class reads and handles command line parameters for the Apex main
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class ApexCommandLineArguments extends CommandLineArgumentsHandler {
47 // A system property holding the root directory for relative paths in the
49 public static final String APEX_RELATIVE_FILE_ROOT = "APEX_RELATIVE_FILE_ROOT";
50 private static final String RELATIVE_FILE_ROOT = "relative file root \"";
54 private String toscaPolicyFilePath = null;
57 private String relativeFileRoot = null;
59 private CommandLine cmd = null;
62 * Construct the options for the CLI editor.
64 public ApexCommandLineArguments() {
65 super(ApexMain.class.getName(), MessageConstants.POLICY_APEX_PDP, apexCustomOptions());
69 * Builds Apex custom options.
71 private static Options apexCustomOptions() {
73 Options options = new Options();
74 options.addOption(Option.builder("h")
76 .desc("outputs the usage of this command")
80 options.addOption(Option.builder("v")
82 .desc("outputs the version of Apex")
86 options.addOption(Option.builder("rfr")
87 .longOpt("relative-file-root")
88 .desc("the root file path for relative file paths specified in the Apex configuration file, "
89 + "defaults to the current directory from where Apex is executed")
91 .argName(APEX_RELATIVE_FILE_ROOT)
95 options.addOption(Option.builder("p").longOpt("tosca-policy-file")
96 .desc("the full path to the ToscaPolicy file to use.").hasArg().argName("TOSCA_POLICY_FILE")
98 .type(String.class).build());
104 * Construct the options for the CLI editor and parse in the given arguments.
106 * @param args The command line arguments
108 public ApexCommandLineArguments(final String[] args) {
109 // Set up the options with the default constructor
112 // Parse the arguments
115 } catch (final CommandLineException e) {
116 throw new ApexRuntimeException("parse error on Apex parameters", e);
121 public String parse(final String[] args) throws CommandLineException {
122 // Clear all our arguments
123 setToscaPolicyFilePath(null);
124 setRelativeFileRoot(null);
127 cmd = new DefaultParser().parse(apexCustomOptions(), args);
128 } catch (final ParseException e) {
129 throw new CommandLineException("invalid command line arguments specified", e);
132 // Arguments left over after Commons CLI does its stuff
133 final String[] remainingArgs = cmd.getArgs();
135 if (remainingArgs.length > 0 && cmd.hasOption('p') || remainingArgs.length > 1) {
136 throw new CommandLineException("too many command line arguments specified: " + Arrays.toString(args));
139 if (remainingArgs.length == 1) {
140 toscaPolicyFilePath = remainingArgs[0];
143 if (cmd.hasOption('h')) {
147 if (cmd.hasOption('v')) {
151 if (cmd.hasOption("rfr")) {
152 setRelativeFileRoot(cmd.getOptionValue("rfr"));
155 if (cmd.hasOption('p')) {
156 setToscaPolicyFilePath(cmd.getOptionValue('p'));
162 public CommandLine getCommandLine() {
167 * Validate the command line options.
169 * @throws ApexException on command argument validation errors
171 public void validateInputFiles() throws ApexException {
173 validateReadableFile("Tosca Policy", toscaPolicyFilePath);
174 } catch (CommandLineException e) {
175 throw new ApexException(e.getMessage());
177 validateRelativeFileRoot();
181 * Sets the root file path for relative file paths in the configuration file.
183 * @param relativeFileRoot the configuration file path
185 public void setRelativeFileRoot(final String relativeFileRoot) {
186 String relativeFileRootValue = relativeFileRoot;
188 if (!ParameterValidationUtils.validateStringParameter(relativeFileRoot)) {
189 relativeFileRootValue = System.getProperty(APEX_RELATIVE_FILE_ROOT);
192 if (!ParameterValidationUtils.validateStringParameter(relativeFileRootValue)) {
193 relativeFileRootValue = System.getProperty("user.dir");
194 } else if (!(new File(relativeFileRootValue).isAbsolute())) {
195 relativeFileRootValue = System.getProperty("user.dir") + File.separator + relativeFileRootValue;
198 this.relativeFileRoot = relativeFileRootValue;
199 System.setProperty(APEX_RELATIVE_FILE_ROOT, relativeFileRootValue);
203 * Validate the relative file root.
205 private void validateRelativeFileRoot() throws ApexException {
206 File relativeFileRootPath = new File(relativeFileRoot);
207 if (!relativeFileRootPath.isDirectory()) {
208 throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" does not exist or is not a directory");
211 if (!relativeFileRootPath.canRead()) {
212 throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" is not a readable directory");
215 if (!relativeFileRootPath.canExecute()) {
216 throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" is not an executable directory");