Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / parameters / eventhandler / EventHandlerParameters.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.parameters.eventhandler;
22
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;
33
34 /**
35  * The parameters for a single event producer, event consumer or synchronous event handler.
36  * 
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.
39  * 
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>
51  *
52  * @author Liam Fallon (liam.fallon@ericsson.com)
53  */
54 public class EventHandlerParameters implements ParameterGroup {
55     // Get a reference to the logger
56     private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class);
57
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;
69
70     /**
71      * Constructor to create an event handler parameters instance.
72      */
73     public EventHandlerParameters() {
74         super();
75
76         // Set the name for the parameters
77         this.name = ApexParameterConstants.EVENT_HANDLER_GROUP_NAME;
78     }
79
80     /**
81      * Gets the name of the event handler.
82      *
83      * @return the event handler name
84      */
85     public String getName() {
86         return name;
87     }
88
89     /**
90      * Sets the name of the event handler.
91      *
92      * @param name the event handler name
93      */
94     public void setName(final String name) {
95         this.name = name;
96     }
97
98     /**
99      * Checks if the name of the event handler is set.
100      * 
101      * @return true if the name is set
102      */
103     public boolean checkSetName() {
104         return !(name == null || name.trim().length() == 0);
105     }
106
107     /**
108      * Gets the carrier technology parameters of the event handler.
109      *
110      * @return the carrierTechnologyParameters of the event handler
111      */
112     public CarrierTechnologyParameters getCarrierTechnologyParameters() {
113         return carrierTechnologyParameters;
114     }
115
116     /**
117      * Sets the carrier technology parameters of the event handler.
118      *
119      * @param carrierTechnologyParameters the carrierTechnologyParameters to set
120      */
121     public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
122         this.carrierTechnologyParameters = carrierTechnologyParameters;
123     }
124
125     /**
126      * Gets the event protocol parameters of the event handler.
127      *
128      * @return the eventProtocolParameters
129      */
130     public EventProtocolParameters getEventProtocolParameters() {
131         return eventProtocolParameters;
132     }
133
134     /**
135      * Sets the event protocol parameters.
136      *
137      * @param eventProtocolParameters the eventProtocolParameters to set
138      */
139     public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
140         this.eventProtocolParameters = eventProtocolParameters;
141     }
142
143     /**
144      * Checks if the event handler is in the given peered mode.
145      *
146      * @param peeredMode the peer mode
147      * @return true, if the event handler is in the peered mode
148      */
149     public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
150         switch (peeredMode) {
151             case SYNCHRONOUS:
152                 return synchronousMode;
153             case REQUESTOR:
154                 return requestorMode;
155             default:
156                 return false;
157         }
158     }
159
160     /**
161      * Sets a peered mode as true or false on the event handler.
162      *
163      * @param peeredMode the peered mode to set
164      * @param peeredModeValue the value to set the peered mode to
165      */
166     public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
167         switch (peeredMode) {
168             case SYNCHRONOUS:
169                 synchronousMode = peeredModeValue;
170                 return;
171             case REQUESTOR:
172                 requestorMode = peeredModeValue;
173                 return;
174             default:
175                 return;
176         }
177     }
178
179     /**
180      * Gets the peer for the event handler in this peered mode.
181      *
182      * @param peeredMode the peered mode to get the peer for
183      * @return the peer
184      */
185     public String getPeer(final EventHandlerPeeredMode peeredMode) {
186         switch (peeredMode) {
187             case SYNCHRONOUS:
188                 return synchronousPeer;
189             case REQUESTOR:
190                 return requestorPeer;
191             default:
192                 return null;
193         }
194     }
195
196     /**
197      * Sets the peer for the event handler in this peered mode.
198      *
199      * @param peeredMode the peered mode to set the peer for
200      * @param peer the peer
201      */
202     public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
203         switch (peeredMode) {
204             case SYNCHRONOUS:
205                 synchronousPeer = peer;
206                 return;
207             case REQUESTOR:
208                 requestorPeer = peer;
209                 return;
210             default:
211                 return;
212         }
213     }
214
215     /**
216      * Get the timeout value for the event handler in peered mode.
217      * 
218      * @param peeredMode the peered mode to get the timeout for
219      * @return the timeout value
220      */
221     public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
222         switch (peeredMode) {
223             case SYNCHRONOUS:
224                 return synchronousTimeout;
225             case REQUESTOR:
226                 return requestorTimeout;
227             default:
228                 return -1;
229         }
230     }
231
232     /**
233      * Set the timeout value for the event handler in peered mode.
234      * 
235      * @param peeredMode the peered mode to set the timeout for
236      * @param timeout the timeout value
237      */
238     public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
239         switch (peeredMode) {
240             case SYNCHRONOUS:
241                 synchronousTimeout = timeout;
242                 return;
243             case REQUESTOR:
244                 requestorTimeout = timeout;
245                 return;
246             default:
247                 return;
248         }
249     }
250
251     /**
252      * Check if an event name is being used.
253      *
254      * @return true if an event name is being used
255      */
256     public boolean isSetEventName() {
257         return eventName != null;
258     }
259
260     /**
261      * Gets the event name for this event handler.
262      *
263      * @return the event name
264      */
265     public String getEventName() {
266         return eventName;
267     }
268
269     /**
270      * Sets the event name for this event handler.
271      *
272      * @param eventName the event name
273      */
274     public void setEventName(final String eventName) {
275         this.eventName = eventName;
276     }
277
278     /**
279      * Check if event name filtering is being used.
280      *
281      * @return true if event name filtering is being used
282      */
283     public boolean isSetEventNameFilter() {
284         return eventNameFilter != null;
285     }
286
287     /**
288      * Gets the event name filter for this event handler.
289      *
290      * @return the event name filter
291      */
292     public String getEventNameFilter() {
293         return eventNameFilter;
294     }
295
296     /**
297      * Sets the event name filter for this event handler.
298      *
299      * @param eventNameFilter the event name filter
300      */
301     public void setEventNameFilter(final String eventNameFilter) {
302         this.eventNameFilter = eventNameFilter;
303     }
304
305     /**
306      * {@inheritDoc}.
307      */
308     @Override
309     public GroupValidationResult validate() {
310         final GroupValidationResult result = new GroupValidationResult(this);
311
312         if (eventProtocolParameters == null) {
313             result.setResult("eventProtocolParameters", ValidationStatus.INVALID,
314                             "event handler eventProtocolParameters not specified or blank");
315         } else {
316             result.setResult("eventProtocolParameters", eventProtocolParameters.validate());
317         }
318
319         if (carrierTechnologyParameters == null) {
320             result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID,
321                             "event handler carrierTechnologyParameters not specified or blank");
322         } else {
323             result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate());
324         }
325
326         if (eventNameFilter != null) {
327             try {
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);
333             }
334         }
335
336         return result;
337     }
338
339     /**
340      * Check if we're using synchronous mode.
341      * 
342      * @return true if if we're using synchronous mode
343      */
344     public boolean isSynchronousMode() {
345         return synchronousMode;
346     }
347
348     /**
349      * The synchronous peer for this event handler.
350      * 
351      * @return the synchronous peer for this event handler
352      */
353     public String getSynchronousPeer() {
354         return synchronousPeer;
355     }
356
357     /**
358      * Get the timeout for synchronous operations.
359      * 
360      * @return the timeout for synchronous operations
361      */
362     public long getSynchronousTimeout() {
363         return synchronousTimeout;
364     }
365
366     /**
367      * Check if this event handler will use requestor mode.
368      * 
369      * @return true if this event handler will use requestor mode
370      */
371     public boolean isRequestorMode() {
372         return requestorMode;
373     }
374
375     /**
376      * The requestor peer for this event handler.
377      * 
378      * @return the requestor peer for this event handler
379      */
380     public String getRequestorPeer() {
381         return requestorPeer;
382     }
383
384     /**
385      * Get the requestor timeout.
386      * 
387      * @return the requestorTimeout.
388      */
389     public long getRequestorTimeout() {
390         return requestorTimeout;
391     }
392
393     /**
394      * {@inheritDoc}.
395      */
396     @Override
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 + "]";
404     }
405 }