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