4337efc811ed15effc15e637b77ed82bcd12237b
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.editors.apex.rest;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Arrays;
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         // @formatter:off
50         options = new Options();
51         options.addOption("h", "help", false, "outputs the usage of this command");
52         options.addOption(
53             Option
54                 .builder("p")
55                 .longOpt("port")
56                 .desc("port to use for the Apex RESTful editor REST calls.")
57                 .hasArg()
58                 .argName("PORT")
59                 .required(false)
60                 .type(Number.class)
61                 .build()
62         );
63         options.addOption(
64             Option
65                 .builder("t")
66                 .longOpt("time-to-live")
67                 .desc("the amount of time in seconds that the server will run for before terminating. "
68                     + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
69                 .hasArg()
70                 .argName("TIME_TO_LIVE")
71                 .required(false)
72                 .type(Number.class)
73                 .build()
74         );
75         options.addOption(
76             Option
77                 .builder("l")
78                 .longOpt("listen")
79                 .desc("the IP address to listen on.  Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
80                     + " to restrict access to the local machine only.")
81                 .hasArg()
82                 .argName("ADDRESS")
83                 .required(false)
84                 .type(String.class)
85                 .build()
86         );
87         options.addOption(
88             Option
89                 .builder("uuid")
90                 .longOpt("upload-userid")
91                 .desc("the userid to use for uploads. Default value is null. Must be specified if the upload-url "
92                     + "parameter is specified")
93                 .hasArg().argName("USERID")
94                 .required(false)
95                 .type(String.class)
96                 .build()
97         );
98         options.addOption(
99             Option
100                 .builder("uurl")
101                 .longOpt("upload-url")
102                 .desc("the URL to use for uploads. Default value is null")
103                 .hasArg()
104                 .argName("USERID")
105                 .required(false)
106                 .type(String.class)
107                 .build()
108         );
109         // @formatter:on
110     }
111
112     /**
113      * Parse the command line options.
114      *
115      * @param args The arguments
116      * @return the apex editor parameters
117      */
118     public ApexEditorParameters parse(final String[] args) {
119         CommandLine commandLine = null;
120         try {
121             commandLine = new DefaultParser().parse(options, args);
122         } catch (final ParseException e) {
123             throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
124         }
125
126         final ApexEditorParameters parameters = new ApexEditorParameters();
127         final String[] remainingArgs = commandLine.getArgs();
128
129         if (commandLine.getArgs().length > 0) {
130             throw new ApexEditorParameterException(
131                 "too many command line arguments specified : " + Arrays.toString(remainingArgs));
132         }
133
134         if (commandLine.hasOption('h')) {
135             parameters.setHelp(true);
136         }
137         try {
138             if (commandLine.hasOption('p')) {
139                 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
140             }
141         } catch (final ParseException e) {
142             throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
143         }
144         try {
145             if (commandLine.hasOption('t')) {
146                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
147             }
148         } catch (final ParseException e) {
149             throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
150         }
151         try {
152             if (commandLine.hasOption('l')) {
153                 parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
154             }
155         } catch (final ParseException e) {
156             throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
157         }
158         try {
159             if (commandLine.hasOption("uuid")) {
160                 parameters.setUploadUserid(commandLine.getParsedOptionValue("uuid").toString());
161             }
162         } catch (final ParseException e) {
163             throw new ApexEditorParameterException("error parsing argument \"upload-uuid\" :" + e.getMessage(), e);
164         }
165         try {
166             if (commandLine.hasOption("uurl")) {
167                 parameters.setUploadUrl(commandLine.getParsedOptionValue("uurl").toString());
168             }
169         } catch (final ParseException e) {
170             throw new ApexEditorParameterException("error parsing argument \"upload-url\" :" + e.getMessage(), e);
171         }
172
173         return parameters;
174     }
175
176     /**
177      * Get help information.
178      *
179      * @param mainClassName the main class name
180      * @return the help
181      */
182     public String getHelp(final String mainClassName) {
183         final StringWriter stringWriter = new StringWriter();
184         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
185
186         final HelpFormatter helpFormatter = new HelpFormatter();
187         helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
188             options, 0, 1, "");
189
190         return stringWriter.toString();
191     }
192 }