d67c6b1c685698df2b4c85f79f94950beb6f1cbc
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / ApexEditorParameterParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.client.editor.rest;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Arrays;
26
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.DefaultParser;
29 import org.apache.commons.cli.HelpFormatter;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33
34 /**
35  * This class reads and handles command line parameters to the Apex CLI editor.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class ApexEditorParameterParser {
40     // Apache Commons CLI options
41     private Options options;
42
43     private static final int COMMAND_HELP_MAX_LINE_WIDTH = 120;
44
45     /**
46      * Construct the options for the CLI editor.
47      */
48     public ApexEditorParameterParser() {
49         options = new Options();
50         options.addOption("h", "help", false, "outputs the usage of this command");
51         options.addOption(
52                 Option.builder("p").longOpt("port").desc("port to use for the Apex RESTful editor REST calls.").hasArg()
53                         .argName("PORT").required(false).type(Number.class).build());
54         options.addOption(Option.builder("t").longOpt("time-to-live")
55                 .desc("the amount of time in seconds that the server will run for before terminating. "
56                         + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
57                 .hasArg().argName("TIME_TO_LIVE").required(false).type(Number.class).build());
58         options.addOption(Option.builder("l").longOpt("listen")
59                 .desc("the IP address to listen on.  Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
60                         + " to restrict access to the local machine only.")
61                 .hasArg().argName("ADDRESS").required(false).type(String.class).build());
62     }
63
64     /**
65      * Parse the command line options.
66      *
67      * @param args The arguments
68      * @return the apex editor parameters
69      */
70     public ApexEditorParameters parse(final String[] args) {
71         CommandLine commandLine = null;
72         try {
73             commandLine = new DefaultParser().parse(options, args);
74         } catch (final ParseException e) {
75             throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
76         }
77
78         final ApexEditorParameters parameters = new ApexEditorParameters();
79         final String[] remainingArgs = commandLine.getArgs();
80
81         if (commandLine.getArgs().length > 0) {
82             throw new ApexEditorParameterException(
83                     "too many command line arguments specified : " + Arrays.toString(remainingArgs));
84         }
85
86         if (commandLine.hasOption('h')) {
87             parameters.setHelp(true);
88         }
89         try {
90             if (commandLine.hasOption('p')) {
91                 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
92             }
93         } catch (final ParseException e) {
94             throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
95         }
96         try {
97             if (commandLine.hasOption('t')) {
98                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
99             }
100         } catch (final ParseException e) {
101             throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
102         }
103         try {
104             if (commandLine.hasOption('l')) {
105                 parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
106             }
107         } catch (final ParseException e) {
108             throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
109         }
110
111         return parameters;
112     }
113
114     /**
115      * Get help information.
116      *
117      * @param mainClassName the main class name
118      * @return the help
119      */
120     public String getHelp(final String mainClassName) {
121         final StringWriter stringWriter = new StringWriter();
122         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
123
124         final HelpFormatter helpFormatter = new HelpFormatter();
125         helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
126                 options, 0, 1, "");
127
128         return stringWriter.toString();
129     }
130 }