Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / RestClientPropertiesDSL.kt
1 /*
2  *  Copyright © 2019 IBM.
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.rest
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 Type DSL for Rest */
32 fun ServiceTemplateBuilder.relationshipTypeConnectsToRestClient() {
33     val relationshipType = BluePrintTypes.relationshipTypeConnectsToRestClient()
34     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
35     this.relationshipTypes!![relationshipType.id!!] = relationshipType
36 }
37
38 fun BluePrintTypes.relationshipTypeConnectsToRestClient(): RelationshipType {
39     return relationshipType(
40         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT,
41         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
42         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
43         description = "Relationship connects to through"
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 DSL for Rest */
56 fun TopologyTemplateBuilder.relationshipTemplateRestClient(
57     name: String,
58     description: String,
59     block: RestClientRelationshipTemplateBuilder.() -> Unit
60 ) {
61     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
62     val relationshipTemplate = RestClientRelationshipTemplateBuilder(name, description).apply(block).build()
63     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
64 }
65
66 open class RestClientRelationshipTemplateBuilder(name: String, description: String) :
67     RelationshipTemplateBuilder(
68         name,
69         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT, description
70     ) {
71
72     fun basicAuth(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
73         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.basicAuthRestClientProperties(block))
74     }
75
76     fun tokenAuth(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
77         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthRestClientProperties(block))
78     }
79
80     fun sslBasicAuth(block: SSLBasicAuthRestClientPropertiesBuilder.() -> Unit) {
81         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.sslBasicAuthRestClientProperties(block))
82     }
83
84     fun sslAuth(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
85         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.sslRestClientProperties(block))
86     }
87 }
88
89 fun BluePrintTypes.basicAuthRestClientProperties(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
90     val assignments = BasicAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
91     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
92     return assignments.asJsonType()
93 }
94
95 fun BluePrintTypes.tokenAuthRestClientProperties(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
96     val assignments = TokenAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
97     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
98     return assignments.asJsonType()
99 }
100
101 fun BluePrintTypes.sslBasicAuthRestClientProperties(block: SSLBasicAuthRestClientPropertiesBuilder.() -> Unit): JsonNode {
102     val assignments = SSLBasicAuthRestClientPropertiesBuilder().apply(block).build()
103     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_SSL_BASIC_AUTH.asJsonPrimitive()
104     return assignments.asJsonType()
105 }
106
107 fun BluePrintTypes.sslRestClientProperties(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
108     val assignments = SslAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
109     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_SSL_NO_AUTH.asJsonPrimitive()
110     return assignments.asJsonType()
111 }
112
113 open class RestClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
114
115     open fun url(url: String) {
116         url(url.asJsonPrimitive())
117     }
118
119     open fun url(url: JsonNode) {
120         property(RestClientProperties::url, url)
121     }
122 }
123
124 open class BasicAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
125
126     open fun password(password: String) {
127         password(password.asJsonPrimitive())
128     }
129
130     open fun password(password: JsonNode) {
131         property(BasicAuthRestClientProperties::password, password)
132     }
133
134     open fun username(username: String) {
135         username(username.asJsonPrimitive())
136     }
137
138     open fun username(username: JsonNode) {
139         property(BasicAuthRestClientProperties::username, username)
140     }
141 }
142
143 open class TokenAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
144
145     open fun token(token: String) {
146         token(token.asJsonPrimitive())
147     }
148
149     open fun token(token: JsonNode) {
150         property(TokenAuthRestClientProperties::token, token)
151     }
152 }
153
154 open class SslAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
155
156     open fun keyStoreInstance(keyStoreInstance: String) {
157         keyStoreInstance(keyStoreInstance.asJsonPrimitive())
158     }
159
160     open fun keyStoreInstance(keyStoreInstance: JsonNode) {
161         property(SSLRestClientProperties::keyStoreInstance, keyStoreInstance)
162     }
163
164     open fun sslTrust(sslTrust: String) {
165         sslTrust(sslTrust.asJsonPrimitive())
166     }
167
168     open fun sslTrust(sslTrust: JsonNode) {
169         property(SSLRestClientProperties::sslTrust, sslTrust)
170     }
171
172     open fun sslTrustPassword(sslTrustPassword: String) {
173         sslTrustPassword(sslTrustPassword.asJsonPrimitive())
174     }
175
176     open fun sslTrustPassword(sslTrustPassword: JsonNode) {
177         property(SSLRestClientProperties::sslTrustPassword, sslTrustPassword)
178     }
179
180     open fun sslKey(sslKey: String) {
181         sslKey(sslKey.asJsonPrimitive())
182     }
183
184     open fun sslKey(sslKey: JsonNode) {
185         property(SSLRestClientProperties::sslKey, sslKey)
186     }
187
188     open fun sslKeyPassword(sslKeyPassword: String) {
189         sslKeyPassword(sslKeyPassword.asJsonPrimitive())
190     }
191
192     open fun sslKeyPassword(sslKeyPassword: JsonNode) {
193         property(SSLRestClientProperties::sslKeyPassword, sslKeyPassword)
194     }
195
196     open fun sslTrustIgnoreHostname(sslTrustIgnoreHostname: String) {
197         sslTrustIgnoreHostname(sslTrustIgnoreHostname.asJsonPrimitive())
198     }
199
200     open fun sslTrustIgnoreHostname(sslTrustIgnoreHostname: JsonNode) {
201         property(SSLRestClientProperties::sslTrustIgnoreHostname, sslTrustIgnoreHostname)
202     }
203 }
204
205 open class SSLBasicAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
206     open fun password(password: String) {
207         password(password.asJsonPrimitive())
208     }
209
210     open fun password(password: JsonNode) {
211         property(SSLBasicAuthRestClientProperties::password, password)
212     }
213
214     open fun username(username: String) {
215         username(username.asJsonPrimitive())
216     }
217
218     open fun username(username: JsonNode) {
219         property(SSLBasicAuthRestClientProperties::username, username)
220     }
221 }
222
223 open class SSLTokenAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
224     // TODO()
225 }