aboutsummaryrefslogtreecommitdiffstats
path: root/gui-editors/gui-editor-apex/src/main/java/org/onap/policy/gui/editors/apex/rest/ApexEditorParameterParser.java
blob: 0c8c6489085fbdb4c15f74a26814a822bd076355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.gui.editors.apex.rest;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * This class reads and handles command line parameters to the Apex CLI editor.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ApexEditorParameterParser {
    // Apache Commons CLI options
    private Options options;

    private static final int COMMAND_HELP_MAX_LINE_WIDTH = 120;

    /**
     * Construct the options for the CLI editor.
     */
    public ApexEditorParameterParser() {
        // @formatter:off
        options = new Options();
        options.addOption("h", "help", false, "outputs the usage of this command");
        options.addOption(
            Option
                .builder("p")
                .longOpt("port")
                .desc("port to use for the Apex RESTful editor REST calls.")
                .hasArg()
                .argName("PORT")
                .required(false)
                .type(Number.class)
                .build()
        );
        options.addOption(
            Option
                .builder("t")
                .longOpt("time-to-live")
                .desc("the amount of time in seconds that the server will run for before terminating. "
                    + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
                .hasArg()
                .argName("TIME_TO_LIVE")
                .required(false)
                .type(Number.class)
                .build()
        );
        options.addOption(
            Option
                .builder("l")
                .longOpt("listen")
                .desc("the IP address to listen on.  Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
                    + " to restrict access to the local machine only.")
                .hasArg()
                .argName("ADDRESS")
                .required(false)
                .type(String.class)
                .build()
        );
        options.addOption(
            Option
                .builder("uuid")
                .longOpt("upload-userid")
                .desc("the userid to use for uploads. Default value is null. Must be specified if the upload-url "
                    + "parameter is specified")
                .hasArg().argName("USERID")
                .required(false)
                .type(String.class)
                .build()
        );
        options.addOption(
            Option
                .builder("uurl")
                .longOpt("upload-url")
                .desc("the URL to use for uploads. Default value is null")
                .hasArg()
                .argName("UPLOAD_URL")
                .required(false)
                .type(String.class)
                .build()
        );
        // @formatter:on
    }

    /**
     * Parse the command line options.
     *
     * @param args The arguments
     * @return the apex editor parameters
     */
    public ApexEditorParameters parse(final String[] args) {
        CommandLine commandLine = null;
        try {
            commandLine = new DefaultParser().parse(options, args);
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
        }

        final var parameters = new ApexEditorParameters();
        final String[] remainingArgs = commandLine.getArgs();

        if (commandLine.getArgs().length > 0) {
            throw new ApexEditorParameterException(
                "too many command line arguments specified : " + Arrays.toString(remainingArgs));
        }

        if (commandLine.hasOption('h')) {
            parameters.setHelp(true);
        }
        try {
            if (commandLine.hasOption('p')) {
                parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
            }
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
        }
        try {
            if (commandLine.hasOption('t')) {
                parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
            }
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
        }
        try {
            if (commandLine.hasOption('l')) {
                parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
            }
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
        }
        try {
            if (commandLine.hasOption("uuid")) {
                parameters.setUploadUserid(commandLine.getParsedOptionValue("uuid").toString());
            }
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("error parsing argument \"upload-uuid\" :" + e.getMessage(), e);
        }
        try {
            if (commandLine.hasOption("uurl")) {
                parameters.setUploadUrl(commandLine.getParsedOptionValue("uurl").toString());
            }
        } catch (final ParseException e) {
            throw new ApexEditorParameterException("error parsing argument \"upload-url\" :" + e.getMessage(), e);
        }

        return parameters;
    }

    /**
     * Get help information.
     *
     * @param mainClassName the main class name
     * @return the help
     */
    public String getHelp(final String mainClassName) {
        final var stringWriter = new StringWriter();
        final var stringPrintWriter = new PrintWriter(stringWriter);

        final var helpFormatter = new HelpFormatter();
        helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
            options, 0, 1, "");

        return stringWriter.toString();
    }
}