7fb4584fedfe3145f40f73550ae8a0642b6b840d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.parameters.eventhandler;
23
24 import java.util.regex.Pattern;
25 import java.util.regex.PatternSyntaxException;
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.BeanValidationResult;
30 import org.onap.policy.common.parameters.BeanValidator;
31 import org.onap.policy.common.parameters.ParameterGroup;
32 import org.onap.policy.common.parameters.ValidationStatus;
33 import org.onap.policy.common.parameters.annotations.NotNull;
34 import org.onap.policy.common.parameters.annotations.Valid;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The parameters for a single event producer, event consumer or synchronous event handler.
40  *
41  * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so
42  * the actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one.
43  *
44  * <p>The following parameters are defined: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of
45  * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The
46  * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology
47  * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false <li>synchronousPeer:
48  * the peer event handler (consumer for producer or producer for consumer) of this event handler in synchronous mode
49  * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
50  * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false <li>requestorPeer: the
51  * peer event handler (consumer for producer or producer for consumer) of this event handler in requestor mode
52  * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
53  * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified, events not matching
54  * the given regular expression are ignored. If it is null, all events are handledDefaults to null. </ol>
55  *
56  * @author Liam Fallon (liam.fallon@ericsson.com)
57  */
58 public class EventHandlerParameters implements ParameterGroup {
59     // Get a reference to the logger
60     private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class);
61
62     private String name = null;
63     private @NotNull @Valid CarrierTechnologyParameters carrierTechnologyParameters = null;
64     private @NotNull @Valid 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      * {@inheritDoc}.
311      */
312     @Override
313     public BeanValidationResult validate() {
314         final BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this);
315
316         if (eventNameFilter != null) {
317             try {
318                 Pattern.compile(eventNameFilter);
319             } catch (final PatternSyntaxException pse) {
320                 String message = "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage();
321                 LOGGER.trace(message, pse);
322                 result.addResult("eventNameFilter", eventNameFilter, ValidationStatus.INVALID, message);
323             }
324         }
325
326         return result;
327     }
328
329     /**
330      * Check if we're using synchronous mode.
331      *
332      * @return true if if we're using synchronous mode
333      */
334     public boolean isSynchronousMode() {
335         return synchronousMode;
336     }
337
338     /**
339      * The synchronous peer for this event handler.
340      *
341      * @return the synchronous peer for this event handler
342      */
343     public String getSynchronousPeer() {
344         return synchronousPeer;
345     }
346
347     /**
348      * Get the timeout for synchronous operations.
349      *
350      * @return the timeout for synchronous operations
351      */
352     public long getSynchronousTimeout() {
353         return synchronousTimeout;
354     }
355
356     /**
357      * Check if this event handler will use requestor mode.
358      *
359      * @return true if this event handler will use requestor mode
360      */
361     public boolean isRequestorMode() {
362         return requestorMode;
363     }
364
365     /**
366      * The requestor peer for this event handler.
367      *
368      * @return the requestor peer for this event handler
369      */
370     public String getRequestorPeer() {
371         return requestorPeer;
372     }
373
374     /**
375      * Get the requestor timeout.
376      *
377      * @return the requestorTimeout.
378      */
379     public long getRequestorTimeout() {
380         return requestorTimeout;
381     }
382
383     /**
384      * {@inheritDoc}.
385      */
386     @Override
387     public String toString() {
388         return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
389                         + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
390                         + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
391                         + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
392                         + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
393                         + eventNameFilter + "]";
394     }
395 }