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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.pdp.monitoring;
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;
35 * This class reads and handles command line parameters to the Pdp Statistics services.
37 * @author Yehui Wang (yehui.wang@est.tech)
39 public class PdpMonitoringServerParameterParser {
40 // Apache Commons CLI options
41 private final Options options;
44 * Construct the options for the Pdp monitoring services.
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());
57 * Parse the command line options.
59 * @param args the arguments
60 * @return parsed parameters
62 public PdpMonitoringServerParameters parse(final String[] args) {
63 CommandLine commandLine = null;
65 commandLine = new DefaultParser().parse(options, args);
66 } catch (final ParseException e) {
67 throw new PdpMonitoringServerParameterException("invalid command line arguments specified", e);
70 final var parameters = new PdpMonitoringServerParameters();
71 final String[] remainingArgs = commandLine.getArgs();
73 if (commandLine.getArgs().length > 0) {
74 throw new PdpMonitoringServerParameterException(
75 "too many command line arguments specified : " + Arrays.toString(remainingArgs));
78 if (commandLine.hasOption('h')) {
79 parameters.setHelpSet(true);
82 if (commandLine.hasOption('p')) {
83 parameters.setPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
85 } catch (final ParseException e) {
86 throw new PdpMonitoringServerParameterException("error parsing argument \"port\"", e);
89 if (commandLine.hasOption('t')) {
90 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
92 } catch (final ParseException e) {
93 throw new PdpMonitoringServerParameterException("error parsing argument \"time-to-live\"", e);
100 * Get help information.
102 * @param mainClassName the main class name for the help output
103 * @return help string
105 public String getHelp(final String mainClassName) {
106 final var stringWriter = new StringWriter();
107 final var stringPrintWriter = new PrintWriter(stringWriter);
109 final HelpFormatter helpFormatter = new HelpFormatter();
110 helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, "");
112 return stringWriter.toString();