Property based GRPC server service.
[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.BluePrintPropertiesService
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 bluePrintPropertiesService: BluePrintPropertiesService) {
30
31     fun blueprintGrpcServerService(jsonNode: JsonNode): BluePrintGrpcServerService {
32         val grpcServerProperties = grpcServerProperties(jsonNode)
33         return blueprintGrpcServerService(grpcServerProperties)
34     }
35
36     fun blueprintGrpcServerService(selector: String): BluePrintGrpcServerService {
37         val prefix = "${GRPCLibConstants.PROPERTY_GRPC_SERVER_PREFIX}$selector"
38         val grpcServerProperties = grpcServerProperties(prefix)
39         return blueprintGrpcServerService(grpcServerProperties)
40     }
41
42
43     /** GRPC Server Lib Property Service */
44     fun grpcServerProperties(jsonNode: JsonNode): GrpcServerProperties {
45         return when (val type = jsonNode.get("type").textValue()) {
46             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
47                 JacksonUtils.readValue(jsonNode, TokenAuthGrpcServerProperties::class.java)!!
48             }
49             GRPCLibConstants.TYPE_TLS_AUTH -> {
50                 JacksonUtils.readValue(jsonNode, TLSAuthGrpcServerProperties::class.java)!!
51             }
52             else -> {
53                 throw BluePrintProcessorException("Grpc type($type) not supported")
54             }
55         }
56     }
57
58     fun grpcServerProperties(prefix: String): GrpcServerProperties {
59         val type = bluePrintPropertiesService.propertyBeanType(
60                 "$prefix.type", String::class.java)
61         return when (type) {
62             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
63                 tokenAuthGrpcServerProperties(prefix)
64             }
65             GRPCLibConstants.TYPE_TLS_AUTH -> {
66                 tlsAuthGrpcServerProperties(prefix)
67             }
68             else -> {
69                 throw BluePrintProcessorException("Grpc type($type) not supported")
70             }
71         }
72     }
73
74     private fun tokenAuthGrpcServerProperties(prefix: String): TokenAuthGrpcServerProperties {
75         return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcServerProperties::class.java)
76     }
77
78     private fun tlsAuthGrpcServerProperties(prefix: String): TLSAuthGrpcServerProperties {
79         return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcServerProperties::class.java)
80     }
81
82     private fun blueprintGrpcServerService(grpcServerProperties: GrpcServerProperties)
83             : BluePrintGrpcServerService {
84         when (grpcServerProperties) {
85             is TLSAuthGrpcServerProperties -> {
86                 return TLSAuthGrpcServerService(grpcServerProperties)
87             }
88             else -> {
89                 throw BluePrintProcessorException("couldn't get grpc client service for properties $grpcServerProperties")
90             }
91         }
92     }
93
94
95     /** GRPC Client Lib Property Service */
96
97     fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
98         val restClientProperties = grpcClientProperties(jsonNode)
99         return blueprintGrpcClientService(restClientProperties)
100     }
101
102     fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
103         val prefix = "${GRPCLibConstants.PROPERTY_GRPC_CLIENT_PREFIX}$selector"
104         val restClientProperties = grpcClientProperties(prefix)
105         return blueprintGrpcClientService(restClientProperties)
106     }
107
108
109     fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
110         val type = jsonNode.get("type").returnNullIfMissing()?.textValue()
111                 ?: BluePrintProcessorException("missing type property")
112         return when (type) {
113             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
114                 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
115             }
116             GRPCLibConstants.TYPE_TLS_AUTH -> {
117                 JacksonUtils.readValue(jsonNode, TLSAuthGrpcClientProperties::class.java)!!
118             }
119             GRPCLibConstants.TYPE_BASIC_AUTH -> {
120                 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
121             }
122             else -> {
123                 throw BluePrintProcessorException("Grpc type($type) not supported")
124             }
125         }
126     }
127
128     fun grpcClientProperties(prefix: String): GrpcClientProperties {
129         val type = bluePrintPropertiesService.propertyBeanType(
130                 "$prefix.type", String::class.java)
131         return when (type) {
132             GRPCLibConstants.TYPE_TOKEN_AUTH -> {
133                 tokenAuthGrpcClientProperties(prefix)
134             }
135             GRPCLibConstants.TYPE_TLS_AUTH -> {
136                 tlsAuthGrpcClientProperties(prefix)
137             }
138             GRPCLibConstants.TYPE_BASIC_AUTH -> {
139                 basicAuthGrpcClientProperties(prefix)
140             }
141             else -> {
142                 throw BluePrintProcessorException("Grpc type($type) not supported")
143
144             }
145         }
146     }
147
148     fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
149             BluePrintGrpcClientService {
150         return when (grpcClientProperties) {
151             is TokenAuthGrpcClientProperties -> {
152                 TokenAuthGrpcClientService(grpcClientProperties)
153             }
154             is TLSAuthGrpcClientProperties -> {
155                 TLSAuthGrpcClientService(grpcClientProperties)
156             }
157             is BasicAuthGrpcClientProperties -> {
158                 BasicAuthGrpcClientService(grpcClientProperties)
159             }
160             else -> {
161                 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
162             }
163         }
164     }
165
166     private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
167         return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
168     }
169
170     private fun tlsAuthGrpcClientProperties(prefix: String): TLSAuthGrpcClientProperties {
171         return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcClientProperties::class.java)
172     }
173
174     private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
175         return bluePrintPropertiesService.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)
176     }
177 }