2 * Copyright © 2017-2019 AT&T, Bell Canada
3 * Modifications Copyright © 2019 IBM.
4 * Modifications Copyright © 2019 Huawei.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
21 import com.fasterxml.jackson.databind.JsonNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.*
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
26 import org.springframework.stereotype.Service
28 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
29 open class BluePrintRestLibPropertyService(private var bluePrintProperties:
30 BluePrintProperties) {
32 private var preInterceptor: PreInterceptor? = null
33 private var postInterceptor: PostInterceptor? = null
35 fun setInterceptors(preInterceptor: PreInterceptor?, postInterceptor: PostInterceptor?) {
36 this.preInterceptor = preInterceptor
37 this.postInterceptor = postInterceptor
40 fun clearInterceptors() {
41 this.preInterceptor = null
42 this.postInterceptor = null
45 open fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
46 val service = preInterceptor?.getInstance(jsonNode)
47 ?: blueprintWebClientService(restClientProperties(jsonNode))
48 return postInterceptor?.getInstance(jsonNode, service) ?: service
51 open fun blueprintWebClientService(selector: String): BlueprintWebClientService {
52 val service = preInterceptor?.getInstance(selector) ?: run {
53 val prefix = "blueprintsprocessor.restclient.$selector"
54 val restClientProperties = restClientProperties(prefix)
55 blueprintWebClientService(restClientProperties)
57 return postInterceptor?.getInstance(selector, service) ?: service
60 fun restClientProperties(prefix: String): RestClientProperties {
61 val type = bluePrintProperties.propertyBeanType(
62 "$prefix.type", String::class.java)
64 RestLibConstants.TYPE_BASIC_AUTH -> {
65 basicAuthRestClientProperties(prefix)
67 RestLibConstants.TYPE_TOKEN_AUTH -> {
68 tokenRestClientProperties(prefix)
70 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
71 sslBasicAuthRestClientProperties(prefix)
73 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
74 sslTokenAuthRestClientProperties(prefix)
76 RestLibConstants.TYPE_SSL_NO_AUTH -> {
77 sslNoAuthRestClientProperties(prefix)
80 RestLibConstants.TYPE_POLICY_MANAGER -> {
81 policyManagerRestClientProperties(prefix)
84 throw BluePrintProcessorException("Rest adaptor($type) is" +
90 fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
92 val type = jsonNode.get("type").textValue()
94 RestLibConstants.TYPE_TOKEN_AUTH -> {
95 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
97 RestLibConstants.TYPE_BASIC_AUTH -> {
98 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
101 RestLibConstants.TYPE_POLICY_MANAGER -> {
102 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
104 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
105 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
107 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
108 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
110 RestLibConstants.TYPE_SSL_NO_AUTH -> {
111 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
114 throw BluePrintProcessorException(
115 "Rest adaptor($type) is not supported")
120 private fun blueprintWebClientService(restClientProperties: RestClientProperties):
121 BlueprintWebClientService {
123 when (restClientProperties) {
124 is SSLRestClientProperties -> {
125 return SSLRestClientService(restClientProperties)
127 is TokenAuthRestClientProperties -> {
128 return TokenAuthRestClientService(restClientProperties)
130 is BasicAuthRestClientProperties -> {
131 return BasicAuthRestClientService(restClientProperties)
134 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type} uri: ${restClientProperties.url}")
139 private fun tokenRestClientProperties(prefix: String):
140 TokenAuthRestClientProperties {
141 return bluePrintProperties.propertyBeanType(
142 prefix, TokenAuthRestClientProperties::class.java)
145 private fun basicAuthRestClientProperties(prefix: String):
146 BasicAuthRestClientProperties {
147 return bluePrintProperties.propertyBeanType(
148 prefix, BasicAuthRestClientProperties::class.java)
151 private fun sslBasicAuthRestClientProperties(prefix: String):
152 SSLRestClientProperties {
154 val sslProps: SSLBasicAuthRestClientProperties =
155 bluePrintProperties.propertyBeanType(
156 prefix, SSLBasicAuthRestClientProperties::class.java)
157 val basicProps: BasicAuthRestClientProperties =
158 bluePrintProperties.propertyBeanType(
159 prefix, BasicAuthRestClientProperties::class.java)
160 sslProps.basicAuth = basicProps
164 private fun sslTokenAuthRestClientProperties(prefix: String):
165 SSLRestClientProperties {
167 val sslProps: SSLTokenAuthRestClientProperties =
168 bluePrintProperties.propertyBeanType(prefix,
169 SSLTokenAuthRestClientProperties::class.java)
170 val basicProps: TokenAuthRestClientProperties =
171 bluePrintProperties.propertyBeanType(prefix,
172 TokenAuthRestClientProperties::class.java)
173 sslProps.tokenAuth = basicProps
177 private fun sslNoAuthRestClientProperties(prefix: String):
178 SSLRestClientProperties {
179 return bluePrintProperties.propertyBeanType(
180 prefix, SSLRestClientProperties::class.java)
183 private fun policyManagerRestClientProperties(prefix: String):
184 PolicyManagerRestClientProperties {
185 return bluePrintProperties.propertyBeanType(
186 prefix, PolicyManagerRestClientProperties::class.java)
189 interface PreInterceptor {
190 fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
192 fun getInstance(selector: String): BlueprintWebClientService?
195 interface PostInterceptor {
196 fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
198 fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService