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