27af9538c8f8942d5223761a7fda765022424262
[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
32 import lombok.Getter;
33 import lombok.Setter;
34
35 import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37
38 /**
39  * This class reads and handles command line parameters to the Apex CLI editor.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 @Setter
44 @Getter
45 public class CommandLineParameters {
46
47     // Default location of the command definition meta data in JSON
48     private static final String JSON_COMMAND_METADATA_RESOURCE = "etc/editor/Commands.json";
49     private static final String APEX_MODEL_PROPERTIES_RESOURCE = "etc/editor/ApexModelProperties.json";
50
51     // The editor parameters
52     private boolean helpSet = false;
53     private String metadataFileName = null;
54     private String apexPropertiesFileName = null;
55     private String commandFileName = null;
56     private String inputModelFileName = null;
57     private String outputModelFileName = null;
58     private String workingDirectory = null;
59     private String logFileName = null;
60     private boolean echo = false;
61     private boolean suppressLog = false;
62     private boolean suppressModelOutput = false;
63     private boolean ignoreCommandFailuresSet = false;
64     private boolean ignoreCommandFailures = false;
65
66     /**
67      * Validates the command line parameters.
68      */
69     public void validate() {
70         CliUtils.validateReadableFile("Metadata File", metadataFileName);
71         CliUtils.validateReadableFile("Properties File", apexPropertiesFileName);
72         CliUtils.validateReadableFile("Command File", commandFileName);
73         CliUtils.validateReadableFile("Input Model File", inputModelFileName);
74         CliUtils.validateWritableFile("Output Model File", outputModelFileName);
75         CliUtils.validateWritableFile("Log File", logFileName);
76         CliUtils.validateWritableDirectory("Working Directory", workingDirectory);
77
78         if (isSuppressLog()) {
79             setEcho(false);
80         } else {
81             if (checkSetCommandFileName()) {
82                 setEcho(true);
83                 if (!isIgnoreCommandFailuresSet()) {
84                     setIgnoreCommandFailuresSet(false);
85                 }
86             } else {
87                 setEcho(false);
88                 if (!isIgnoreCommandFailuresSet()) {
89                     setIgnoreCommandFailuresSet(true);
90                 }
91             }
92         }
93     }
94
95     /**
96      * Gets the command metadata for the editor commands as a stream.
97      *
98      * @return the command metadata for the editor commands as a stream.
99      * @throws IOException the IO exception
100      */
101     public InputStream getMetadataStream() throws IOException {
102         if (metadataFileName == null) {
103             return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
104         } else {
105             return new FileInputStream(new File(metadataFileName));
106         }
107     }
108
109     /**
110      * Gets the location of command metadata for the editor commands.
111      *
112      * @return the location of command metadata for the editor commands
113      */
114     public String getMetadataLocation() {
115         if (metadataFileName == null) {
116             return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
117         } else {
118             return "file: \"" + metadataFileName + "\"";
119         }
120     }
121
122     /**
123      * Gets the properties that are used for command default values as a stream.
124      *
125      * @return the properties that are used for command default values as a stream
126      * @throws IOException the IO exception
127      */
128     public InputStream getApexPropertiesStream() throws IOException {
129         if (apexPropertiesFileName == null) {
130             return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
131         } else {
132             return new FileInputStream(new File(apexPropertiesFileName));
133         }
134     }
135
136     /**
137      * Gets the location of the properties that are used for command default values.
138      *
139      * @return the location of the properties that are used for command default values
140      */
141     public String getApexPropertiesLocation() {
142         if (metadataFileName == null) {
143             return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
144         } else {
145             return "file: \"" + apexPropertiesFileName + "\"";
146         }
147     }
148
149     /**
150      * Gets the input stream on which commands are being received.
151      *
152      * @return the input stream on which commands are being received
153      * @throws IOException the IO exception
154      */
155     public InputStream getCommandInputStream() throws IOException {
156         if (commandFileName == null) {
157             return System.in;
158         } else {
159             return new FileInputStream(new File(commandFileName));
160         }
161     }
162
163     /**
164      * Gets the output stream on which command result messages are being output.
165      *
166      * @return the output stream on which command result messages are being output
167      * @throws IOException the IO exception
168      */
169     public OutputStream getOutputStream() throws IOException {
170         // Check if log suppression is active, if so, consume all output on a byte array output stream
171         if (isSuppressLog()) {
172             return new ByteArrayOutputStream();
173
174         }
175         if (logFileName == null) {
176             return System.out;
177         } else {
178             File logFile = new File(logFileName);
179             if (!logFile.getParentFile().exists()) {
180                 logFile.getParentFile().mkdirs();
181             }
182             return new FileOutputStream(logFile, true);
183         }
184     }
185
186     /**
187      * Check if the file name of the command metadata file for the editor commands is set.
188      *
189      * @return true, if the file name of the command metadata file for the editor commands is set
190      */
191     public boolean checkSetMetadataFileName() {
192         return metadataFileName != null && metadataFileName.length() > 0;
193     }
194
195     /**
196      * Check if the file name of the file containing properties that are used for command default values is set.
197      *
198      * @return true, if the file name of the file containing properties that are used for command default values is set
199      */
200     public boolean checkSetApexPropertiesFileName() {
201         return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
202     }
203
204     /**
205      * Check if the name of the file containing commands to be streamed into the CLI editor is set.
206      *
207      * @return true, if the name of the file containing commands to be streamed into the CLI editor is set
208      */
209     public boolean checkSetCommandFileName() {
210         return commandFileName != null && commandFileName.length() > 0;
211     }
212
213     /**
214      * Check if the name of the file containing the Apex model that will be used to initialize the Apex model in the CLI
215      * editor is set.
216      *
217      * @return true, if the name of the file containing the Apex model that will be used to initialize the Apex model in
218      *         the CLI editor is set
219      */
220     public boolean checkSetInputModelFileName() {
221         return inputModelFileName != null && inputModelFileName.length() > 0;
222     }
223
224     /**
225      * Check if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set.
226      *
227      * @return true, if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set
228      */
229     public boolean checkSetOutputModelFileName() {
230         return outputModelFileName != null && outputModelFileName.length() > 0;
231     }
232
233     /**
234      * Check if the name of the file to which the Apex CLI editor will log commands and responses is set.
235      *
236      * @return true, if the name of the file to which the Apex CLI editor will log commands and responses is set
237      */
238     public boolean checkSetLogFileName() {
239         return logFileName != null;
240     }
241
242     /**
243      * {@inheritDoc}.
244      */
245     @Override
246     public String toString() {
247         return "CLIParameters [helpSet=" + helpSet + ", metadataFileName=" + metadataFileName
248                 + ", apexPropertiesFileName=" + apexPropertiesFileName + ", commandFileName=" + commandFileName
249                 + ", inputModelFileName=" + inputModelFileName + ", outputModelFileName=" + outputModelFileName
250                 + ", logFileName=" + logFileName + ", echo=" + echo + ", suppressLog=" + suppressLog
251                 + ", suppressModelOutput=" + suppressModelOutput + "]";
252     }
253 }