Add DMaaP simulator for CSIT testing
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / startstop / DmaapSimCommandLineArguments.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.sim.dmaap.startstop;
22
23 import java.io.File;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.net.URL;
27 import java.util.Arrays;
28
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.models.sim.dmaap.DmaapSimException;
37 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
38
39
40 /**
41  * This class reads and handles command line parameters for the DMaaP simulator service.
42  */
43 public class DmaapSimCommandLineArguments {
44     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
45     private static final int HELP_LINE_LENGTH = 120;
46
47     private final Options options;
48     private String configurationFilePath = null;
49
50     /**
51      * Construct the options for the CLI editor.
52      */
53     public DmaapSimCommandLineArguments() {
54         //@formatter:off
55         options = new Options();
56         options.addOption(Option.builder("h")
57                 .longOpt("help")
58                 .desc("outputs the usage of this command")
59                 .required(false)
60                 .type(Boolean.class)
61                 .build());
62         options.addOption(Option.builder("v")
63                 .longOpt("version")
64                 .desc("outputs the version of DMaaP simulator")
65                 .required(false)
66                 .type(Boolean.class)
67                 .build());
68         options.addOption(Option.builder("c")
69                 .longOpt("config-file")
70                 .desc("the full path to the configuration file to use, "
71                         + "the configuration file must be a Json file containing the DMaaP simulator parameters")
72                 .hasArg()
73                 .argName("CONFIG_FILE")
74                 .required(false)
75                 .type(String.class)
76                 .build());
77         //@formatter:on
78     }
79
80     /**
81      * Construct the options for the CLI editor and parse in the given arguments.
82      *
83      * @param args The command line arguments
84      */
85     public DmaapSimCommandLineArguments(final String[] args) {
86         // Set up the options with the default constructor
87         this();
88
89         // Parse the arguments
90         try {
91             parse(args);
92         } catch (final DmaapSimException e) {
93             throw new DmaapSimRuntimeException("parse error on DMaaP simulator parameters", e);
94         }
95     }
96
97     /**
98      * Parse the command line options.
99      *
100      * @param args The command line arguments
101      * @return a string with a message for help and version, or null if there is no message
102      * @throws DmaapSimException on command argument errors
103      */
104     public String parse(final String[] args) throws DmaapSimException {
105         // Clear all our arguments
106         setConfigurationFilePath(null);
107
108         CommandLine commandLine = null;
109         try {
110             commandLine = new DefaultParser().parse(options, args);
111         } catch (final ParseException e) {
112             throw new DmaapSimException("invalid command line arguments specified : " + e.getMessage());
113         }
114
115         // Arguments left over after Commons CLI does its stuff
116         final String[] remainingArgs = commandLine.getArgs();
117
118         if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 0) {
119             throw new DmaapSimException("too many command line arguments specified : " + Arrays.toString(args));
120         }
121
122         if (remainingArgs.length == 1) {
123             configurationFilePath = remainingArgs[0];
124         }
125
126         if (commandLine.hasOption('h')) {
127             return help(Main.class.getCanonicalName());
128         }
129
130         if (commandLine.hasOption('v')) {
131             return version();
132         }
133
134         if (commandLine.hasOption('c')) {
135             setConfigurationFilePath(commandLine.getOptionValue('c'));
136         }
137
138         return null;
139     }
140
141     /**
142      * Validate the command line options.
143      *
144      * @throws DmaapSimException on command argument validation errors
145      */
146     public void validate() throws DmaapSimException {
147         validateReadableFile("DMaaP simulator configuration", configurationFilePath);
148     }
149
150     /**
151      * Print version information for DMaaP simulator.
152      *
153      * @return the version string
154      */
155     public String version() {
156         return ResourceUtils.getResourceAsString("version.txt");
157     }
158
159     /**
160      * Print help information for DMaaP simulator.
161      *
162      * @param mainClassName the main class name
163      * @return the help string
164      */
165     public String help(final String mainClassName) {
166         final HelpFormatter helpFormatter = new HelpFormatter();
167         final StringWriter stringWriter = new StringWriter();
168         final PrintWriter printWriter = new PrintWriter(stringWriter);
169
170         helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
171                 0, "");
172
173         return stringWriter.toString();
174     }
175
176     /**
177      * Gets the configuration file path.
178      *
179      * @return the configuration file path
180      */
181     public String getConfigurationFilePath() {
182         return configurationFilePath;
183     }
184
185     /**
186      * Gets the full expanded configuration file path.
187      *
188      * @return the configuration file path
189      */
190     public String getFullConfigurationFilePath() {
191         return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
192     }
193
194     /**
195      * Sets the configuration file path.
196      *
197      * @param configurationFilePath the configuration file path
198      */
199     public void setConfigurationFilePath(final String configurationFilePath) {
200         this.configurationFilePath = configurationFilePath;
201
202     }
203
204     /**
205      * Check set configuration file path.
206      *
207      * @return true, if check set configuration file path
208      */
209     public boolean checkSetConfigurationFilePath() {
210         return configurationFilePath != null && configurationFilePath.length() > 0;
211     }
212
213     /**
214      * Validate readable file.
215      *
216      * @param fileTag the file tag
217      * @param fileName the file name
218      * @throws DmaapSimException on the file name passed as a parameter
219      */
220     private void validateReadableFile(final String fileTag, final String fileName) throws DmaapSimException {
221         if (fileName == null || fileName.length() == 0) {
222             throw new DmaapSimException(fileTag + " file was not specified as an argument");
223         }
224
225         // The file name refers to a resource on the local file system
226         final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
227         if (fileUrl == null) {
228             throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
229         }
230
231         final File theFile = new File(fileUrl.getPath());
232         if (!theFile.exists()) {
233             throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
234         }
235         if (!theFile.isFile()) {
236             throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
237         }
238         if (!theFile.canRead()) {
239             throw new DmaapSimException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");
240         }
241     }
242 }