8f528a2ef374b9611e4bc6ca2b01db7c5b2a0e17
[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     fun restClientProperties(prefix: String): RestClientProperties {
67         val type = bluePrintPropertiesService.propertyBeanType(
68             "$prefix.type", String::class.java
69         )
70         return when (type) {
71             RestLibConstants.TYPE_BASIC_AUTH -> {
72                 basicAuthRestClientProperties(prefix)
73             }
74             RestLibConstants.TYPE_TOKEN_AUTH -> {
75                 tokenRestClientProperties(prefix)
76             }
77             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
78                 sslBasicAuthRestClientProperties(prefix)
79             }
80             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
81                 sslTokenAuthRestClientProperties(prefix)
82             }
83             RestLibConstants.TYPE_SSL_NO_AUTH -> {
84                 sslNoAuthRestClientProperties(prefix)
85             }
86
87             RestLibConstants.TYPE_POLICY_MANAGER -> {
88                 policyManagerRestClientProperties(prefix)
89             }
90             else -> {
91                 throw BluePrintProcessorException(
92                     "Rest adaptor($type) is" +
93                             " not supported"
94                 )
95             }
96         }
97     }
98
99     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
100
101         val type = jsonNode.get("type").textValue()
102         return when (type) {
103             RestLibConstants.TYPE_TOKEN_AUTH -> {
104                 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
105             }
106             RestLibConstants.TYPE_BASIC_AUTH -> {
107                 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
108             }
109
110             RestLibConstants.TYPE_POLICY_MANAGER -> {
111                 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
112             }
113             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
114                 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
115             }
116             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
117                 JacksonUtils.readValue(jsonNode, SSLTokenAuthRestClientProperties::class.java)!!
118             }
119             RestLibConstants.TYPE_SSL_NO_AUTH -> {
120                 JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!!
121             }
122             else -> {
123                 throw BluePrintProcessorException(
124                     "Rest adaptor($type) is not supported"
125                 )
126             }
127         }
128     }
129
130     private fun blueprintWebClientService(restClientProperties: RestClientProperties):
131             BlueprintWebClientService {
132
133         when (restClientProperties) {
134             is SSLRestClientProperties -> {
135                 return SSLRestClientService(restClientProperties)
136             }
137             is TokenAuthRestClientProperties -> {
138                 return TokenAuthRestClientService(restClientProperties)
139             }
140             is BasicAuthRestClientProperties -> {
141                 return BasicAuthRestClientService(restClientProperties)
142             }
143             else -> {
144                 throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type}  uri: ${restClientProperties.url}")
145             }
146         }
147     }
148
149     private fun tokenRestClientProperties(prefix: String):
150             TokenAuthRestClientProperties {
151         return bluePrintPropertiesService.propertyBeanType(
152             prefix, TokenAuthRestClientProperties::class.java
153         )
154     }
155
156     private fun basicAuthRestClientProperties(prefix: String):
157             BasicAuthRestClientProperties {
158         return bluePrintPropertiesService.propertyBeanType(
159             prefix, BasicAuthRestClientProperties::class.java
160         )
161     }
162
163     private fun sslBasicAuthRestClientProperties(prefix: String):
164             SSLRestClientProperties {
165
166         val sslProps: SSLBasicAuthRestClientProperties =
167             bluePrintPropertiesService.propertyBeanType(
168                 prefix, SSLBasicAuthRestClientProperties::class.java
169             )
170         val basicProps: BasicAuthRestClientProperties =
171             bluePrintPropertiesService.propertyBeanType(
172                 prefix, BasicAuthRestClientProperties::class.java
173             )
174         sslProps.basicAuth = basicProps
175         return sslProps
176     }
177
178     private fun sslTokenAuthRestClientProperties(prefix: String):
179             SSLRestClientProperties {
180
181         val sslProps: SSLTokenAuthRestClientProperties =
182             bluePrintPropertiesService.propertyBeanType(
183                 prefix,
184                 SSLTokenAuthRestClientProperties::class.java
185             )
186         val basicProps: TokenAuthRestClientProperties =
187             bluePrintPropertiesService.propertyBeanType(
188                 prefix,
189                 TokenAuthRestClientProperties::class.java
190             )
191         sslProps.tokenAuth = basicProps
192         return sslProps
193     }
194
195     private fun sslNoAuthRestClientProperties(prefix: String):
196             SSLRestClientProperties {
197         return bluePrintPropertiesService.propertyBeanType(
198             prefix, SSLRestClientProperties::class.java
199         )
200     }
201
202     private fun policyManagerRestClientProperties(prefix: String):
203             PolicyManagerRestClientProperties {
204         return bluePrintPropertiesService.propertyBeanType(
205             prefix, PolicyManagerRestClientProperties::class.java
206         )
207     }
208
209     interface PreInterceptor {
210         fun getInstance(jsonNode: JsonNode): BlueprintWebClientService?
211
212         fun getInstance(selector: String): BlueprintWebClientService?
213     }
214
215     interface PostInterceptor {
216         fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService
217
218         fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService
219     }
220 }