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 lombok.ToString;
28 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
29 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
30 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.common.parameters.ObjectValidationResult;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.common.parameters.ValidationStatus;
35 import org.onap.policy.common.parameters.annotations.Min;
36 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
37 import org.onap.policy.models.base.Validated;
40 * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
43 * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
44 * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
45 * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
46 * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
47 * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
48 * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
49 * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
51 * @author Liam Fallon (liam.fallon@ericsson.com)
56 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
58 /** The label of this carrier technology. */
59 public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
61 /** The producer plugin class for the FILE carrier technology. */
62 public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
64 /** The consumer plugin class for the FILE carrier technology. */
65 public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
68 private static final String FILE_NAME_TOKEN = "fileName";
70 private String fileName;
71 private boolean standardIo = false;
72 private boolean standardError = false;
73 private boolean streamingMode = false;
74 private @Min(0) long startDelay = 0;
78 * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
81 public FileCarrierTechnologyParameters() {
84 // Set the carrier technology properties for the FILE carrier technology
85 this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
86 this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
87 this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
94 public String getName() {
95 return this.getLabel();
102 public BeanValidationResult validate() {
103 final BeanValidationResult result = super.validate();
105 if (!standardIo && !standardError) {
106 result.addResult(validateFileName());
109 if (standardIo || standardError) {
110 streamingMode = true;
118 * Validate the file name parameter.
120 * @return the result of the validation
122 private ValidationResult validateFileName() {
123 if (!ParameterValidationUtils.validateStringParameter(fileName)) {
124 return new ObjectValidationResult(FILE_NAME_TOKEN, fileName, ValidationStatus.INVALID, Validated.IS_BLANK);
127 String absoluteFileName = null;
129 // Resolve the file name if it is a relative file name
130 File theFile = new File(fileName);
131 if (theFile.isAbsolute()) {
132 absoluteFileName = fileName;
134 absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
135 theFile = new File(absoluteFileName);
138 // Check if the file exists, the file should be a regular file and should be readable
139 if (theFile.exists()) {
140 return validateExistingFile(absoluteFileName, theFile);
142 // The path to the file should exist and should be writable
143 return validateNewFileParent(absoluteFileName, theFile);
148 * Validate an existing file is OK.
150 * @param absoluteFileName the absolute file name of the file
151 * @param theFile the file that exists
152 * @return the result of the validation
154 private ValidationResult validateExistingFile(String absoluteFileName, File theFile) {
155 // Check that the file is a regular file
156 if (!theFile.isFile()) {
157 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
158 "is not a plain file");
161 fileName = absoluteFileName;
163 if (!theFile.canRead()) {
164 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
173 * Validate the parent of a new file is OK.
175 * @param absoluteFileName the absolute file name of the file
176 * @param theFile the file that exists
177 * @return the result of the validation
179 private ValidationResult validateNewFileParent(String absoluteFileName, File theFile) {
180 // Check that the parent of the file is a directory
181 if (!theFile.getParentFile().exists()) {
182 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
183 "parent of file does not exist");
185 } else if (!theFile.getParentFile().isDirectory()) {
186 // Check that the parent of the file is a directory
187 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
188 "parent of file is not directory");
191 fileName = absoluteFileName;
193 if (!theFile.getParentFile().canRead()) {
194 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,