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;
35 import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
39 * This class reads and handles command line parameters to the Apex CLI editor.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class CommandLineParameters {
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";
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;
67 * Validates the command line parameters.
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);
78 if (isSuppressLog()) {
81 if (checkSetCommandFileName()) {
83 if (!isIgnoreCommandFailuresSet()) {
84 setIgnoreCommandFailuresSet(false);
88 if (!isIgnoreCommandFailuresSet()) {
89 setIgnoreCommandFailuresSet(true);
96 * Gets the command metadata for the editor commands as a stream.
98 * @return the command metadata for the editor commands as a stream.
99 * @throws IOException the IO exception
101 public InputStream getMetadataStream() throws IOException {
102 if (metadataFileName == null) {
103 return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
105 return new FileInputStream(new File(metadataFileName));
110 * Gets the location of command metadata for the editor commands.
112 * @return the location of command metadata for the editor commands
114 public String getMetadataLocation() {
115 if (metadataFileName == null) {
116 return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
118 return "file: \"" + metadataFileName + "\"";
123 * Gets the properties that are used for command default values as a stream.
125 * @return the properties that are used for command default values as a stream
126 * @throws IOException the IO exception
128 public InputStream getApexPropertiesStream() throws IOException {
129 if (apexPropertiesFileName == null) {
130 return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
132 return new FileInputStream(new File(apexPropertiesFileName));
137 * Gets the location of the properties that are used for command default values.
139 * @return the location of the properties that are used for command default values
141 public String getApexPropertiesLocation() {
142 if (metadataFileName == null) {
143 return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
145 return "file: \"" + apexPropertiesFileName + "\"";
150 * Gets the input stream on which commands are being received.
152 * @return the input stream on which commands are being received
153 * @throws IOException the IO exception
155 public InputStream getCommandInputStream() throws IOException {
156 if (commandFileName == null) {
159 return new FileInputStream(new File(commandFileName));
164 * Gets the output stream on which command result messages are being output.
166 * @return the output stream on which command result messages are being output
167 * @throws IOException the IO exception
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();
175 if (logFileName == null) {
178 File logFile = new File(logFileName);
179 if (!logFile.getParentFile().exists()) {
180 logFile.getParentFile().mkdirs();
182 return new FileOutputStream(logFile, true);
187 * Check if the file name of the command metadata file for the editor commands is set.
189 * @return true, if the file name of the command metadata file for the editor commands is set
191 public boolean checkSetMetadataFileName() {
192 return metadataFileName != null && metadataFileName.length() > 0;
196 * Check if the file name of the file containing properties that are used for command default values is set.
198 * @return true, if the file name of the file containing properties that are used for command default values is set
200 public boolean checkSetApexPropertiesFileName() {
201 return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
205 * Check if the name of the file containing commands to be streamed into the CLI editor is set.
207 * @return true, if the name of the file containing commands to be streamed into the CLI editor is set
209 public boolean checkSetCommandFileName() {
210 return commandFileName != null && commandFileName.length() > 0;
214 * Check if the name of the file containing the Apex model that will be used to initialize the Apex model in the CLI
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
220 public boolean checkSetInputModelFileName() {
221 return inputModelFileName != null && inputModelFileName.length() > 0;
225 * Check if the name of the file that the Apex CLI editor will save the Apex model to when it exits is set.
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
229 public boolean checkSetOutputModelFileName() {
230 return outputModelFileName != null && outputModelFileName.length() > 0;
234 * Check if the name of the file to which the Apex CLI editor will log commands and responses is set.
236 * @return true, if the name of the file to which the Apex CLI editor will log commands and responses is set
238 public boolean checkSetLogFileName() {
239 return logFileName != null;
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 + "]";