Use lombok in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / XacmlPdpCommandLineArguments.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.startstop;
22
23 import java.io.File;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Arrays;
27 import lombok.Getter;
28 import lombok.Setter;
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;
38
39
40 /**
41  * This class reads and handles command line parameters for the policy xacml pdp main program.
42  */
43 public class XacmlPdpCommandLineArguments {
44     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
45     private static final int HELP_LINE_LENGTH = 120;
46
47     // Apache Commons CLI options
48     private final Options options;
49
50     // The command line options
51     @Getter
52     @Setter
53     private String configurationFilePath = null;
54     @Getter
55     @Setter
56     private String propertyFilePath = null;
57
58     /**
59      * Construct the options for the CLI editor.
60      */
61     public XacmlPdpCommandLineArguments() {
62         //@formatter:off
63         options = new Options();
64         options.addOption(Option.builder("h")
65                 .longOpt("help")
66                 .desc("outputs the usage of this command")
67                 .required(false)
68                 .type(Boolean.class)
69                 .build());
70         options.addOption(Option.builder("v")
71                 .longOpt("version")
72                 .desc("outputs the version of policy xacml pdp")
73                 .required(false)
74                 .type(Boolean.class)
75                 .build());
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")
80                 .hasArg()
81                 .argName("CONFIG_FILE")
82                 .required(false)
83                 .type(String.class)
84                 .build());
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")
89                 .hasArg()
90                 .argName("PROP_FILE")
91                 .required(false)
92                 .type(String.class)
93                 .build());
94         //@formatter:on
95     }
96
97     /**
98      * Construct the options for the CLI editor and parse in the given arguments.
99      *
100      * @param args The command line arguments
101      */
102     public XacmlPdpCommandLineArguments(final String[] args) {
103         // Set up the options with the default constructor
104         this();
105
106         // Parse the arguments
107         try {
108             parse(args);
109         } catch (final PolicyXacmlPdpException e) {
110             throw new PolicyXacmlPdpRuntimeException("parse error on policy xacml pdp parameters", e);
111         }
112     }
113
114     /**
115      * Parse the command line options.
116      *
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
120      */
121     public String parse(final String[] args) throws PolicyXacmlPdpException {
122         // Clear all our arguments
123         setConfigurationFilePath(null);
124         setPropertyFilePath(null);
125
126         CommandLine commandLine = null;
127         try {
128             commandLine = new DefaultParser().parse(options, args);
129         } catch (final ParseException e) {
130             throw new PolicyXacmlPdpException("invalid command line arguments specified : " + e.getMessage());
131         }
132
133         // Arguments left over after Commons CLI does its stuff
134         final String[] remainingArgs = commandLine.getArgs();
135
136         if (remainingArgs.length > 0) {
137             throw new PolicyXacmlPdpException("too many command line arguments specified : " + Arrays.toString(args));
138         }
139
140         if (remainingArgs.length == 1) {
141             configurationFilePath = remainingArgs[0];
142         }
143
144         if (commandLine.hasOption('h')) {
145             return help(Main.class.getName());
146         }
147
148         if (commandLine.hasOption('v')) {
149             return version();
150         }
151
152         if (commandLine.hasOption('c')) {
153             setConfigurationFilePath(commandLine.getOptionValue('c'));
154         }
155
156         if (commandLine.hasOption('p')) {
157             setPropertyFilePath(commandLine.getOptionValue('p'));
158         }
159
160         return null;
161     }
162
163     /**
164      * Validate the command line options.
165      *
166      * @throws PolicyXacmlPdpException on command argument validation errors
167      */
168     public void validate() throws PolicyXacmlPdpException {
169         validateReadableFile("policy xacml pdp configuration", configurationFilePath);
170     }
171
172     /**
173      * Print version information for policy xacml pdp.
174      *
175      * @return the version string
176      */
177     public String version() {
178         return ResourceUtils.getResourceAsString("version.txt");
179     }
180
181     /**
182      * Print help information for policy xacml pdp.
183      *
184      * @param mainClassName the main class name
185      * @return the help string
186      */
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);
191
192         helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
193                 0, "");
194
195         return stringWriter.toString();
196     }
197
198     /**
199      * Gets the full expanded configuration file path.
200      *
201      * @return the configuration file path
202      */
203     public String getFullConfigurationFilePath() {
204         return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
205     }
206
207     /**
208      * Check set configuration file path.
209      *
210      * @return true, if check set configuration file path
211      */
212     public boolean checkSetConfigurationFilePath() {
213         return configurationFilePath != null && !configurationFilePath.isEmpty();
214     }
215
216     /**
217      * Gets the full expanded property file path.
218      *
219      * @return the property file path
220      */
221     public String getFullPropertyFilePath() {
222         return ResourceUtils.getFilePath4Resource(getPropertyFilePath());
223     }
224
225     /**
226      * Validate readable file.
227      *
228      * @param fileTag the file tag
229      * @param fileName the file name
230      * @throws PolicyXacmlPdpException on the file name passed as a parameter
231      */
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");
235         }
236
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");
241         }
242
243         final var theFile = new File(fileUrl.getPath());
244         if (!theFile.exists()) {
245             throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
246         }
247         if (!theFile.isFile()) {
248             throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
249         }
250         if (!theFile.canRead()) {
251             throw new PolicyXacmlPdpException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");
252         }
253     }
254 }