Migrate "ms/controllerblueprints" from ccsdk/apps
[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  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.DME2RestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.PolicyManagerRestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.springframework.stereotype.Service
32
33 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
34 open class BluePrintRestLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
35
36     fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
37         val restClientProperties = restClientProperties(jsonNode)
38         return blueprintWebClientService(restClientProperties)
39     }
40
41     fun blueprintWebClientService(selector: String): BlueprintWebClientService {
42         val prefix = "blueprintsprocessor.restclient.$selector"
43         val restClientProperties = restClientProperties(prefix)
44         return blueprintWebClientService(restClientProperties)
45     }
46
47     fun restClientProperties(prefix: String): RestClientProperties {
48         val type = bluePrintProperties.propertyBeanType("$prefix.type", String::class.java)
49         return when (type) {
50             RestLibConstants.TYPE_BASIC_AUTH -> {
51                 basicAuthRestClientProperties(prefix)
52             }
53             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
54                 sslBasicAuthRestClientProperties(prefix)
55             }
56             RestLibConstants.TYPE_DME2_PROXY -> {
57                 dme2ProxyClientProperties(prefix)
58             }
59             RestLibConstants.TYPE_POLICY_MANAGER -> {
60                 policyManagerRestClientProperties(prefix)
61             }
62             else -> {
63                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
64             }
65         }
66     }
67
68     private fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
69         val type = jsonNode.get("type").textValue()
70         return when (type) {
71             RestLibConstants.TYPE_TOKEN_AUTH -> {
72                 JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!!
73             }
74             RestLibConstants.TYPE_BASIC_AUTH -> {
75                 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
76             }
77             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
78                 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
79             }
80             RestLibConstants.TYPE_DME2_PROXY -> {
81                 JacksonUtils.readValue(jsonNode, DME2RestClientProperties::class.java)!!
82             }
83             RestLibConstants.TYPE_POLICY_MANAGER -> {
84                 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
85             }
86             else -> {
87                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
88             }
89         }
90     }
91
92     private fun blueprintWebClientService(restClientProperties: RestClientProperties): BlueprintWebClientService {
93         when (restClientProperties) {
94             is TokenAuthRestClientProperties -> {
95                 return TokenAuthRestClientService(restClientProperties)
96             }
97             is BasicAuthRestClientProperties -> {
98                 return BasicAuthRestClientService(restClientProperties)
99             }
100             is SSLBasicAuthRestClientProperties -> {
101                 return SSLBasicAuthRestClientService(restClientProperties)
102             }
103             is DME2RestClientProperties -> {
104                 return DME2ProxyRestClientService(restClientProperties)
105             }
106             else -> {
107                 throw BluePrintProcessorException("couldn't get rest service for")
108             }
109         }
110     }
111
112     private fun basicAuthRestClientProperties(prefix: String): BasicAuthRestClientProperties {
113         return bluePrintProperties.propertyBeanType(prefix, BasicAuthRestClientProperties::class.java)
114     }
115
116     private fun sslBasicAuthRestClientProperties(prefix: String): SSLBasicAuthRestClientProperties {
117         return bluePrintProperties.propertyBeanType(prefix, SSLBasicAuthRestClientProperties::class.java)
118     }
119
120     private fun dme2ProxyClientProperties(prefix: String): DME2RestClientProperties {
121         return bluePrintProperties.propertyBeanType(prefix, DME2RestClientProperties::class.java)
122     }
123
124     private fun policyManagerRestClientProperties(prefix: String): PolicyManagerRestClientProperties {
125         return bluePrintProperties.propertyBeanType(prefix, PolicyManagerRestClientProperties::class.java)
126     }
127 }
128
129