Flexible DSL Types and Templates definition.
[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.ServiceTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
29 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
30
31 /** Relationships Types DSL for Message Producer */
32 fun ServiceTemplateBuilder.relationshipTypeConnectsToMessageProducer() {
33     val relationshipType = BluePrintTypes.relationshipTypeConnectsToMessageProducer()
34     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
35     this.relationshipTypes!![relationshipType.id!!] = relationshipType
36 }
37
38 fun BluePrintTypes.relationshipTypeConnectsToMessageProducer(): RelationshipType {
39     return relationshipType(
40         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_PRODUCER,
41         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
42         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
43         description = "Relationship connects to through message producer."
44     ) {
45         property(
46             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
47             BluePrintConstants.DATA_TYPE_MAP,
48             true,
49             "Connection Config details."
50         )
51         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
52     }
53 }
54
55 fun ServiceTemplateBuilder.relationshipTypeConnectsToMessageConsumer() {
56     val relationshipType = BluePrintTypes.relationshipTypeConnectsToMessageConsumer()
57     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
58     this.relationshipTypes!![relationshipType.id!!] = relationshipType
59 }
60
61 fun BluePrintTypes.relationshipTypeConnectsToMessageConsumer(): RelationshipType {
62     return relationshipType(
63         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_CONSUMER,
64         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
65         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
66         description = "Relationship type connects to message consumer."
67     ) {
68         property(
69             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
70             BluePrintConstants.DATA_TYPE_MAP,
71             true,
72             "Connection Config details."
73         )
74         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
75     }
76 }
77
78 /** Relationships Templates DSL for Message Producer */
79 fun TopologyTemplateBuilder.relationshipTemplateMessageProducer(
80     name: String,
81     description: String,
82     block: MessageProducerRelationshipTemplateBuilder.() -> Unit
83 ) {
84     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
85     val relationshipTemplate =
86         MessageProducerRelationshipTemplateBuilder(name, description).apply(block).build()
87     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
88 }
89
90 class MessageProducerRelationshipTemplateBuilder(name: String, description: String) :
91     RelationshipTemplateBuilder(
92         name,
93         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_PRODUCER, description
94     ) {
95
96     fun kafkaBasicAuth(block: KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder.() -> Unit) {
97         property(
98             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
99             BluePrintTypes.kafkaBasicAuthMessageProducerProperties(block)
100         )
101     }
102 }
103
104 fun BluePrintTypes.kafkaBasicAuthMessageProducerProperties(block: KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
105     val assignments = KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder().apply(block).build()
106     assignments[KafkaBasicAuthMessageProducerProperties::type.name] =
107         MessageLibConstants.TYPE_KAFKA_BASIC_AUTH.asJsonPrimitive()
108     return assignments.asJsonType()
109 }
110
111 open class MessageProducerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder()
112
113 class KafkaBasicAuthMessageProducerPropertiesAssignmentBuilder : MessageProducerPropertiesAssignmentBuilder() {
114
115     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
116
117     fun bootstrapServers(bootstrapServers: JsonNode) =
118         property(KafkaBasicAuthMessageProducerProperties::bootstrapServers, bootstrapServers)
119
120     fun topic(topic: String) = topic(topic.asJsonPrimitive())
121
122     fun topic(topic: JsonNode) =
123         property(KafkaBasicAuthMessageProducerProperties::topic, topic)
124
125     fun clientId(clientId: String) = bootstrapServers(clientId.asJsonPrimitive())
126
127     fun clientId(clientId: JsonNode) =
128         property(KafkaBasicAuthMessageProducerProperties::clientId, clientId)
129
130     fun acks(acks: String) = acks(acks.asJsonPrimitive())
131
132     fun acks(acks: JsonNode) = property(KafkaBasicAuthMessageProducerProperties::acks, acks)
133
134     fun retries(retries: Int) = retries(retries.asJsonPrimitive())
135
136     fun retries(retries: JsonNode) = property(KafkaBasicAuthMessageProducerProperties::retries, retries)
137
138     fun enableIdempotence(enableIdempotence: Boolean) = enableIdempotence(enableIdempotence.asJsonPrimitive())
139
140     fun enableIdempotence(enableIdempotence: JsonNode) =
141         property(KafkaBasicAuthMessageProducerProperties::enableIdempotence, enableIdempotence)
142 }
143
144 /** Relationships Templates DSL for Message Consumer */
145 fun TopologyTemplateBuilder.relationshipTemplateMessageConsumer(
146     name: String,
147     description: String,
148     block: MessageConsumerRelationshipTemplateBuilder.() -> Unit
149 ) {
150     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
151     val relationshipTemplate =
152         MessageConsumerRelationshipTemplateBuilder(name, description).apply(block).build()
153     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
154 }
155
156 class MessageConsumerRelationshipTemplateBuilder(name: String, description: String) :
157     RelationshipTemplateBuilder(
158         name,
159         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_MESSAGE_CONSUMER, description
160     ) {
161
162     fun kafkaBasicAuth(block: KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder.() -> Unit) {
163         property(
164             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
165             BluePrintTypes.kafkaBasicAuthMessageConsumerProperties(block)
166         )
167     }
168
169     fun kafkaStreamsBasicAuth(block: KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder.() -> Unit) {
170         property(
171             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
172             BluePrintTypes.kafkaStreamsBasicAuthConsumerProperties(block)
173         )
174     }
175 }
176
177 fun BluePrintTypes.kafkaBasicAuthMessageConsumerProperties(block: KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
178     val assignments = KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder().apply(block).build()
179     assignments[KafkaBasicAuthMessageConsumerProperties::type.name] =
180         MessageLibConstants.TYPE_KAFKA_BASIC_AUTH.asJsonPrimitive()
181     return assignments.asJsonType()
182 }
183
184 fun BluePrintTypes.kafkaStreamsBasicAuthConsumerProperties(block: KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder.() -> Unit): JsonNode {
185     val assignments = KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder().apply(block).build()
186     assignments[KafkaStreamsBasicAuthConsumerProperties::type.name] =
187         MessageLibConstants.TYPE_KAFKA_STREAMS_BASIC_AUTH.asJsonPrimitive()
188     return assignments.asJsonType()
189 }
190
191 open class MessageConsumerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder()
192
193 open class KafkaMessageConsumerPropertiesAssignmentBuilder : MessageConsumerPropertiesAssignmentBuilder() {
194
195     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
196
197     fun bootstrapServers(bootstrapServers: JsonNode) =
198         property(KafkaMessageConsumerProperties::bootstrapServers, bootstrapServers)
199
200     fun groupId(groupId: String) = groupId(groupId.asJsonPrimitive())
201
202     fun groupId(groupId: JsonNode) =
203         property(KafkaMessageConsumerProperties::groupId, groupId)
204
205     fun clientId(clientId: String) = clientId(clientId.asJsonPrimitive())
206
207     fun clientId(clientId: JsonNode) =
208         property(KafkaMessageConsumerProperties::clientId, clientId)
209
210     fun topic(topic: String) = topic(topic.asJsonPrimitive())
211
212     fun topic(topic: JsonNode) =
213         property(KafkaMessageConsumerProperties::topic, topic)
214
215     fun autoCommit(autoCommit: Boolean) = autoCommit(autoCommit.asJsonPrimitive())
216
217     fun autoCommit(autoCommit: JsonNode) =
218         property(KafkaMessageConsumerProperties::autoCommit, autoCommit)
219
220     fun autoOffsetReset(autoOffsetReset: String) = autoOffsetReset(autoOffsetReset.asJsonPrimitive())
221
222     fun autoOffsetReset(autoOffsetReset: JsonNode) =
223         property(KafkaMessageConsumerProperties::autoOffsetReset, autoOffsetReset)
224
225     fun pollMillSec(pollMillSec: Int) = pollMillSec(pollMillSec.asJsonPrimitive())
226
227     fun pollMillSec(pollMillSec: JsonNode) =
228         property(KafkaMessageConsumerProperties::pollMillSec, pollMillSec)
229
230     fun pollRecords(pollRecords: Int) = pollRecords(pollRecords.asJsonPrimitive())
231
232     fun pollRecords(pollRecords: JsonNode) =
233         property(KafkaMessageConsumerProperties::pollRecords, pollRecords)
234 }
235
236 /** KafkaBasicAuthMessageConsumerProperties assignment builder */
237 class KafkaBasicAuthMessageConsumerPropertiesAssignmentBuilder : KafkaMessageConsumerPropertiesAssignmentBuilder()
238
239 /** KafkaStreamsConsumerProperties assignment builder */
240 open class KafkaStreamsConsumerPropertiesAssignmentBuilder : MessageConsumerPropertiesAssignmentBuilder() {
241
242     fun bootstrapServers(bootstrapServers: String) = bootstrapServers(bootstrapServers.asJsonPrimitive())
243
244     fun bootstrapServers(bootstrapServers: JsonNode) =
245         property(KafkaStreamsConsumerProperties::bootstrapServers, bootstrapServers)
246
247     fun applicationId(applicationId: String) = bootstrapServers(applicationId.asJsonPrimitive())
248
249     fun applicationId(applicationId: JsonNode) =
250         property(KafkaStreamsConsumerProperties::applicationId, applicationId)
251
252     fun topic(topic: String) = topic(topic.asJsonPrimitive())
253
254     fun topic(topic: JsonNode) =
255         property(KafkaStreamsConsumerProperties::topic, topic)
256
257     fun autoOffsetReset(autoOffsetReset: String) = autoOffsetReset(autoOffsetReset.asJsonPrimitive())
258
259     fun autoOffsetReset(autoOffsetReset: JsonNode) =
260         property(KafkaStreamsConsumerProperties::autoOffsetReset, autoOffsetReset)
261
262     fun processingGuarantee(processingGuarantee: String) = processingGuarantee(processingGuarantee.asJsonPrimitive())
263
264     fun processingGuarantee(processingGuarantee: JsonNode) =
265         property(KafkaStreamsConsumerProperties::processingGuarantee, processingGuarantee)
266 }
267
268 class KafkaStreamsBasicAuthConsumerPropertiesAssignmentBuilder : KafkaStreamsConsumerPropertiesAssignmentBuilder()