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.model.basicmodel.service.AbstractParameters;
27 import org.onap.policy.apex.service.parameters.ApexParameterValidator;
28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
29 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
32 * The parameters for a single event producer, event consumer or synchronous event handler.
34 * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an
35 * event protocol so the actual parameters for each one are the same. Therefore, we use the same
36 * class for the parameters of each one.
38 * <p>The following parameters are defined:
40 * <li>carrierTechnologyParameters: The carrier technology is the type of messaging infrastructure
41 * used to carry events. Examples are File, Kafka or REST.
42 * <li>eventProtocolParameters: The format that the events are in when being carried. Examples are
43 * JSON, XML, or Java Beans. carrier technology
44 * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false
45 * <li>synchronousPeer: the peer event handler (consumer for producer or producer for consumer) of
46 * this event handler in synchronous mode
47 * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before
49 * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false
50 * <li>requestorPeer: the peer event handler (consumer for producer or producer for consumer) of
51 * this event handler in requestor mode
52 * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they
54 * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified,
55 * events not matching the given regular expression are ignored. If it is null, all events are
56 * handledDefaults to null.
59 * @author Liam Fallon (liam.fallon@ericsson.com)
61 public class EventHandlerParameters extends AbstractParameters implements ApexParameterValidator {
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() {
78 super(EventHandlerParameters.class.getCanonicalName());
82 * Constructor to create an event handler parameters instance with the name of a sub class of
85 * @param parameterClassName the class name of a sub class of this class
87 public EventHandlerParameters(final String parameterClassName) {
88 super(parameterClassName);
92 * Gets the name of the event handler.
94 * @return the event handler name
96 public String getName() {
101 * Sets the name of the event handler.
103 * @param name the event handler name
105 public void setName(final String name) {
110 * Checks if the name of the event handler is set.
112 * @return true if the name is set
114 public boolean checkSetName() {
115 return !(name == null || name.trim().length() == 0);
119 * Gets the carrier technology parameters of the event handler.
121 * @return the carrierTechnologyParameters of the event handler
123 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
124 return carrierTechnologyParameters;
128 * Sets the carrier technology parameters of the event handler.
130 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
132 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
133 this.carrierTechnologyParameters = carrierTechnologyParameters;
137 * Gets the event protocol parameters of the event handler.
139 * @return the eventProtocolParameters
141 public EventProtocolParameters getEventProtocolParameters() {
142 return eventProtocolParameters;
146 * Sets the event protocol parameters.
148 * @param eventProtocolParameters the eventProtocolParameters to set
150 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
151 this.eventProtocolParameters = eventProtocolParameters;
156 * Checks if the event handler is in the given peered mode.
158 * @param peeredMode the peer mode
159 * @return true, if the event handler is in the peered mode
161 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
162 switch (peeredMode) {
164 return synchronousMode;
166 return requestorMode;
173 * Sets a peered mode as true or false on the event handler.
175 * @param peeredMode the peered mode to set
176 * @param peeredModeValue the value to set the peered mode to
178 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
179 switch (peeredMode) {
181 synchronousMode = peeredModeValue;
184 requestorMode = peeredModeValue;
192 * Gets the peer for the event handler in this peered mode.
194 * @param peeredMode the peered mode to get the peer for
197 public String getPeer(final EventHandlerPeeredMode peeredMode) {
198 switch (peeredMode) {
200 return synchronousPeer;
202 return requestorPeer;
209 * Sets the peer for the event handler in this peered mode.
211 * @param peeredMode the peered mode to set the peer for
212 * @param peer the peer
214 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
215 switch (peeredMode) {
217 synchronousPeer = peer;
220 requestorPeer = peer;
228 * Get the timeout value for the event handler in peered mode.
230 * @param peeredMode the peered mode to get the timeout for
231 * @return the timeout value
233 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
234 switch (peeredMode) {
236 return synchronousTimeout;
238 return requestorTimeout;
245 * Set the timeout value for the event handler in peered mode.
247 * @param peeredMode the peered mode to set the timeout for
248 * @param timeout the timeout value
250 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
251 switch (peeredMode) {
253 synchronousTimeout = timeout;
256 requestorTimeout = timeout;
264 * Check if an event name is being used.
266 * @return true if an event name is being used
268 public boolean isSetEventName() {
269 return eventName != null;
273 * Gets the event name for this event handler.
275 * @return the event name
277 public String getEventName() {
282 * Sets the event name for this event handler.
284 * @param eventName the event name
286 public void setEventName(final String eventName) {
287 this.eventName = eventName;
291 * Check if event name filtering is being used.
293 * @return true if event name filtering is being used
295 public boolean isSetEventNameFilter() {
296 return eventNameFilter != null;
300 * Gets the event name filter for this event handler.
302 * @return the event name filter
304 public String getEventNameFilter() {
305 return eventNameFilter;
309 * Sets the event name filter for this event handler.
311 * @param eventNameFilter the event name filter
313 public void setEventNameFilter(final String eventNameFilter) {
314 this.eventNameFilter = eventNameFilter;
320 * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
323 public String validate() {
324 final StringBuilder errorMessageBuilder = new StringBuilder();
326 if (eventProtocolParameters == null) {
327 errorMessageBuilder.append(" event handler eventProtocolParameters not specified or blank\n");
329 errorMessageBuilder.append(eventProtocolParameters.validate());
332 if (carrierTechnologyParameters == null) {
333 errorMessageBuilder.append(" event handler carrierTechnologyParameters not specified or blank\n");
335 errorMessageBuilder.append(carrierTechnologyParameters.validate());
338 if (eventNameFilter != null) {
340 Pattern.compile(eventNameFilter);
341 } catch (final PatternSyntaxException pse) {
342 errorMessageBuilder.append(" event handler eventNameFilter is not a valid regular expression: "
343 + pse.getMessage() + "\n");
346 return errorMessageBuilder.toString();
352 * @see java.lang.Object#toString()
355 public String toString() {
356 return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
357 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode=" + synchronousMode
358 + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout=" + synchronousTimeout
359 + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer + ", requestorTimeout="
360 + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter=" + eventNameFilter + "]";