Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / GrpcPropertiesDSL.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.grpc
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 GRPC Server Producer */
32 fun ServiceTemplateBuilder.relationshipTypeConnectsToGrpcServer() {
33     val relationshipType = BlueprintTypes.relationshipTypeConnectsToGrpcServer()
34     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
35     this.relationshipTypes!![relationshipType.id!!] = relationshipType
36 }
37
38 fun BlueprintTypes.relationshipTypeConnectsToGrpcServer(): RelationshipType {
39     return relationshipType(
40         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_SERVER,
41         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
42         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
43         description = "Relationship connects to through GRPC Server."
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.relationshipTypeConnectsToGrpcClient() {
56     val relationshipType = BlueprintTypes.relationshipTypeConnectsToGrpcClient()
57     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
58     this.relationshipTypes!![relationshipType.id!!] = relationshipType
59 }
60
61 fun BlueprintTypes.relationshipTypeConnectsToGrpcClient(): RelationshipType {
62     return relationshipType(
63         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_CLIENT,
64         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
65         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
66         description = "Relationship connects to through GRPC Client."
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 for GRPC Server */
79 fun TopologyTemplateBuilder.relationshipTemplateGrpcServer(
80     name: String,
81     description: String,
82     block: GrpcServerRelationshipTemplateBuilder.() -> Unit
83 ) {
84     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
85     val relationshipTemplate = GrpcServerRelationshipTemplateBuilder(name, description).apply(block).build()
86     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
87 }
88
89 class GrpcServerRelationshipTemplateBuilder(name: String, description: String) :
90     RelationshipTemplateBuilder(
91         name,
92         BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_SERVER, description
93     ) {
94
95     fun tokenAuth(block: GrpcServerTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
96         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tokenAuthGrpcServerProperties(block))
97     }
98
99     fun tlsAuth(block: GrpcServerTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
100         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tlsAuthGrpcServerProperties(block))
101     }
102 }
103
104 fun BlueprintTypes.tokenAuthGrpcServerProperties(block: GrpcServerTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
105     val assignments = GrpcServerTokenAuthPropertiesAssignmentBuilder().apply(block).build()
106     assignments[GrpcServerProperties::type.name] = GRPCLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
107     return assignments.asJsonNode()
108 }
109
110 fun BlueprintTypes.tlsAuthGrpcServerProperties(block: GrpcServerTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
111     val assignments = GrpcServerTLSAuthPropertiesAssignmentBuilder().apply(block).build()
112     assignments[GrpcServerProperties::type.name] = GRPCLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
113     return assignments.asJsonNode()
114 }
115
116 open class GrpcServerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
117
118     fun port(port: Int) = port(port.asJsonPrimitive())
119
120     fun port(port: JsonNode) =
121         property(GrpcServerProperties::port, port)
122 }
123
124 open class GrpcServerTokenAuthPropertiesAssignmentBuilder : GrpcServerPropertiesAssignmentBuilder() {
125
126     fun token(selector: String) = token(selector.asJsonPrimitive())
127
128     fun token(selector: JsonNode) = property(TokenAuthGrpcServerProperties::token, selector)
129 }
130
131 open class GrpcServerTLSAuthPropertiesAssignmentBuilder : GrpcServerPropertiesAssignmentBuilder() {
132
133     fun certChain(certChain: String) = certChain(certChain.asJsonPrimitive())
134
135     fun certChain(certChain: JsonNode) = property(TLSAuthGrpcServerProperties::certChain, certChain)
136
137     fun privateKey(privateKey: String) = privateKey(privateKey.asJsonPrimitive())
138
139     fun privateKey(privateKey: JsonNode) = property(TLSAuthGrpcServerProperties::privateKey, privateKey)
140
141     fun trustCertCollection(trustCertCollection: String) = trustCertCollection(trustCertCollection.asJsonPrimitive())
142
143     fun trustCertCollection(trustCertCollection: JsonNode) =
144         property(TLSAuthGrpcServerProperties::trustCertCollection, trustCertCollection)
145 }
146
147 /** Relationships Templates for GRPC Client */
148 fun TopologyTemplateBuilder.relationshipTemplateGrpcClient(
149     name: String,
150     description: String,
151     block: GrpcClientRelationshipTemplateBuilder.() -> Unit
152 ) {
153     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
154     val relationshipTemplate = GrpcClientRelationshipTemplateBuilder(name, description).apply(block).build()
155     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
156 }
157
158 class GrpcClientRelationshipTemplateBuilder(name: String, description: String) :
159     RelationshipTemplateBuilder(
160         name,
161         BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_CLIENT, description
162     ) {
163
164     fun basicAuth(block: GrpcClientBasicAuthPropertiesAssignmentBuilder.() -> Unit) {
165         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.basicAuthGrpcClientProperties(block))
166     }
167
168     fun tokenAuth(block: GrpcClientTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
169         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tokenAuthGrpcClientProperties(block))
170     }
171
172     fun tlsAuth(block: GrpcClientTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
173         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tlsAuthGrpcClientProperties(block))
174     }
175 }
176
177 fun BlueprintTypes.basicAuthGrpcClientProperties(block: GrpcClientBasicAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
178     val assignments = GrpcClientBasicAuthPropertiesAssignmentBuilder().apply(block).build()
179     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
180     return assignments.asJsonNode()
181 }
182
183 fun BlueprintTypes.tokenAuthGrpcClientProperties(block: GrpcClientTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
184     val assignments = GrpcClientTokenAuthPropertiesAssignmentBuilder().apply(block).build()
185     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
186     return assignments.asJsonNode()
187 }
188
189 fun BlueprintTypes.tlsAuthGrpcClientProperties(block: GrpcClientTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
190     val assignments = GrpcClientTLSAuthPropertiesAssignmentBuilder().apply(block).build()
191     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
192     return assignments.asJsonNode()
193 }
194
195 open class GrpcClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
196
197     fun host(host: String) = host(host.asJsonPrimitive())
198
199     fun host(host: JsonNode) =
200         property(GrpcClientProperties::host, host)
201
202     fun port(port: Int) = port(port.asJsonPrimitive())
203
204     fun port(port: JsonNode) =
205         property(GrpcClientProperties::port, port)
206 }
207
208 open class GrpcClientBasicAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
209
210     fun username(username: String) = username(username.asJsonPrimitive())
211
212     fun username(username: JsonNode) = property(BasicAuthGrpcClientProperties::username, username)
213
214     fun password(password: String) = password(password.asJsonPrimitive())
215
216     fun password(password: JsonNode) = property(BasicAuthGrpcClientProperties::password, password)
217 }
218
219 open class GrpcClientTokenAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
220
221     fun token(selector: String) = token(selector.asJsonPrimitive())
222
223     fun token(selector: JsonNode) = property(TokenAuthGrpcClientProperties::token, selector)
224 }
225
226 open class GrpcClientTLSAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
227
228     fun trustCertCollection(trustCertCollection: String) = trustCertCollection(trustCertCollection.asJsonPrimitive())
229
230     fun trustCertCollection(trustCertCollection: JsonNode) =
231         property(TLSAuthGrpcClientProperties::trustCertCollection, trustCertCollection)
232
233     fun clientCertChain(clientCertChain: String) = clientCertChain(clientCertChain.asJsonPrimitive())
234
235     fun clientCertChain(clientCertChain: JsonNode) =
236         property(TLSAuthGrpcClientProperties::clientCertChain, clientCertChain)
237
238     fun clientPrivateKey(clientPrivateKey: String) = clientPrivateKey(clientPrivateKey.asJsonPrimitive())
239
240     fun clientPrivateKey(clientPrivateKey: JsonNode) =
241         property(TLSAuthGrpcClientProperties::clientPrivateKey, clientPrivateKey)
242 }