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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.auth.clieditor;
24 import java.io.ByteArrayOutputStream;
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;
33 import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
37 * This class reads and handles command line parameters to the Apex CLI editor.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class CommandLineParameters {
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";
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;
65 * Validates the command line parameters.
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);
76 if (isSuppressLog()) {
79 if (checkSetCommandFileName()) {
81 if (!isIgnoreCommandFailuresSet()) {
82 setIgnoreCommandFailuresSet(false);
86 if (!isIgnoreCommandFailuresSet()) {
87 setIgnoreCommandFailuresSet(true);
94 * Gets the command metadata for the editor commands as a stream.
96 * @return the command metadata for the editor commands as a stream.
97 * @throws IOException the IO exception
99 public InputStream getMetadataStream() throws IOException {
100 if (metadataFileName == null) {
101 return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
103 return new FileInputStream(new File(metadataFileName));
108 * Gets the location of command metadata for the editor commands.
110 * @return the location of command metadata for the editor commands
112 public String getMetadataLocation() {
113 if (metadataFileName == null) {
114 return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
116 return "file: \"" + metadataFileName + "\"";
121 * Gets the properties that are used for command default values as a stream.
123 * @return the properties that are used for command default values as a stream
124 * @throws IOException the IO exception
126 public InputStream getApexPropertiesStream() throws IOException {
127 if (apexPropertiesFileName == null) {
128 return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
130 return new FileInputStream(new File(apexPropertiesFileName));
135 * Gets the location of the properties that are used for command default values.
137 * @return the location of the properties that are used for command default values
139 public String getApexPropertiesLocation() {
140 if (metadataFileName == null) {
141 return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
143 return "file: \"" + apexPropertiesFileName + "\"";
148 * Gets the input stream on which commands are being received.
150 * @return the input stream on which commands are being received
151 * @throws IOException the IO exception
153 public InputStream getCommandInputStream() throws IOException {
154 if (commandFileName == null) {
157 return new FileInputStream(new File(commandFileName));
162 * Gets the output stream on which command result messages are being output.
164 * @return the output stream on which command result messages are being output
165 * @throws IOException the IO exception
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();
173 if (logFileName == null) {
176 File logFile = new File(logFileName);
177 if (!logFile.getParentFile().exists()) {
178 logFile.getParentFile().mkdirs();
180 return new FileOutputStream(logFile, true);
185 * Check if the file name of the command metadata file for the editor commands is set.
187 * @return true, if the file name of the command metadata file for the editor commands is set
189 public boolean checkSetMetadataFileName() {
190 return metadataFileName != null && metadataFileName.length() > 0;
194 * Check if the file name of the file containing properties that are used for command default values is set.
196 * @return true, if the file name of the file containing properties that are used for command default values is set
198 public boolean checkSetApexPropertiesFileName() {
199 return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
203 * Check if the name of the file containing commands to be streamed into the CLI editor is set.
205 * @return true, if the name of the file containing commands to be streamed into the CLI editor is set
207 public boolean checkSetCommandFileName() {
208 return commandFileName != null && commandFileName.length() > 0;
212 * Check if the name of the file containing the Apex model that will be used to initialize the Apex model in the CLI
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
218 public boolean checkSetInputModelFileName() {
219 return inputModelFileName != null && inputModelFileName.length() > 0;
223 * Check if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set.
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
227 public boolean checkSetOutputModelFileName() {
228 return outputModelFileName != null && outputModelFileName.length() > 0;
232 * Check if the name of the file to which the Apex CLI editor will log commands and responses is set.
234 * @return true, if the name of the file to which the Apex CLI editor will log commands and responses is set
236 public boolean checkSetLogFileName() {
237 return logFileName != null;
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 + "]";