5d68e5d432502ac9427852547a7000c176b647a0
[ccsdk/cds.git] /
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     open fun password(password: String) {
116         password(password.asJsonPrimitive())
117     }
118
119     open fun password(password: JsonNode) {
120         property(BasicAuthRestClientProperties::password, password)
121     }
122
123     open fun username(username: String) {
124         username(username.asJsonPrimitive())
125     }
126
127     open fun username(username: JsonNode) {
128         property(BasicAuthRestClientProperties::username, username)
129     }
130 }
131
132 open class TokenAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
133     open fun token(token: String) {
134         token(token.asJsonPrimitive())
135     }
136
137     open fun token(token: JsonNode) {
138         property(TokenAuthRestClientProperties::token, token)
139     }
140 }
141
142 open class SslAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
143     open fun keyStoreInstance(keyStoreInstance: String) {
144         keyStoreInstance(keyStoreInstance.asJsonPrimitive())
145     }
146
147     open fun keyStoreInstance(keyStoreInstance: JsonNode) {
148         property(SSLRestClientProperties::keyStoreInstance, keyStoreInstance)
149     }
150
151     open fun sslTrust(sslTrust: String) {
152         sslTrust(sslTrust.asJsonPrimitive())
153     }
154
155     open fun sslTrust(sslTrust: JsonNode) {
156         property(SSLRestClientProperties::sslTrust, sslTrust)
157     }
158
159     open fun sslTrustPassword(sslTrustPassword: String) {
160         sslTrustPassword(sslTrustPassword.asJsonPrimitive())
161     }
162
163     open fun sslTrustPassword(sslTrustPassword: JsonNode) {
164         property(SSLRestClientProperties::sslTrustPassword, sslTrustPassword)
165     }
166
167     open fun sslKey(sslKey: String) {
168         sslKey(sslKey.asJsonPrimitive())
169     }
170
171     open fun sslKey(sslKey: JsonNode) {
172         property(SSLRestClientProperties::sslKey, sslKey)
173     }
174
175     open fun sslKeyPassword(sslKeyPassword: String) {
176         sslKeyPassword(sslKeyPassword.asJsonPrimitive())
177     }
178
179     open fun sslKeyPassword(sslKeyPassword: JsonNode) {
180         property(SSLRestClientProperties::sslKeyPassword, sslKeyPassword)
181     }
182 }
183
184 open class SSLBasicAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
185     // TODO()
186 }
187
188 open class SSLTokenAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
189     // TODO()
190 }