31cd28d8d070fbdcea83bd31ffbf16e3ab79e593
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin;
22
23 import java.io.File;
24
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;
31
32 /**
33  * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
34  * and output.
35  *
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>
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
47     // @formatter:off
48     /** The label of this carrier technology. */
49     public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
50
51     /** The producer plugin class for the FILE carrier technology. */
52     public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
53
54     /** The consumer plugin class for the FILE carrier technology. */
55     public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
56
57     // Recurring strings
58     private static final String FILE_NAME_TOKEN = "fileName";
59
60     private String fileName;
61     private boolean standardIo = false;
62     private boolean standardError = false;
63     private boolean streamingMode = false;
64     private long startDelay = 0;
65     // @formatter:on
66
67     /**
68      * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
69      * service.
70      */
71     public FileCarrierTechnologyParameters() {
72         super();
73
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);
78     }
79
80     /**
81      * Gets the file name from which to read or to which to write events.
82      *
83      * @return the file name from which to read or to which to write events
84      */
85     public String getFileName() {
86         return fileName;
87     }
88
89     /**
90      * Checks if is standard IO should be used for input or output.
91      *
92      * @return true, if standard IO should be used for input or output
93      */
94     public boolean isStandardIo() {
95         return standardIo;
96     }
97
98     /**
99      * Checks if is standard error should be used for output.
100      *
101      * @return true, if standard error should be used for output
102      */
103     public boolean isStandardError() {
104         return standardError;
105     }
106
107     /**
108      * Checks if is streaming mode is on.
109      *
110      * @return true, if streaming mode is on
111      */
112     public boolean isStreamingMode() {
113         return streamingMode;
114     }
115
116     /**
117      * Sets the file name from which to read or to which to write events.
118      *
119      * @param fileName the file name from which to read or to which to write events
120      */
121     public void setFileName(final String fileName) {
122         this.fileName = fileName;
123     }
124
125     /**
126      * Sets if standard IO should be used for event input or output.
127      *
128      * @param standardIo if standard IO should be used for event input or output
129      */
130     public void setStandardIo(final boolean standardIo) {
131         this.standardIo = standardIo;
132     }
133
134     /**
135      * Sets if standard error should be used for event output.
136      *
137      * @param standardError if standard error should be used for event output
138      */
139     public void setStandardError(final boolean standardError) {
140         this.standardError = standardError;
141     }
142
143     /**
144      * Sets streaming mode.
145      *
146      * @param streamingMode the streaming mode value
147      */
148     public void setStreamingMode(final boolean streamingMode) {
149         this.streamingMode = streamingMode;
150     }
151
152     /**
153      * Gets the delay in milliseconds before the plugin starts processing.
154      * 
155      * @return the delay
156      */
157     public long getStartDelay() {
158         return startDelay;
159     }
160
161     /**
162      * Sets the delay in milliseconds before the plugin starts processing.
163      * 
164      * @param startDelay the delay
165      */
166     public void setStartDelay(final long startDelay) {
167         this.startDelay = startDelay;
168     }
169
170     /*
171      * (non-Javadoc)
172      *
173      * @see org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters# toString()
174      */
175     @Override
176     public String toString() {
177         return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
178                         + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
179                         + startDelay + "]";
180     }
181
182     /*
183      * (non-Javadoc)
184      * 
185      * @see org.onap.policy.common.parameters.ParameterGroup#getName()
186      */
187     @Override
188     public String getName() {
189         return this.getLabel();
190     }
191
192     /*
193      * (non-Javadoc)
194      *
195      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
196      */
197     @Override
198     public GroupValidationResult validate() {
199         final GroupValidationResult result = super.validate();
200
201         if (!standardIo && !standardError) {
202             validateFileName(result);
203         }
204
205         if (standardIo || standardError) {
206             streamingMode = true;
207         }
208
209         if (startDelay < 0) {
210             result.setResult("startDelay", ValidationStatus.INVALID,
211                             "startDelay must be zero or a positive number of milliseconds");
212         }
213
214         return result;
215     }
216     
217
218     /**
219      * Validate the file name parameter.
220      * 
221      * @param result the variable in which to store the result of the validation
222      */
223     private void validateFileName(final GroupValidationResult result) {
224         if (!ParameterValidationUtils.validateStringParameter(fileName)) {
225             result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID,
226                             "\"" + fileName + "\" invalid, must be specified as a non-empty string");
227             return;
228         }
229
230         String absoluteFileName = null;
231
232         // Resolve the file name if it is a relative file name
233         File theFile = new File(fileName);
234         if (theFile.isAbsolute()) {
235             absoluteFileName = fileName;
236         } else {
237             absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
238             theFile = new File(absoluteFileName);
239         }
240
241         // Check if the file exists, the file should be a regular file and should be readable
242         if (theFile.exists()) {
243             validateExistingFile(result, absoluteFileName, theFile);
244         }
245         // The path to the file should exist and should be writable
246         else {
247             validateNewFileParent(result, absoluteFileName, theFile);
248         }
249     }
250
251     /**
252      * Validate an existing file is OK.
253      * 
254      * @param result the result of the validation
255      * @param absoluteFileName the absolute file name of the file
256      * @param theFile the file that exists
257      */
258     private void validateExistingFile(final GroupValidationResult result, String absoluteFileName, File theFile) {
259         // Check that the file is a regular file
260         if (!theFile.isFile()) {
261             result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not a plain file");
262         }
263         else {
264             fileName = absoluteFileName;
265
266             if (!theFile.canRead()) {
267                 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
268             }
269         }
270     }
271
272     /**
273      * Validate the parent of a new file is OK.
274      * 
275      * @param result the result of the validation
276      * @param absoluteFileName the absolute file name of the file
277      * @param theFile the file that exists
278      */
279     private void validateNewFileParent(final GroupValidationResult result, String absoluteFileName, File theFile) {
280         // Check that the parent of the file is a directory
281         if (!theFile.getParentFile().exists()) {
282             result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file does not exist");
283         }
284         // Check that the parent of the file is a directory
285         else if (!theFile.getParentFile().isDirectory()) {
286             result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file is not directory");
287         }
288         else {
289             fileName = absoluteFileName;
290
291             if (!theFile.getParentFile().canRead()) {
292                 result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
293             }
294         }
295     }
296 }