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;
26 import javax.ws.rs.core.Response;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.cli.Options;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
35 * This class reads and handles command line parameters.
38 public class CommonCommandLineArguments {
39 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
40 private static final int HELP_LINE_LENGTH = 120;
43 * Construct the options for the policy participant.
45 * @param options the options for the command line
47 public CommonCommandLineArguments(final Options options) {
49 options.addOption(Option.builder("h")
51 .desc("outputs the usage of this command")
55 options.addOption(Option.builder("v")
57 .desc("outputs the version of policy participant")
61 options.addOption(Option.builder("c")
62 .longOpt("config-file")
63 .desc("the full path to the configuration file to use, "
64 + "the configuration file must be a Json file containing the "
65 + "policy participant parameters")
67 .argName("CONFIG_FILE")
75 * Validate the command line options.
77 * @param configurationFilePath the path to the configuration file
78 * @throws ControlLoopException on command argument validation errors
80 public void validate(final String configurationFilePath) throws ControlLoopException {
81 validateReadableFile("policy participant configuration", configurationFilePath);
85 * Print version information for policy participant.
87 * @return the version string
89 public String version() {
90 return ResourceUtils.getResourceAsString("version.txt");
94 * Print help information for policy participant.
96 * @param mainClassName the main class name
97 * @param options the options for the command
98 * @return the help string
100 public String help(final String mainClassName, final Options options) {
101 final var helpFormatter = new HelpFormatter();
102 final var stringWriter = new StringWriter();
103 final var printWriter = new PrintWriter(stringWriter);
105 helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
108 return stringWriter.toString();
112 * Validate readable file.
114 * @param fileTag the file tag
115 * @param fileName the file name
116 * @throws ControlLoopException on the file name passed as a parameter
118 private void validateReadableFile(final String fileTag, final String fileName) throws ControlLoopException {
119 if (StringUtils.isEmpty(fileName)) {
120 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
121 fileTag + " file was not specified as an argument");
124 // The file name refers to a resource on the local file system
125 final var fileUrl = ResourceUtils.getUrl4Resource(fileName);
126 if (fileUrl == null) {
127 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
128 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
131 final var theFile = new File(fileUrl.getPath());
132 if (!theFile.exists()) {
133 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
134 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist");
136 if (!theFile.isFile()) {
137 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
138 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
140 if (!theFile.canRead()) {
141 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
142 fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable");