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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.pdp.monitoring;
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;
36 * This class reads and handles command line parameters to the Pdp Statistics services.
38 * @author Yehui Wang (yehui.wang@est.tech)
40 public class PdpMonitoringServerParameterParser {
41 // Apache Commons CLI options
42 private final Options options;
45 * Construct the options for the Pdp monitoring services.
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());
58 * Parse the command line options.
60 * @param args the arguments
61 * @return parsed parameters
63 public PdpMonitoringServerParameters parse(final String[] args) {
64 CommandLine commandLine = null;
66 commandLine = new DefaultParser().parse(options, args);
67 } catch (final ParseException e) {
68 throw new PdpMonitoringServerParameterException("invalid command line arguments specified", e);
71 final var parameters = new PdpMonitoringServerParameters();
72 final String[] remainingArgs = commandLine.getArgs();
74 if (commandLine.getArgs().length > 0) {
75 throw new PdpMonitoringServerParameterException(
76 "too many command line arguments specified : " + Arrays.toString(remainingArgs));
79 if (commandLine.hasOption('h')) {
80 parameters.setHelpSet(true);
83 if (commandLine.hasOption('p')) {
84 parameters.setPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
86 } catch (final ParseException e) {
87 throw new PdpMonitoringServerParameterException("error parsing argument \"port\"", e);
90 if (commandLine.hasOption('t')) {
91 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
93 } catch (final ParseException e) {
94 throw new PdpMonitoringServerParameterException("error parsing argument \"time-to-live\"", e);
101 * Get help information.
103 * @param mainClassName the main class name for the help output
104 * @return help string
106 public String getHelp(final String mainClassName) {
107 final var stringWriter = new StringWriter();
108 final var stringPrintWriter = new PrintWriter(stringWriter);
110 final var helpFormatter = new HelpFormatter();
111 helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, "");
113 return stringWriter.toString();