54167e8a6704a181753d14233b4976a33b685205
[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.runtime.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.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
33 import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35
36 /**
37  * This class reads and handles command line parameters for the control loop runtime service.
38  */
39 public class ClRuntimeCommandLineArguments {
40     private final Options options;
41     private final CommonCommandLineArguments commonCommandLineArguments;
42
43     @Getter
44     @Setter
45     private String configurationFilePath = null;
46
47     /**
48      * Construct the options for the control loop runtime component.
49      */
50     public ClRuntimeCommandLineArguments() {
51         options = new Options();
52         commonCommandLineArguments = new CommonCommandLineArguments(options);
53     }
54
55     /**
56      * Construct the options for the CLI editor and parse in the given arguments.
57      *
58      * @param args The command line arguments
59      * @throws ControlLoopRuntimeException if the arguments are invalid
60      */
61     public ClRuntimeCommandLineArguments(final String[] args) {
62         // Set up the options with the default constructor
63         this();
64
65         // Parse the arguments
66         try {
67             parse(args);
68         } catch (final ControlLoopException e) {
69             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
70                 "parse error on control loop runtime parameters", e);
71         }
72     }
73
74     /**
75      * Parse the command line options.
76      *
77      * @param args The command line arguments
78      * @return a string with a message for help and version, or null if there is no message
79      * @throws ControlLoopException on command argument errors
80      */
81     public String parse(final String[] args) throws ControlLoopException {
82         // Clear all our arguments
83         setConfigurationFilePath(null);
84         CommandLine commandLine = null;
85         try {
86             commandLine = new DefaultParser().parse(options, args);
87         } catch (final ParseException e) {
88             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
89                 "invalid command line arguments specified : " + e.getMessage());
90         }
91
92         // Arguments left over after Commons CLI does its stuff
93         final String[] remainingArgs = commandLine.getArgs();
94
95         if (remainingArgs.length > 0) {
96             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
97                 "too many command line arguments specified : " + Arrays.toString(args));
98         }
99
100         if (commandLine.hasOption('h')) {
101             return commonCommandLineArguments.help(Main.class.getName(), options);
102         }
103
104         if (commandLine.hasOption('v')) {
105             return commonCommandLineArguments.version();
106         }
107
108         if (commandLine.hasOption('c')) {
109             setConfigurationFilePath(commandLine.getOptionValue('c'));
110         }
111
112         return null;
113     }
114
115     /**
116      * Validate the command line options.
117      *
118      * @throws ControlLoopException on command argument validation errors
119      */
120     public void validate() throws ControlLoopException {
121         commonCommandLineArguments.validate(configurationFilePath);
122     }
123
124     /**
125      * Gets the full expanded configuration file path.
126      *
127      * @return the configuration file path
128      */
129     public String getFullConfigurationFilePath() {
130         return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
131     }
132 }