2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.service.engine.event.impl.filecarrierplugin;
27 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
28 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
29 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.common.parameters.ObjectValidationResult;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.common.parameters.ValidationStatus;
34 import org.onap.policy.common.parameters.annotations.Min;
35 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
36 import org.onap.policy.models.base.Validated;
39 * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
42 * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
43 * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
44 * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
45 * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
46 * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
47 * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
48 * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
50 * @author Liam Fallon (liam.fallon@ericsson.com)
54 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
56 /** The label of this carrier technology. */
57 public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
59 /** The producer plugin class for the FILE carrier technology. */
60 public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
62 /** The consumer plugin class for the FILE carrier technology. */
63 public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
66 private static final String FILE_NAME_TOKEN = "fileName";
68 private String fileName;
69 private boolean standardIo = false;
70 private boolean standardError = false;
71 private boolean streamingMode = false;
72 private @Min(0) long startDelay = 0;
76 * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
79 public FileCarrierTechnologyParameters() {
82 // Set the carrier technology properties for the FILE carrier technology
83 this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
84 this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
85 this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
92 public String toString() {
93 return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
94 + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
102 public String getName() {
103 return this.getLabel();
110 public BeanValidationResult validate() {
111 final BeanValidationResult result = super.validate();
113 if (!standardIo && !standardError) {
114 result.addResult(validateFileName());
117 if (standardIo || standardError) {
118 streamingMode = true;
126 * Validate the file name parameter.
128 * @return the result of the validation
130 private ValidationResult validateFileName() {
131 if (!ParameterValidationUtils.validateStringParameter(fileName)) {
132 return new ObjectValidationResult(FILE_NAME_TOKEN, fileName, ValidationStatus.INVALID, Validated.IS_BLANK);
135 String absoluteFileName = null;
137 // Resolve the file name if it is a relative file name
138 File theFile = new File(fileName);
139 if (theFile.isAbsolute()) {
140 absoluteFileName = fileName;
142 absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
143 theFile = new File(absoluteFileName);
146 // Check if the file exists, the file should be a regular file and should be readable
147 if (theFile.exists()) {
148 return validateExistingFile(absoluteFileName, theFile);
150 // The path to the file should exist and should be writable
151 return validateNewFileParent(absoluteFileName, theFile);
156 * Validate an existing file is OK.
158 * @param absoluteFileName the absolute file name of the file
159 * @param theFile the file that exists
160 * @return the result of the validation
162 private ValidationResult validateExistingFile(String absoluteFileName, File theFile) {
163 // Check that the file is a regular file
164 if (!theFile.isFile()) {
165 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
166 "is not a plain file");
169 fileName = absoluteFileName;
171 if (!theFile.canRead()) {
172 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
181 * Validate the parent of a new file is OK.
183 * @param absoluteFileName the absolute file name of the file
184 * @param theFile the file that exists
185 * @return the result of the validation
187 private ValidationResult validateNewFileParent(String absoluteFileName, File theFile) {
188 // Check that the parent of the file is a directory
189 if (!theFile.getParentFile().exists()) {
190 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
191 "parent of file does not exist");
193 } else if (!theFile.getParentFile().isDirectory()) {
194 // Check that the parent of the file is a directory
195 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
196 "parent of file is not directory");
199 fileName = absoluteFileName;
201 if (!theFile.getParentFile().canRead()) {
202 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,