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