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;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The parameters for a single event producer, event consumer or synchronous event handler.
38 * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so
39 * the actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one.
41 * <p>The following parameters are defined: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of
42 * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The
43 * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology
44 * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false <li>synchronousPeer:
45 * the peer event handler (consumer for producer or producer for consumer) of this event handler in synchronous mode
46 * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
47 * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false <li>requestorPeer: the
48 * peer event handler (consumer for producer or producer for consumer) of this event handler in requestor mode
49 * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
50 * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified, events not matching
51 * the given regular expression are ignored. If it is null, all events are handledDefaults to null. </ol>
53 * @author Liam Fallon (liam.fallon@ericsson.com)
55 public class EventHandlerParameters implements ParameterGroup {
56 // Get a reference to the logger
57 private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class);
59 private String name = null;
60 private CarrierTechnologyParameters carrierTechnologyParameters = null;
61 private EventProtocolParameters eventProtocolParameters = null;
62 private boolean synchronousMode = false;
63 private String synchronousPeer = null;
64 private long synchronousTimeout = 0;
65 private boolean requestorMode = false;
66 private String requestorPeer = null;
67 private long requestorTimeout = 0;
68 private String eventName = null;
69 private String eventNameFilter = null;
72 * Constructor to create an event handler parameters instance.
74 public EventHandlerParameters() {
77 // Set the name for the parameters
78 this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
82 * Gets the name of the event handler.
84 * @return the event handler name
86 public String getName() {
91 * Sets the name of the event handler.
93 * @param name the event handler name
95 public void setName(final String name) {
100 * Checks if the name of the event handler is set.
102 * @return true if the name is set
104 public boolean checkSetName() {
105 return !(name == null || name.trim().length() == 0);
109 * Gets the carrier technology parameters of the event handler.
111 * @return the carrierTechnologyParameters of the event handler
113 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
114 return carrierTechnologyParameters;
118 * Sets the carrier technology parameters of the event handler.
120 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
122 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
123 this.carrierTechnologyParameters = carrierTechnologyParameters;
127 * Gets the event protocol parameters of the event handler.
129 * @return the eventProtocolParameters
131 public EventProtocolParameters getEventProtocolParameters() {
132 return eventProtocolParameters;
136 * Sets the event protocol parameters.
138 * @param eventProtocolParameters the eventProtocolParameters to set
140 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
141 this.eventProtocolParameters = eventProtocolParameters;
145 * Checks if the event handler is in the given peered mode.
147 * @param peeredMode the peer mode
148 * @return true, if the event handler is in the peered mode
150 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
151 switch (peeredMode) {
153 return synchronousMode;
155 return requestorMode;
162 * Sets a peered mode as true or false on the event handler.
164 * @param peeredMode the peered mode to set
165 * @param peeredModeValue the value to set the peered mode to
167 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
168 switch (peeredMode) {
170 synchronousMode = peeredModeValue;
173 requestorMode = peeredModeValue;
181 * Gets the peer for the event handler in this peered mode.
183 * @param peeredMode the peered mode to get the peer for
186 public String getPeer(final EventHandlerPeeredMode peeredMode) {
187 switch (peeredMode) {
189 return synchronousPeer;
191 return requestorPeer;
198 * Sets the peer for the event handler in this peered mode.
200 * @param peeredMode the peered mode to set the peer for
201 * @param peer the peer
203 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
204 switch (peeredMode) {
206 synchronousPeer = peer;
209 requestorPeer = peer;
217 * Get the timeout value for the event handler in peered mode.
219 * @param peeredMode the peered mode to get the timeout for
220 * @return the timeout value
222 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
223 switch (peeredMode) {
225 return synchronousTimeout;
227 return requestorTimeout;
234 * Set the timeout value for the event handler in peered mode.
236 * @param peeredMode the peered mode to set the timeout for
237 * @param timeout the timeout value
239 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
240 switch (peeredMode) {
242 synchronousTimeout = timeout;
245 requestorTimeout = timeout;
253 * Check if an event name is being used.
255 * @return true if an event name is being used
257 public boolean isSetEventName() {
258 return eventName != null;
262 * Gets the event name for this event handler.
264 * @return the event name
266 public String getEventName() {
271 * Sets the event name for this event handler.
273 * @param eventName the event name
275 public void setEventName(final String eventName) {
276 this.eventName = eventName;
280 * Check if event name filtering is being used.
282 * @return true if event name filtering is being used
284 public boolean isSetEventNameFilter() {
285 return eventNameFilter != null;
289 * Gets the event name filter for this event handler.
291 * @return the event name filter
293 public String getEventNameFilter() {
294 return eventNameFilter;
298 * Sets the event name filter for this event handler.
300 * @param eventNameFilter the event name filter
302 public void setEventNameFilter(final String eventNameFilter) {
303 this.eventNameFilter = eventNameFilter;
310 public GroupValidationResult validate() {
311 final GroupValidationResult result = new GroupValidationResult(this);
313 if (eventProtocolParameters == null) {
314 result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
315 "event handler eventProtocolParameters not specified or blank");
317 result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
320 if (carrierTechnologyParameters == null) {
321 result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
322 "event handler carrierTechnologyParameters not specified or blank");
324 result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
327 if (eventNameFilter != null) {
329 Pattern.compile(eventNameFilter);
330 } catch (final PatternSyntaxException pse) {
331 String message = "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage();
332 LOGGER.trace(message, pse);
333 result.setResult("eventNameFilter", ValidationStatus.INVALID, message);
341 * Check if we're using synchronous mode.
343 * @return true if if we're using synchronous mode
345 public boolean isSynchronousMode() {
346 return synchronousMode;
350 * The synchronous peer for this event handler.
352 * @return the synchronous peer for this event handler
354 public String getSynchronousPeer() {
355 return synchronousPeer;
359 * Get the timeout for synchronous operations.
361 * @return the timeout for synchronous operations
363 public long getSynchronousTimeout() {
364 return synchronousTimeout;
368 * Check if this event handler will use requestor mode.
370 * @return true if this event handler will use requestor mode
372 public boolean isRequestorMode() {
373 return requestorMode;
377 * The requestor peer for this event handler.
379 * @return the requestor peer for this event handler
381 public String getRequestorPeer() {
382 return requestorPeer;
386 * Get the requestor timeout.
388 * @return the requestorTimeout.
390 public long getRequestorTimeout() {
391 return requestorTimeout;
398 public String toString() {
399 return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
400 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
401 + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
402 + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
403 + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
404 + eventNameFilter + "]";