354e38dc798f814b6bc3bc07799dca0ba758f56a
[ccsdk/cds.git] /
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.TopologyTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
29
30 /** Relationships Types DSL for GRPC Server Producer */
31 fun BluePrintTypes.relationshipTypeConnectsToGrpcServer(): RelationshipType {
32     return relationshipType(
33         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_SERVER,
34         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
35         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
36         description = "Relationship connects to through GRPC Server."
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.relationshipTypeConnectsToGrpcClient(): RelationshipType {
49     return relationshipType(
50         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_CLIENT,
51         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
52         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
53         description = "Relationship connects to through GRPC Client."
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 for GRPC Server */
66 fun TopologyTemplateBuilder.relationshipTemplateGrpcServer(
67     name: String,
68     description: String,
69     block: GrpcServerRelationshipTemplateBuilder.() -> Unit
70 ) {
71     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
72     val relationshipTemplate = GrpcServerRelationshipTemplateBuilder(name, description).apply(block).build()
73     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
74 }
75
76 class GrpcServerRelationshipTemplateBuilder(name: String, description: String) :
77     RelationshipTemplateBuilder(
78         name,
79         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_SERVER, description
80     ) {
81
82     fun tokenAuth(block: GrpcServerTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
83         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthGrpcServerProperties(block))
84     }
85
86     fun tlsAuth(block: GrpcServerTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
87         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tlsAuthGrpcServerProperties(block))
88     }
89 }
90
91 fun BluePrintTypes.tokenAuthGrpcServerProperties(block: GrpcServerTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
92     val assignments = GrpcServerTokenAuthPropertiesAssignmentBuilder().apply(block).build()
93     assignments[GrpcServerProperties::type.name] = GRPCLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
94     return assignments.asJsonNode()
95 }
96
97 fun BluePrintTypes.tlsAuthGrpcServerProperties(block: GrpcServerTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
98     val assignments = GrpcServerTLSAuthPropertiesAssignmentBuilder().apply(block).build()
99     assignments[GrpcServerProperties::type.name] = GRPCLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
100     return assignments.asJsonNode()
101 }
102
103 open class GrpcServerPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
104
105     fun port(port: Int) = port(port.asJsonPrimitive())
106
107     fun port(port: JsonNode) =
108         property(GrpcServerProperties::port, port)
109 }
110
111 open class GrpcServerTokenAuthPropertiesAssignmentBuilder : GrpcServerPropertiesAssignmentBuilder() {
112
113     fun token(selector: String) = token(selector.asJsonPrimitive())
114
115     fun token(selector: JsonNode) = property(TokenAuthGrpcServerProperties::token, selector)
116 }
117
118 open class GrpcServerTLSAuthPropertiesAssignmentBuilder : GrpcServerPropertiesAssignmentBuilder() {
119
120     fun certChain(certChain: String) = certChain(certChain.asJsonPrimitive())
121
122     fun certChain(certChain: JsonNode) = property(TLSAuthGrpcServerProperties::certChain, certChain)
123
124     fun privateKey(privateKey: String) = privateKey(privateKey.asJsonPrimitive())
125
126     fun privateKey(privateKey: JsonNode) = property(TLSAuthGrpcServerProperties::privateKey, privateKey)
127
128     fun trustCertCollection(trustCertCollection: String) = trustCertCollection(trustCertCollection.asJsonPrimitive())
129
130     fun trustCertCollection(trustCertCollection: JsonNode) =
131         property(TLSAuthGrpcServerProperties::trustCertCollection, trustCertCollection)
132 }
133
134 /** Relationships Templates for GRPC Client */
135 fun TopologyTemplateBuilder.relationshipTemplateGrpcClient(
136     name: String,
137     description: String,
138     block: GrpcClientRelationshipTemplateBuilder.() -> Unit
139 ) {
140     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
141     val relationshipTemplate = GrpcClientRelationshipTemplateBuilder(name, description).apply(block).build()
142     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
143 }
144
145 class GrpcClientRelationshipTemplateBuilder(name: String, description: String) :
146     RelationshipTemplateBuilder(
147         name,
148         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_GRPC_CLIENT, description
149     ) {
150
151     fun basicAuth(block: GrpcClientBasicAuthPropertiesAssignmentBuilder.() -> Unit) {
152         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.basicAuthGrpcClientProperties(block))
153     }
154
155     fun tokenAuth(block: GrpcClientTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
156         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthGrpcClientProperties(block))
157     }
158
159     fun tlsAuth(block: GrpcClientTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
160         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tlsAuthGrpcClientProperties(block))
161     }
162 }
163
164 fun BluePrintTypes.basicAuthGrpcClientProperties(block: GrpcClientBasicAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
165     val assignments = GrpcClientBasicAuthPropertiesAssignmentBuilder().apply(block).build()
166     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
167     return assignments.asJsonNode()
168 }
169
170 fun BluePrintTypes.tokenAuthGrpcClientProperties(block: GrpcClientTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
171     val assignments = GrpcClientTokenAuthPropertiesAssignmentBuilder().apply(block).build()
172     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
173     return assignments.asJsonNode()
174 }
175
176 fun BluePrintTypes.tlsAuthGrpcClientProperties(block: GrpcClientTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
177     val assignments = GrpcClientTLSAuthPropertiesAssignmentBuilder().apply(block).build()
178     assignments[GrpcClientProperties::type.name] = GRPCLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
179     return assignments.asJsonNode()
180 }
181
182 open class GrpcClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
183
184     fun host(host: String) = host(host.asJsonPrimitive())
185
186     fun host(host: JsonNode) =
187         property(GrpcClientProperties::host, host)
188
189     fun port(port: Int) = port(port.asJsonPrimitive())
190
191     fun port(port: JsonNode) =
192         property(GrpcClientProperties::port, port)
193 }
194
195 open class GrpcClientBasicAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
196
197     fun username(username: String) = username(username.asJsonPrimitive())
198
199     fun username(username: JsonNode) = property(BasicAuthGrpcClientProperties::username, username)
200
201     fun password(password: String) = password(password.asJsonPrimitive())
202
203     fun password(password: JsonNode) = property(BasicAuthGrpcClientProperties::password, password)
204 }
205
206 open class GrpcClientTokenAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
207
208     fun token(selector: String) = token(selector.asJsonPrimitive())
209
210     fun token(selector: JsonNode) = property(TokenAuthGrpcClientProperties::token, selector)
211 }
212
213 open class GrpcClientTLSAuthPropertiesAssignmentBuilder : GrpcClientPropertiesAssignmentBuilder() {
214
215     fun trustCertCollection(trustCertCollection: String) = trustCertCollection(trustCertCollection.asJsonPrimitive())
216
217     fun trustCertCollection(trustCertCollection: JsonNode) =
218         property(TLSAuthGrpcClientProperties::trustCertCollection, trustCertCollection)
219
220     fun clientCertChain(clientCertChain: String) = clientCertChain(clientCertChain.asJsonPrimitive())
221
222     fun clientCertChain(clientCertChain: JsonNode) =
223         property(TLSAuthGrpcClientProperties::clientCertChain, clientCertChain)
224
225     fun clientPrivateKey(clientPrivateKey: String) = clientPrivateKey(clientPrivateKey.asJsonPrimitive())
226
227     fun clientPrivateKey(clientPrivateKey: JsonNode) =
228         property(TLSAuthGrpcClientProperties::clientPrivateKey, clientPrivateKey)
229 }