7eaf4c8e15bab757c3589a2287edf30378e7d43a
[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.engine.parameters.dummyclasses;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Properties;
26
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ValidationStatus;
30
31 /**
32  * Apex parameters for SuperDooper as an event carrier technology.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class SuperDooperCarrierTechnologyParameters extends CarrierTechnologyParameters {
37     // Default parameter values
38     private static final String DEFAULT_ACKS = "all";
39     private static final String DEFAULT_BOOTSTRAP_SERVERS = "localhost:9092";
40     private static final int DEFAULT_RETRIES = 0;
41     private static final int DEFAULT_BATCH_SIZE = 16384;
42     private static final int DEFAULT_LINGER_TIME = 1;
43     private static final long DEFAULT_BUFFER_MEMORY = 33554432;
44     private static final String DEFAULT_GROUP_ID = "default-group-id";
45     private static final boolean DEFAULT_ENABLE_AUTO_COMMIT = true;
46     private static final int DEFAULT_AUTO_COMMIT_TIME = 1000;
47     private static final int DEFAULT_SESSION_TIMEOUT = 30000;
48     private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
49     private static final int DEFAULT_CONSUMER_POLL_TIME = 100;
50     private static final String[] DEFAULT_CONSUMER_TOPIC_LIST = { "apex-in" };
51     private static final String DEFAULT_KEY_SERIALIZER = "org.apache.superDooper.common.serialization.StringSerializer";
52     private static final String DEFAULT_VALUE_SERIALIZER = "org.apache.superDooper.common.serialization.StringSerializer";
53     private static final String DEFAULT_KEY_DESERIALIZER = "org.apache.superDooper.common.serialization.StringDeserializer";
54     private static final String DEFAULT_VALUE_DESERIALIZER = "org.apache.superDooper.common.serialization.StringDeserializer";
55
56     // Parameter property map tokens
57     private static final String PROPERTY_BOOTSTRAP_SERVERS = "bootstrap.servers";
58     private static final String PROPERTY_ACKS = "acks";
59     private static final String PROPERTY_RETRIES = "retries";
60     private static final String PROPERTY_BATCH_SIZE = "batch.size";
61     private static final String PROPERTY_LINGER_TIME = "linger.ms";
62     private static final String PROPERTY_BUFFER_MEMORY = "buffer.memory";
63     private static final String PROPERTY_GROUP_ID = "group.id";
64     private static final String PROPERTY_ENABLE_AUTO_COMMIT = "enable.auto.commit";
65     private static final String PROPERTY_AUTO_COMMIT_TIME = "auto.commit.interval.ms";
66     private static final String PROPERTY_SESSION_TIMEOUT = "session.timeout.ms";
67     private static final String PROPERTY_KEY_SERIALIZER = "key.serializer";
68     private static final String PROPERTY_VALUE_SERIALIZER = "value.serializer";
69     private static final String PROPERTY_KEY_DESERIALIZER = "key.deserializer";
70     private static final String PROPERTY_VALUE_DESERIALIZER = "value.deserializer";
71
72     // superDooper carrier parameters
73     private String bootstrapServers = DEFAULT_BOOTSTRAP_SERVERS;
74     private String acks = DEFAULT_ACKS;
75     private int retries = DEFAULT_RETRIES;
76     private int batchSize = DEFAULT_BATCH_SIZE;
77     private int lingerTime = DEFAULT_LINGER_TIME;
78     private long bufferMemory = DEFAULT_BUFFER_MEMORY;
79     private String groupId = DEFAULT_GROUP_ID;
80     private boolean enableAutoCommit = DEFAULT_ENABLE_AUTO_COMMIT;
81     private int autoCommitTime = DEFAULT_AUTO_COMMIT_TIME;
82     private int sessionTimeout = DEFAULT_SESSION_TIMEOUT;
83     private String producerTopic = DEFAULT_PRODUCER_TOPIC;
84     private int consumerPollTime = DEFAULT_CONSUMER_POLL_TIME;
85     private String[] consumerTopicList = DEFAULT_CONSUMER_TOPIC_LIST;
86     private String keySerializer = DEFAULT_KEY_SERIALIZER;
87     private String valueSerializer = DEFAULT_VALUE_SERIALIZER;
88     private String keyDeserializer = DEFAULT_KEY_DESERIALIZER;
89     private String valueDeserializer = DEFAULT_VALUE_DESERIALIZER;
90
91     /**
92      * Constructor to create a file carrier technology parameters instance and register the instance with the parameter
93      * service.
94      */
95     public SuperDooperCarrierTechnologyParameters() {
96         super();
97
98         // Set the carrier technology properties for the FILE carrier technology
99         this.setLabel("SUPER_DOOPER");
100         this.setEventProducerPluginClass(
101                         "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventProducer");
102         this.setEventConsumerPluginClass(
103                         "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventSubscriber");
104     }
105
106     /**
107      * Gets the superDooper producer properties.
108      *
109      * @return the superDooper producer properties
110      */
111     public Properties getSuperDooperProducerProperties() {
112         final Properties superDooperProperties = new Properties();
113
114         superDooperProperties.put(PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers);
115         superDooperProperties.put(PROPERTY_ACKS, acks);
116         superDooperProperties.put(PROPERTY_RETRIES, retries);
117         superDooperProperties.put(PROPERTY_BATCH_SIZE, batchSize);
118         superDooperProperties.put(PROPERTY_LINGER_TIME, lingerTime);
119         superDooperProperties.put(PROPERTY_BUFFER_MEMORY, bufferMemory);
120         superDooperProperties.put(PROPERTY_KEY_SERIALIZER, keySerializer);
121         superDooperProperties.put(PROPERTY_VALUE_SERIALIZER, valueSerializer);
122
123         return superDooperProperties;
124     }
125
126     /**
127      * Gets the superDooper consumer properties.
128      *
129      * @return the superDooper consumer properties
130      */
131     public Properties getSuperDooperConsumerProperties() {
132         final Properties superDooperProperties = new Properties();
133
134         superDooperProperties.put(PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers);
135         superDooperProperties.put(PROPERTY_GROUP_ID, groupId);
136         superDooperProperties.put(PROPERTY_ENABLE_AUTO_COMMIT, enableAutoCommit);
137         superDooperProperties.put(PROPERTY_AUTO_COMMIT_TIME, autoCommitTime);
138         superDooperProperties.put(PROPERTY_SESSION_TIMEOUT, sessionTimeout);
139         superDooperProperties.put(PROPERTY_KEY_DESERIALIZER, keyDeserializer);
140         superDooperProperties.put(PROPERTY_VALUE_DESERIALIZER, valueDeserializer);
141
142         return superDooperProperties;
143     }
144
145     /**
146      * Gets the bootstrap servers.
147      *
148      * @return the bootstrap servers
149      */
150     public String getBootstrapServers() {
151         return bootstrapServers;
152     }
153
154     /**
155      * Gets the acks.
156      *
157      * @return the acks
158      */
159     public String getAcks() {
160         return acks;
161     }
162
163     /**
164      * Gets the retries.
165      *
166      * @return the retries
167      */
168     public int getRetries() {
169         return retries;
170     }
171
172     /**
173      * Gets the batch size.
174      *
175      * @return the batch size
176      */
177     public int getBatchSize() {
178         return batchSize;
179     }
180
181     /**
182      * Gets the linger time.
183      *
184      * @return the linger time
185      */
186     public int getLingerTime() {
187         return lingerTime;
188     }
189
190     /**
191      * Gets the buffer memory.
192      *
193      * @return the buffer memory
194      */
195     public long getBufferMemory() {
196         return bufferMemory;
197     }
198
199     /**
200      * Gets the group id.
201      *
202      * @return the group id
203      */
204     public String getGroupId() {
205         return groupId;
206     }
207
208     /**
209      * Checks if is enable auto commit.
210      *
211      * @return true, if checks if is enable auto commit
212      */
213     public boolean isEnableAutoCommit() {
214         return enableAutoCommit;
215     }
216
217     /**
218      * Gets the auto commit time.
219      *
220      * @return the auto commit time
221      */
222     public int getAutoCommitTime() {
223         return autoCommitTime;
224     }
225
226     /**
227      * Gets the session timeout.
228      *
229      * @return the session timeout
230      */
231     public int getSessionTimeout() {
232         return sessionTimeout;
233     }
234
235     /**
236      * Gets the producer topic.
237      *
238      * @return the producer topic
239      */
240     public String getProducerTopic() {
241         return producerTopic;
242     }
243
244     /**
245      * Gets the consumer poll time.
246      *
247      * @return the consumer poll time
248      */
249     public long getConsumerPollTime() {
250         return consumerPollTime;
251     }
252
253     /**
254      * Gets the consumer topic list.
255      *
256      * @return the consumer topic list
257      */
258     public Collection<String> getConsumerTopicList() {
259         return Arrays.asList(consumerTopicList);
260     }
261
262     /**
263      * Gets the key serializer.
264      *
265      * @return the key serializer
266      */
267     public String getKeySerializer() {
268         return keySerializer;
269     }
270
271     /**
272      * Gets the value serializer.
273      *
274      * @return the value serializer
275      */
276     public String getValueSerializer() {
277         return valueSerializer;
278     }
279
280     /**
281      * Gets the key deserializer.
282      *
283      * @return the key deserializer
284      */
285     public String getKeyDeserializer() {
286         return keyDeserializer;
287     }
288
289     /**
290      * Gets the value deserializer.
291      *
292      * @return the value deserializer
293      */
294     public String getValueDeserializer() {
295         return valueDeserializer;
296     }
297
298     /**
299      * Sets the bootstrap servers.
300      *
301      * @param bootstrapServers the new bootstrap servers
302      */
303     public void setBootstrapServers(String bootstrapServers) {
304         this.bootstrapServers = bootstrapServers;
305     }
306
307     /**
308      * Sets the acks.
309      *
310      * @param acks the new acks
311      */
312     public void setAcks(String acks) {
313         this.acks = acks;
314     }
315
316     /**
317      * Sets the retries.
318      *
319      * @param retries the new retries
320      */
321     public void setRetries(int retries) {
322         this.retries = retries;
323     }
324
325     /**
326      * Sets the batch size.
327      *
328      * @param batchSize the new batch size
329      */
330     public void setBatchSize(int batchSize) {
331         this.batchSize = batchSize;
332     }
333
334     /**
335      * Sets the linger time.
336      *
337      * @param lingerTime the new linger time
338      */
339     public void setLingerTime(int lingerTime) {
340         this.lingerTime = lingerTime;
341     }
342
343     /**
344      * Sets the buffer memory.
345      *
346      * @param bufferMemory the new buffer memory
347      */
348     public void setBufferMemory(long bufferMemory) {
349         this.bufferMemory = bufferMemory;
350     }
351
352     /**
353      * Sets the group id.
354      *
355      * @param groupId the new group id
356      */
357     public void setGroupId(String groupId) {
358         this.groupId = groupId;
359     }
360
361     /**
362      * Sets the enable auto commit.
363      *
364      * @param enableAutoCommit the new enable auto commit
365      */
366     public void setEnableAutoCommit(boolean enableAutoCommit) {
367         this.enableAutoCommit = enableAutoCommit;
368     }
369
370     /**
371      * Sets the auto commit time.
372      *
373      * @param autoCommitTime the new auto commit time
374      */
375     public void setAutoCommitTime(int autoCommitTime) {
376         this.autoCommitTime = autoCommitTime;
377     }
378
379     /**
380      * Sets the session timeout.
381      *
382      * @param sessionTimeout the new session timeout
383      */
384     public void setSessionTimeout(int sessionTimeout) {
385         this.sessionTimeout = sessionTimeout;
386     }
387
388     /**
389      * Sets the producer topic.
390      *
391      * @param producerTopic the new producer topic
392      */
393     public void setProducerTopic(String producerTopic) {
394         this.producerTopic = producerTopic;
395     }
396
397     /**
398      * Sets the consumer poll time.
399      *
400      * @param consumerPollTime the new consumer poll time
401      */
402     public void setConsumerPollTime(int consumerPollTime) {
403         this.consumerPollTime = consumerPollTime;
404     }
405
406     /**
407      * Sets the consumer topic list.
408      *
409      * @param consumerTopicList the new consumer topic list
410      */
411     public void setConsumerTopicList(String[] consumerTopicList) {
412         this.consumerTopicList = consumerTopicList;
413     }
414
415     /**
416      * Sets the key serializer.
417      *
418      * @param keySerializer the new key serializer
419      */
420     public void setKeySerializer(String keySerializer) {
421         this.keySerializer = keySerializer;
422     }
423
424     /**
425      * Sets the value serializer.
426      *
427      * @param valueSerializer the new value serializer
428      */
429     public void setValueSerializer(String valueSerializer) {
430         this.valueSerializer = valueSerializer;
431     }
432
433     /**
434      * Sets the key deserializer.
435      *
436      * @param keyDeserializer the new key deserializer
437      */
438     public void setKeyDeserializer(String keyDeserializer) {
439         this.keyDeserializer = keyDeserializer;
440     }
441
442     /**
443      * Sets the value deserializer.
444      *
445      * @param valueDeserializer the new value deserializer
446      */
447     public void setValueDeserializer(String valueDeserializer) {
448         this.valueDeserializer = valueDeserializer;
449     }
450
451     /*
452      * (non-Javadoc)
453      * 
454      * @see org.onap.policy.common.parameters.ParameterGroup#getName()
455      */
456     @Override
457     public String getName() {
458         return this.getLabel();
459     }
460
461     /*
462      * (non-Javadoc)
463      * 
464      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
465      */
466     @Override
467     public GroupValidationResult validate() {
468         final GroupValidationResult result = super.validate();
469
470         if (bootstrapServers == null || bootstrapServers.trim().length() == 0) {
471             result.setResult("bootstrapServers", ValidationStatus.INVALID,
472                             "bootstrapServers not specified, must be specified as a string of form host:port");
473         }
474
475         if (acks == null || acks.trim().length() == 0) {
476             result.setResult("acks", ValidationStatus.INVALID,
477                             "acks not specified, must be specified as a string with values [0|1|all]");
478         }
479
480         if (retries < 0) {
481             result.setResult("retries", ValidationStatus.INVALID, "[" + retries + "] invalid, must be specified as retries >= 0");
482         }
483
484         if (batchSize < 0) {
485             result.setResult("batchSize", ValidationStatus.INVALID, "[" + batchSize + "] invalid, must be specified as batchSize >= 0");
486         }
487
488         if (lingerTime < 0) {
489             result.setResult("lingerTime", ValidationStatus.INVALID, "[" + lingerTime + "] invalid, must be specified as lingerTime >= 0");
490         }
491
492         if (bufferMemory < 0) {
493             result.setResult("bufferMemory", ValidationStatus.INVALID, "[" + bufferMemory + "] invalid, must be specified as bufferMemory >= 0");
494         }
495
496         if (groupId == null || groupId.trim().length() == 0) {
497             result.setResult("groupId", ValidationStatus.INVALID, "not specified, must be specified as a string");
498         }
499
500         if (autoCommitTime < 0) {
501             result.setResult("autoCommitTime", ValidationStatus.INVALID, "[" + autoCommitTime
502                             + "] invalid, must be specified as autoCommitTime >= 0");
503         }
504
505         if (sessionTimeout < 0) {
506             result.setResult("sessionTimeout", ValidationStatus.INVALID, "sessionTimeout [" + sessionTimeout
507                             + "] invalid, must be specified as sessionTimeout >= 0");
508         }
509
510         if (producerTopic == null || producerTopic.trim().length() == 0) {
511             result.setResult("producerTopic", ValidationStatus.INVALID, "producerTopic not specified, must be specified as a string");
512         }
513
514         if (consumerPollTime < 0) {
515             result.setResult("consumerPollTime", ValidationStatus.INVALID, "[" + consumerPollTime
516                             + "] invalid, must be specified as consumerPollTime >= 0");
517         }
518
519         if (consumerTopicList == null || consumerTopicList.length == 0) {
520             result.setResult("consumerTopicList", ValidationStatus.INVALID, "not specified, must be specified as a list of strings");
521         }
522
523         StringBuilder consumerTopicMessageBuilder = new StringBuilder();
524         for (final String consumerTopic : consumerTopicList) {
525             if (consumerTopic == null || consumerTopic.trim().length() == 0) {
526                 consumerTopicMessageBuilder.append("  invalid consumer topic \"" + consumerTopic
527                                 + "\" specified on consumerTopicList, consumer topics must be specified as strings");
528             }
529         }
530         
531         if (consumerTopicMessageBuilder.length() > 0) {
532             result.setResult("consumerTopicList", ValidationStatus.INVALID, consumerTopicMessageBuilder.toString());
533         }
534
535         if (keySerializer == null || keySerializer.trim().length() == 0) {
536             result.setResult("keySerializer", ValidationStatus.INVALID, "not specified, must be specified as a string");
537         }
538
539         if (valueSerializer == null || valueSerializer.trim().length() == 0) {
540             result.setResult("valueSerializer", ValidationStatus.INVALID, "not specified, must be specified as a string");
541         }
542
543         if (keyDeserializer == null || keyDeserializer.trim().length() == 0) {
544             result.setResult("keyDeserializer", ValidationStatus.INVALID, "not specified, must be specified as a string");
545         }
546
547         if (valueDeserializer == null || valueDeserializer.trim().length() == 0) {
548             result.setResult("valueDeserializer", ValidationStatus.INVALID,
549                             "not specified, must be specified as a string");
550         }
551
552         return result;
553     }
554 }