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;
25 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
26 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
27 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ParameterGroup;
30 import org.onap.policy.common.parameters.ValidationStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The parameters for a single event producer, event consumer or synchronous event handler.
37 * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so
38 * the actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one.
40 * <p>The following parameters are defined: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of
41 * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The
42 * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology
43 * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false <li>synchronousPeer:
44 * the peer event handler (consumer for producer or producer for consumer) of this event handler in synchronous mode
45 * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
46 * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false <li>requestorPeer: the
47 * peer event handler (consumer for producer or producer for consumer) of this event handler in requestor mode
48 * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
49 * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified, events not matching
50 * the given regular expression are ignored. If it is null, all events are handledDefaults to null. </ol>
52 * @author Liam Fallon (liam.fallon@ericsson.com)
54 public class EventHandlerParameters implements ParameterGroup {
55 // Get a reference to the logger
56 private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class);
58 private String name = null;
59 private CarrierTechnologyParameters carrierTechnologyParameters = null;
60 private EventProtocolParameters eventProtocolParameters = null;
61 private boolean synchronousMode = false;
62 private String synchronousPeer = null;
63 private long synchronousTimeout = 0;
64 private boolean requestorMode = false;
65 private String requestorPeer = null;
66 private long requestorTimeout = 0;
67 private String eventName = null;
68 private String eventNameFilter = null;
71 * Constructor to create an event handler parameters instance.
73 public EventHandlerParameters() {
76 // Set the name for the parameters
77 this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
81 * Gets the name of the event handler.
83 * @return the event handler name
85 public String getName() {
90 * Sets the name of the event handler.
92 * @param name the event handler name
94 public void setName(final String name) {
99 * Checks if the name of the event handler is set.
101 * @return true if the name is set
103 public boolean checkSetName() {
104 return !(name == null || name.trim().length() == 0);
108 * Gets the carrier technology parameters of the event handler.
110 * @return the carrierTechnologyParameters of the event handler
112 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
113 return carrierTechnologyParameters;
117 * Sets the carrier technology parameters of the event handler.
119 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
121 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
122 this.carrierTechnologyParameters = carrierTechnologyParameters;
126 * Gets the event protocol parameters of the event handler.
128 * @return the eventProtocolParameters
130 public EventProtocolParameters getEventProtocolParameters() {
131 return eventProtocolParameters;
135 * Sets the event protocol parameters.
137 * @param eventProtocolParameters the eventProtocolParameters to set
139 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
140 this.eventProtocolParameters = eventProtocolParameters;
144 * Checks if the event handler is in the given peered mode.
146 * @param peeredMode the peer mode
147 * @return true, if the event handler is in the peered mode
149 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
150 switch (peeredMode) {
152 return synchronousMode;
154 return requestorMode;
161 * Sets a peered mode as true or false on the event handler.
163 * @param peeredMode the peered mode to set
164 * @param peeredModeValue the value to set the peered mode to
166 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
167 switch (peeredMode) {
169 synchronousMode = peeredModeValue;
172 requestorMode = peeredModeValue;
180 * Gets the peer for the event handler in this peered mode.
182 * @param peeredMode the peered mode to get the peer for
185 public String getPeer(final EventHandlerPeeredMode peeredMode) {
186 switch (peeredMode) {
188 return synchronousPeer;
190 return requestorPeer;
197 * Sets the peer for the event handler in this peered mode.
199 * @param peeredMode the peered mode to set the peer for
200 * @param peer the peer
202 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
203 switch (peeredMode) {
205 synchronousPeer = peer;
208 requestorPeer = peer;
216 * Get the timeout value for the event handler in peered mode.
218 * @param peeredMode the peered mode to get the timeout for
219 * @return the timeout value
221 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
222 switch (peeredMode) {
224 return synchronousTimeout;
226 return requestorTimeout;
233 * Set the timeout value for the event handler in peered mode.
235 * @param peeredMode the peered mode to set the timeout for
236 * @param timeout the timeout value
238 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
239 switch (peeredMode) {
241 synchronousTimeout = timeout;
244 requestorTimeout = timeout;
252 * Check if an event name is being used.
254 * @return true if an event name is being used
256 public boolean isSetEventName() {
257 return eventName != null;
261 * Gets the event name for this event handler.
263 * @return the event name
265 public String getEventName() {
270 * Sets the event name for this event handler.
272 * @param eventName the event name
274 public void setEventName(final String eventName) {
275 this.eventName = eventName;
279 * Check if event name filtering is being used.
281 * @return true if event name filtering is being used
283 public boolean isSetEventNameFilter() {
284 return eventNameFilter != null;
288 * Gets the event name filter for this event handler.
290 * @return the event name filter
292 public String getEventNameFilter() {
293 return eventNameFilter;
297 * Sets the event name filter for this event handler.
299 * @param eventNameFilter the event name filter
301 public void setEventNameFilter(final String eventNameFilter) {
302 this.eventNameFilter = eventNameFilter;
309 public GroupValidationResult validate() {
310 final GroupValidationResult result = new GroupValidationResult(this);
312 if (eventProtocolParameters == null) {
313 result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
314 "event handler eventProtocolParameters not specified or blank");
316 result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
319 if (carrierTechnologyParameters == null) {
320 result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
321 "event handler carrierTechnologyParameters not specified or blank");
323 result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
326 if (eventNameFilter != null) {
328 Pattern.compile(eventNameFilter);
329 } catch (final PatternSyntaxException pse) {
330 String message = "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage();
331 LOGGER.trace(message, pse);
332 result.setResult("eventNameFilter", ValidationStatus.INVALID, message);
340 * Check if we're using synchronous mode.
342 * @return true if if we're using synchronous mode
344 public boolean isSynchronousMode() {
345 return synchronousMode;
349 * The synchronous peer for this event handler.
351 * @return the synchronous peer for this event handler
353 public String getSynchronousPeer() {
354 return synchronousPeer;
358 * Get the timeout for synchronous operations.
360 * @return the timeout for synchronous operations
362 public long getSynchronousTimeout() {
363 return synchronousTimeout;
367 * Check if this event handler will use requestor mode.
369 * @return true if this event handler will use requestor mode
371 public boolean isRequestorMode() {
372 return requestorMode;
376 * The requestor peer for this event handler.
378 * @return the requestor peer for this event handler
380 public String getRequestorPeer() {
381 return requestorPeer;
385 * Get the requestor timeout.
387 * @return the requestorTimeout.
389 public long getRequestorTimeout() {
390 return requestorTimeout;
397 public String toString() {
398 return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
399 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
400 + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
401 + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
402 + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
403 + eventNameFilter + "]";