2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. 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.pdpx.main.startstop;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import java.util.Arrays;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.DefaultParser;
30 import org.apache.commons.cli.HelpFormatter;
31 import org.apache.commons.cli.Option;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
40 * This class reads and handles command line parameters for the policy xacml pdp main program.
42 public class XacmlPdpCommandLineArguments {
43 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
44 private static final int HELP_LINE_LENGTH = 120;
46 // Apache Commons CLI options
47 private final Options options;
49 // The command line options
50 private String configurationFilePath = null;
51 private String propertyFilePath = null;
54 * Construct the options for the CLI editor.
56 public XacmlPdpCommandLineArguments() {
58 options = new Options();
59 options.addOption(Option.builder("h")
61 .desc("outputs the usage of this command")
65 options.addOption(Option.builder("v")
67 .desc("outputs the version of policy xacml pdp")
71 options.addOption(Option.builder("c")
72 .longOpt("config-file")
73 .desc("the full path to the configuration file to use, "
74 + "the configuration file must be a Json file containing the policy xacml pdp parameters")
76 .argName("CONFIG_FILE")
80 options.addOption(Option.builder("p")
81 .longOpt("property-file")
82 .desc("the full path to the topic property file to use, "
83 + "the property file contains the policy pap topic properties")
93 * Construct the options for the CLI editor and parse in the given arguments.
95 * @param args The command line arguments
97 public XacmlPdpCommandLineArguments(final String[] args) {
98 // Set up the options with the default constructor
101 // Parse the arguments
104 } catch (final PolicyXacmlPdpException e) {
105 throw new PolicyXacmlPdpRuntimeException("parse error on policy xacml pdp parameters", e);
110 * Parse the command line options.
112 * @param args The command line arguments
113 * @return a string with a message for help and version, or null if there is no message
114 * @throws PolicyXacmlPdpException on command argument errors
116 public String parse(final String[] args) throws PolicyXacmlPdpException {
117 // Clear all our arguments
118 setConfigurationFilePath(null);
119 setPropertyFilePath(null);
121 CommandLine commandLine = null;
123 commandLine = new DefaultParser().parse(options, args);
124 } catch (final ParseException e) {
125 throw new PolicyXacmlPdpException("invalid command line arguments specified : " + e.getMessage());
128 // Arguments left over after Commons CLI does its stuff
129 final String[] remainingArgs = commandLine.getArgs();
131 if (remainingArgs.length > 0) {
132 throw new PolicyXacmlPdpException("too many command line arguments specified : " + Arrays.toString(args));
135 if (remainingArgs.length == 1) {
136 configurationFilePath = remainingArgs[0];
139 if (commandLine.hasOption('h')) {
140 return help(Main.class.getName());
143 if (commandLine.hasOption('v')) {
147 if (commandLine.hasOption('c')) {
148 setConfigurationFilePath(commandLine.getOptionValue('c'));
151 if (commandLine.hasOption('p')) {
152 setPropertyFilePath(commandLine.getOptionValue('p'));
159 * Validate the command line options.
161 * @throws PolicyXacmlPdpException on command argument validation errors
163 public void validate() throws PolicyXacmlPdpException {
164 validateReadableFile("policy xacml pdp configuration", configurationFilePath);
168 * Print version information for policy xacml pdp.
170 * @return the version string
172 public String version() {
173 return ResourceUtils.getResourceAsString("version.txt");
177 * Print help information for policy xacml pdp.
179 * @param mainClassName the main class name
180 * @return the help string
182 public String help(final String mainClassName) {
183 final HelpFormatter helpFormatter = new HelpFormatter();
184 final StringWriter stringWriter = new StringWriter();
185 final PrintWriter printWriter = new PrintWriter(stringWriter);
187 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
190 return stringWriter.toString();
194 * Gets the configuration file path.
196 * @return the configuration file path
198 public String getConfigurationFilePath() {
199 return configurationFilePath;
203 * Gets the full expanded configuration file path.
205 * @return the configuration file path
207 public String getFullConfigurationFilePath() {
208 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
212 * Sets the configuration file path.
214 * @param configurationFilePath the configuration file path
216 public void setConfigurationFilePath(final String configurationFilePath) {
217 this.configurationFilePath = configurationFilePath;
222 * Check set configuration file path.
224 * @return true, if check set configuration file path
226 public boolean checkSetConfigurationFilePath() {
227 return configurationFilePath != null && !configurationFilePath.isEmpty();
231 * Gets the property file path.
233 * @return the property file path
235 public String getPropertyFilePath() {
236 return propertyFilePath;
240 * Gets the full expanded property file path.
242 * @return the property file path
244 public String getFullPropertyFilePath() {
245 return ResourceUtils.getFilePath4Resource(getPropertyFilePath());
249 * Sets the property file path.
251 * @param propertyFilePath the property file path
253 public void setPropertyFilePath(final String propertyFilePath) {
254 this.propertyFilePath = propertyFilePath;
259 * Validate readable file.
261 * @param fileTag the file tag
262 * @param fileName the file name
263 * @throws PolicyXacmlPdpException on the file name passed as a parameter
265 private void validateReadableFile(final String fileTag, final String fileName) throws PolicyXacmlPdpException {
266 if (fileName == null || fileName.isEmpty()) {
267 throw new PolicyXacmlPdpException(fileTag + " file was not specified as an argument");
270 // The file name refers to a resource on the local file system
271 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
272 if (fileUrl == null) {
273 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
276 final File theFile = new File(fileUrl.getPath());
277 if (!theFile.exists()) {
278 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
280 if (!theFile.isFile()) {
281 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
283 if (!theFile.canRead()) {
284 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");