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