5d9d2904460b11ff9a41bd9a2b183015cd677990
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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.gui.pdp.monitoring;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Arrays;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.DefaultParser;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.ParseException;
32
33 /**
34  * This class reads and handles command line parameters to the Pdp Statistics services.
35  *
36  * @author Yehui Wang (yehui.wang@est.tech)
37  */
38 public class PdpMonitoringServerParameterParser {
39     // Apache Commons CLI options
40     private final Options options;
41
42     /**
43      * Construct the options for the Pdp monitoring services.
44      */
45     public PdpMonitoringServerParameterParser() {
46         options = new Options();
47         options.addOption("h", "help", false, "outputs the usage of this command");
48         options.addOption(Option.builder("p").longOpt("port").desc("port to use for the Pdp Services REST calls")
49                 .hasArg().argName("PORT").required(false).type(Number.class).build());
50         options.addOption(Option.builder("t").longOpt("time-to-live")
51                 .desc("the amount of time in seconds that the server will run for before terminating").hasArg()
52                 .argName("TIME_TO_LIVE").required(false).type(Number.class).build());
53     }
54
55     /**
56      * Parse the command line options.
57      *
58      * @param args the arguments
59      * @return parsed parameters
60      */
61     public PdpMonitoringServerParameters parse(final String[] args) {
62         CommandLine commandLine = null;
63         try {
64             commandLine = new DefaultParser().parse(options, args);
65         } catch (final ParseException e) {
66             throw new PdpMonitoringServerParameterException("invalid command line arguments specified", e);
67         }
68
69         final PdpMonitoringServerParameters parameters = new PdpMonitoringServerParameters();
70         final String[] remainingArgs = commandLine.getArgs();
71
72         if (commandLine.getArgs().length > 0) {
73             throw new PdpMonitoringServerParameterException(
74                     "too many command line arguments specified : " + Arrays.toString(remainingArgs));
75         }
76
77         if (commandLine.hasOption('h')) {
78             parameters.setHelpSet(true);
79         }
80         try {
81             if (commandLine.hasOption('p')) {
82                 parameters.setPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
83             }
84         } catch (final ParseException e) {
85             throw new PdpMonitoringServerParameterException("error parsing argument \"port\"", e);
86         }
87         try {
88             if (commandLine.hasOption('t')) {
89                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
90             }
91         } catch (final ParseException e) {
92             throw new PdpMonitoringServerParameterException("error parsing argument \"time-to-live\"", e);
93         }
94
95         return parameters;
96     }
97
98     /**
99      * Get help information.
100      *
101      * @param mainClassName the main class name for the help output
102      * @return help string
103      */
104     public String getHelp(final String mainClassName) {
105         final StringWriter stringWriter = new StringWriter();
106         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
107
108         final HelpFormatter helpFormatter = new HelpFormatter();
109         helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, "");
110
111         return stringWriter.toString();
112     }
113 }