9a7b3c303fda8685f3d9cfa086415f4c626dd77f
[ccsdk/cds.git] /
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.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
27
28 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
29 open class BluePrintRestLibPropertyService(private var bluePrintProperties:
30                                            BluePrintProperties) {
31
32     private var preInterceptor: PreInterceptor? = null
33     private var postInterceptor: PostInterceptor? = null
34
35     fun setInterceptors(preInterceptor: PreInterceptor?, postInterceptor: PostInterceptor?) {
36         this.preInterceptor = preInterceptor
37         this.postInterceptor = postInterceptor
38     }
39
40     fun clearInterceptors() {
41         this.preInterceptor = null
42         this.postInterceptor = null
43     }
44
45     open fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
46         val service = preInterceptor?.getInstance(jsonNode)
47                 ?: blueprintWebClientService(restClientProperties(jsonNode))
48         return postInterceptor?.getInstance(jsonNode, service) ?: service
49     }
50
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)
56         }
57         return postInterceptor?.getInstance(selector, service) ?: service
58     }
59
60     fun restClientProperties(prefix: String): RestClientProperties {
61         val type = bluePrintProperties.propertyBeanType(
62             "$prefix.type", String::class.java)
63         return when (type) {
64             RestLibConstants.TYPE_BASIC_AUTH -> {
65                 basicAuthRestClientProperties(prefix)
66             }
67             RestLibConstants.TYPE_TOKEN_AUTH -> {
68                 tokenRestClientProperties(prefix)
69             }
70             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
71                 sslBasicAuthRestClientProperties(prefix)
72             }
73             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
74                 sslTokenAuthRestClientProperties(prefix)
75             }
76             RestLibConstants.TYPE_SSL_NO_AUTH -> {
77                 sslNoAuthRestClientProperties(prefix)
78             }
79
80             RestLibConstants.TYPE_POLICY_MANAGER -> {
81                 policyManagerRestClientProperties(prefix)
82             }
83             else -> {
84                 throw BluePrintProcessorException("Rest adaptor($type) is" +
85                     " not supported")
86             }
87         }
88     }
89
90     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
91
92         val type = jsonNode.get("type").textValue()
93         return when (type) {
94             RestLibConstants.TYPE_TOKEN_AUTH -> {
95                 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
96             }
97             RestLibConstants.TYPE_BASIC_AUTH -> {
98                 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
99             }
100
101             RestLibConstants.TYPE_POLICY_MANAGER -> {
102                 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
103             }
104             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
105                 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
106             }
107             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
108                 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
109             }
110             RestLibConstants.TYPE_SSL_NO_AUTH -> {
111                 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
112             }
113             else -> {
114                 throw BluePrintProcessorException(
115                     "Rest adaptor($type) is not supported")
116             }
117         }
118     }
119
120     private fun blueprintWebClientService(restClientProperties: RestClientProperties):
121         BlueprintWebClientService {
122
123         when (restClientProperties) {
124             is SSLRestClientProperties -> {
125                 return SSLRestClientService(restClientProperties)
126             }
127             is TokenAuthRestClientProperties -> {
128                 return TokenAuthRestClientService(restClientProperties)
129             }
130             is BasicAuthRestClientProperties -> {
131                 return BasicAuthRestClientService(restClientProperties)
132             }
133             else -> {
134                 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type}  uri: ${restClientProperties.url}")
135             }
136         }
137     }
138
139     private fun tokenRestClientProperties(prefix: String):
140         TokenAuthRestClientProperties {
141         return bluePrintProperties.propertyBeanType(
142             prefix, TokenAuthRestClientProperties::class.java)
143     }
144
145     private fun basicAuthRestClientProperties(prefix: String):
146         BasicAuthRestClientProperties {
147         return bluePrintProperties.propertyBeanType(
148             prefix, BasicAuthRestClientProperties::class.java)
149     }
150
151     private fun sslBasicAuthRestClientProperties(prefix: String):
152         SSLRestClientProperties {
153
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
161         return sslProps
162     }
163
164     private fun sslTokenAuthRestClientProperties(prefix: String):
165         SSLRestClientProperties {
166
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
174         return sslProps
175     }
176
177     private fun sslNoAuthRestClientProperties(prefix: String):
178         SSLRestClientProperties {
179         return bluePrintProperties.propertyBeanType(
180             prefix, SSLRestClientProperties::class.java)
181     }
182
183     private fun policyManagerRestClientProperties(prefix: String):
184         PolicyManagerRestClientProperties {
185         return bluePrintProperties.propertyBeanType(
186             prefix, PolicyManagerRestClientProperties::class.java)
187     }
188
189     interface PreInterceptor {
190         fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
191
192         fun getInstance(selector: String): BlueprintWebClientService?
193     }
194
195     interface PostInterceptor {
196         fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
197
198         fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService
199     }
200 }
201
202