aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParameters.java
blob: 73438cc7e335224d66415e06ca55847798192cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin;

import java.io.File;

import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
import org.onap.policy.common.parameters.GroupValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.common.utils.validation.ParameterValidationUtils;

/**
 * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
 * and output.
 *
 * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
 * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
 * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
 * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
 * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
 * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
 * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
    // @formatter:off
    /** The label of this carrier technology. */
    public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";

    /** The producer plugin class for the FILE carrier technology. */
    public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getCanonicalName();

    /** The consumer plugin class for the FILE carrier technology. */
    public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getCanonicalName();

    // Recurring strings
    private static final String FILE_NAME_TOKEN = "fileName";

    private String fileName;
    private boolean standardIo = false;
    private boolean standardError = false;
    private boolean streamingMode = false;
    private long startDelay = 0;
    // @formatter:on

    /**
     * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
     * service.
     */
    public FileCarrierTechnologyParameters() {
        super();

        // Set the carrier technology properties for the FILE carrier technology
        this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
        this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
        this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
    }

    /**
     * Gets the file name from which to read or to which to write events.
     *
     * @return the file name from which to read or to which to write events
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * Checks if is standard IO should be used for input or output.
     *
     * @return true, if standard IO should be used for input or output
     */
    public boolean isStandardIo() {
        return standardIo;
    }

    /**
     * Checks if is standard error should be used for output.
     *
     * @return true, if standard error should be used for output
     */
    public boolean isStandardError() {
        return standardError;
    }

    /**
     * Checks if is streaming mode is on.
     *
     * @return true, if streaming mode is on
     */
    public boolean isStreamingMode() {
        return streamingMode;
    }

    /**
     * Sets the file name from which to read or to which to write events.
     *
     * @param fileName the file name from which to read or to which to write events
     */
    public void setFileName(final String fileName) {
        this.fileName = fileName;
    }

    /**
     * Sets if standard IO should be used for event input or output.
     *
     * @param standardIo if standard IO should be used for event input or output
     */
    public void setStandardIo(final boolean standardIo) {
        this.standardIo = standardIo;
    }

    /**
     * Sets if standard error should be used for event output.
     *
     * @param standardError if standard error should be used for event output
     */
    public void setStandardError(final boolean standardError) {
        this.standardError = standardError;
    }

    /**
     * Sets streaming mode.
     *
     * @param streamingMode the streaming mode value
     */
    public void setStreamingMode(final boolean streamingMode) {
        this.streamingMode = streamingMode;
    }

    /**
     * Gets the delay in milliseconds before the plugin starts processing.
     * 
     * @return the delay
     */
    public long getStartDelay() {
        return startDelay;
    }

    /**
     * Sets the delay in milliseconds before the plugin starts processing.
     * 
     * @param startDelay the delay
     */
    public void setStartDelay(final long startDelay) {
        this.startDelay = startDelay;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters# toString()
     */
    @Override
    public String toString() {
        return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
                        + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
                        + startDelay + "]";
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.onap.policy.common.parameters.ParameterGroup#getName()
     */
    @Override
    public String getName() {
        return this.getLabel();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
     */
    @Override
    public GroupValidationResult validate() {
        final GroupValidationResult result = super.validate();

        if (!standardIo && !standardError) {
            validateFileName(result);
        }

        if (standardIo || standardError) {
            streamingMode = true;
        }

        if (startDelay < 0) {
            result.setResult("startDelay", ValidationStatus.INVALID,
                            "startDelay must be zero or a positive number of milliseconds");
        }

        return result;
    }
    

    /**
     * Validate the file name parameter.
     * 
     * @param result the variable in which to store the result of the validation
     */
    private void validateFileName(final GroupValidationResult result) {
        if (!ParameterValidationUtils.validateStringParameter(fileName)) {
            result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID,
                            "\"" + fileName + "\" invalid, must be specified as a non-empty string");
            return;
        }

        String absoluteFileName = null;

        // Resolve the file name if it is a relative file name
        File theFile = new File(fileName);
        if (theFile.isAbsolute()) {
            absoluteFileName = fileName;
        } else {
            absoluteFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator + fileName;
            theFile = new File(absoluteFileName);
        }

        // Check if the file exists, the file should be a regular file and should be readable
        if (theFile.exists()) {
            validateExistingFile(result, absoluteFileName, theFile);
        }
        // The path to the file should exist and should be writable
        else {
            validateNewFileParent(result, absoluteFileName, theFile);
        }
    }

    /**
     * Validate an existing file is OK.
     * 
     * @param result the result of the validation
     * @param absoluteFileName the absolute file name of the file
     * @param theFile the file that exists
     */
    private void validateExistingFile(final GroupValidationResult result, String absoluteFileName, File theFile) {
        // Check that the file is a regular file
        if (!theFile.isFile()) {
            result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not a plain file");
        }
        else {
            fileName = absoluteFileName;

            if (!theFile.canRead()) {
                result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
            }
        }
    }

    /**
     * Validate the parent of a new file is OK.
     * 
     * @param result the result of the validation
     * @param absoluteFileName the absolute file name of the file
     * @param theFile the file that exists
     */
    private void validateNewFileParent(final GroupValidationResult result, String absoluteFileName, File theFile) {
        // Check that the parent of the file is a directory
        if (!theFile.getParentFile().exists()) {
            result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file does not exist");
        }
        // Check that the parent of the file is a directory
        else if (!theFile.getParentFile().isDirectory()) {
            result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file is not directory");
        }
        else {
            fileName = absoluteFileName;

            if (!theFile.getParentFile().canRead()) {
                result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable");
            }
        }
    }
}