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
37 * the 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: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of
40 * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The
41 * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology
42 * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false <li>synchronousPeer:
43 * the peer event handler (consumer for producer or producer for consumer) of this event handler in synchronous mode
44 * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
45 * <li>requestorMode: true if the event handler is working in requestor mode, defaults to false <li>requestorPeer: the
46 * peer event handler (consumer for producer or producer for consumer) of this event handler in requestor mode
47 * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they are timed out
48 * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified, events not matching
49 * the given regular expression are ignored. If it is null, all events are handledDefaults to null. </ol>
51 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class EventHandlerParameters implements ParameterGroup {
54 private String name = null;
55 private CarrierTechnologyParameters carrierTechnologyParameters = null;
56 private EventProtocolParameters eventProtocolParameters = null;
57 private boolean synchronousMode = false;
58 private String synchronousPeer = null;
59 private long synchronousTimeout = 0;
60 private boolean requestorMode = false;
61 private String requestorPeer = null;
62 private long requestorTimeout = 0;
63 private String eventName = null;
64 private String eventNameFilter = null;
67 * Constructor to create an event handler parameters instance.
69 public EventHandlerParameters() {
72 // Set the name for the parameters
73 this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
77 * Gets the name of the event handler.
79 * @return the event handler name
81 public String getName() {
86 * Sets the name of the event handler.
88 * @param name the event handler name
90 public void setName(final String name) {
95 * Checks if the name of the event handler is set.
97 * @return true if the name is set
99 public boolean checkSetName() {
100 return !(name == null || name.trim().length() == 0);
104 * Gets the carrier technology parameters of the event handler.
106 * @return the carrierTechnologyParameters of the event handler
108 public CarrierTechnologyParameters getCarrierTechnologyParameters() {
109 return carrierTechnologyParameters;
113 * Sets the carrier technology parameters of the event handler.
115 * @param carrierTechnologyParameters the carrierTechnologyParameters to set
117 public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
118 this.carrierTechnologyParameters = carrierTechnologyParameters;
122 * Gets the event protocol parameters of the event handler.
124 * @return the eventProtocolParameters
126 public EventProtocolParameters getEventProtocolParameters() {
127 return eventProtocolParameters;
131 * Sets the event protocol parameters.
133 * @param eventProtocolParameters the eventProtocolParameters to set
135 public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
136 this.eventProtocolParameters = eventProtocolParameters;
140 * Checks if the event handler is in the given peered mode.
142 * @param peeredMode the peer mode
143 * @return true, if the event handler is in the peered mode
145 public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
146 switch (peeredMode) {
148 return synchronousMode;
150 return requestorMode;
157 * Sets a peered mode as true or false on the event handler.
159 * @param peeredMode the peered mode to set
160 * @param peeredModeValue the value to set the peered mode to
162 public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
163 switch (peeredMode) {
165 synchronousMode = peeredModeValue;
168 requestorMode = peeredModeValue;
176 * Gets the peer for the event handler in this peered mode.
178 * @param peeredMode the peered mode to get the peer for
181 public String getPeer(final EventHandlerPeeredMode peeredMode) {
182 switch (peeredMode) {
184 return synchronousPeer;
186 return requestorPeer;
193 * Sets the peer for the event handler in this peered mode.
195 * @param peeredMode the peered mode to set the peer for
196 * @param peer the peer
198 public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
199 switch (peeredMode) {
201 synchronousPeer = peer;
204 requestorPeer = peer;
212 * Get the timeout value for the event handler in peered mode.
214 * @param peeredMode the peered mode to get the timeout for
215 * @return the timeout value
217 public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
218 switch (peeredMode) {
220 return synchronousTimeout;
222 return requestorTimeout;
229 * Set the timeout value for the event handler in peered mode.
231 * @param peeredMode the peered mode to set the timeout for
232 * @param timeout the timeout value
234 public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
235 switch (peeredMode) {
237 synchronousTimeout = timeout;
240 requestorTimeout = timeout;
248 * Check if an event name is being used.
250 * @return true if an event name is being used
252 public boolean isSetEventName() {
253 return eventName != null;
257 * Gets the event name for this event handler.
259 * @return the event name
261 public String getEventName() {
266 * Sets the event name for this event handler.
268 * @param eventName the event name
270 public void setEventName(final String eventName) {
271 this.eventName = eventName;
275 * Check if event name filtering is being used.
277 * @return true if event name filtering is being used
279 public boolean isSetEventNameFilter() {
280 return eventNameFilter != null;
284 * Gets the event name filter for this event handler.
286 * @return the event name filter
288 public String getEventNameFilter() {
289 return eventNameFilter;
293 * Sets the event name filter for this event handler.
295 * @param eventNameFilter the event name filter
297 public void setEventNameFilter(final String eventNameFilter) {
298 this.eventNameFilter = eventNameFilter;
304 * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
307 public GroupValidationResult validate() {
308 final GroupValidationResult result = new GroupValidationResult(this);
310 if (eventProtocolParameters == null) {
311 result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
312 "event handler eventProtocolParameters not specified or blank");
314 result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
317 if (carrierTechnologyParameters == null) {
318 result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
319 "event handler carrierTechnologyParameters not specified or blank");
321 result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
324 if (eventNameFilter != null) {
326 Pattern.compile(eventNameFilter);
327 } catch (final PatternSyntaxException pse) {
328 result.setResult("eventNameFilter", ValidationStatus.INVALID,
329 "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage());
337 * Check if we're using synchronous mode.
339 * @return true if if we're using synchronous mode
341 public boolean isSynchronousMode() {
342 return synchronousMode;
346 * The synchronous peer for this event handler.
348 * @return the synchronous peer for this event handler
350 public String getSynchronousPeer() {
351 return synchronousPeer;
355 * Get the timeout for synchronous operations.
357 * @return the timeout for synchronous operations
359 public long getSynchronousTimeout() {
360 return synchronousTimeout;
364 * Check if this event handler will use requestor mode.
366 * @return true if this event handler will use requestor mode
368 public boolean isRequestorMode() {
369 return requestorMode;
373 * The requestor peer for this event handler.
375 * @return the requestor peer for this event handler
377 public String getRequestorPeer() {
378 return requestorPeer;
382 * Get the requestor timeout.
383 * @return the requestorTimeout.
385 public long getRequestorTimeout() {
386 return requestorTimeout;
392 * @see java.lang.Object#toString()
395 public String toString() {
396 return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
397 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode="
398 + synchronousMode + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout="
399 + synchronousTimeout + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer
400 + ", requestorTimeout=" + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter="
401 + eventNameFilter + "]";