76fa005b838440e8b7e891b2a7cf875f18cdc978
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.pdp.monitoring;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Arrays;
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 Pdp Statistics services.
36  *
37  * @author Yehui Wang (yehui.wang@est.tech)
38  */
39 public class PdpMonitoringServerParameterParser {
40     // Apache Commons CLI options
41     private final Options options;
42
43     /**
44      * Construct the options for the Pdp monitoring services.
45      */
46     public PdpMonitoringServerParameterParser() {
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 Pdp 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 PdpMonitoringServerParameters 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 PdpMonitoringServerParameterException("invalid command line arguments specified", e);
68         }
69
70         final var parameters = new PdpMonitoringServerParameters();
71         final String[] remainingArgs = commandLine.getArgs();
72
73         if (commandLine.getArgs().length > 0) {
74             throw new PdpMonitoringServerParameterException(
75                     "too many command line arguments specified : " + Arrays.toString(remainingArgs));
76         }
77
78         if (commandLine.hasOption('h')) {
79             parameters.setHelpSet(true);
80         }
81         try {
82             if (commandLine.hasOption('p')) {
83                 parameters.setPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
84             }
85         } catch (final ParseException e) {
86             throw new PdpMonitoringServerParameterException("error parsing argument \"port\"", e);
87         }
88         try {
89             if (commandLine.hasOption('t')) {
90                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
91             }
92         } catch (final ParseException e) {
93             throw new PdpMonitoringServerParameterException("error parsing argument \"time-to-live\"", e);
94         }
95
96         return parameters;
97     }
98
99     /**
100      * Get help information.
101      *
102      * @param mainClassName the main class name for the help output
103      * @return help string
104      */
105     public String getHelp(final String mainClassName) {
106         final var stringWriter = new StringWriter();
107         final var stringPrintWriter = new PrintWriter(stringWriter);
108
109         final HelpFormatter helpFormatter = new HelpFormatter();
110         helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, "");
111
112         return stringWriter.toString();
113     }
114 }