2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.startstop;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import java.util.Arrays;
28 import javax.ws.rs.core.Response;
31 import org.apache.commons.cli.CommandLine;
32 import org.apache.commons.cli.DefaultParser;
33 import org.apache.commons.cli.HelpFormatter;
34 import org.apache.commons.cli.Option;
35 import org.apache.commons.cli.Options;
36 import org.apache.commons.cli.ParseException;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
39 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
40 import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
44 * This class reads and handles command line parameters for the control loop runtime service.
47 public class ParticipantDcaeCommandLineArguments {
48 private static final String FILE_MESSAGE_PREAMBLE = " file \"";
49 private static final int HELP_LINE_LENGTH = 120;
51 private final Options options;
52 private final CommonCommandLineArguments commonCommandLineArguments;
56 private String configurationFilePath = null;
59 * Construct the options for the dcae participant.
61 public ParticipantDcaeCommandLineArguments() {
62 options = new Options();
63 commonCommandLineArguments = new CommonCommandLineArguments(options);
67 * Construct the options for the CLI editor and parse in the given arguments.
69 * @param args The command line arguments
71 public ParticipantDcaeCommandLineArguments(final String[] args) {
72 // Set up the options with the default constructor
75 // Parse the arguments
78 } catch (final ControlLoopException e) {
79 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
80 "parse error on dcae participant parameters", e);
85 * Parse the command line options.
87 * @param args The command line arguments
88 * @return a string with a message for help and version, or null if there is no message
89 * @throws ControlLoopException on command argument errors
91 public String parse(final String[] args) throws ControlLoopException {
92 // Clear all our arguments
93 setConfigurationFilePath(null);
94 CommandLine commandLine = null;
96 commandLine = new DefaultParser().parse(options, args);
97 } catch (final ParseException e) {
98 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
99 "invalid command line arguments specified : " + e.getMessage());
102 // Arguments left over after Commons CLI does its stuff
103 final String[] remainingArgs = commandLine.getArgs();
105 if (remainingArgs.length > 0) {
106 throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
107 "too many command line arguments specified : " + Arrays.toString(args));
110 if (commandLine.hasOption('h')) {
111 return commonCommandLineArguments.help(Main.class.getName(), options);
114 if (commandLine.hasOption('v')) {
115 return commonCommandLineArguments.version();
118 if (commandLine.hasOption('c')) {
119 setConfigurationFilePath(commandLine.getOptionValue('c'));
126 * Validate the command line options.
128 * @throws ControlLoopException on command argument validation errors
130 public void validate() throws ControlLoopException {
131 commonCommandLineArguments.validate(configurationFilePath);
135 * Gets the full expanded configuration file path.
137 * @return the configuration file path
139 public String getFullConfigurationFilePath() {
140 return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
144 * Check set configuration file path.
146 * @return true, if check set configuration file path
148 public boolean checkSetConfigurationFilePath() {
149 return !StringUtils.isEmpty(configurationFilePath);