aboutsummaryrefslogtreecommitdiffstats
path: root/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineParameters.java
blob: 87c7a0828775aa43ac107d06613aa55b14e498d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.auth.clieditor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import lombok.Getter;
import lombok.Setter;
import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
import org.onap.policy.common.utils.resources.ResourceUtils;

/**
 * This class reads and handles command line parameters to the Apex CLI editor.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
@Setter
@Getter
public class CommandLineParameters {

    // Default location of the command definition meta data in JSON
    private static final String JSON_COMMAND_METADATA_RESOURCE = "etc/editor/Commands.json";
    private static final String APEX_MODEL_PROPERTIES_RESOURCE = "etc/editor/ApexModelProperties.json";

    // The editor parameters
    private boolean helpSet = false;
    private String metadataFileName = null;
    private String apexPropertiesFileName = null;
    private String commandFileName = null;
    private String inputModelFileName = null;
    private String outputModelFileName = null;
    private String workingDirectory = null;
    private String logFileName = null;
    private boolean echo = false;
    private boolean suppressLog = false;
    private boolean suppressModelOutput = false;
    private boolean ignoreCommandFailuresSet = false;
    private boolean ignoreCommandFailures = false;

    /**
     * Validates the command line parameters.
     */
    public void validate() {
        CliUtils.validateReadableFile("Metadata File", metadataFileName);
        CliUtils.validateReadableFile("Properties File", apexPropertiesFileName);
        CliUtils.validateReadableFile("Command File", commandFileName);
        CliUtils.validateReadableFile("Input Model File", inputModelFileName);
        CliUtils.validateWritableFile("Output Model File", outputModelFileName);
        CliUtils.validateWritableFile("Log File", logFileName);
        CliUtils.validateWritableDirectory("Working Directory", workingDirectory);

        if (isSuppressLog()) {
            setEcho(false);
        } else {
            if (checkSetCommandFileName()) {
                setEcho(true);
                if (!isIgnoreCommandFailuresSet()) {
                    setIgnoreCommandFailuresSet(false);
                }
            } else {
                setEcho(false);
                if (!isIgnoreCommandFailuresSet()) {
                    setIgnoreCommandFailuresSet(true);
                }
            }
        }
    }

    /**
     * Gets the command metadata for the editor commands as a stream.
     *
     * @return the command metadata for the editor commands as a stream.
     * @throws IOException the IO exception
     */
    public InputStream getMetadataStream() throws IOException {
        if (metadataFileName == null) {
            return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
        } else {
            return new FileInputStream(new File(metadataFileName));
        }
    }

    /**
     * Gets the location of command metadata for the editor commands.
     *
     * @return the location of command metadata for the editor commands
     */
    public String getMetadataLocation() {
        if (metadataFileName == null) {
            return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
        } else {
            return "file: \"" + metadataFileName + "\"";
        }
    }

    /**
     * Gets the properties that are used for command default values as a stream.
     *
     * @return the properties that are used for command default values as a stream
     * @throws IOException the IO exception
     */
    public InputStream getApexPropertiesStream() throws IOException {
        if (apexPropertiesFileName == null) {
            return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
        } else {
            return new FileInputStream(new File(apexPropertiesFileName));
        }
    }

    /**
     * Gets the location of the properties that are used for command default values.
     *
     * @return the location of the properties that are used for command default values
     */
    public String getApexPropertiesLocation() {
        if (metadataFileName == null) {
            return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
        } else {
            return "file: \"" + apexPropertiesFileName + "\"";
        }
    }

    /**
     * Gets the input stream on which commands are being received.
     *
     * @return the input stream on which commands are being received
     * @throws IOException the IO exception
     */
    public InputStream getCommandInputStream() throws IOException {
        if (commandFileName == null) {
            return System.in;
        } else {
            return new FileInputStream(new File(commandFileName));
        }
    }

    /**
     * Gets the output stream on which command result messages are being output.
     *
     * @return the output stream on which command result messages are being output
     * @throws IOException the IO exception
     */
    public OutputStream getOutputStream() throws IOException {
        // Check if log suppression is active, if so, consume all output on a byte array output stream
        if (isSuppressLog()) {
            return new ByteArrayOutputStream();

        }
        if (logFileName == null) {
            return System.out;
        } else {
            return new FileOutputStream(new File(logFileName), true);
        }
    }

    /**
     * Check if the file name of the command metadata file for the editor commands is set.
     *
     * @return true, if the file name of the command metadata file for the editor commands is set
     */
    public boolean checkSetMetadataFileName() {
        return metadataFileName != null && metadataFileName.length() > 0;
    }

    /**
     * Check if the file name of the file containing properties that are used for command default
     * values is set.
     *
     * @return true, if the file name of the file containing properties that are used for command
     *         default values is set
     */
    public boolean checkSetApexPropertiesFileName() {
        return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
    }

    /**
     * Check if the name of the file containing commands to be streamed into the CLI editor is set.
     *
     * @return true, if the name of the file containing commands to be streamed into the CLI editor
     *         is set
     */
    public boolean checkSetCommandFileName() {
        return commandFileName != null && commandFileName.length() > 0;
    }

    /**
     * Check if the name of the file containing the Apex model that will be used to initialize the
     * Apex model in the CLI editor is set.
     *
     * @return true, if the name of the file containing the Apex model that will be used to
     *         initialize the Apex model in the CLI editor is set
     */
    public boolean checkSetInputModelFileName() {
        return inputModelFileName != null && inputModelFileName.length() > 0;
    }

    /**
     * Check if the name of the file that the Apex CLI editor will save the Apex model to when it
     * exits is set.
     *
     * @return true, if the name of the file that the Apex CLI editor will save the Apex model to
     *         when it exits is set
     */
    public boolean checkSetOutputModelFileName() {
        return outputModelFileName != null && outputModelFileName.length() > 0;
    }

    /**
     * Check if the name of the file to which the Apex CLI editor will log commands and responses is
     * set.
     *
     * @return true, if the name of the file to which the Apex CLI editor will log commands and
     *         responses is set
     */
    public boolean checkSetLogFileName() {
        return logFileName != null;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String toString() {
        return "CLIParameters [helpSet=" + helpSet + ", metadataFileName=" + metadataFileName
                + ", apexPropertiesFileName=" + apexPropertiesFileName + ", commandFileName=" + commandFileName
                + ", inputModelFileName=" + inputModelFileName + ", outputModelFileName=" + outputModelFileName
                + ", logFileName=" + logFileName + ", echo=" + echo + ", suppressLog=" + suppressLog
                + ", suppressModelOutput=" + suppressModelOutput + "]";
    }
}