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