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