088533a714be039052479ccad405a4dbbfb93c1b
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / BluePrintGrpcLibPropertyService.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
21 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.BasicAuthGrpcClientProperties
22 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
23 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GrpcClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.springframework.stereotype.Service
28
29 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
30 open class BluePrintGrpcLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
31
32     fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
33         val restClientProperties = grpcClientProperties(jsonNode)
34         return blueprintGrpcClientService(restClientProperties)
35     }
36
37     fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
38         val prefix = "blueprintsprocessor.grpcclient.$selector"
39         val restClientProperties = grpcClientProperties(prefix)
40         return blueprintGrpcClientService(restClientProperties)
41     }
42
43
44     fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
45         val type = jsonNode.get("type").textValue()
46         return when (type) {
47             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
48                 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
49             }
50             GRPCLibConstants.TYPE_BASIC_AUTH -> {
51                 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
52             }
53             else -> {
54                 throw BluePrintProcessorException("Grpc type($type) not supported")
55             }
56         }
57     }
58
59     fun grpcClientProperties(prefix: String): GrpcClientProperties {
60         val type = bluePrintProperties.propertyBeanType(
61                 "$prefix.type", String::class.java)
62         return when (type) {
63             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
64                 tokenAuthGrpcClientProperties(prefix)
65             }
66             GRPCLibConstants.TYPE_BASIC_AUTH -> {
67                 basicAuthGrpcClientProperties(prefix)
68             }
69             else -> {
70                 throw BluePrintProcessorException("Grpc type($type) not supported")
71
72             }
73         }
74     }
75
76     private fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
77             BluePrintGrpcClientService {
78         when (grpcClientProperties) {
79             is TokenAuthGrpcClientProperties -> {
80                 return TokenAuthGrpcClientService(grpcClientProperties)
81             }
82             is BasicAuthGrpcClientProperties -> {
83                 return BasicAuthGrpcClientService(grpcClientProperties)
84             }
85             else -> {
86                 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
87             }
88         }
89     }
90
91     private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
92         return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
93     }
94
95     private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
96         return bluePrintProperties.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)
97     }
98 }