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