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