525da259f9a73b61a5edb6aa38794bd6e335178b
[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 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;
33
34 /**
35  * This class reads and handles command line parameters.
36  *
37  */
38 public class CommonCommandLineArguments {
39     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
40     private static final int HELP_LINE_LENGTH = 120;
41
42     /**
43      * Construct the options for the policy participant.
44      *
45      * @param options the options for the command line
46      */
47     public CommonCommandLineArguments(final Options options) {
48         //@formatter:off
49         options.addOption(Option.builder("h")
50                 .longOpt("help")
51                 .desc("outputs the usage of this command")
52                 .required(false)
53                 .type(Boolean.class)
54                 .build());
55         options.addOption(Option.builder("v")
56                 .longOpt("version")
57                 .desc("outputs the version of policy participant")
58                 .required(false)
59                 .type(Boolean.class)
60                 .build());
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")
66                 .hasArg()
67                 .argName("CONFIG_FILE")
68                 .required(false)
69                 .type(String.class)
70                 .build());
71         //@formatter:on
72     }
73
74     /**
75      * Validate the command line options.
76      *
77      * @param configurationFilePath the path to the configuration file
78      * @throws ControlLoopException on command argument validation errors
79      */
80     public void validate(final String configurationFilePath) throws ControlLoopException {
81         validateReadableFile("policy participant configuration", configurationFilePath);
82     }
83
84     /**
85      * Print version information for policy participant.
86      *
87      * @return the version string
88      */
89     public String version() {
90         return ResourceUtils.getResourceAsString("version.txt");
91     }
92
93     /**
94      * Print help information for policy participant.
95      *
96      * @param mainClassName the main class name
97      * @param options the options for the command
98      * @return the help string
99      */
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);
104
105         helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0,
106                 0, "");
107
108         return stringWriter.toString();
109     }
110
111     /**
112      * Validate readable file.
113      *
114      * @param fileTag the file tag
115      * @param fileName the file name
116      * @throws ControlLoopException on the file name passed as a parameter
117      */
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");
122         }
123
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");
129         }
130
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");
135         }
136         if (!theFile.isFile()) {
137             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
138                     fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file");
139         }
140         if (!theFile.canRead()) {
141             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
142                     fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable");
143         }
144     }
145 }