f36bb858b2d1f8b5b1b42b4f1ad259da650d31aa
[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.io.File;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.net.URL;
27 import java.util.Arrays;
28 import javax.ws.rs.core.Response;
29 import lombok.Getter;
30 import lombok.Setter;
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;
42
43 /**
44  * This class reads and handles command line parameters for the control loop runtime service.
45  */
46 public class ClRuntimeCommandLineArguments {
47     private static final String FILE_MESSAGE_PREAMBLE = " file \"";
48     private static final int HELP_LINE_LENGTH = 120;
49
50     private final Options options;
51     private final CommonCommandLineArguments commonCommandLineArguments;
52
53     @Getter()
54     @Setter()
55     private String configurationFilePath = null;
56
57     /**
58      * Construct the options for the control loop runtime component.
59      */
60     public ClRuntimeCommandLineArguments() {
61         options = new Options();
62         commonCommandLineArguments = new CommonCommandLineArguments(options);
63     }
64
65     /**
66      * Construct the options for the CLI editor and parse in the given arguments.
67      *
68      * @param args The command line arguments
69      */
70     public ClRuntimeCommandLineArguments(final String[] args) {
71         // Set up the options with the default constructor
72         this();
73
74         // Parse the arguments
75         try {
76             parse(args);
77         } catch (final ControlLoopException e) {
78             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
79                     "parse error on control loop runtime parameters", e);
80         }
81     }
82
83     /**
84      * Parse the command line options.
85      *
86      * @param args The command line arguments
87      * @return a string with a message for help and version, or null if there is no message
88      * @throws ControlLoopException on command argument errors
89      */
90     public String parse(final String[] args) throws ControlLoopException {
91         // Clear all our arguments
92         setConfigurationFilePath(null);
93         CommandLine commandLine = null;
94         try {
95             commandLine = new DefaultParser().parse(options, args);
96         } catch (final ParseException e) {
97             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
98                     "invalid command line arguments specified : " + e.getMessage());
99         }
100
101         // Arguments left over after Commons CLI does its stuff
102         final String[] remainingArgs = commandLine.getArgs();
103
104         if (remainingArgs.length > 0) {
105             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
106                     "too many command line arguments specified : " + Arrays.toString(args));
107         }
108
109         if (commandLine.hasOption('h')) {
110             return commonCommandLineArguments.help(Main.class.getName(), options);
111         }
112
113         if (commandLine.hasOption('v')) {
114             return commonCommandLineArguments.version();
115         }
116
117         if (commandLine.hasOption('c')) {
118             setConfigurationFilePath(commandLine.getOptionValue('c'));
119         }
120
121         return null;
122     }
123
124     /**
125      * Validate the command line options.
126      *
127      * @throws ControlLoopException on command argument validation errors
128      */
129     public void validate() throws ControlLoopException {
130         commonCommandLineArguments.validate(configurationFilePath);
131     }
132
133     /**
134      * Gets the full expanded configuration file path.
135      *
136      * @return the configuration file path
137      */
138     public String getFullConfigurationFilePath() {
139         return ResourceUtils.getFilePath4Resource(getConfigurationFilePath());
140     }
141
142     /**
143      * Sets the configuration file path.
144      *
145      * @param configurationFilePath the configuration file path
146      */
147     public void setConfigurationFilePath(final String configurationFilePath) {
148         this.configurationFilePath = configurationFilePath;
149
150     }
151 }