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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.parameters.eventhandler;
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;
39 * The parameters for a single event producer, event consumer or synchronous event handler.
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.
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>
56 * @author Liam Fallon (liam.fallon@ericsson.com)
58 public class EventHandlerParameters implements ParameterGroup {
59 // Get a reference to the logger
60 private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class);
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;
75 * Constructor to create an event handler parameters instance.
77 public EventHandlerParameters() {
80 // Set the name for the parameters
81 this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
85 * Gets the name of the event handler.
87 * @return the event handler name
89 public String getName() {
94 * Sets the name of the event handler.
96 * @param name the event handler name
98 public void setName(final String name) {
103 * Checks if the name of the event handler is set.
105 * @return true if the name is set
107 public boolean checkSetName() {
108 return !(name == null || name.trim().length() == 0);
112 * Gets the carrier technology parameters of the event handler.
114 * @return the carrierTechnologyParameters of the event handler
116 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
117 return carrierTechnologyParameters;
121 * Sets the carrier technology parameters of the event handler.
123 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
125 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
126 this.carrierTechnologyParameters = carrierTechnologyParameters;
130 * Gets the event protocol parameters of the event handler.
132 * @return the eventProtocolParameters
134 public EventProtocolParameters getEventProtocolParameters() {
135 return eventProtocolParameters;
139 * Sets the event protocol parameters.
141 * @param eventProtocolParameters the eventProtocolParameters to set
143 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
144 this.eventProtocolParameters = eventProtocolParameters;
148 * Checks if the event handler is in the given peered mode.
150 * @param peeredMode the peer mode
151 * @return true, if the event handler is in the peered mode
153 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
154 switch (peeredMode) {
156 return synchronousMode;
158 return requestorMode;
165 * Sets a peered mode as true or false on the event handler.
167 * @param peeredMode the peered mode to set
168 * @param peeredModeValue the value to set the peered mode to
170 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
171 switch (peeredMode) {
173 synchronousMode = peeredModeValue;
176 requestorMode = peeredModeValue;
184 * Gets the peer for the event handler in this peered mode.
186 * @param peeredMode the peered mode to get the peer for
189 public String getPeer(final EventHandlerPeeredMode peeredMode) {
190 switch (peeredMode) {
192 return synchronousPeer;
194 return requestorPeer;
201 * Sets the peer for the event handler in this peered mode.
203 * @param peeredMode the peered mode to set the peer for
204 * @param peer the peer
206 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
207 switch (peeredMode) {
209 synchronousPeer = peer;
212 requestorPeer = peer;
220 * Get the timeout value for the event handler in peered mode.
222 * @param peeredMode the peered mode to get the timeout for
223 * @return the timeout value
225 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
226 switch (peeredMode) {
228 return synchronousTimeout;
230 return requestorTimeout;
237 * Set the timeout value for the event handler in peered mode.
239 * @param peeredMode the peered mode to set the timeout for
240 * @param timeout the timeout value
242 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
243 switch (peeredMode) {
245 synchronousTimeout = timeout;
248 requestorTimeout = timeout;
256 * Check if an event name is being used.
258 * @return true if an event name is being used
260 public boolean isSetEventName() {
261 return eventName != null;
265 * Gets the event name for this event handler.
267 * @return the event name
269 public String getEventName() {
274 * Sets the event name for this event handler.
276 * @param eventName the event name
278 public void setEventName(final String eventName) {
279 this.eventName = eventName;
283 * Check if event name filtering is being used.
285 * @return true if event name filtering is being used
287 public boolean isSetEventNameFilter() {
288 return eventNameFilter != null;
292 * Gets the event name filter for this event handler.
294 * @return the event name filter
296 public String getEventNameFilter() {
297 return eventNameFilter;
301 * Sets the event name filter for this event handler.
303 * @param eventNameFilter the event name filter
305 public void setEventNameFilter(final String eventNameFilter) {
306 this.eventNameFilter = eventNameFilter;
313 public BeanValidationResult validate() {
314 final BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this);
316 if (eventNameFilter != null) {
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);
330 * Check if we're using synchronous mode.
332 * @return true if if we're using synchronous mode
334 public boolean isSynchronousMode() {
335 return synchronousMode;
339 * The synchronous peer for this event handler.
341 * @return the synchronous peer for this event handler
343 public String getSynchronousPeer() {
344 return synchronousPeer;
348 * Get the timeout for synchronous operations.
350 * @return the timeout for synchronous operations
352 public long getSynchronousTimeout() {
353 return synchronousTimeout;
357 * Check if this event handler will use requestor mode.
359 * @return true if this event handler will use requestor mode
361 public boolean isRequestorMode() {
362 return requestorMode;
366 * The requestor peer for this event handler.
368 * @return the requestor peer for this event handler
370 public String getRequestorPeer() {
371 return requestorPeer;
375 * Get the requestor timeout.
377 * @return the requestorTimeout.
379 public long getRequestorTimeout() {
380 return requestorTimeout;
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 + "]";