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