Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / nats-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / nats / NatsPropertiesDSL.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.nats
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.asJsonNode
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
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 NATS Producer */
32 fun ServiceTemplateBuilder.relationshipTypeConnectsToNats() {
33     val relationshipType = BlueprintTypes.relationshipTypeConnectsToNats()
34     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
35     this.relationshipTypes!![relationshipType.id!!] = relationshipType
36 }
37
38 fun BlueprintTypes.relationshipTypeConnectsToNats(): RelationshipType {
39     return relationshipType(
40         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS,
41         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
42         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
43         description = "Relationship connects to through NATS Client."
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 /** Relationships Templates for Nats */
56 fun TopologyTemplateBuilder.relationshipTemplateNats(
57     name: String,
58     description: String,
59     block: NatsRelationshipTemplateBuilder.() -> Unit
60 ) {
61     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
62     val relationshipTemplate = NatsRelationshipTemplateBuilder(name, description).apply(block).build()
63     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
64 }
65
66 class NatsRelationshipTemplateBuilder(name: String, description: String) :
67     RelationshipTemplateBuilder(
68         name,
69         BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, description
70     ) {
71
72     fun tokenAuth(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
73         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tokenAuthNatsProperties(block))
74     }
75
76     fun tlsAuth(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
77         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tlsAuthNatsProperties(block))
78     }
79 }
80
81 fun BlueprintTypes.tokenAuthNatsProperties(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
82     val assignments = NatsTokenAuthPropertiesAssignmentBuilder().apply(block).build()
83     assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
84     return assignments.asJsonNode()
85 }
86
87 fun BlueprintTypes.tlsAuthNatsProperties(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
88     val assignments = NatsTLSAuthPropertiesAssignmentBuilder().apply(block).build()
89     assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
90     return assignments.asJsonNode()
91 }
92
93 open class NatsConnectionPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
94
95     fun clusterId(clusterId: String) = clusterId(clusterId.asJsonPrimitive())
96
97     fun clusterId(clusterId: JsonNode) = property(NatsConnectionProperties::clusterId, clusterId)
98
99     fun clientId(clientId: String) = clientId(clientId.asJsonPrimitive())
100
101     fun clientId(clientId: JsonNode) = property(NatsConnectionProperties::clientId, clientId)
102
103     fun host(host: String) = host(host.asJsonPrimitive())
104
105     fun host(host: JsonNode) = property(NatsConnectionProperties::host, host)
106
107     fun monitoringSelector(monitoringSelector: String) = monitoringSelector(monitoringSelector.asJsonPrimitive())
108
109     fun monitoringSelector(monitoringSelector: JsonNode) =
110         property(NatsConnectionProperties::monitoringSelector, monitoringSelector)
111 }
112
113 class NatsTokenAuthPropertiesAssignmentBuilder : NatsConnectionPropertiesAssignmentBuilder() {
114
115     fun token(selector: String) = token(selector.asJsonPrimitive())
116
117     fun token(selector: JsonNode) = property(TokenAuthNatsConnectionProperties::token, selector)
118 }
119
120 class NatsTLSAuthPropertiesAssignmentBuilder : NatsConnectionPropertiesAssignmentBuilder()