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)
79 RestLibConstants.TYPE_DME2_PROXY -> {
80 dme2ProxyClientProperties(prefix)
82 RestLibConstants.TYPE_POLICY_MANAGER -> {
83 policyManagerRestClientProperties(prefix)
86 throw BluePrintProcessorException("Rest adaptor($type) is" +
92 fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
94 val type = jsonNode.get("type").textValue()
96 RestLibConstants.TYPE_TOKEN_AUTH -> {
97 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
99 RestLibConstants.TYPE_BASIC_AUTH -> {
100 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
102 RestLibConstants.TYPE_DME2_PROXY -> {
103 JacksonUtils.readValue(jsonNode, DME2RestClientProperties::class.java)!!
105 RestLibConstants.TYPE_POLICY_MANAGER -> {
106 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
108 RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
109 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
111 RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
112 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
114 RestLibConstants.TYPE_SSL_NO_AUTH -> {
115 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
118 throw BluePrintProcessorException(
119 "Rest adaptor($type) is not supported")
124 private fun blueprintWebClientService(restClientProperties: RestClientProperties):
125 BlueprintWebClientService {
127 when (restClientProperties) {
128 is SSLRestClientProperties -> {
129 return SSLRestClientService(restClientProperties)
131 is TokenAuthRestClientProperties -> {
132 return TokenAuthRestClientService(restClientProperties)
134 is BasicAuthRestClientProperties -> {
135 return BasicAuthRestClientService(restClientProperties)
137 is DME2RestClientProperties -> {
138 return DME2ProxyRestClientService(restClientProperties)
141 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type} uri: ${restClientProperties.url}")
146 private fun tokenRestClientProperties(prefix: String):
147 TokenAuthRestClientProperties {
148 return bluePrintProperties.propertyBeanType(
149 prefix, TokenAuthRestClientProperties::class.java)
152 private fun basicAuthRestClientProperties(prefix: String):
153 BasicAuthRestClientProperties {
154 return bluePrintProperties.propertyBeanType(
155 prefix, BasicAuthRestClientProperties::class.java)
158 private fun sslBasicAuthRestClientProperties(prefix: String):
159 SSLRestClientProperties {
161 val sslProps: SSLBasicAuthRestClientProperties =
162 bluePrintProperties.propertyBeanType(
163 prefix, SSLBasicAuthRestClientProperties::class.java)
164 val basicProps: BasicAuthRestClientProperties =
165 bluePrintProperties.propertyBeanType(
166 prefix, BasicAuthRestClientProperties::class.java)
167 sslProps.basicAuth = basicProps
171 private fun sslTokenAuthRestClientProperties(prefix: String):
172 SSLRestClientProperties {
174 val sslProps: SSLTokenAuthRestClientProperties =
175 bluePrintProperties.propertyBeanType(prefix,
176 SSLTokenAuthRestClientProperties::class.java)
177 val basicProps: TokenAuthRestClientProperties =
178 bluePrintProperties.propertyBeanType(prefix,
179 TokenAuthRestClientProperties::class.java)
180 sslProps.tokenAuth = basicProps
184 private fun sslNoAuthRestClientProperties(prefix: String):
185 SSLRestClientProperties {
186 return bluePrintProperties.propertyBeanType(
187 prefix, SSLRestClientProperties::class.java)
190 private fun dme2ProxyClientProperties(prefix: String):
191 DME2RestClientProperties {
192 return bluePrintProperties.propertyBeanType(
193 prefix, DME2RestClientProperties::class.java)
196 private fun policyManagerRestClientProperties(prefix: String):
197 PolicyManagerRestClientProperties {
198 return bluePrintProperties.propertyBeanType(
199 prefix, PolicyManagerRestClientProperties::class.java)
202 interface PreInterceptor {
203 fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
205 fun getInstance(selector: String): BlueprintWebClientService?
208 interface PostInterceptor {
209 fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
211 fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService