5812ebdb3167ddd35ac4a3eca8e27b09e2eeebf1
[policy/apex-pdp.git] / client / client-full / src / main / java / org / onap / policy / apex / client / full / rest / ApexServicesRestParameterParser.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.full.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 RESTful services.
36  *
37  * @author Michael Watkins (michael.watkins@ericsson.com)
38  */
39 public class ApexServicesRestParameterParser {
40     // Apache Commons CLI options
41     private Options options;
42
43     /**
44      * Construct the options for the CLI RESTful services.
45      */
46     public ApexServicesRestParameterParser() {
47         options = new Options();
48         options.addOption("h", "help", false, "outputs the usage of this command");
49         options.addOption(Option.builder("p").longOpt("port").desc("port to use for the Apex Services REST calls")
50                 .hasArg().argName("PORT").required(false).type(Number.class).build());
51         options.addOption(Option.builder("t").longOpt("time-to-live")
52                 .desc("the amount of time in seconds that the server will run for before terminating").hasArg()
53                 .argName("TIME_TO_LIVE").required(false).type(Number.class).build());
54     }
55
56     /**
57      * Parse the command line options.
58      *
59      * @param args the arguments
60      * @return parsed parameters
61      */
62     public ApexServicesRestParameters parse(final String[] args) {
63         CommandLine commandLine = null;
64         try {
65             commandLine = new DefaultParser().parse(options, args);
66         } catch (final ParseException e) {
67             throw new ApexServicesRestParameterException(
68                     "invalid command line arguments specified : " + e.getMessage());
69         }
70
71         final ApexServicesRestParameters parameters = new ApexServicesRestParameters();
72         final String[] remainingArgs = commandLine.getArgs();
73
74         if (commandLine.getArgs().length > 0) {
75             throw new ApexServicesRestParameterException(
76                     "too many command line arguments specified : " + Arrays.toString(remainingArgs));
77         }
78
79         if (commandLine.hasOption('h')) {
80             parameters.setHelp(true);
81         }
82         try {
83             if (commandLine.hasOption('p')) {
84                 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
85             }
86         } catch (final ParseException e) {
87             throw new ApexServicesRestParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
88         }
89         try {
90             if (commandLine.hasOption('t')) {
91                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
92             }
93         } catch (final ParseException e) {
94             throw new ApexServicesRestParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(),
95                     e);
96         }
97
98         return parameters;
99     }
100
101     /**
102      * Get help information.
103      *
104      * @param mainClassName the main class name for the help output
105      * @return help string
106      */
107     public String getHelp(final String mainClassName) {
108         final StringWriter stringWriter = new StringWriter();
109         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
110
111         final HelpFormatter helpFormatter = new HelpFormatter();
112         helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, "");
113
114         return stringWriter.toString();
115     }
116 }