3ef3d4cb0dcb186323cc9d550cc98fc0465519f5
[policy/clamp.git] /
1 /*-
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
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.clamp.controlloop.common.startstop;
22
23 import java.io.File;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.net.URL;
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;
34
35 /**
36  * This class reads and handles command line parameters.
37  *
38  */
39 public class CommonCommandLineArguments {
40     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
41     private static final int HELP_LINE_LENGTH = 120;
42
43     /**
44      * Construct the options for the policy participant.
45      */
46     public CommonCommandLineArguments(final Options options) {
47         //@formatter:off
48         options.addOption(Option.builder("h")
49                 .longOpt("help")
50                 .desc("outputs the usage of this command")
51                 .required(false)
52                 .type(Boolean.class)
53                 .build());
54         options.addOption(Option.builder("v")
55                 .longOpt("version")
56                 .desc("outputs the version of policy participant")
57                 .required(false)
58                 .type(Boolean.class)
59                 .build());
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")
65                 .hasArg()
66                 .argName("CONFIG_FILE")
67                 .required(false)
68                 .type(String.class)
69                 .build());
70         //@formatter:on
71     }
72
73     /**
74      * Validate the command line options.
75      *
76      * @throws ControlLoopException on command argument validation errors
77      */
78     public void validate(final String configurationFilePath) throws ControlLoopException {
79         validateReadableFile("policy participant configuration", configurationFilePath);
80     }
81
82     /**
83      * Print version information for policy participant.
84      *
85      * @return the version string
86      */
87     public String version() {
88         return ResourceUtils.getResourceAsString("version.txt");
89     }
90
91     /**
92      * Print help information for policy participant.
93      *
94      * @param mainClassName the main class name
95      * @return the help string
96      */
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);
101
102         helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
103                 0, "");
104
105         return stringWriter.toString();
106     }
107
108     /**
109      * Validate readable file.
110      *
111      * @param fileTag the file tag
112      * @param fileName the file name
113      * @throws ControlLoopException on the file name passed as a parameter
114      */
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");
119         }
120
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");
126         }
127
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");
132         }
133         if (!theFile.isFile()) {
134             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
135                     fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
136         }
137         if (!theFile.canRead()) {
138             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
139                     fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable");
140         }
141     }
142 }