2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin;
25 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
26 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ValidationStatus;
30 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
33 * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
36 * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
37 * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
38 * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
39 * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
40 * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
41 * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
42 * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
48 /** The label of this carrier technology. */
49 public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
51 /** The producer plugin class for the FILE carrier technology. */
52 public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
54 /** The consumer plugin class for the FILE carrier technology. */
55 public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
58 private static final String FILE_NAME_TOKEN = "fileName";
60 private String fileName;
61 private boolean standardIo = false;
62 private boolean standardError = false;
63 private boolean streamingMode = false;
64 private long startDelay = 0;
68 * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
71 public FileCarrierTechnologyParameters() {
74 // Set the carrier technology properties for the FILE carrier technology
75 this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
76 this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
77 this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
81 * Gets the file name from which to read or to which to write events.
83 * @return the file name from which to read or to which to write events
85 public String getFileName() {
90 * Checks if is standard IO should be used for input or output.
92 * @return true, if standard IO should be used for input or output
94 public boolean isStandardIo() {
99 * Checks if is standard error should be used for output.
101 * @return true, if standard error should be used for output
103 public boolean isStandardError() {
104 return standardError;
108 * Checks if is streaming mode is on.
110 * @return true, if streaming mode is on
112 public boolean isStreamingMode() {
113 return streamingMode;
117 * Sets the file name from which to read or to which to write events.
119 * @param fileName the file name from which to read or to which to write events
121 public void setFileName(final String fileName) {
122 this.fileName = fileName;
126 * Sets if standard IO should be used for event input or output.
128 * @param standardIo if standard IO should be used for event input or output
130 public void setStandardIo(final boolean standardIo) {
131 this.standardIo = standardIo;
135 * Sets if standard error should be used for event output.
137 * @param standardError if standard error should be used for event output
139 public void setStandardError(final boolean standardError) {
140 this.standardError = standardError;
144 * Sets streaming mode.
146 * @param streamingMode the streaming mode value
148 public void setStreamingMode(final boolean streamingMode) {
149 this.streamingMode = streamingMode;
153 * Gets the delay in milliseconds before the plugin starts processing.
157 public long getStartDelay() {
162 * Sets the delay in milliseconds before the plugin starts processing.
164 * @param startDelay the delay
166 public void setStartDelay(final long startDelay) {
167 this.startDelay = startDelay;
174 public String toString() {
175 return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
176 + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
184 public String getName() {
185 return this.getLabel();
192 public GroupValidationResult validate() {
193 final GroupValidationResult result = super.validate();
195 if (!standardIo && !standardError) {
196 validateFileName(result);
199 if (standardIo || standardError) {
200 streamingMode = true;
203 if (startDelay < 0) {
204 result.setResult("startDelay", ValidationStatus.INVALID,
205 "startDelay must be zero or a positive number of milliseconds");
213 * Validate the file name parameter.
215 * @param result the variable in which to store the result of the validation
217 private void validateFileName(final GroupValidationResult result) {
218 if (!ParameterValidationUtils.validateStringParameter(fileName)) {
219 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID,
220 "\"" + fileName + "\" invalid, must be specified as a non-empty string");
224 String absoluteFileName = null;
226 // Resolve the file name if it is a relative file name
227 File theFile = new File(fileName);
228 if (theFile.isAbsolute()) {
229 absoluteFileName = fileName;
231 absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
232 theFile = new File(absoluteFileName);
235 // Check if the file exists, the file should be a regular file and should be readable
236 if (theFile.exists()) {
237 validateExistingFile(result, absoluteFileName, theFile);
239 // The path to the file should exist and should be writable
241 validateNewFileParent(result, absoluteFileName, theFile);
246 * Validate an existing file is OK.
248 * @param result the result of the validation
249 * @param absoluteFileName the absolute file name of the file
250 * @param theFile the file that exists
252 private void validateExistingFile(final GroupValidationResult result, String absoluteFileName, File theFile) {
253 // Check that the file is a regular file
254 if (!theFile.isFile()) {
255 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not a plain file");
258 fileName = absoluteFileName;
260 if (!theFile.canRead()) {
261 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
267 * Validate the parent of a new file is OK.
269 * @param result the result of the validation
270 * @param absoluteFileName the absolute file name of the file
271 * @param theFile the file that exists
273 private void validateNewFileParent(final GroupValidationResult result, String absoluteFileName, File theFile) {
274 // Check that the parent of the file is a directory
275 if (!theFile.getParentFile().exists()) {
276 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file does not exist");
278 // Check that the parent of the file is a directory
279 else if (!theFile.getParentFile().isDirectory()) {
280 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file is not directory");
283 fileName = absoluteFileName;
285 if (!theFile.getParentFile().canRead()) {
286 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");