Address more sonars in apex-pdp
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / FileCarrierTechnologyParameters.java
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 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;
38
39 /**
40  * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
41  * and output.
42  *
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>
50  *
51  * @author Liam Fallon (liam.fallon@ericsson.com)
52  */
53 @Getter
54 @Setter
55 @ToString
56 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
57     // @formatter:off
58     /** The label of this carrier technology. */
59     public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
60
61     /** The producer plugin class for the FILE carrier technology. */
62     public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getName();
63
64     /** The consumer plugin class for the FILE carrier technology. */
65     public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getName();
66
67     // Recurring strings
68     private static final String FILE_NAME_TOKEN = "fileName";
69
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;
75     // @formatter:on
76
77     /**
78      * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
79      * service.
80      */
81     public FileCarrierTechnologyParameters() {
82         super();
83
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);
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
94     public String getName() {
95         return this.getLabel();
96     }
97
98     /**
99      * {@inheritDoc}.
100      */
101     @Override
102     public BeanValidationResult validate() {
103         final BeanValidationResult result = super.validate();
104
105         if (!standardIo && !standardError) {
106             result.addResult(validateFileName());
107         }
108
109         if (standardIo || standardError) {
110             streamingMode = true;
111         }
112
113         return result;
114     }
115
116
117     /**
118      * Validate the file name parameter.
119      *
120      * @return the result of the validation
121      */
122     private ValidationResult validateFileName() {
123         if (!ParameterValidationUtils.validateStringParameter(fileName)) {
124             return new ObjectValidationResult(FILE_NAME_TOKEN, fileName, ValidationStatus.INVALID, Validated.IS_BLANK);
125         }
126
127         String absoluteFileName = null;
128
129         // Resolve the file name if it is a relative file name
130         var theFile = new File(fileName);
131         if (theFile.isAbsolute()) {
132             absoluteFileName = fileName;
133         } else {
134             absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
135             theFile = new File(absoluteFileName);
136         }
137
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);
141         } else {
142             // The path to the file should exist and should be writable
143             return validateNewFileParent(absoluteFileName, theFile);
144         }
145     }
146
147     /**
148      * Validate an existing file is OK.
149      *
150      * @param absoluteFileName the absolute file name of the file
151      * @param theFile the file that exists
152      * @return the result of the validation
153      */
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");
159
160         } else {
161             fileName = absoluteFileName;
162
163             if (!theFile.canRead()) {
164                 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
165                                 "is not readable");
166             }
167
168             return null;
169         }
170     }
171
172     /**
173      * Validate the parent of a new file is OK.
174      *
175      * @param absoluteFileName the absolute file name of the file
176      * @param theFile the file that exists
177      * @return the result of the validation
178      */
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");
184
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");
189
190         } else {
191             fileName = absoluteFileName;
192
193             if (!theFile.getParentFile().canRead()) {
194                 return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID,
195                                 "is not readable");
196             }
197
198             return null;
199         }
200     }
201 }