afd877fd271e7d82c3262f671538a42a730b4f39
[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.parameters.eventhandler;
22
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
25
26 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.parameters.ParameterGroup;
31 import org.onap.policy.common.parameters.ValidationStatus;
32
33 /**
34  * The parameters for a single event producer, event consumer or synchronous event handler.
35  * 
36  * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so
37  * the actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one.
38  * 
39  * <p>The following parameters are defined: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of
40  * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The
41  * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology
42  * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false <li>synchronousPeer:
43  * the peer event handler (consumer for producer or producer for consumer) of this event handler in synchronous mode
44  * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
45  * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false <li>requestorPeer: the
46  * peer event handler (consumer for producer or producer for consumer) of this event handler in requestor mode
47  * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
48  * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified, events not matching
49  * the given regular expression are ignored. If it is null, all events are handledDefaults to null. </ol>
50  *
51  * @author Liam Fallon (liam.fallon@ericsson.com)
52  */
53 public class EventHandlerParameters implements ParameterGroup {
54     private String name = null;
55     private CarrierTechnologyParameters carrierTechnologyParameters = null;
56     private EventProtocolParameters eventProtocolParameters = null;
57     private boolean synchronousMode = false;
58     private String synchronousPeer = null;
59     private long synchronousTimeout = 0;
60     private boolean requestorMode = false;
61     private String requestorPeer = null;
62     private long requestorTimeout = 0;
63     private String eventName = null;
64     private String eventNameFilter = null;
65
66     /**
67      * Constructor to create an event handler parameters instance.
68      */
69     public EventHandlerParameters() {
70         super();
71
72         // Set the name for the parameters
73         this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
74     }
75
76     /**
77      * Gets the name of the event handler.
78      *
79      * @return the event handler name
80      */
81     public String getName() {
82         return name;
83     }
84
85     /**
86      * Sets the name of the event handler.
87      *
88      * @param name the event handler name
89      */
90     public void setName(final String name) {
91         this.name = name;
92     }
93
94     /**
95      * Checks if the name of the event handler is set.
96      * 
97      * @return true if the name is set
98      */
99     public boolean checkSetName() {
100         return !(name == null || name.trim().length() == 0);
101     }
102
103     /**
104      * Gets the carrier technology parameters of the event handler.
105      *
106      * @return the carrierTechnologyParameters of the event handler
107      */
108     public CarrierTechnologyParameters getCarrierTechnologyParameters() {
109         return carrierTechnologyParameters;
110     }
111
112     /**
113      * Sets the carrier technology parameters of the event handler.
114      *
115      * @param carrierTechnologyParameters the carrierTechnologyParameters to set
116      */
117     public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
118         this.carrierTechnologyParameters = carrierTechnologyParameters;
119     }
120
121     /**
122      * Gets the event protocol parameters of the event handler.
123      *
124      * @return the eventProtocolParameters
125      */
126     public EventProtocolParameters getEventProtocolParameters() {
127         return eventProtocolParameters;
128     }
129
130     /**
131      * Sets the event protocol parameters.
132      *
133      * @param eventProtocolParameters the eventProtocolParameters to set
134      */
135     public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
136         this.eventProtocolParameters = eventProtocolParameters;
137     }
138
139     /**
140      * Checks if the event handler is in the given peered mode.
141      *
142      * @param peeredMode the peer mode
143      * @return true, if the event handler is in the peered mode
144      */
145     public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
146         switch (peeredMode) {
147             case SYNCHRONOUS:
148                 return synchronousMode;
149             case REQUESTOR:
150                 return requestorMode;
151             default:
152                 return false;
153         }
154     }
155
156     /**
157      * Sets a peered mode as true or false on the event handler.
158      *
159      * @param peeredMode the peered mode to set
160      * @param peeredModeValue the value to set the peered mode to
161      */
162     public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
163         switch (peeredMode) {
164             case SYNCHRONOUS:
165                 synchronousMode = peeredModeValue;
166                 return;
167             case REQUESTOR:
168                 requestorMode = peeredModeValue;
169                 return;
170             default:
171                 return;
172         }
173     }
174
175     /**
176      * Gets the peer for the event handler in this peered mode.
177      *
178      * @param peeredMode the peered mode to get the peer for
179      * @return the peer
180      */
181     public String getPeer(final EventHandlerPeeredMode peeredMode) {
182         switch (peeredMode) {
183             case SYNCHRONOUS:
184                 return synchronousPeer;
185             case REQUESTOR:
186                 return requestorPeer;
187             default:
188                 return null;
189         }
190     }
191
192     /**
193      * Sets the peer for the event handler in this peered mode.
194      *
195      * @param peeredMode the peered mode to set the peer for
196      * @param peer the peer
197      */
198     public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
199         switch (peeredMode) {
200             case SYNCHRONOUS:
201                 synchronousPeer = peer;
202                 return;
203             case REQUESTOR:
204                 requestorPeer = peer;
205                 return;
206             default:
207                 return;
208         }
209     }
210
211     /**
212      * Get the timeout value for the event handler in peered mode.
213      * 
214      * @param peeredMode the peered mode to get the timeout for
215      * @return the timeout value
216      */
217     public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
218         switch (peeredMode) {
219             case SYNCHRONOUS:
220                 return synchronousTimeout;
221             case REQUESTOR:
222                 return requestorTimeout;
223             default:
224                 return -1;
225         }
226     }
227
228     /**
229      * Set the timeout value for the event handler in peered mode.
230      * 
231      * @param peeredMode the peered mode to set the timeout for
232      * @param timeout the timeout value
233      */
234     public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
235         switch (peeredMode) {
236             case SYNCHRONOUS:
237                 synchronousTimeout = timeout;
238                 return;
239             case REQUESTOR:
240                 requestorTimeout = timeout;
241                 return;
242             default:
243                 return;
244         }
245     }
246
247     /**
248      * Check if an event name is being used.
249      *
250      * @return true if an event name is being used
251      */
252     public boolean isSetEventName() {
253         return eventName != null;
254     }
255
256     /**
257      * Gets the event name for this event handler.
258      *
259      * @return the event name
260      */
261     public String getEventName() {
262         return eventName;
263     }
264
265     /**
266      * Sets the event name for this event handler.
267      *
268      * @param eventName the event name
269      */
270     public void setEventName(final String eventName) {
271         this.eventName = eventName;
272     }
273
274     /**
275      * Check if event name filtering is being used.
276      *
277      * @return true if event name filtering is being used
278      */
279     public boolean isSetEventNameFilter() {
280         return eventNameFilter != null;
281     }
282
283     /**
284      * Gets the event name filter for this event handler.
285      *
286      * @return the event name filter
287      */
288     public String getEventNameFilter() {
289         return eventNameFilter;
290     }
291
292     /**
293      * Sets the event name filter for this event handler.
294      *
295      * @param eventNameFilter the event name filter
296      */
297     public void setEventNameFilter(final String eventNameFilter) {
298         this.eventNameFilter = eventNameFilter;
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
305      */
306     @Override
307     public GroupValidationResult validate() {
308         final GroupValidationResult result = new GroupValidationResult(this);
309
310         if (eventProtocolParameters == null) {
311             result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
312                             "event handler eventProtocolParameters not specified or blank");
313         } else {
314             result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
315         }
316
317         if (carrierTechnologyParameters == null) {
318             result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
319                             "event handler carrierTechnologyParameters not specified or blank");
320         } else {
321             result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
322         }
323
324         if (eventNameFilter != null) {
325             try {
326                 Pattern.compile(eventNameFilter);
327             } catch (final PatternSyntaxException pse) {
328                 result.setResult("eventNameFilter", ValidationStatus.INVALID,
329                                 "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage());
330             }
331         }
332
333         return result;
334     }
335
336     /**
337      * Check if we're using synchronous mode.
338      * 
339      * @return true if if we're using synchronous mode
340      */
341     public boolean isSynchronousMode() {
342         return synchronousMode;
343     }
344
345     /**
346      * The synchronous peer for this event handler.
347      * 
348      * @return the synchronous peer for this event handler
349      */
350     public String getSynchronousPeer() {
351         return synchronousPeer;
352     }
353
354     /**
355      * Get the timeout for synchronous operations.
356      * 
357      * @return the timeout for synchronous operations
358      */
359     public long getSynchronousTimeout() {
360         return synchronousTimeout;
361     }
362
363     /**
364      * Check if this event handler will use requestor mode.
365      * 
366      * @return true if this event handler will use requestor mode
367      */
368     public boolean isRequestorMode() {
369         return requestorMode;
370     }
371
372     /**
373      * The requestor peer for this event handler.
374      * 
375      * @return the requestor peer for this event handler
376      */
377     public String getRequestorPeer() {
378         return requestorPeer;
379     }
380
381     /**
382      * Get the requestor timeout.
383      * @return the requestorTimeout.
384      */
385     public long getRequestorTimeout() {
386         return requestorTimeout;
387     }
388
389     /*
390      * (non-Javadoc)
391      * 
392      * @see java.lang.Object#toString()
393      */
394     @Override
395     public String toString() {
396         return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
397                         + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
398                         + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
399                         + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
400                         + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
401                         + eventNameFilter + "]";
402     }
403 }