Add declarative acceptance tests
[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.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     open fun blueprintWebClientService(jsonNode: JsonNode):
41             BlueprintWebClientService {
42         val restClientProperties = restClientProperties(jsonNode)
43         return blueprintWebClientService(restClientProperties)
44     }
45
46     open 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_TOKEN_AUTH -> {
60                 tokenRestClientProperties(prefix)
61             }
62             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
63                 sslBasicAuthRestClientProperties(prefix)
64             }
65             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
66                 sslTokenAuthRestClientProperties(prefix)
67             }
68             RestLibConstants.TYPE_SSL_NO_AUTH -> {
69                 sslNoAuthRestClientProperties(prefix)
70             }
71             RestLibConstants.TYPE_DME2_PROXY -> {
72                 dme2ProxyClientProperties(prefix)
73             }
74             RestLibConstants.TYPE_POLICY_MANAGER -> {
75                 policyManagerRestClientProperties(prefix)
76             }
77             else -> {
78                 throw BluePrintProcessorException("Rest adaptor($type) is" +
79                         " not supported")
80             }
81         }
82     }
83
84     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
85
86         val type = jsonNode.get("type").textValue()
87         return when (type) {
88             RestLibConstants.TYPE_TOKEN_AUTH -> {
89                 JacksonUtils.readValue(jsonNode,
90                         TokenAuthRestClientProperties::class.java)!!
91             }
92             RestLibConstants.TYPE_BASIC_AUTH -> {
93                 JacksonUtils.readValue(jsonNode,
94                         BasicAuthRestClientProperties::class.java)!!
95             }
96             RestLibConstants.TYPE_DME2_PROXY -> {
97                 JacksonUtils.readValue(jsonNode,
98                         DME2RestClientProperties::class.java)!!
99             }
100             RestLibConstants.TYPE_POLICY_MANAGER -> {
101                 JacksonUtils.readValue(jsonNode,
102                         PolicyManagerRestClientProperties::class.java)!!
103             }
104             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
105                 JacksonUtils.readValue(jsonNode,
106                         SSLBasicAuthRestClientProperties::class.java)!!
107             }
108             RestLibConstants.TYPE_SSL_TOKEN_AUTH -> {
109                 JacksonUtils.readValue(jsonNode,
110                         SSLTokenAuthRestClientProperties::class.java)!!
111             }
112             RestLibConstants.TYPE_SSL_NO_AUTH -> {
113                 JacksonUtils.readValue(
114                         jsonNode, SSLRestClientProperties::class.java)!!
115             }
116             else -> {
117                 throw BluePrintProcessorException("Rest adaptor($type) is" +
118                         " not supported")
119             }
120         }
121     }
122     
123     private fun blueprintWebClientService(
124             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 " +
142                         "service for")
143             }
144         }
145     }
146
147     private fun tokenRestClientProperties(prefix: String):
148             TokenAuthRestClientProperties {
149         return bluePrintProperties.propertyBeanType(
150                 prefix, TokenAuthRestClientProperties::class.java)
151     }
152
153     private fun basicAuthRestClientProperties(prefix: String):
154             BasicAuthRestClientProperties {
155         return bluePrintProperties.propertyBeanType(
156                 prefix, BasicAuthRestClientProperties::class.java)
157     }
158
159     private fun sslBasicAuthRestClientProperties(prefix: String):
160             SSLRestClientProperties {
161
162         val sslProps: SSLBasicAuthRestClientProperties =
163                 bluePrintProperties.propertyBeanType(
164                         prefix, SSLBasicAuthRestClientProperties::class.java)
165         val basicProps : BasicAuthRestClientProperties =
166                 bluePrintProperties.propertyBeanType(
167                         prefix, BasicAuthRestClientProperties::class.java)
168         sslProps.basicAuth = basicProps
169         return sslProps
170     }
171
172     private fun sslTokenAuthRestClientProperties(prefix: String):
173             SSLRestClientProperties {
174
175         val sslProps: SSLTokenAuthRestClientProperties =
176                 bluePrintProperties.propertyBeanType(prefix,
177                         SSLTokenAuthRestClientProperties::class.java)
178         val basicProps : TokenAuthRestClientProperties =
179                 bluePrintProperties.propertyBeanType(prefix,
180                         TokenAuthRestClientProperties::class.java)
181         sslProps.tokenAuth = basicProps
182         return sslProps
183     }
184
185     private fun sslNoAuthRestClientProperties(prefix: String):
186             SSLRestClientProperties {
187         return bluePrintProperties.propertyBeanType(
188                 prefix, SSLRestClientProperties::class.java)
189     }
190
191     private fun dme2ProxyClientProperties(prefix: String):
192             DME2RestClientProperties {
193         return bluePrintProperties.propertyBeanType(
194                 prefix, DME2RestClientProperties::class.java)
195     }
196
197     private fun policyManagerRestClientProperties(prefix: String):
198             PolicyManagerRestClientProperties {
199         return bluePrintProperties.propertyBeanType(
200                 prefix, PolicyManagerRestClientProperties::class.java)
201     }
202 }
203
204