f4933a3adaa8fad9c5359a865adc46841c37c001
[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  *  Modifications Copyright © 2018-2019 AT&T Intellectual Property.
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.grpc.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.grpc.*
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
26 import org.springframework.stereotype.Service
27
28 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
29 open class BluePrintGrpcLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
30
31     /** GRPC Server Lib Property Service */
32     fun grpcServerProperties(jsonNode: JsonNode): GrpcServerProperties {
33         return when (val type = jsonNode.get("type").textValue()) {
34             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
35                 JacksonUtils.readValue(jsonNode, TokenAuthGrpcServerProperties::class.java)!!
36             }
37             GRPCLibConstants.TYPE_TLS_AUTH -> {
38                 JacksonUtils.readValue(jsonNode, TLSAuthGrpcServerProperties::class.java)!!
39             }
40             else -> {
41                 throw BluePrintProcessorException("Grpc type($type) not supported")
42             }
43         }
44     }
45
46     fun grpcServerProperties(prefix: String): GrpcServerProperties {
47         val type = bluePrintProperties.propertyBeanType(
48                 "$prefix.type", String::class.java)
49         return when (type) {
50             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
51                 tokenAuthGrpcServerProperties(prefix)
52             }
53             GRPCLibConstants.TYPE_TLS_AUTH -> {
54                 tlsAuthGrpcServerProperties(prefix)
55             }
56             else -> {
57                 throw BluePrintProcessorException("Grpc type($type) not supported")
58             }
59         }
60     }
61
62     private fun tokenAuthGrpcServerProperties(prefix: String): TokenAuthGrpcServerProperties {
63         return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcServerProperties::class.java)
64     }
65
66     private fun tlsAuthGrpcServerProperties(prefix: String): TLSAuthGrpcServerProperties {
67         return bluePrintProperties.propertyBeanType(prefix, TLSAuthGrpcServerProperties::class.java)
68     }
69
70     /** GRPC Client Lib Property Service */
71
72     fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
73         val restClientProperties = grpcClientProperties(jsonNode)
74         return blueprintGrpcClientService(restClientProperties)
75     }
76
77     fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
78         val prefix = "blueprintsprocessor.grpcclient.$selector"
79         val restClientProperties = grpcClientProperties(prefix)
80         return blueprintGrpcClientService(restClientProperties)
81     }
82
83
84     fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
85         val type = jsonNode.get("type").returnNullIfMissing()?.textValue()
86                 ?: BluePrintProcessorException("missing type property")
87         return when (type) {
88             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
89                 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
90             }
91             GRPCLibConstants.TYPE_TLS_AUTH -> {
92                 JacksonUtils.readValue(jsonNode, TLSAuthGrpcClientProperties::class.java)!!
93             }
94             GRPCLibConstants.TYPE_BASIC_AUTH -> {
95                 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
96             }
97             else -> {
98                 throw BluePrintProcessorException("Grpc type($type) not supported")
99             }
100         }
101     }
102
103     fun grpcClientProperties(prefix: String): GrpcClientProperties {
104         val type = bluePrintProperties.propertyBeanType(
105                 "$prefix.type", String::class.java)
106         return when (type) {
107             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
108                 tokenAuthGrpcClientProperties(prefix)
109             }
110             GRPCLibConstants.TYPE_TLS_AUTH -> {
111                 tlsAuthGrpcClientProperties(prefix)
112             }
113             GRPCLibConstants.TYPE_BASIC_AUTH -> {
114                 basicAuthGrpcClientProperties(prefix)
115             }
116             else -> {
117                 throw BluePrintProcessorException("Grpc type($type) not supported")
118
119             }
120         }
121     }
122
123     fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
124             BluePrintGrpcClientService {
125         return when (grpcClientProperties) {
126             is TokenAuthGrpcClientProperties -> {
127                 TokenAuthGrpcClientService(grpcClientProperties)
128             }
129             is TLSAuthGrpcClientProperties -> {
130                 TLSAuthGrpcClientService(grpcClientProperties)
131             }
132             is BasicAuthGrpcClientProperties -> {
133                 BasicAuthGrpcClientService(grpcClientProperties)
134             }
135             else -> {
136                 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
137             }
138         }
139     }
140
141     private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
142         return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
143     }
144
145     private fun tlsAuthGrpcClientProperties(prefix: String): TLSAuthGrpcClientProperties {
146         return bluePrintProperties.propertyBeanType(prefix, TLSAuthGrpcClientProperties::class.java)
147     }
148
149     private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
150         return bluePrintProperties.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)
151     }
152 }