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.common.startstop;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import javax.ws.rs.core.Response;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
36 * This class reads and handles command line parameters.
39 public class CommonCommandLineArguments {
40 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
41 private static final int HELP_LINE_LENGTH = 120;
44 * Construct the options for the policy participant.
46 public CommonCommandLineArguments(final Options options) {
48 options.addOption(Option.builder("h")
50 .desc("outputs the usage of this command")
54 options.addOption(Option.builder("v")
56 .desc("outputs the version of policy participant")
60 options.addOption(Option.builder("c")
61 .longOpt("config-file")
62 .desc("the full path to the configuration file to use, "
63 + "the configuration file must be a Json file containing the "
64 + "policy participant parameters")
66 .argName("CONFIG_FILE")
74 * Validate the command line options.
76 * @throws ControlLoopException on command argument validation errors
78 public void validate(final String configurationFilePath) throws ControlLoopException {
79 validateReadableFile("policy participant configuration", configurationFilePath);
83 * Print version information for policy participant.
85 * @return the version string
87 public String version() {
88 return ResourceUtils.getResourceAsString("version.txt");
92 * Print help information for policy participant.
94 * @param mainClassName the main class name
95 * @return the help string
97 public String help(final String mainClassName, final Options options) {
98 final HelpFormatter helpFormatter = new HelpFormatter();
99 final StringWriter stringWriter = new StringWriter();
100 final PrintWriter printWriter = new PrintWriter(stringWriter);
102 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
105 return stringWriter.toString();
109 * Validate readable file.
111 * @param fileTag the file tag
112 * @param fileName the file name
113 * @throws ControlLoopException on the file name passed as a parameter
115 private void validateReadableFile(final String fileTag, final String fileName) throws ControlLoopException {
116 if (StringUtils.isEmpty(fileName)) {
117 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
118 fileTag + " file was not specified as an argument");
121 // The file name refers to a resource on the local file system
122 final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
123 if (fileUrl == null) {
124 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
125 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
128 final File theFile = new File(fileUrl.getPath());
129 if (!theFile.exists()) {
130 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
131 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
133 if (!theFile.isFile()) {
134 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
135 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
137 if (!theFile.canRead()) {
138 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
139 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");