aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt
blob: a2c68ec6881877523f9c928d955fe5f9d274f834 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright © 2017-2019 AT&T, Bell Canada
 * Modifications Copyright © 2019 IBM.
 * Modifications Copyright © 2019 Huawei.
 *
 * 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.service

import com.fasterxml.jackson.databind.JsonNode
import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.PolicyManagerRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.springframework.stereotype.Service

@Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
open class BluePrintRestLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {

    private var preInterceptor: PreInterceptor? = null
    private var postInterceptor: PostInterceptor? = null

    fun setInterceptors(preInterceptor: PreInterceptor?, postInterceptor: PostInterceptor?) {
        this.preInterceptor = preInterceptor
        this.postInterceptor = postInterceptor
    }

    fun clearInterceptors() {
        this.preInterceptor = null
        this.postInterceptor = null
    }

    open fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
        val service = preInterceptor?.getInstance(jsonNode)
            ?: blueprintWebClientService(restClientProperties(jsonNode))
        return postInterceptor?.getInstance(jsonNode, service) ?: service
    }

    open fun blueprintWebClientService(selector: String): BlueprintWebClientService {
        val service = preInterceptor?.getInstance(selector) ?: run {
            val prefix = "blueprintsprocessor.restclient.$selector"
            val restClientProperties = restClientProperties(prefix)
            blueprintWebClientService(restClientProperties)
        }
        return postInterceptor?.getInstance(selector, service) ?: service
    }

    fun restClientProperties(prefix: String): RestClientProperties {
        val type = bluePrintPropertiesService.propertyBeanType(
            "$prefix.type", String::class.java
        )
        return when (type) {
            RestLibConstants.TYPE_BASIC_AUTH -> {
                basicAuthRestClientProperties(prefix)
            }
            RestLibConstants.TYPE_TOKEN_AUTH -> {
                tokenRestClientProperties(prefix)
            }
            RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
                sslBasicAuthRestClientProperties(prefix)
            }
            RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
                sslTokenAuthRestClientProperties(prefix)
            }
            RestLibConstants.TYPE_SSL_NO_AUTH -> {
                sslNoAuthRestClientProperties(prefix)
            }

            RestLibConstants.TYPE_POLICY_MANAGER -> {
                policyManagerRestClientProperties(prefix)
            }
            else -> {
                throw BluePrintProcessorException(
                    "Rest adaptor($type) is" +
                        " not supported"
                )
            }
        }
    }

    fun restClientProperties(jsonNode: JsonNode): RestClientProperties {

        val type = jsonNode.get("type").textValue()
        return when (type) {
            RestLibConstants.TYPE_TOKEN_AUTH -> {
                JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
            }
            RestLibConstants.TYPE_BASIC_AUTH -> {
                JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
            }

            RestLibConstants.TYPE_POLICY_MANAGER -> {
                JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
            }
            RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
                JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
            }
            RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
                JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
            }
            RestLibConstants.TYPE_SSL_NO_AUTH -> {
                JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
            }
            else -> {
                throw BluePrintProcessorException(
                    "Rest adaptor($type) is not supported"
                )
            }
        }
    }

    private fun blueprintWebClientService(restClientProperties: RestClientProperties):
        BlueprintWebClientService {

            when (restClientProperties) {
                is SSLRestClientProperties -> {
                    return SSLRestClientService(restClientProperties)
                }
                is TokenAuthRestClientProperties -> {
                    return TokenAuthRestClientService(restClientProperties)
                }
                is BasicAuthRestClientProperties -> {
                    return BasicAuthRestClientService(restClientProperties)
                }
                else -> {
                    throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type}  uri: ${restClientProperties.url}")
                }
            }
        }

    private fun tokenRestClientProperties(prefix: String):
        TokenAuthRestClientProperties {
            return bluePrintPropertiesService.propertyBeanType(
                prefix, TokenAuthRestClientProperties::class.java
            )
        }

    private fun basicAuthRestClientProperties(prefix: String):
        BasicAuthRestClientProperties {
            return bluePrintPropertiesService.propertyBeanType(
                prefix, BasicAuthRestClientProperties::class.java
            )
        }

    private fun sslBasicAuthRestClientProperties(prefix: String):
        SSLRestClientProperties {

            val sslProps: SSLBasicAuthRestClientProperties =
                bluePrintPropertiesService.propertyBeanType(
                    prefix, SSLBasicAuthRestClientProperties::class.java
                )
            val basicProps: BasicAuthRestClientProperties =
                bluePrintPropertiesService.propertyBeanType(
                    prefix, BasicAuthRestClientProperties::class.java
                )
            sslProps.basicAuth = basicProps
            return sslProps
        }

    private fun sslTokenAuthRestClientProperties(prefix: String):
        SSLRestClientProperties {

            val sslProps: SSLTokenAuthRestClientProperties =
                bluePrintPropertiesService.propertyBeanType(
                    prefix,
                    SSLTokenAuthRestClientProperties::class.java
                )
            val basicProps: TokenAuthRestClientProperties =
                bluePrintPropertiesService.propertyBeanType(
                    prefix,
                    TokenAuthRestClientProperties::class.java
                )
            sslProps.tokenAuth = basicProps
            return sslProps
        }

    private fun sslNoAuthRestClientProperties(prefix: String):
        SSLRestClientProperties {
            return bluePrintPropertiesService.propertyBeanType(
                prefix, SSLRestClientProperties::class.java
            )
        }

    private fun policyManagerRestClientProperties(prefix: String):
        PolicyManagerRestClientProperties {
            return bluePrintPropertiesService.propertyBeanType(
                prefix, PolicyManagerRestClientProperties::class.java
            )
        }

    interface PreInterceptor {

        fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?

        fun getInstance(selector: String): BlueprintWebClientService?
    }

    interface PostInterceptor {

        fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService

        fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService
    }
}