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.
36 * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so the
37 * actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one.
39 * <p>The following parameters are defined:
41 * <li>carrierTechnologyParameters: The carrier technology is the type of messaging infrastructure used to carry events.
42 * Examples are File, Kafka or REST.
43 * <li>eventProtocolParameters: The format that the events are in when being carried. Examples are JSON, XML, or Java
44 * Beans. carrier technology
45 * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false
46 * <li>synchronousPeer: the peer event handler (consumer for producer or producer for consumer) of this event handler in
48 * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
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 this event handler in
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.
57 * @author Liam Fallon (liam.fallon@ericsson.com)
59 public class EventHandlerParameters implements ParameterGroup {
60 private String name = null;
61 private CarrierTechnologyParameters carrierTechnologyParameters = null;
62 private EventProtocolParameters eventProtocolParameters = null;
63 private boolean synchronousMode = false;
64 private String synchronousPeer = null;
65 private long synchronousTimeout = 0;
66 private boolean requestorMode = false;
67 private String requestorPeer = null;
68 private long requestorTimeout = 0;
69 private String eventName = null;
70 private String eventNameFilter = null;
73 * Constructor to create an event handler parameters instance.
75 public EventHandlerParameters() {
78 // Set the name for the parameters
79 this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
83 * Gets the name of the event handler.
85 * @return the event handler name
87 public String getName() {
92 * Sets the name of the event handler.
94 * @param name the event handler name
96 public void setName(final String name) {
101 * Checks if the name of the event handler is set.
103 * @return true if the name is set
105 public boolean checkSetName() {
106 return !(name == null || name.trim().length() == 0);
110 * Gets the carrier technology parameters of the event handler.
112 * @return the carrierTechnologyParameters of the event handler
114 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
115 return carrierTechnologyParameters;
119 * Sets the carrier technology parameters of the event handler.
121 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
123 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
124 this.carrierTechnologyParameters = carrierTechnologyParameters;
128 * Gets the event protocol parameters of the event handler.
130 * @return the eventProtocolParameters
132 public EventProtocolParameters getEventProtocolParameters() {
133 return eventProtocolParameters;
137 * Sets the event protocol parameters.
139 * @param eventProtocolParameters the eventProtocolParameters to set
141 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
142 this.eventProtocolParameters = eventProtocolParameters;
146 * Checks if the event handler is in the given peered mode.
148 * @param peeredMode the peer mode
149 * @return true, if the event handler is in the peered mode
151 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
152 switch (peeredMode) {
154 return synchronousMode;
156 return requestorMode;
163 * Sets a peered mode as true or false on the event handler.
165 * @param peeredMode the peered mode to set
166 * @param peeredModeValue the value to set the peered mode to
168 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
169 switch (peeredMode) {
171 synchronousMode = peeredModeValue;
174 requestorMode = peeredModeValue;
182 * Gets the peer for the event handler in this peered mode.
184 * @param peeredMode the peered mode to get the peer for
187 public String getPeer(final EventHandlerPeeredMode peeredMode) {
188 switch (peeredMode) {
190 return synchronousPeer;
192 return requestorPeer;
199 * Sets the peer for the event handler in this peered mode.
201 * @param peeredMode the peered mode to set the peer for
202 * @param peer the peer
204 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
205 switch (peeredMode) {
207 synchronousPeer = peer;
210 requestorPeer = peer;
218 * Get the timeout value for the event handler in peered mode.
220 * @param peeredMode the peered mode to get the timeout for
221 * @return the timeout value
223 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
224 switch (peeredMode) {
226 return synchronousTimeout;
228 return requestorTimeout;
235 * Set the timeout value for the event handler in peered mode.
237 * @param peeredMode the peered mode to set the timeout for
238 * @param timeout the timeout value
240 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
241 switch (peeredMode) {
243 synchronousTimeout = timeout;
246 requestorTimeout = timeout;
254 * Check if an event name is being used.
256 * @return true if an event name is being used
258 public boolean isSetEventName() {
259 return eventName != null;
263 * Gets the event name for this event handler.
265 * @return the event name
267 public String getEventName() {
272 * Sets the event name for this event handler.
274 * @param eventName the event name
276 public void setEventName(final String eventName) {
277 this.eventName = eventName;
281 * Check if event name filtering is being used.
283 * @return true if event name filtering is being used
285 public boolean isSetEventNameFilter() {
286 return eventNameFilter != null;
290 * Gets the event name filter for this event handler.
292 * @return the event name filter
294 public String getEventNameFilter() {
295 return eventNameFilter;
299 * Sets the event name filter for this event handler.
301 * @param eventNameFilter the event name filter
303 public void setEventNameFilter(final String eventNameFilter) {
304 this.eventNameFilter = eventNameFilter;
310 * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
313 public GroupValidationResult validate() {
314 final GroupValidationResult result = new GroupValidationResult(this);
316 if (eventProtocolParameters == null) {
317 result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
318 "event handler eventProtocolParameters not specified or blank");
320 result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
323 if (carrierTechnologyParameters == null) {
324 result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
325 "event handler carrierTechnologyParameters not specified or blank");
327 result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
330 if (eventNameFilter != null) {
332 Pattern.compile(eventNameFilter);
333 } catch (final PatternSyntaxException pse) {
334 result.setResult("eventNameFilter", ValidationStatus.INVALID,
335 "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage());
343 * Check if we're using synchronous mode
345 * @return true if if we're using synchronous mode
347 public boolean isSynchronousMode() {
348 return synchronousMode;
352 * The synchronous peer for this event handler
353 * @return the synchronous peer for this event handler
355 public String getSynchronousPeer() {
356 return synchronousPeer;
360 * 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
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
377 * @return the requestor peer for this event handler
379 public String getRequestorPeer() {
380 return requestorPeer;
384 * @return the requestorTimeout
386 public long getRequestorTimeout() {
387 return requestorTimeout;
393 * @see java.lang.Object#toString()
396 public String toString() {
397 return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
398 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
399 + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
400 + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
401 + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
402 + eventNameFilter + "]";