eceb0b4f9498e0bec66e6b17823eb5d9ea458192
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin;
23
24 import java.io.File;
25 import lombok.Getter;
26 import lombok.Setter;
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;
37
38 /**
39  * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
40  * and output.
41  *
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>
49  *
50  * @author Liam Fallon (liam.fallon@ericsson.com)
51  */
52 @Getter
53 @Setter
54 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
55     // @formatter:off
56     /** The label of this carrier technology. */
57     public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
58
59     /** The producer plugin class for the FILE carrier technology. */
60     public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
61
62     /** The consumer plugin class for the FILE carrier technology. */
63     public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
64
65     // Recurring strings
66     private static final String FILE_NAME_TOKEN = "fileName";
67
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;
73     // @formatter:on
74
75     /**
76      * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
77      * service.
78      */
79     public FileCarrierTechnologyParameters() {
80         super();
81
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);
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public String toString() {
93         return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
94                         + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
95                         + startDelay + "]";
96     }
97
98     /**
99      * {@inheritDoc}.
100      */
101     @Override
102     public String getName() {
103         return this.getLabel();
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public BeanValidationResult validate() {
111         final BeanValidationResult result = super.validate();
112
113         if (!standardIo && !standardError) {
114             result.addResult(validateFileName());
115         }
116
117         if (standardIo || standardError) {
118             streamingMode = true;
119         }
120
121         return result;
122     }
123
124
125     /**
126      * Validate the file name parameter.
127      *
128      * @return the result of the validation
129      */
130     private ValidationResult validateFileName() {
131         if (!ParameterValidationUtils.validateStringParameter(fileName)) {
132             return new ObjectValidationResult(FILE_NAME_TOKEN, fileName, ValidationStatus.INVALID, Validated.IS_BLANK);
133         }
134
135         String absoluteFileName = null;
136
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;
141         } else {
142             absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
143             theFile = new File(absoluteFileName);
144         }
145
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);
149         } else {
150             // The path to the file should exist and should be writable
151             return validateNewFileParent(absoluteFileName, theFile);
152         }
153     }
154
155     /**
156      * Validate an existing file is OK.
157      *
158      * @param absoluteFileName the absolute file name of the file
159      * @param theFile the file that exists
160      * @return the result of the validation
161      */
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");
167
168         } else {
169             fileName = absoluteFileName;
170
171             if (!theFile.canRead()) {
172                 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
173                                 "is not readable");
174             }
175
176             return null;
177         }
178     }
179
180     /**
181      * Validate the parent of a new file is OK.
182      *
183      * @param absoluteFileName the absolute file name of the file
184      * @param theFile the file that exists
185      * @return the result of the validation
186      */
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");
192
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");
197
198         } else {
199             fileName = absoluteFileName;
200
201             if (!theFile.getParentFile().canRead()) {
202                 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
203                                 "is not readable");
204             }
205
206             return null;
207         }
208     }
209 }