UATExecutor support for k8sConnectionPlugin
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / BluePrintRestLibPropertyService.kt
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2019 Huawei.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
20
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
34
35 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
36 open class BluePrintRestLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
37
38     private var preInterceptor: PreInterceptor? = null
39     private var postInterceptor: PostInterceptor? = null
40
41     fun setInterceptors(preInterceptor: PreInterceptor?, postInterceptor: PostInterceptor?) {
42         this.preInterceptor = preInterceptor
43         this.postInterceptor = postInterceptor
44     }
45
46     fun clearInterceptors() {
47         this.preInterceptor = null
48         this.postInterceptor = null
49     }
50
51     open fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
52         val service = preInterceptor?.getInstance(jsonNode)
53             ?: blueprintWebClientService(restClientProperties(jsonNode))
54         return postInterceptor?.getInstance(jsonNode, service) ?: service
55     }
56
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)
62         }
63         return postInterceptor?.getInstance(selector, service) ?: service
64     }
65
66     open fun interceptExternalBlueprintWebClientService(
67         externalService: BlueprintWebClientService,
68         selector: String
69     ): BlueprintWebClientService {
70         val service = preInterceptor?.getInstance(selector) ?: externalService
71         return postInterceptor?.getInstance(selector, service) ?: service
72     }
73
74     fun restClientProperties(prefix: String): RestClientProperties {
75         val type = bluePrintPropertiesService.propertyBeanType(
76             "$prefix.type", String::class.java
77         )
78         return when (type) {
79             RestLibConstants.TYPE_BASIC_AUTH -> {
80                 basicAuthRestClientProperties(prefix)
81             }
82             RestLibConstants.TYPE_TOKEN_AUTH -> {
83                 tokenRestClientProperties(prefix)
84             }
85             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
86                 sslBasicAuthRestClientProperties(prefix)
87             }
88             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
89                 sslTokenAuthRestClientProperties(prefix)
90             }
91             RestLibConstants.TYPE_SSL_NO_AUTH -> {
92                 sslNoAuthRestClientProperties(prefix)
93             }
94
95             RestLibConstants.TYPE_POLICY_MANAGER -> {
96                 policyManagerRestClientProperties(prefix)
97             }
98             else -> {
99                 throw BluePrintProcessorException(
100                     "Rest adaptor($type) is" +
101                         " not supported"
102                 )
103             }
104         }
105     }
106
107     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
108
109         val type = jsonNode.get("type").textValue()
110         return when (type) {
111             RestLibConstants.TYPE_TOKEN_AUTH -> {
112                 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
113             }
114             RestLibConstants.TYPE_BASIC_AUTH -> {
115                 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
116             }
117
118             RestLibConstants.TYPE_POLICY_MANAGER -> {
119                 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
120             }
121             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
122                 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
123             }
124             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
125                 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
126             }
127             RestLibConstants.TYPE_SSL_NO_AUTH -> {
128                 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
129             }
130             else -> {
131                 throw BluePrintProcessorException(
132                     "Rest adaptor($type) is not supported"
133                 )
134             }
135         }
136     }
137
138     private fun blueprintWebClientService(restClientProperties: RestClientProperties):
139         BlueprintWebClientService {
140
141             when (restClientProperties) {
142                 is SSLRestClientProperties -> {
143                     return SSLRestClientService(restClientProperties)
144                 }
145                 is TokenAuthRestClientProperties -> {
146                     return TokenAuthRestClientService(restClientProperties)
147                 }
148                 is BasicAuthRestClientProperties -> {
149                     return BasicAuthRestClientService(restClientProperties)
150                 }
151                 else -> {
152                     throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type}  uri: ${restClientProperties.url}")
153                 }
154             }
155         }
156
157     private fun tokenRestClientProperties(prefix: String):
158         TokenAuthRestClientProperties {
159             return bluePrintPropertiesService.propertyBeanType(
160                 prefix, TokenAuthRestClientProperties::class.java
161             )
162         }
163
164     private fun basicAuthRestClientProperties(prefix: String):
165         BasicAuthRestClientProperties {
166             return bluePrintPropertiesService.propertyBeanType(
167                 prefix, BasicAuthRestClientProperties::class.java
168             )
169         }
170
171     private fun sslBasicAuthRestClientProperties(prefix: String): SSLRestClientProperties {
172         return bluePrintPropertiesService.propertyBeanType(
173             prefix, SSLBasicAuthRestClientProperties::class.java
174         )
175     }
176
177     private fun sslTokenAuthRestClientProperties(prefix: String):
178         SSLRestClientProperties {
179
180             val sslProps: SSLTokenAuthRestClientProperties =
181                 bluePrintPropertiesService.propertyBeanType(
182                     prefix,
183                     SSLTokenAuthRestClientProperties::class.java
184                 )
185             val basicProps: TokenAuthRestClientProperties =
186                 bluePrintPropertiesService.propertyBeanType(
187                     prefix,
188                     TokenAuthRestClientProperties::class.java
189                 )
190             sslProps.tokenAuth = basicProps
191             return sslProps
192         }
193
194     private fun sslNoAuthRestClientProperties(prefix: String):
195         SSLRestClientProperties {
196             return bluePrintPropertiesService.propertyBeanType(
197                 prefix, SSLRestClientProperties::class.java
198             )
199         }
200
201     private fun policyManagerRestClientProperties(prefix: String):
202         PolicyManagerRestClientProperties {
203             return bluePrintPropertiesService.propertyBeanType(
204                 prefix, PolicyManagerRestClientProperties::class.java
205             )
206         }
207
208     interface PreInterceptor {
209
210         fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
211
212         fun getInstance(selector: String): BlueprintWebClientService?
213     }
214
215     interface PostInterceptor {
216
217         fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
218
219         fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService
220     }
221 }