2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021 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;
26 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.common.utils.resources.ResourceUtils;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
37 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
41 * This class reads and handles command line parameters for the policy xacml pdp main program.
43 public class XacmlPdpCommandLineArguments {
44 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
45 private static final int HELP_LINE_LENGTH = 120;
47 // Apache Commons CLI options
48 private final Options options;
50 // The command line options
53 private String configurationFilePath = null;
56 private String propertyFilePath = null;
59 * Construct the options for the CLI editor.
61 public XacmlPdpCommandLineArguments() {
63 options = new Options();
64 options.addOption(Option.builder("h")
66 .desc("outputs the usage of this command")
70 options.addOption(Option.builder("v")
72 .desc("outputs the version of policy xacml pdp")
76 options.addOption(Option.builder("c")
77 .longOpt("config-file")
78 .desc("the full path to the configuration file to use, "
79 + "the configuration file must be a Json file containing the policy xacml pdp parameters")
81 .argName("CONFIG_FILE")
85 options.addOption(Option.builder("p")
86 .longOpt("property-file")
87 .desc("the full path to the topic property file to use, "
88 + "the property file contains the policy pap topic properties")
98 * Construct the options for the CLI editor and parse in the given arguments.
100 * @param args The command line arguments
102 public XacmlPdpCommandLineArguments(final String[] args) {
103 // Set up the options with the default constructor
106 // Parse the arguments
109 } catch (final PolicyXacmlPdpException e) {
110 throw new PolicyXacmlPdpRuntimeException("parse error on policy xacml pdp parameters", e);
115 * Parse the command line options.
117 * @param args The command line arguments
118 * @return a string with a message for help and version, or null if there is no message
119 * @throws PolicyXacmlPdpException on command argument errors
121 public String parse(final String[] args) throws PolicyXacmlPdpException {
122 // Clear all our arguments
123 setConfigurationFilePath(null);
124 setPropertyFilePath(null);
126 CommandLine commandLine = null;
128 commandLine = new DefaultParser().parse(options, args);
129 } catch (final ParseException e) {
130 throw new PolicyXacmlPdpException("invalid command line arguments specified : " + e.getMessage());
133 // Arguments left over after Commons CLI does its stuff
134 final String[] remainingArgs = commandLine.getArgs();
136 if (remainingArgs.length > 0) {
137 throw new PolicyXacmlPdpException("too many command line arguments specified : " + Arrays.toString(args));
140 if (remainingArgs.length == 1) {
141 configurationFilePath = remainingArgs[0];
144 if (commandLine.hasOption('h')) {
145 return help(Main.class.getName());
148 if (commandLine.hasOption('v')) {
152 if (commandLine.hasOption('c')) {
153 setConfigurationFilePath(commandLine.getOptionValue('c'));
156 if (commandLine.hasOption('p')) {
157 setPropertyFilePath(commandLine.getOptionValue('p'));
164 * Validate the command line options.
166 * @throws PolicyXacmlPdpException on command argument validation errors
168 public void validate() throws PolicyXacmlPdpException {
169 validateReadableFile("policy xacml pdp configuration", configurationFilePath);
173 * Print version information for policy xacml pdp.
175 * @return the version string
177 public String version() {
178 return ResourceUtils.getResourceAsString("version.txt");
182 * Print help information for policy xacml pdp.
184 * @param mainClassName the main class name
185 * @return the help string
187 public String help(final String mainClassName) {
188 final var helpFormatter = new HelpFormatter();
189 final var stringWriter = new StringWriter();
190 final var printWriter = new PrintWriter(stringWriter);
192 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
195 return stringWriter.toString();
199 * Gets the full expanded configuration file path.
201 * @return the configuration file path
203 public String getFullConfigurationFilePath() {
204 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
208 * Check set configuration file path.
210 * @return true, if check set configuration file path
212 public boolean checkSetConfigurationFilePath() {
213 return configurationFilePath != null && !configurationFilePath.isEmpty();
217 * Gets the full expanded property file path.
219 * @return the property file path
221 public String getFullPropertyFilePath() {
222 return ResourceUtils.getFilePath4Resource(getPropertyFilePath());
226 * Validate readable file.
228 * @param fileTag the file tag
229 * @param fileName the file name
230 * @throws PolicyXacmlPdpException on the file name passed as a parameter
232 private void validateReadableFile(final String fileTag, final String fileName) throws PolicyXacmlPdpException {
233 if (fileName == null || fileName.isEmpty()) {
234 throw new PolicyXacmlPdpException(fileTag + " file was not specified as an argument");
237 // The file name refers to a resource on the local file system
238 final var fileUrl = ResourceUtils.getUrl4Resource(fileName);
239 if (fileUrl == null) {
240 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
243 final var theFile = new File(fileUrl.getPath());
244 if (!theFile.exists()) {
245 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
247 if (!theFile.isFile()) {
248 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
250 if (!theFile.canRead()) {
251 throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");