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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.parameters.eventhandler;
23 import java.util.regex.Pattern;
24 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.GroupValidationResult;
30 import org.onap.policy.common.parameters.ParameterGroup;
31 import org.onap.policy.common.parameters.ValidationStatus;
34 * The parameters for a single event producer, event consumer or synchronous event handler.
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.
41 * The following parameters are defined:
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
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
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.
59 * @author Liam Fallon (liam.fallon@ericsson.com)
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;
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;
312 * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
315 public GroupValidationResult validate() {
316 final GroupValidationResult result = new GroupValidationResult(this);
318 if (eventProtocolParameters == null) {
319 result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
320 "event handler eventProtocolParameters not specified or blank");
322 result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
325 if (carrierTechnologyParameters == null) {
326 result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
327 "event handler carrierTechnologyParameters not specified or blank");
329 result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
332 if (eventNameFilter != null) {
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());
347 * @see java.lang.Object#toString()
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 + "]";