Changes for checkstyle 8.32
[policy/apex-pdp.git] / auth / cli-editor / src / main / java / org / onap / policy / apex / auth / clieditor / CommandLineParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.auth.clieditor;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import lombok.Getter;
32 import lombok.Setter;
33 import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35
36 /**
37  * This class reads and handles command line parameters to the Apex CLI editor.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 @Setter
42 @Getter
43 public class CommandLineParameters {
44
45     // Default location of the command definition meta data in JSON
46     private static final String JSON_COMMAND_METADATA_RESOURCE = "etc/editor/Commands.json";
47     private static final String APEX_MODEL_PROPERTIES_RESOURCE = "etc/editor/ApexModelProperties.json";
48
49     // The editor parameters
50     private boolean helpSet = false;
51     private String metadataFileName = null;
52     private String apexPropertiesFileName = null;
53     private String commandFileName = null;
54     private String inputModelFileName = null;
55     private String outputModelFileName = null;
56     private String workingDirectory = null;
57     private String logFileName = null;
58     private boolean echo = false;
59     private boolean suppressLog = false;
60     private boolean suppressModelOutput = false;
61     private boolean ignoreCommandFailuresSet = false;
62     private boolean ignoreCommandFailures = false;
63
64     /**
65      * Validates the command line parameters.
66      */
67     public void validate() {
68         CliUtils.validateReadableFile("Metadata File", metadataFileName);
69         CliUtils.validateReadableFile("Properties File", apexPropertiesFileName);
70         CliUtils.validateReadableFile("Command File", commandFileName);
71         CliUtils.validateReadableFile("Input Model File", inputModelFileName);
72         CliUtils.validateWritableFile("Output Model File", outputModelFileName);
73         CliUtils.validateWritableFile("Log File", logFileName);
74         CliUtils.validateWritableDirectory("Working Directory", workingDirectory);
75
76         if (isSuppressLog()) {
77             setEcho(false);
78         } else {
79             if (checkSetCommandFileName()) {
80                 setEcho(true);
81                 if (!isIgnoreCommandFailuresSet()) {
82                     setIgnoreCommandFailuresSet(false);
83                 }
84             } else {
85                 setEcho(false);
86                 if (!isIgnoreCommandFailuresSet()) {
87                     setIgnoreCommandFailuresSet(true);
88                 }
89             }
90         }
91     }
92
93     /**
94      * Gets the command metadata for the editor commands as a stream.
95      *
96      * @return the command metadata for the editor commands as a stream.
97      * @throws IOException the IO exception
98      */
99     public InputStream getMetadataStream() throws IOException {
100         if (metadataFileName == null) {
101             return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
102         } else {
103             return new FileInputStream(new File(metadataFileName));
104         }
105     }
106
107     /**
108      * Gets the location of command metadata for the editor commands.
109      *
110      * @return the location of command metadata for the editor commands
111      */
112     public String getMetadataLocation() {
113         if (metadataFileName == null) {
114             return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
115         } else {
116             return "file: \"" + metadataFileName + "\"";
117         }
118     }
119
120     /**
121      * Gets the properties that are used for command default values as a stream.
122      *
123      * @return the properties that are used for command default values as a stream
124      * @throws IOException the IO exception
125      */
126     public InputStream getApexPropertiesStream() throws IOException {
127         if (apexPropertiesFileName == null) {
128             return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
129         } else {
130             return new FileInputStream(new File(apexPropertiesFileName));
131         }
132     }
133
134     /**
135      * Gets the location of the properties that are used for command default values.
136      *
137      * @return the location of the properties that are used for command default values
138      */
139     public String getApexPropertiesLocation() {
140         if (metadataFileName == null) {
141             return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
142         } else {
143             return "file: \"" + apexPropertiesFileName + "\"";
144         }
145     }
146
147     /**
148      * Gets the input stream on which commands are being received.
149      *
150      * @return the input stream on which commands are being received
151      * @throws IOException the IO exception
152      */
153     public InputStream getCommandInputStream() throws IOException {
154         if (commandFileName == null) {
155             return System.in;
156         } else {
157             return new FileInputStream(new File(commandFileName));
158         }
159     }
160
161     /**
162      * Gets the output stream on which command result messages are being output.
163      *
164      * @return the output stream on which command result messages are being output
165      * @throws IOException the IO exception
166      */
167     public OutputStream getOutputStream() throws IOException {
168         // Check if log suppression is active, if so, consume all output on a byte array output stream
169         if (isSuppressLog()) {
170             return new ByteArrayOutputStream();
171
172         }
173         if (logFileName == null) {
174             return System.out;
175         } else {
176             File logFile = new File(logFileName);
177             if (!logFile.getParentFile().exists()) {
178                 logFile.getParentFile().mkdirs();
179             }
180             return new FileOutputStream(logFile, true);
181         }
182     }
183
184     /**
185      * Check if the file name of the command metadata file for the editor commands is set.
186      *
187      * @return true, if the file name of the command metadata file for the editor commands is set
188      */
189     public boolean checkSetMetadataFileName() {
190         return metadataFileName != null && metadataFileName.length() > 0;
191     }
192
193     /**
194      * Check if the file name of the file containing properties that are used for command default values is set.
195      *
196      * @return true, if the file name of the file containing properties that are used for command default values is set
197      */
198     public boolean checkSetApexPropertiesFileName() {
199         return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
200     }
201
202     /**
203      * Check if the name of the file containing commands to be streamed into the CLI editor is set.
204      *
205      * @return true, if the name of the file containing commands to be streamed into the CLI editor is set
206      */
207     public boolean checkSetCommandFileName() {
208         return commandFileName != null && commandFileName.length() > 0;
209     }
210
211     /**
212      * Check if the name of the file containing the Apex model that will be used to initialize the Apex model in the CLI
213      * editor is set.
214      *
215      * @return true, if the name of the file containing the Apex model that will be used to initialize the Apex model in
216      *         the CLI editor is set
217      */
218     public boolean checkSetInputModelFileName() {
219         return inputModelFileName != null && inputModelFileName.length() > 0;
220     }
221
222     /**
223      * Check if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set.
224      *
225      * @return true, if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set
226      */
227     public boolean checkSetOutputModelFileName() {
228         return outputModelFileName != null && outputModelFileName.length() > 0;
229     }
230
231     /**
232      * Check if the name of the file to which the Apex CLI editor will log commands and responses is set.
233      *
234      * @return true, if the name of the file to which the Apex CLI editor will log commands and responses is set
235      */
236     public boolean checkSetLogFileName() {
237         return logFileName != null;
238     }
239
240     /**
241      * {@inheritDoc}.
242      */
243     @Override
244     public String toString() {
245         return "CLIParameters [helpSet=" + helpSet + ", metadataFileName=" + metadataFileName
246                 + ", apexPropertiesFileName=" + apexPropertiesFileName + ", commandFileName=" + commandFileName
247                 + ", inputModelFileName=" + inputModelFileName + ", outputModelFileName=" + outputModelFileName
248                 + ", logFileName=" + logFileName + ", echo=" + echo + ", suppressLog=" + suppressLog
249                 + ", suppressModelOutput=" + suppressModelOutput + "]";
250     }
251 }