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.BluePrintPropertiesService
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.PolicyManagerRestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.springframework.stereotype.Service
35 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
36 open class BluePrintRestLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
38 private var preInterceptor: PreInterceptor? = null
39 private var postInterceptor: PostInterceptor? = null
41 fun setInterceptors(preInterceptor: PreInterceptor?, postInterceptor: PostInterceptor?) {
42 this.preInterceptor = preInterceptor
43 this.postInterceptor = postInterceptor
46 fun clearInterceptors() {
47 this.preInterceptor = null
48 this.postInterceptor = null
51 open fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
52 val service = preInterceptor?.getInstance(jsonNode)
53 ?: blueprintWebClientService(restClientProperties(jsonNode))
54 return postInterceptor?.getInstance(jsonNode, service) ?: service
57 open fun blueprintWebClientService(selector: String): BlueprintWebClientService {
58 val service = preInterceptor?.getInstance(selector) ?: run {
59 val prefix = "blueprintsprocessor.restclient.$selector"
60 val restClientProperties = restClientProperties(prefix)
61 blueprintWebClientService(restClientProperties)
63 return postInterceptor?.getInstance(selector, service) ?: service
66 open fun interceptExternalBlueprintWebClientService(
67 externalService: BlueprintWebClientService,
69 ): BlueprintWebClientService {
70 val service = preInterceptor?.getInstance(selector) ?: externalService
71 return postInterceptor?.getInstance(selector, service) ?: service
74 fun restClientProperties(prefix: String): RestClientProperties {
75 val type = bluePrintPropertiesService.propertyBeanType(
76 "$prefix.type", String::class.java
79 RestLibConstants.TYPE_BASIC_AUTH -> {
80 basicAuthRestClientProperties(prefix)
82 RestLibConstants.TYPE_TOKEN_AUTH -> {
83 tokenRestClientProperties(prefix)
85 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
86 sslBasicAuthRestClientProperties(prefix)
88 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
89 sslTokenAuthRestClientProperties(prefix)
91 RestLibConstants.TYPE_SSL_NO_AUTH -> {
92 sslNoAuthRestClientProperties(prefix)
95 RestLibConstants.TYPE_POLICY_MANAGER -> {
96 policyManagerRestClientProperties(prefix)
99 throw BluePrintProcessorException(
100 "Rest adaptor($type) is" +
107 fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
109 val type = jsonNode.get("type").textValue()
111 RestLibConstants.TYPE_TOKEN_AUTH -> {
112 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
114 RestLibConstants.TYPE_BASIC_AUTH -> {
115 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
118 RestLibConstants.TYPE_POLICY_MANAGER -> {
119 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
121 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
122 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
124 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
125 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
127 RestLibConstants.TYPE_SSL_NO_AUTH -> {
128 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
131 throw BluePrintProcessorException(
132 "Rest adaptor($type) is not supported"
138 private fun blueprintWebClientService(restClientProperties: RestClientProperties):
139 BlueprintWebClientService {
141 when (restClientProperties) {
142 is SSLRestClientProperties -> {
143 return SSLRestClientService(restClientProperties)
145 is TokenAuthRestClientProperties -> {
146 return TokenAuthRestClientService(restClientProperties)
148 is BasicAuthRestClientProperties -> {
149 return BasicAuthRestClientService(restClientProperties)
152 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type} uri: ${restClientProperties.url}")
157 private fun tokenRestClientProperties(prefix: String):
158 TokenAuthRestClientProperties {
159 return bluePrintPropertiesService.propertyBeanType(
160 prefix, TokenAuthRestClientProperties::class.java
164 private fun basicAuthRestClientProperties(prefix: String):
165 BasicAuthRestClientProperties {
166 return bluePrintPropertiesService.propertyBeanType(
167 prefix, BasicAuthRestClientProperties::class.java
171 private fun sslBasicAuthRestClientProperties(prefix: String): SSLRestClientProperties {
172 return bluePrintPropertiesService.propertyBeanType(
173 prefix, SSLBasicAuthRestClientProperties::class.java
177 private fun sslTokenAuthRestClientProperties(prefix: String):
178 SSLRestClientProperties {
180 val sslProps: SSLTokenAuthRestClientProperties =
181 bluePrintPropertiesService.propertyBeanType(
183 SSLTokenAuthRestClientProperties::class.java
185 val basicProps: TokenAuthRestClientProperties =
186 bluePrintPropertiesService.propertyBeanType(
188 TokenAuthRestClientProperties::class.java
190 sslProps.tokenAuth = basicProps
194 private fun sslNoAuthRestClientProperties(prefix: String):
195 SSLRestClientProperties {
196 return bluePrintPropertiesService.propertyBeanType(
197 prefix, SSLRestClientProperties::class.java
201 private fun policyManagerRestClientProperties(prefix: String):
202 PolicyManagerRestClientProperties {
203 return bluePrintPropertiesService.propertyBeanType(
204 prefix, PolicyManagerRestClientProperties::class.java
208 interface PreInterceptor {
210 fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
212 fun getInstance(selector: String): BlueprintWebClientService?
215 interface PostInterceptor {
217 fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
219 fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService