a6bbc39d80464cf3217bfe43f7371ca9926c2525
[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.BasicAuthRestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.DME2RestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.PolicyManagerRestClientProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties
31 import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
34 import org.springframework.stereotype.Service
35
36 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
37 open class BluePrintRestLibPropertyService(private var bluePrintProperties:
38                                            BluePrintProperties) {
39
40     fun blueprintWebClientService(jsonNode: JsonNode):
41             BlueprintWebClientService {
42         val restClientProperties = restClientProperties(jsonNode)
43         return blueprintWebClientService(restClientProperties)
44     }
45
46     fun blueprintWebClientService(selector: String): BlueprintWebClientService {
47         val prefix = "blueprintsprocessor.restclient.$selector"
48         val restClientProperties = restClientProperties(prefix)
49         return blueprintWebClientService(restClientProperties)
50     }
51
52     fun restClientProperties(prefix: String): RestClientProperties {
53         val type = bluePrintProperties.propertyBeanType(
54                 "$prefix.type", String::class.java)
55         return when (type) {
56             RestLibConstants.TYPE_BASIC_AUTH -> {
57                 basicAuthRestClientProperties(prefix)
58             }
59             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
60                 sslBasicAuthRestClientProperties(prefix)
61             }
62             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
63                 sslTokenAuthRestClientProperties(prefix)
64             }
65             RestLibConstants.TYPE_SSL_NO_AUTH -> {
66                 sslNoAuthRestClientProperties(prefix)
67             }
68             RestLibConstants.TYPE_DME2_PROXY -> {
69                 dme2ProxyClientProperties(prefix)
70             }
71             RestLibConstants.TYPE_POLICY_MANAGER -> {
72                 policyManagerRestClientProperties(prefix)
73             }
74             else -> {
75                 throw BluePrintProcessorException("Rest adaptor($type) is" +
76                         " not supported")
77             }
78         }
79     }
80
81     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
82
83         val type = jsonNode.get("type").textValue()
84         return when (type) {
85             RestLibConstants.TYPE_TOKEN_AUTH -> {
86                 JacksonUtils.readValue(jsonNode,
87                         TokenAuthRestClientProperties::class.java)!!
88             }
89             RestLibConstants.TYPE_BASIC_AUTH -> {
90                 JacksonUtils.readValue(jsonNode,
91                         BasicAuthRestClientProperties::class.java)!!
92             }
93             RestLibConstants.TYPE_DME2_PROXY -> {
94                 JacksonUtils.readValue(jsonNode,
95                         DME2RestClientProperties::class.java)!!
96             }
97             RestLibConstants.TYPE_POLICY_MANAGER -> {
98                 JacksonUtils.readValue(jsonNode,
99                         PolicyManagerRestClientProperties::class.java)!!
100             }
101             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
102                 JacksonUtils.readValue(jsonNode,
103                         SSLBasicAuthRestClientProperties::class.java)!!
104             }
105             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
106                 JacksonUtils.readValue(jsonNode,
107                         SSLTokenAuthRestClientProperties::class.java)!!
108             }
109             RestLibConstants.TYPE_SSL_NO_AUTH -> {
110                 JacksonUtils.readValue(
111                         jsonNode, SSLRestClientProperties::class.java)!!
112             }
113             else -> {
114                 throw BluePrintProcessorException("Rest adaptor($type) is" +
115                         " not supported")
116             }
117         }
118     }
119     
120     private fun blueprintWebClientService(
121             restClientProperties: RestClientProperties):
122             BlueprintWebClientService {
123
124         when (restClientProperties) {
125             is SSLRestClientProperties -> {
126                 return SSLRestClientService(restClientProperties)
127             }
128             is TokenAuthRestClientProperties -> {
129                 return TokenAuthRestClientService(restClientProperties)
130             }
131             is BasicAuthRestClientProperties -> {
132                 return BasicAuthRestClientService(restClientProperties)
133             }
134             is DME2RestClientProperties -> {
135                 return DME2ProxyRestClientService(restClientProperties)
136             }
137             else -> {
138                 throw BluePrintProcessorException("couldn't get rest " +
139                         "service for")
140             }
141         }
142     }
143
144     private fun basicAuthRestClientProperties(prefix: String):
145             BasicAuthRestClientProperties {
146         return bluePrintProperties.propertyBeanType(
147                 prefix, BasicAuthRestClientProperties::class.java)
148     }
149
150     private fun sslBasicAuthRestClientProperties(prefix: String):
151             SSLRestClientProperties {
152
153         val sslProps: SSLBasicAuthRestClientProperties =
154                 bluePrintProperties.propertyBeanType(
155                         prefix, SSLBasicAuthRestClientProperties::class.java)
156         val basicProps : BasicAuthRestClientProperties =
157                 bluePrintProperties.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                 bluePrintProperties.propertyBeanType(prefix,
168                         SSLTokenAuthRestClientProperties::class.java)
169         val basicProps : TokenAuthRestClientProperties =
170                 bluePrintProperties.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 bluePrintProperties.propertyBeanType(
179                 prefix, SSLRestClientProperties::class.java)
180     }
181
182     private fun dme2ProxyClientProperties(prefix: String):
183             DME2RestClientProperties {
184         return bluePrintProperties.propertyBeanType(
185                 prefix, DME2RestClientProperties::class.java)
186     }
187
188     private fun policyManagerRestClientProperties(prefix: String):
189             PolicyManagerRestClientProperties {
190         return bluePrintProperties.propertyBeanType(
191                 prefix, PolicyManagerRestClientProperties::class.java)
192     }
193 }
194
195