aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/RestClientPropertiesDSL.kt
blob: ca1046dde692374c19d00732574c981f68f7b31f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  Copyright © 2019 IBM.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.rest

import com.fasterxml.jackson.databind.JsonNode
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.RelationshipTemplateBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.ServiceTemplateBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType

/** Relationships Type DSL for Rest */
fun ServiceTemplateBuilder.relationshipTypeConnectsToRestClient() {
    val relationshipType = BluePrintTypes.relationshipTypeConnectsToRestClient()
    if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
    this.relationshipTypes!![relationshipType.id!!] = relationshipType
}

fun BluePrintTypes.relationshipTypeConnectsToRestClient(): RelationshipType {
    return relationshipType(
        id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT,
        version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
        derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
        description = "Relationship connects to through"
    ) {
        property(
            BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
            BluePrintConstants.DATA_TYPE_MAP,
            true,
            "Connection Config details."
        )
        validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
    }
}

/** Relationships Templates DSL for Rest */
fun TopologyTemplateBuilder.relationshipTemplateRestClient(
    name: String,
    description: String,
    block: RestClientRelationshipTemplateBuilder.() -> Unit
) {
    if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
    val relationshipTemplate = RestClientRelationshipTemplateBuilder(name, description).apply(block).build()
    relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
}

open class RestClientRelationshipTemplateBuilder(name: String, description: String) :
    RelationshipTemplateBuilder(
        name,
        BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_REST_CLIENT, description
    ) {

    fun basicAuth(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
        property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.basicAuthRestClientProperties(block))
    }

    fun tokenAuth(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
        property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthRestClientProperties(block))
    }

    fun sslAuth(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit) {
        property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.sslRestClientProperties(block))
    }
}

fun BluePrintTypes.basicAuthRestClientProperties(block: BasicAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
    val assignments = BasicAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
    assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
    return assignments.asJsonType()
}

fun BluePrintTypes.tokenAuthRestClientProperties(block: TokenAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
    val assignments = TokenAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
    assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
    return assignments.asJsonType()
}

fun BluePrintTypes.sslRestClientProperties(block: SslAuthRestClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
    val assignments = SslAuthRestClientPropertiesAssignmentBuilder().apply(block).build()
    assignments[RestClientProperties::type.name] = RestLibConstants.TYPE_SSL_NO_AUTH.asJsonPrimitive()
    return assignments.asJsonType()
}

open class RestClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {

    open fun url(url: String) {
        url(url.asJsonPrimitive())
    }

    open fun url(url: JsonNode) {
        property(RestClientProperties::url, url)
    }
}

open class BasicAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {

    open fun password(password: String) {
        password(password.asJsonPrimitive())
    }

    open fun password(password: JsonNode) {
        property(BasicAuthRestClientProperties::password, password)
    }

    open fun username(username: String) {
        username(username.asJsonPrimitive())
    }

    open fun username(username: JsonNode) {
        property(BasicAuthRestClientProperties::username, username)
    }
}

open class TokenAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {

    open fun token(token: String) {
        token(token.asJsonPrimitive())
    }

    open fun token(token: JsonNode) {
        property(TokenAuthRestClientProperties::token, token)
    }
}

open class SslAuthRestClientPropertiesAssignmentBuilder : RestClientPropertiesAssignmentBuilder() {

    open fun keyStoreInstance(keyStoreInstance: String) {
        keyStoreInstance(keyStoreInstance.asJsonPrimitive())
    }

    open fun keyStoreInstance(keyStoreInstance: JsonNode) {
        property(SSLRestClientProperties::keyStoreInstance, keyStoreInstance)
    }

    open fun sslTrust(sslTrust: String) {
        sslTrust(sslTrust.asJsonPrimitive())
    }

    open fun sslTrust(sslTrust: JsonNode) {
        property(SSLRestClientProperties::sslTrust, sslTrust)
    }

    open fun sslTrustPassword(sslTrustPassword: String) {
        sslTrustPassword(sslTrustPassword.asJsonPrimitive())
    }

    open fun sslTrustPassword(sslTrustPassword: JsonNode) {
        property(SSLRestClientProperties::sslTrustPassword, sslTrustPassword)
    }

    open fun sslKey(sslKey: String) {
        sslKey(sslKey.asJsonPrimitive())
    }

    open fun sslKey(sslKey: JsonNode) {
        property(SSLRestClientProperties::sslKey, sslKey)
    }

    open fun sslKeyPassword(sslKeyPassword: String) {
        sslKeyPassword(sslKeyPassword.asJsonPrimitive())
    }

    open fun sslKeyPassword(sslKeyPassword: JsonNode) {
        property(SSLRestClientProperties::sslKeyPassword, sslKeyPassword)
    }
}

open class SSLBasicAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
    // TODO()
}

open class SSLTokenAuthRestClientPropertiesBuilder : SslAuthRestClientPropertiesAssignmentBuilder() {
    // TODO()
}