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;
24 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
25 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
26 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
27 import org.onap.policy.common.parameters.GroupValidationResult;
28 import org.onap.policy.common.parameters.ValidationStatus;
29 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
32 * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
35 * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
36 * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
37 * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
38 * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
39 * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
40 * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
41 * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
47 /** The label of this carrier technology. */
48 public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
50 /** The producer plugin class for the FILE carrier technology. */
51 public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
53 /** The consumer plugin class for the FILE carrier technology. */
54 public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
57 private static final String FILE_NAME_TOKEN = "fileName";
59 private String fileName;
60 private boolean standardIo = false;
61 private boolean standardError = false;
62 private boolean streamingMode = false;
63 private long startDelay = 0;
67 * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
70 public FileCarrierTechnologyParameters() {
73 // Set the carrier technology properties for the FILE carrier technology
74 this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
75 this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
76 this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
80 * Gets the file name from which to read or to which to write events.
82 * @return the file name from which to read or to which to write events
84 public String getFileName() {
89 * Checks if is standard IO should be used for input or output.
91 * @return true, if standard IO should be used for input or output
93 public boolean isStandardIo() {
98 * Checks if is standard error should be used for output.
100 * @return true, if standard error should be used for output
102 public boolean isStandardError() {
103 return standardError;
107 * Checks if is streaming mode is on.
109 * @return true, if streaming mode is on
111 public boolean isStreamingMode() {
112 return streamingMode;
116 * Sets the file name from which to read or to which to write events.
118 * @param fileName the file name from which to read or to which to write events
120 public void setFileName(final String fileName) {
121 this.fileName = fileName;
125 * Sets if standard IO should be used for event input or output.
127 * @param standardIo if standard IO should be used for event input or output
129 public void setStandardIo(final boolean standardIo) {
130 this.standardIo = standardIo;
134 * Sets if standard error should be used for event output.
136 * @param standardError if standard error should be used for event output
138 public void setStandardError(final boolean standardError) {
139 this.standardError = standardError;
143 * Sets streaming mode.
145 * @param streamingMode the streaming mode value
147 public void setStreamingMode(final boolean streamingMode) {
148 this.streamingMode = streamingMode;
152 * Gets the delay in milliseconds before the plugin starts processing.
156 public long getStartDelay() {
161 * Sets the delay in milliseconds before the plugin starts processing.
163 * @param startDelay the delay
165 public void setStartDelay(final long startDelay) {
166 this.startDelay = startDelay;
173 public String toString() {
174 return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
175 + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
183 public String getName() {
184 return this.getLabel();
191 public GroupValidationResult validate() {
192 final GroupValidationResult result = super.validate();
194 if (!standardIo && !standardError) {
195 validateFileName(result);
198 if (standardIo || standardError) {
199 streamingMode = true;
202 if (startDelay < 0) {
203 result.setResult("startDelay", ValidationStatus.INVALID,
204 "startDelay must be zero or a positive number of milliseconds");
212 * Validate the file name parameter.
214 * @param result the variable in which to store the result of the validation
216 private void validateFileName(final GroupValidationResult result) {
217 if (!ParameterValidationUtils.validateStringParameter(fileName)) {
218 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID,
219 "\"" + fileName + "\" invalid, must be specified as a non-empty string");
223 String absoluteFileName = null;
225 // Resolve the file name if it is a relative file name
226 File theFile = new File(fileName);
227 if (theFile.isAbsolute()) {
228 absoluteFileName = fileName;
230 absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
231 theFile = new File(absoluteFileName);
234 // Check if the file exists, the file should be a regular file and should be readable
235 if (theFile.exists()) {
236 validateExistingFile(result, absoluteFileName, theFile);
238 // The path to the file should exist and should be writable
239 validateNewFileParent(result, absoluteFileName, theFile);
244 * Validate an existing file is OK.
246 * @param result the result of the validation
247 * @param absoluteFileName the absolute file name of the file
248 * @param theFile the file that exists
250 private void validateExistingFile(final GroupValidationResult result, String absoluteFileName, File theFile) {
251 // Check that the file is a regular file
252 if (!theFile.isFile()) {
253 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not a plain file");
255 fileName = absoluteFileName;
257 if (!theFile.canRead()) {
258 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
264 * Validate the parent of a new file is OK.
266 * @param result the result of the validation
267 * @param absoluteFileName the absolute file name of the file
268 * @param theFile the file that exists
270 private void validateNewFileParent(final GroupValidationResult result, String absoluteFileName, File theFile) {
271 // Check that the parent of the file is a directory
272 if (!theFile.getParentFile().exists()) {
273 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file does not exist");
274 } else if (!theFile.getParentFile().isDirectory()) {
275 // Check that the parent of the file is a directory
276 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file is not directory");
278 fileName = absoluteFileName;
280 if (!theFile.getParentFile().canRead()) {
281 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");