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 fun restClientProperties(prefix: String): RestClientProperties {
67 val type = bluePrintPropertiesService.propertyBeanType(
68 "$prefix.type", String::class.java
71 RestLibConstants.TYPE_BASIC_AUTH -> {
72 basicAuthRestClientProperties(prefix)
74 RestLibConstants.TYPE_TOKEN_AUTH -> {
75 tokenRestClientProperties(prefix)
77 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
78 sslBasicAuthRestClientProperties(prefix)
80 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
81 sslTokenAuthRestClientProperties(prefix)
83 RestLibConstants.TYPE_SSL_NO_AUTH -> {
84 sslNoAuthRestClientProperties(prefix)
87 RestLibConstants.TYPE_POLICY_MANAGER -> {
88 policyManagerRestClientProperties(prefix)
91 throw BluePrintProcessorException(
92 "Rest adaptor($type) is" +
99 fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
101 val type = jsonNode.get("type").textValue()
103 RestLibConstants.TYPE_TOKEN_AUTH -> {
104 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
106 RestLibConstants.TYPE_BASIC_AUTH -> {
107 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
110 RestLibConstants.TYPE_POLICY_MANAGER -> {
111 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
113 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
114 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
116 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
117 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
119 RestLibConstants.TYPE_SSL_NO_AUTH -> {
120 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
123 throw BluePrintProcessorException(
124 "Rest adaptor($type) is not supported"
130 private fun blueprintWebClientService(restClientProperties: RestClientProperties):
131 BlueprintWebClientService {
133 when (restClientProperties) {
134 is SSLRestClientProperties -> {
135 return SSLRestClientService(restClientProperties)
137 is TokenAuthRestClientProperties -> {
138 return TokenAuthRestClientService(restClientProperties)
140 is BasicAuthRestClientProperties -> {
141 return BasicAuthRestClientService(restClientProperties)
144 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type} uri: ${restClientProperties.url}")
149 private fun tokenRestClientProperties(prefix: String):
150 TokenAuthRestClientProperties {
151 return bluePrintPropertiesService.propertyBeanType(
152 prefix, TokenAuthRestClientProperties::class.java
156 private fun basicAuthRestClientProperties(prefix: String):
157 BasicAuthRestClientProperties {
158 return bluePrintPropertiesService.propertyBeanType(
159 prefix, BasicAuthRestClientProperties::class.java
163 private fun sslBasicAuthRestClientProperties(prefix: String):
164 SSLRestClientProperties {
166 val sslProps: SSLBasicAuthRestClientProperties =
167 bluePrintPropertiesService.propertyBeanType(
168 prefix, SSLBasicAuthRestClientProperties::class.java
170 val basicProps: BasicAuthRestClientProperties =
171 bluePrintPropertiesService.propertyBeanType(
172 prefix, BasicAuthRestClientProperties::class.java
174 sslProps.basicAuth = basicProps
178 private fun sslTokenAuthRestClientProperties(prefix: String):
179 SSLRestClientProperties {
181 val sslProps: SSLTokenAuthRestClientProperties =
182 bluePrintPropertiesService.propertyBeanType(
184 SSLTokenAuthRestClientProperties::class.java
186 val basicProps: TokenAuthRestClientProperties =
187 bluePrintPropertiesService.propertyBeanType(
189 TokenAuthRestClientProperties::class.java
191 sslProps.tokenAuth = basicProps
195 private fun sslNoAuthRestClientProperties(prefix: String):
196 SSLRestClientProperties {
197 return bluePrintPropertiesService.propertyBeanType(
198 prefix, SSLRestClientProperties::class.java
202 private fun policyManagerRestClientProperties(prefix: String):
203 PolicyManagerRestClientProperties {
204 return bluePrintPropertiesService.propertyBeanType(
205 prefix, PolicyManagerRestClientProperties::class.java
209 interface PreInterceptor {
211 fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
213 fun getInstance(selector: String): BlueprintWebClientService?
216 interface PostInterceptor {
218 fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
220 fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService