705caa2e28182f739dd1fc079004bb2ef3d21542
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.blueprintsprocessor.rest.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties
22 import org.onap.ccsdk.apps.blueprintsprocessor.rest.*
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
25 import org.springframework.stereotype.Service
26
27 @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
28 open class BluePrintRestLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
29
30     fun restClientProperties(prefix: String): RestClientProperties {
31         val type = bluePrintProperties.propertyBeanType("$prefix.type", String::class.java)
32         return when (type) {
33             RestLibConstants.TYPE_BASIC_AUTH -> {
34                 basicAuthRestClientProperties(prefix)
35             }
36             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
37                 sslBasicAuthRestClientProperties(prefix)
38             }
39             RestLibConstants.TYPE_DME2_PROXY -> {
40                 dme2ProxyClientProperties(prefix)
41             }
42             RestLibConstants.TYPE_POLICY_MANAGER -> {
43                 policyManagerRestClientProperties(prefix)
44             }
45             else -> {
46                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
47             }
48         }
49     }
50
51     fun restClientProperties(jsonNode: JsonNode): RestClientProperties {
52         val type = jsonNode.get("type").textValue()
53         return when (type) {
54             RestLibConstants.TYPE_BASIC_AUTH -> {
55                 JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!!
56             }
57             RestLibConstants.TYPE_SSL_BASIC_AUTH -> {
58                 JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!!
59             }
60             RestLibConstants.TYPE_DME2_PROXY -> {
61                 JacksonUtils.readValue(jsonNode, DME2RestClientProperties::class.java)!!
62             }
63             RestLibConstants.TYPE_POLICY_MANAGER -> {
64                 JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!!
65             }
66             else -> {
67                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
68             }
69         }
70     }
71
72
73     fun blueprintWebClientService(selector: String): BlueprintWebClientService {
74         val prefix = "blueprintsprocessor.restclient.$selector"
75         val restClientProperties = restClientProperties(prefix)
76         return blueprintWebClientService(restClientProperties)
77     }
78
79     fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService {
80         val restClientProperties = restClientProperties(jsonNode)
81         return blueprintWebClientService(restClientProperties)
82     }
83
84     fun blueprintWebClientService(restClientProperties: RestClientProperties): BlueprintWebClientService {
85         when (restClientProperties) {
86             is BasicAuthRestClientProperties -> {
87                 return BasicAuthRestClientService(restClientProperties)
88             }
89             is SSLBasicAuthRestClientProperties -> {
90                 return SSLBasicAuthRestClientService(restClientProperties)
91             }
92             is DME2RestClientProperties -> {
93                 return DME2ProxyRestClientService(restClientProperties)
94             }
95             else -> {
96                 throw BluePrintProcessorException("couldn't get rest service for")
97             }
98         }
99     }
100
101     fun basicAuthRestClientProperties(prefix: String): BasicAuthRestClientProperties {
102         return bluePrintProperties.propertyBeanType(prefix, BasicAuthRestClientProperties::class.java)
103     }
104
105     fun sslBasicAuthRestClientProperties(prefix: String): SSLBasicAuthRestClientProperties {
106         return bluePrintProperties.propertyBeanType(prefix, SSLBasicAuthRestClientProperties::class.java)
107     }
108
109     fun dme2ProxyClientProperties(prefix: String): DME2RestClientProperties {
110         return bluePrintProperties.propertyBeanType(prefix, DME2RestClientProperties::class.java)
111     }
112
113     fun policyManagerRestClientProperties(prefix: String): PolicyManagerRestClientProperties {
114         return bluePrintProperties.propertyBeanType(prefix, PolicyManagerRestClientProperties::class.java)
115     }
116 }
117
118