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