4c25cb5bfda30ba59376e32e8b73bf3a04509bea
[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.TopologyTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
29
30 /** Relationships Type DSL for Rest */
31 fun BluePrintTypes.relationshipTypeConnectsToRestClient(): RelationshipType {
32     return relationshipType(
33         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT,
34         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
35         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
36         description = "Relationship connects to through"
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 /** Relationships Templates DSL for Rest */
49 fun TopologyTemplateBuilder.relationshipTemplateRestClient(
50     name: String,
51     description: String,
52     block: RestClientRelationshipTemplateBuilder.() -> Unit
53 ) {
54     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
55     val relationshipTemplate = RestClientRelationshipTemplateBuilder(name, description).apply(block).build()
56     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
57 }
58
59 open class RestClientRelationshipTemplateBuilder(name: String, description: String) :
60     RelationshipTemplateBuilder(
61         name,
62         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT, description
63     ) {
64
65     fun basicAuth(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
66         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.basicAuthRestClientProperties(block))
67     }
68
69     fun tokenAuth(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
70         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthRestClientProperties(block))
71     }
72
73     fun sslAuth(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
74         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.sslRestClientProperties(block))
75     }
76 }
77
78 fun BluePrintTypes.basicAuthRestClientProperties(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
79     val assignments = BasicAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
80     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
81     return assignments.asJsonType()
82 }
83
84 fun BluePrintTypes.tokenAuthRestClientProperties(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
85     val assignments = TokenAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
86     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
87     return assignments.asJsonType()
88 }
89
90 fun BluePrintTypes.sslRestClientProperties(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
91     val assignments = SslAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
92     assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_SSL_NO_AUTH.asJsonPrimitive()
93     return assignments.asJsonType()
94 }
95
96 open class RestClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
97
98     open fun url(url: String) {
99         url(url.asJsonPrimitive())
100     }
101
102     open fun url(url: JsonNode) {
103         property(RestClientProperties::url, url)
104     }
105 }
106
107 open class BasicAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
108     open fun password(password: String) {
109         password(password.asJsonPrimitive())
110     }
111
112     open fun password(password: JsonNode) {
113         property(BasicAuthRestClientProperties::password, password)
114     }
115
116     open fun username(username: String) {
117         username(username.asJsonPrimitive())
118     }
119
120     open fun username(username: JsonNode) {
121         property(BasicAuthRestClientProperties::username, username)
122     }
123 }
124
125 open class TokenAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
126     open fun token(token: String) {
127         token(token.asJsonPrimitive())
128     }
129
130     open fun token(token: JsonNode) {
131         property(TokenAuthRestClientProperties::token, token)
132     }
133 }
134
135 open class SslAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {
136     open fun keyStoreInstance(keyStoreInstance: String) {
137         keyStoreInstance(keyStoreInstance.asJsonPrimitive())
138     }
139
140     open fun keyStoreInstance(keyStoreInstance: JsonNode) {
141         property(SSLRestClientProperties::keyStoreInstance, keyStoreInstance)
142     }
143
144     open fun sslTrust(sslTrust: String) {
145         sslTrust(sslTrust.asJsonPrimitive())
146     }
147
148     open fun sslTrust(sslTrust: JsonNode) {
149         property(SSLRestClientProperties::sslTrust, sslTrust)
150     }
151
152     open fun sslTrustPassword(sslTrustPassword: String) {
153         sslTrustPassword(sslTrustPassword.asJsonPrimitive())
154     }
155
156     open fun sslTrustPassword(sslTrustPassword: JsonNode) {
157         property(SSLRestClientProperties::sslTrustPassword, sslTrustPassword)
158     }
159
160     open fun sslKey(sslKey: String) {
161         sslKey(sslKey.asJsonPrimitive())
162     }
163
164     open fun sslKey(sslKey: JsonNode) {
165         property(SSLRestClientProperties::sslKey, sslKey)
166     }
167
168     open fun sslKeyPassword(sslKeyPassword: String) {
169         sslKeyPassword(sslKeyPassword.asJsonPrimitive())
170     }
171
172     open fun sslKeyPassword(sslKeyPassword: JsonNode) {
173         property(SSLRestClientProperties::sslKeyPassword, sslKeyPassword)
174     }
175 }
176
177 open class SSLBasicAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
178     // TODO()
179 }
180
181 open class SSLTokenAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
182     // TODO()
183 }