34589f31bffe9c286df03861b947b10f0b8837a3
[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.model.basicmodel.service.AbstractParameters;
27 import org.onap.policy.apex.service.parameters.ApexParameterValidator;
28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
29 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
30
31 /**
32  * The parameters for a single event producer, event consumer or synchronous event handler.
33  * <p>
34  * Event producers, consumers, and synchronous event handlers all use a carrier technology and an
35  * event protocol so the actual parameters for each one are the same. Therefore, we use the same
36  * class for the parameters of each one.
37  * <p>
38  * The following parameters are defined:
39  * <ol>
40  * <li>carrierTechnologyParameters: The carrier technology is the type of messaging infrastructure
41  * used to carry events. Examples are File, Kafka or REST.
42  * <li>eventProtocolParameters: The format that the events are in when being carried. Examples are
43  * JSON, XML, or Java Beans. carrier technology
44  * <li>synchronousMode: true if the event handler is working in synchronous mode, defaults to false
45  * <li>synchronousPeer: the peer event handler (consumer for producer or producer for consumer) of
46  * this event handler in synchronous mode
47  * <li>synchronousTimeout: the amount of time to wait for the reply to synchronous events before
48  * 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
51  * this event handler in requestor mode
52  * <li>requestorTimeout: the amount of time to wait for the reply to synchronous events before they
53  * are timed out
54  * <li>eventNameFilter: a regular expression to apply to events on this event handler. If specified,
55  * events not matching the given regular expression are ignored. If it is null, all events are
56  * handledDefaults to null.
57  * </ol>
58  *
59  * @author Liam Fallon (liam.fallon@ericsson.com)
60  */
61 public class EventHandlerParameters extends AbstractParameters implements ApexParameterValidator {
62     private String name = null;
63     private CarrierTechnologyParameters carrierTechnologyParameters = null;
64     private EventProtocolParameters eventProtocolParameters = null;
65     private boolean synchronousMode = false;
66     private String synchronousPeer = null;
67     private long synchronousTimeout = 0;
68     private boolean requestorMode = false;
69     private String requestorPeer = null;
70     private long requestorTimeout = 0;
71     private String eventName = null;
72     private String eventNameFilter = null;
73
74     /**
75      * Constructor to create an event handler parameters instance.
76      */
77     public EventHandlerParameters() {
78         super(EventHandlerParameters.class.getCanonicalName());
79     }
80
81     /**
82      * Constructor to create an event handler parameters instance with the name of a sub class of
83      * this class.
84      *
85      * @param parameterClassName the class name of a sub class of this class
86      */
87     public EventHandlerParameters(final String parameterClassName) {
88         super(parameterClassName);
89     }
90
91     /**
92      * Gets the name of the event handler.
93      *
94      * @return the event handler name
95      */
96     public String getName() {
97         return name;
98     }
99
100     /**
101      * Sets the name of the event handler.
102      *
103      * @param name the event handler name
104      */
105     public void setName(final String name) {
106         this.name = name;
107     }
108
109     /**
110      * Checks if the name of the event handler is set.
111      * 
112      * @return true if the name is set
113      */
114     public boolean checkSetName() {
115         return !(name == null || name.trim().length() == 0);
116     }
117
118     /**
119      * Gets the carrier technology parameters of the event handler.
120      *
121      * @return the carrierTechnologyParameters of the event handler
122      */
123     public CarrierTechnologyParameters getCarrierTechnologyParameters() {
124         return carrierTechnologyParameters;
125     }
126
127     /**
128      * Sets the carrier technology parameters of the event handler.
129      *
130      * @param carrierTechnologyParameters the carrierTechnologyParameters to set
131      */
132     public void setCarrierTechnologyParameters(final CarrierTechnologyParameters carrierTechnologyParameters) {
133         this.carrierTechnologyParameters = carrierTechnologyParameters;
134     }
135
136     /**
137      * Gets the event protocol parameters of the event handler.
138      *
139      * @return the eventProtocolParameters
140      */
141     public EventProtocolParameters getEventProtocolParameters() {
142         return eventProtocolParameters;
143     }
144
145     /**
146      * Sets the event protocol parameters.
147      *
148      * @param eventProtocolParameters the eventProtocolParameters to set
149      */
150     public void setEventProtocolParameters(final EventProtocolParameters eventProtocolParameters) {
151         this.eventProtocolParameters = eventProtocolParameters;
152     }
153
154
155     /**
156      * Checks if the event handler is in the given peered mode.
157      *
158      * @param peeredMode the peer mode
159      * @return true, if the event handler is in the peered mode
160      */
161     public boolean isPeeredMode(final EventHandlerPeeredMode peeredMode) {
162         switch (peeredMode) {
163             case SYNCHRONOUS:
164                 return synchronousMode;
165             case REQUESTOR:
166                 return requestorMode;
167             default:
168                 return false;
169         }
170     }
171
172     /**
173      * Sets a peered mode as true or false on the event handler.
174      *
175      * @param peeredMode the peered mode to set
176      * @param peeredModeValue the value to set the peered mode to
177      */
178     public void setPeeredMode(final EventHandlerPeeredMode peeredMode, final boolean peeredModeValue) {
179         switch (peeredMode) {
180             case SYNCHRONOUS:
181                 synchronousMode = peeredModeValue;
182                 return;
183             case REQUESTOR:
184                 requestorMode = peeredModeValue;
185                 return;
186             default:
187                 return;
188         }
189     }
190
191     /**
192      * Gets the peer for the event handler in this peered mode.
193      *
194      * @param peeredMode the peered mode to get the peer for
195      * @return the peer
196      */
197     public String getPeer(final EventHandlerPeeredMode peeredMode) {
198         switch (peeredMode) {
199             case SYNCHRONOUS:
200                 return synchronousPeer;
201             case REQUESTOR:
202                 return requestorPeer;
203             default:
204                 return null;
205         }
206     }
207
208     /**
209      * Sets the peer for the event handler in this peered mode.
210      *
211      * @param peeredMode the peered mode to set the peer for
212      * @param peer the peer
213      */
214     public void setPeer(final EventHandlerPeeredMode peeredMode, final String peer) {
215         switch (peeredMode) {
216             case SYNCHRONOUS:
217                 synchronousPeer = peer;
218                 return;
219             case REQUESTOR:
220                 requestorPeer = peer;
221                 return;
222             default:
223                 return;
224         }
225     }
226
227     /**
228      * Get the timeout value for the event handler in peered mode.
229      * 
230      * @param peeredMode the peered mode to get the timeout for
231      * @return the timeout value
232      */
233     public long getPeerTimeout(final EventHandlerPeeredMode peeredMode) {
234         switch (peeredMode) {
235             case SYNCHRONOUS:
236                 return synchronousTimeout;
237             case REQUESTOR:
238                 return requestorTimeout;
239             default:
240                 return -1;
241         }
242     }
243
244     /**
245      * Set the timeout value for the event handler in peered mode.
246      * 
247      * @param peeredMode the peered mode to set the timeout for
248      * @param timeout the timeout value
249      */
250     public void setPeerTimeout(final EventHandlerPeeredMode peeredMode, final long timeout) {
251         switch (peeredMode) {
252             case SYNCHRONOUS:
253                 synchronousTimeout = timeout;
254                 return;
255             case REQUESTOR:
256                 requestorTimeout = timeout;
257                 return;
258             default:
259                 return;
260         }
261     }
262
263     /**
264      * Check if an event name is being used.
265      *
266      * @return true if an event name is being used
267      */
268     public boolean isSetEventName() {
269         return eventName != null;
270     }
271
272     /**
273      * Gets the event name for this event handler.
274      *
275      * @return the event name
276      */
277     public String getEventName() {
278         return eventName;
279     }
280
281     /**
282      * Sets the event name for this event handler.
283      *
284      * @param eventName the event name
285      */
286     public void setEventName(final String eventName) {
287         this.eventName = eventName;
288     }
289
290     /**
291      * Check if event name filtering is being used.
292      *
293      * @return true if event name filtering is being used
294      */
295     public boolean isSetEventNameFilter() {
296         return eventNameFilter != null;
297     }
298
299     /**
300      * Gets the event name filter for this event handler.
301      *
302      * @return the event name filter
303      */
304     public String getEventNameFilter() {
305         return eventNameFilter;
306     }
307
308     /**
309      * Sets the event name filter for this event handler.
310      *
311      * @param eventNameFilter the event name filter
312      */
313     public void setEventNameFilter(final String eventNameFilter) {
314         this.eventNameFilter = eventNameFilter;
315     }
316
317     /*
318      * (non-Javadoc)
319      *
320      * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
321      */
322     @Override
323     public String validate() {
324         final StringBuilder errorMessageBuilder = new StringBuilder();
325
326         if (eventProtocolParameters == null) {
327             errorMessageBuilder.append("  event handler eventProtocolParameters not specified or blank\n");
328         } else {
329             errorMessageBuilder.append(eventProtocolParameters.validate());
330         }
331
332         if (carrierTechnologyParameters == null) {
333             errorMessageBuilder.append("  event handler carrierTechnologyParameters not specified or blank\n");
334         } else {
335             errorMessageBuilder.append(carrierTechnologyParameters.validate());
336         }
337
338         if (eventNameFilter != null) {
339             try {
340                 Pattern.compile(eventNameFilter);
341             } catch (final PatternSyntaxException pse) {
342                 errorMessageBuilder.append("  event handler eventNameFilter is not a valid regular expression: "
343                         + pse.getMessage() + "\n");
344             }
345         }
346         return errorMessageBuilder.toString();
347     }
348
349     /*
350      * (non-Javadoc)
351      * 
352      * @see java.lang.Object#toString()
353      */
354     @Override
355     public String toString() {
356         return "EventHandlerParameters [name=" + name + ", carrierTechnologyParameters=" + carrierTechnologyParameters
357                 + ", eventProtocolParameters=" + eventProtocolParameters + ", synchronousMode=" + synchronousMode
358                 + ", synchronousPeer=" + synchronousPeer + ", synchronousTimeout=" + synchronousTimeout
359                 + ", requestorMode=" + requestorMode + ", requestorPeer=" + requestorPeer + ", requestorTimeout="
360                 + requestorTimeout + ", eventName=" + eventName + ", eventNameFilter=" + eventNameFilter + "]";
361     }
362 }