2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
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.clamp.controlloop.participant.simulator.main.startstop;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import java.util.Arrays;
28 import javax.ws.rs.core.Response;
31 import org.apache.commons.cli.CommandLine;
32 import org.apache.commons.cli.DefaultParser;
33 import org.apache.commons.cli.HelpFormatter;
34 import org.apache.commons.cli.Option;
35 import org.apache.commons.cli.Options;
36 import org.apache.commons.cli.ParseException;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
39 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
43 * This class reads and handles command line parameters for the participant simulator service.
46 public class ParticipantSimulatorCommandLineArguments {
47 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
48 private static final int HELP_LINE_LENGTH = 120;
50 private final Options options;
53 private String configurationFilePath = null;
56 * Construct the options for the participant component.
58 public ParticipantSimulatorCommandLineArguments() {
60 options = new Options();
61 options.addOption(Option.builder("h")
63 .desc("outputs the usage of this command")
67 options.addOption(Option.builder("v")
69 .desc("outputs the version of participant")
73 options.addOption(Option.builder("c")
74 .longOpt("config-file")
75 .desc("the full path to the configuration file to use, "
76 + "the configuration file must be a Json file containing the participant parameters")
78 .argName("CONFIG_FILE")
86 * Construct the options for the participant component and parse in the given arguments.
88 * @param args The command line arguments
90 public ParticipantSimulatorCommandLineArguments(final String[] args) {
91 // Set up the options with the default constructor
94 // Parse the arguments
97 } catch (final ControlLoopException e) {
98 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
99 "parse error on participant parameters", e);
104 * Parse the command line options.
106 * @param args The command line arguments
107 * @return a string with a message for help and version, or null if there is no message
108 * @throws ControlLoopException on command argument errors
110 public String parse(final String[] args) throws ControlLoopException {
111 // Clear all our arguments
112 setConfigurationFilePath(null);
114 CommandLine commandLine = null;
116 commandLine = new DefaultParser().parse(options, args);
117 } catch (final ParseException e) {
118 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
119 "invalid command line arguments specified : " + e.getMessage());
122 // Arguments left over after Commons CLI does its stuff
123 final String[] remainingArgs = commandLine.getArgs();
125 if (remainingArgs.length > 0) {
126 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
127 "too many command line arguments specified : " + Arrays.toString(args));
130 if (commandLine.hasOption('h')) {
131 return help(Main.class.getName());
134 if (commandLine.hasOption('v')) {
138 if (commandLine.hasOption('c')) {
139 setConfigurationFilePath(commandLine.getOptionValue('c'));
146 * Validate the command line options.
148 * @throws ControlLoopException on command argument validation errors
150 public void validate() throws ControlLoopException {
151 validateReadableFile("participant configuration", configurationFilePath);
155 * Print version information for participant.
157 * @return the version string
159 public String version() {
160 return ResourceUtils.getResourceAsString("version.txt");
164 * Print help information for participant.
166 * @param mainClassName the main class name
167 * @return the help string
169 public String help(final String mainClassName) {
170 final HelpFormatter helpFormatter = new HelpFormatter();
171 final StringWriter stringWriter = new StringWriter();
172 final PrintWriter printWriter = new PrintWriter(stringWriter);
174 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
177 return stringWriter.toString();
181 * Gets the full expanded configuration file path.
183 * @return the configuration file path
185 public String getFullConfigurationFilePath() {
186 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
190 * Check set configuration file path.
192 * @return true, if check set configuration file path
194 public boolean checkSetConfigurationFilePath() {
195 return !StringUtils.isEmpty(configurationFilePath);
199 * Validate readable file.
201 * @param fileTag the file tag
202 * @param fileName the file name
203 * @throws ControlLoopException on the file name passed as a parameter
205 private void validateReadableFile(final String fileTag, final String fileName) throws ControlLoopException {
206 if (StringUtils.isEmpty(fileName)) {
207 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
208 fileTag + " file was not specified as an argument");
211 // The file name refers to a resource on the local file system
212 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
213 if (fileUrl == null) {
214 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
215 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
218 final File theFile = new File(fileUrl.getPath());
219 if (!theFile.exists()) {
220 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
221 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
223 if (!theFile.isFile()) {
224 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
225 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
227 if (!theFile.canRead()) {
228 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
229 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");