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