af7a189f64a07627be2e5f9a54d5cd888148eead
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.policy.main.startstop;
22
23 import java.util.Arrays;
24 import javax.ws.rs.core.Response;
25 import lombok.Getter;
26 import lombok.Setter;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.DefaultParser;
29 import org.apache.commons.cli.Options;
30 import org.apache.commons.cli.ParseException;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
34 import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36
37 /**
38  * This class reads and handles command line parameters for the policy participant.
39  *
40  */
41 public class ParticipantPolicyCommandLineArguments {
42     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
43     private static final int HELP_LINE_LENGTH = 120;
44
45     private final Options options;
46     private final CommonCommandLineArguments commonCommandLineArguments;
47
48     @Getter()
49     @Setter()
50     private String configurationFilePath = null;
51
52     /**
53      * Construct the options for the policy participant.
54      */
55     public ParticipantPolicyCommandLineArguments() {
56         options = new Options();
57         commonCommandLineArguments = new CommonCommandLineArguments(options);
58     }
59
60     /**
61      * Construct the options for the CLI editor and parse in the given arguments.
62      *
63      * @param args The command line arguments
64      */
65     public ParticipantPolicyCommandLineArguments(final String[] args) {
66         // Set up the options with the default constructor
67         this();
68
69         // Parse the arguments
70         try {
71             parse(args);
72         } catch (final ControlLoopException e) {
73             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
74                     "parse error on policy participant parameters", e);
75         }
76     }
77
78     /**
79      * Parse the command line options.
80      *
81      * @param args The command line arguments
82      * @return a string with a message for help and version, or null if there is no message
83      * @throws ControlLoopException on command argument errors
84      */
85     public String parse(final String[] args) throws ControlLoopException {
86         // Clear all our arguments
87         setConfigurationFilePath(null);
88         CommandLine commandLine = null;
89         try {
90             commandLine = new DefaultParser().parse(options, args);
91         } catch (final ParseException e) {
92             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
93                     "invalid command line arguments specified : " + e.getMessage());
94         }
95
96         // Arguments left over after Commons CLI does its stuff
97         final String[] remainingArgs = commandLine.getArgs();
98
99         if (remainingArgs.length > 0) {
100             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
101                     "too many command line arguments specified : " + Arrays.toString(args));
102         }
103
104         if (commandLine.hasOption('h')) {
105             return commonCommandLineArguments.help(Main.class.getName(), options);
106         }
107
108         if (commandLine.hasOption('v')) {
109             return commonCommandLineArguments.version();
110         }
111
112         if (commandLine.hasOption('c')) {
113             setConfigurationFilePath(commandLine.getOptionValue('c'));
114         }
115
116         return null;
117     }
118
119     /**
120      * Validate the command line options.
121      *
122      * @throws ControlLoopException on command argument validation errors
123      */
124     public void validate() throws ControlLoopException {
125         commonCommandLineArguments.validate(configurationFilePath);
126     }
127
128     /**
129      * Gets the full expanded configuration file path.
130      *
131      * @return the configuration file path
132      */
133     public String getFullConfigurationFilePath() {
134         return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
135     }
136
137     /**
138      * Check set configuration file path.
139      *
140      * @return true, if check set configuration file path
141      */
142     public boolean checkSetConfigurationFilePath() {
143         return !StringUtils.isEmpty(configurationFilePath);
144     }
145 }