c6e9239488fe25e7d4771429c96bf18cf80734a6
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / MessagePropertiesDSL.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.message
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
25 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
26 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.RelationshipTemplateBuilder
27 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
29
30 /** Relationships Types DSL for Message Producer */
31 fun BluePrintTypes.relationshipTypeConnectsToMessageProducer(): RelationshipType {
32     return relationshipType(
33         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_PRODUCER,
34         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
35         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
36         description = "Relationship connects to through message producer."
37     ) {
38         property(
39             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
40             BluePrintConstants.DATA_TYPE_MAP,
41             true,
42             "Connection Config details."
43         )
44         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
45     }
46 }
47
48 fun BluePrintTypes.relationshipTypeConnectsToMessageConsumer(): RelationshipType {
49     return relationshipType(
50         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_CONSUMER,
51         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
52         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
53         description = "Relationship type connects to message consumer."
54     ) {
55         property(
56             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
57             BluePrintConstants.DATA_TYPE_MAP,
58             true,
59             "Connection Config details."
60         )
61         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
62     }
63 }
64
65 /** Relationships Templates DSL for Message Producer */
66 fun TopologyTemplateBuilder.relationshipTemplateMessageProducer(
67     name: String,
68     description: String,
69     block: MessageProducerRelationshipTemplateBuilder.() -> Unit
70 ) {
71     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
72     val relationshipTemplate =
73         MessageProducerRelationshipTemplateBuilder(name, description).apply(block).build()
74     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
75 }
76
77 class MessageProducerRelationshipTemplateBuilder(name: String, description: String) :
78     RelationshipTemplateBuilder(
79         name,
80         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_PRODUCER, description
81     ) {
82
83     fun kafkaBasicAuth(block: KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder.() -> Unit) {
84         property(
85             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
86             BluePrintTypes.kafkaBasicAuthMessageProducerProperties(block)
87         )
88     }
89 }
90
91 fun BluePrintTypes.kafkaBasicAuthMessageProducerProperties(block: KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
92     val assignments = KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder().apply(block).build()
93     assignments[KafkaBasicAuthMessageProducerProperties::type.name] =
94         MessageLibConstants.TYPE_KAFKA_BASIC_AUTH.asJsonPrimitive()
95     return assignments.asJsonType()
96 }
97
98 open class MessageProducerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder()
99
100 class KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder : MessageProducerPropertiesAssignmentBuilder() {
101
102     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
103
104     fun bootstrapServers(bootstrapServers: JsonNode) =
105         property(KafkaBasicAuthMessageProducerProperties::bootstrapServers, bootstrapServers)
106
107     fun topic(topic: String) = topic(topic.asJsonPrimitive())
108
109     fun topic(topic: JsonNode) =
110         property(KafkaBasicAuthMessageProducerProperties::topic, topic)
111
112     fun clientId(clientId: String) = bootstrapServers(clientId.asJsonPrimitive())
113
114     fun clientId(clientId: JsonNode) =
115         property(KafkaBasicAuthMessageProducerProperties::clientId, clientId)
116
117     fun acks(acks: String) = acks(acks.asJsonPrimitive())
118
119     fun acks(acks: JsonNode) = property(KafkaBasicAuthMessageProducerProperties::acks, acks)
120
121     fun retries(retries: Int) = retries(retries.asJsonPrimitive())
122
123     fun retries(retries: JsonNode) = property(KafkaBasicAuthMessageProducerProperties::retries, retries)
124
125     fun enableIdempotence(enableIdempotence: Boolean) = enableIdempotence(enableIdempotence.asJsonPrimitive())
126
127     fun enableIdempotence(enableIdempotence: JsonNode) =
128         property(KafkaBasicAuthMessageProducerProperties::enableIdempotence, enableIdempotence)
129 }
130
131 /** Relationships Templates DSL for Message Consumer */
132 fun TopologyTemplateBuilder.relationshipTemplateMessageConsumer(
133     name: String,
134     description: String,
135     block: MessageConsumerRelationshipTemplateBuilder.() -> Unit
136 ) {
137     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
138     val relationshipTemplate =
139         MessageConsumerRelationshipTemplateBuilder(name, description).apply(block).build()
140     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
141 }
142
143 class MessageConsumerRelationshipTemplateBuilder(name: String, description: String) :
144     RelationshipTemplateBuilder(
145         name,
146         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_CONSUMER, description
147     ) {
148
149     fun kafkaBasicAuth(block: KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder.() -> Unit) {
150         property(
151             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
152             BluePrintTypes.kafkaBasicAuthMessageConsumerProperties(block)
153         )
154     }
155
156     fun kafkaStreamsBasicAuth(block: KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder.() -> Unit) {
157         property(
158             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
159             BluePrintTypes.kafkaStreamsBasicAuthConsumerProperties(block)
160         )
161     }
162 }
163
164 fun BluePrintTypes.kafkaBasicAuthMessageConsumerProperties(block: KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
165     val assignments = KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder().apply(block).build()
166     assignments[KafkaBasicAuthMessageConsumerProperties::type.name] =
167         MessageLibConstants.TYPE_KAFKA_BASIC_AUTH.asJsonPrimitive()
168     return assignments.asJsonType()
169 }
170
171 fun BluePrintTypes.kafkaStreamsBasicAuthConsumerProperties(block: KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
172     val assignments = KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder().apply(block).build()
173     assignments[KafkaStreamsBasicAuthConsumerProperties::type.name] =
174         MessageLibConstants.TYPE_KAFKA_STREAMS_BASIC_AUTH.asJsonPrimitive()
175     return assignments.asJsonType()
176 }
177
178 open class MessageConsumerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder()
179
180 open class KafkaMessageConsumerPropertiesAssignmentBuilder : MessageConsumerPropertiesAssignmentBuilder() {
181
182     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
183
184     fun bootstrapServers(bootstrapServers: JsonNode) =
185         property(KafkaMessageConsumerProperties::bootstrapServers, bootstrapServers)
186
187     fun groupId(groupId: String) = groupId(groupId.asJsonPrimitive())
188
189     fun groupId(groupId: JsonNode) =
190         property(KafkaMessageConsumerProperties::groupId, groupId)
191
192     fun clientId(clientId: String) = clientId(clientId.asJsonPrimitive())
193
194     fun clientId(clientId: JsonNode) =
195         property(KafkaMessageConsumerProperties::clientId, clientId)
196
197     fun topic(topic: String) = topic(topic.asJsonPrimitive())
198
199     fun topic(topic: JsonNode) =
200         property(KafkaMessageConsumerProperties::topic, topic)
201
202     fun autoCommit(autoCommit: Boolean) = autoCommit(autoCommit.asJsonPrimitive())
203
204     fun autoCommit(autoCommit: JsonNode) =
205         property(KafkaMessageConsumerProperties::autoCommit, autoCommit)
206
207     fun autoOffsetReset(autoOffsetReset: String) = autoOffsetReset(autoOffsetReset.asJsonPrimitive())
208
209     fun autoOffsetReset(autoOffsetReset: JsonNode) =
210         property(KafkaMessageConsumerProperties::autoOffsetReset, autoOffsetReset)
211
212     fun pollMillSec(pollMillSec: Int) = pollMillSec(pollMillSec.asJsonPrimitive())
213
214     fun pollMillSec(pollMillSec: JsonNode) =
215         property(KafkaMessageConsumerProperties::pollMillSec, pollMillSec)
216
217     fun pollRecords(pollRecords: Int) = pollRecords(pollRecords.asJsonPrimitive())
218
219     fun pollRecords(pollRecords: JsonNode) =
220         property(KafkaMessageConsumerProperties::pollRecords, pollRecords)
221 }
222
223 /** KafkaBasicAuthMessageConsumerProperties assignment builder */
224 class KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder : KafkaMessageConsumerPropertiesAssignmentBuilder()
225
226 /** KafkaStreamsConsumerProperties assignment builder */
227 open class KafkaStreamsConsumerPropertiesAssignmentBuilder : MessageConsumerPropertiesAssignmentBuilder() {
228
229     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
230
231     fun bootstrapServers(bootstrapServers: JsonNode) =
232         property(KafkaStreamsConsumerProperties::bootstrapServers, bootstrapServers)
233
234     fun applicationId(applicationId: String) = bootstrapServers(applicationId.asJsonPrimitive())
235
236     fun applicationId(applicationId: JsonNode) =
237         property(KafkaStreamsConsumerProperties::applicationId, applicationId)
238
239     fun topic(topic: String) = topic(topic.asJsonPrimitive())
240
241     fun topic(topic: JsonNode) =
242         property(KafkaStreamsConsumerProperties::topic, topic)
243
244     fun autoOffsetReset(autoOffsetReset: String) = autoOffsetReset(autoOffsetReset.asJsonPrimitive())
245
246     fun autoOffsetReset(autoOffsetReset: JsonNode) =
247         property(KafkaStreamsConsumerProperties::autoOffsetReset, autoOffsetReset)
248
249     fun processingGuarantee(processingGuarantee: String) = processingGuarantee(processingGuarantee.asJsonPrimitive())
250
251     fun processingGuarantee(processingGuarantee: JsonNode) =
252         property(KafkaStreamsConsumerProperties::processingGuarantee, processingGuarantee)
253 }
254
255 class KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder : KafkaStreamsConsumerPropertiesAssignmentBuilder()