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