cbfe18016ce025b7f89bf03234b0f0edde4c609a
[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 org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
24 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
25 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
28 import org.onap.policy.common.utils.resources.ResourceUtils;
29
30 /**
31  * This class holds the parameters that allows transport of events into and out of Apex using files and standard input
32  * and output.
33  *
34  * <p>The following parameters are defined: <ol> <li>fileName: The full path to the file from which to read events or to
35  * which to write events. <li>standardIO: If this flag is set to true, then standard input is used to read events in or
36  * standard output is used to write events and the fileName parameter is ignored if present <li>standardError: If this
37  * flag is set to true, then standard error is used to write events <li>streamingMode: If this flag is set to true, then
38  * streaming mode is set for reading events and event handling will wait on the input stream for events until the stream
39  * is closed. If streaming model is off, then event reading completes when the end of input is detected. <li>startDelay:
40  * The amount of milliseconds to wait at startup startup before processing the first event. </ol>
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters {
45     // @formatter:off
46     /** The label of this carrier technology. */
47     public static final String FILE_CARRIER_TECHNOLOGY_LABEL = "FILE";
48
49     /** The producer plugin class for the FILE carrier technology. */
50     public static final String FILE_EVENT_PRODUCER_PLUGIN_CLASS = ApexFileEventProducer.class.getCanonicalName();
51
52     /** The consumer plugin class for the FILE carrier technology. */
53     public static final String FILE_EVENT_CONSUMER_PLUGIN_CLASS = ApexFileEventConsumer.class.getCanonicalName();
54
55     private String fileName;
56     private boolean standardIo = false;
57     private boolean standardError = false;
58     private boolean streamingMode = false;
59     private long startDelay = 0;
60     // @formatter:on
61
62     /**
63      * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
64      * service.
65      */
66     public FileCarrierTechnologyParameters() {
67         super();
68
69         // Set the carrier technology properties for the FILE carrier technology
70         this.setLabel(FILE_CARRIER_TECHNOLOGY_LABEL);
71         this.setEventProducerPluginClass(FILE_EVENT_PRODUCER_PLUGIN_CLASS);
72         this.setEventConsumerPluginClass(FILE_EVENT_CONSUMER_PLUGIN_CLASS);
73     }
74
75     /**
76      * Gets the file name from which to read or to which to write events.
77      *
78      * @return the file name from which to read or to which to write events
79      */
80     public String getFileName() {
81         return ResourceUtils.getFilePath4Resource(fileName);
82     }
83
84     /**
85      * Checks if is standard IO should be used for input or output.
86      *
87      * @return true, if standard IO should be used for input or output
88      */
89     public boolean isStandardIo() {
90         return standardIo;
91     }
92
93     /**
94      * Checks if is standard error should be used for output.
95      *
96      * @return true, if standard error should be used for output
97      */
98     public boolean isStandardError() {
99         return standardError;
100     }
101
102     /**
103      * Checks if is streaming mode is on.
104      *
105      * @return true, if streaming mode is on
106      */
107     public boolean isStreamingMode() {
108         return streamingMode;
109     }
110
111     /**
112      * Sets the file name from which to read or to which to write events.
113      *
114      * @param fileName the file name from which to read or to which to write events
115      */
116     public void setFileName(final String fileName) {
117         this.fileName = fileName;
118     }
119
120     /**
121      * Sets if standard IO should be used for event input or output.
122      *
123      * @param standardIo if standard IO should be used for event input or output
124      */
125     public void setStandardIo(final boolean standardIo) {
126         this.standardIo = standardIo;
127     }
128
129     /**
130      * Sets if standard error should be used for event output.
131      *
132      * @param standardError if standard error should be used for event output
133      */
134     public void setStandardError(final boolean standardError) {
135         this.standardError = standardError;
136     }
137
138     /**
139      * Sets streaming mode.
140      *
141      * @param streamingMode the streaming mode value
142      */
143     public void setStreamingMode(final boolean streamingMode) {
144         this.streamingMode = streamingMode;
145     }
146
147     /**
148      * Gets the delay in milliseconds before the plugin starts processing.
149      * 
150      * @return the delay
151      */
152     public long getStartDelay() {
153         return startDelay;
154     }
155
156     /**
157      * Sets the delay in milliseconds before the plugin starts processing.
158      * 
159      * @param startDelay the delay
160      */
161     public void setStartDelay(final long startDelay) {
162         this.startDelay = startDelay;
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters# toString()
169      */
170     @Override
171     public String toString() {
172         return "FILECarrierTechnologyParameters [fileName=" + fileName + ", standardIO=" + standardIo
173                         + ", standardError=" + standardError + ", streamingMode=" + streamingMode + ", startDelay="
174                         + startDelay + "]";
175     }
176
177     /*
178      * (non-Javadoc)
179      * 
180      * @see org.onap.policy.common.parameters.ParameterGroup#getName()
181      */
182     @Override
183     public String getName() {
184         return this.getLabel();
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
191      */
192     @Override
193     public GroupValidationResult validate() {
194         final GroupValidationResult result = super.validate();
195
196         if (!standardIo && !standardError && (fileName == null || fileName.trim().length() == 0)) {
197             result.setResult("fileName", ValidationStatus.INVALID, "fileName not specified or is blank or null, "
198                             + "it must be specified as a valid file location");
199         }
200
201         if (standardIo || standardError) {
202             streamingMode = true;
203         }
204
205         if (startDelay < 0) {
206             result.setResult("startDelay", ValidationStatus.INVALID,
207                             "startDelay must be zero or a positive number of milliseconds");
208         }
209
210         return result;
211     }
212 }