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 sslAuth(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
81         property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.sslRestClientProperties(block))
82     }
83 }
84
85 fun BlueprintTypes.basicAuthRestClientProperties(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
86     val assignments = BasicAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
87     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
88     return assignments.asJsonType()
89 }
90
91 fun BlueprintTypes.tokenAuthRestClientProperties(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
92     val assignments = TokenAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
93     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
94     return assignments.asJsonType()
95 }
96
97 fun BlueprintTypes.sslRestClientProperties(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
98     val assignments = SslAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
99     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_SSL_NO_AUTH.asJsonPrimitive()
100     return assignments.asJsonType()
101 }
102
103 open class RestClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
104
105     open fun url(url: String) {
106         url(url.asJsonPrimitive())
107     }
108
109     open fun url(url: JsonNode) {
110         property(RestClientProperties::url, url)
111     }
112 }
113
114 open class BasicAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
115
116     open fun password(password: String) {
117         password(password.asJsonPrimitive())
118     }
119
120     open fun password(password: JsonNode) {
121         property(BasicAuthRestClientProperties::password, password)
122     }
123
124     open fun username(username: String) {
125         username(username.asJsonPrimitive())
126     }
127
128     open fun username(username: JsonNode) {
129         property(BasicAuthRestClientProperties::username, username)
130     }
131 }
132
133 open class TokenAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
134
135     open fun token(token: String) {
136         token(token.asJsonPrimitive())
137     }
138
139     open fun token(token: JsonNode) {
140         property(TokenAuthRestClientProperties::token, token)
141     }
142 }
143
144 open class SslAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
145
146     open fun keyStoreInstance(keyStoreInstance: String) {
147         keyStoreInstance(keyStoreInstance.asJsonPrimitive())
148     }
149
150     open fun keyStoreInstance(keyStoreInstance: JsonNode) {
151         property(SSLRestClientProperties::keyStoreInstance, keyStoreInstance)
152     }
153
154     open fun sslTrust(sslTrust: String) {
155         sslTrust(sslTrust.asJsonPrimitive())
156     }
157
158     open fun sslTrust(sslTrust: JsonNode) {
159         property(SSLRestClientProperties::sslTrust, sslTrust)
160     }
161
162     open fun sslTrustPassword(sslTrustPassword: String) {
163         sslTrustPassword(sslTrustPassword.asJsonPrimitive())
164     }
165
166     open fun sslTrustPassword(sslTrustPassword: JsonNode) {
167         property(SSLRestClientProperties::sslTrustPassword, sslTrustPassword)
168     }
169
170     open fun sslKey(sslKey: String) {
171         sslKey(sslKey.asJsonPrimitive())
172     }
173
174     open fun sslKey(sslKey: JsonNode) {
175         property(SSLRestClientProperties::sslKey, sslKey)
176     }
177
178     open fun sslKeyPassword(sslKeyPassword: String) {
179         sslKeyPassword(sslKeyPassword.asJsonPrimitive())
180     }
181
182     open fun sslKeyPassword(sslKeyPassword: JsonNode) {
183         property(SSLRestClientProperties::sslKeyPassword, sslKeyPassword)
184     }
185 }
186
187 open class SSLBasicAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
188     // TODO()
189 }
190
191 open class SSLTokenAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
192     // TODO()
193 }