2 * Copyright © 2019 IBM.
3 * Modifications Copyright © 2018-2019 AT&T Intellectual Property.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
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
28 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
29 open class BluePrintGrpcLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
31 fun blueprintGrpcServerService(jsonNode: JsonNode): BluePrintGrpcServerService {
32 val grpcServerProperties = grpcServerProperties(jsonNode)
33 return blueprintGrpcServerService(grpcServerProperties)
36 fun blueprintGrpcServerService(selector: String): BluePrintGrpcServerService {
37 val prefix = "${GRPCLibConstants.PROPERTY_GRPC_SERVER_PREFIX}$selector"
38 val grpcServerProperties = grpcServerProperties(prefix)
39 return blueprintGrpcServerService(grpcServerProperties)
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)!!
49 GRPCLibConstants.TYPE_TLS_AUTH -> {
50 JacksonUtils.readValue(jsonNode, TLSAuthGrpcServerProperties::class.java)!!
53 throw BluePrintProcessorException("Grpc type($type) not supported")
58 fun grpcServerProperties(prefix: String): GrpcServerProperties {
59 val type = bluePrintPropertiesService.propertyBeanType(
60 "$prefix.type", String::class.java)
62 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
63 tokenAuthGrpcServerProperties(prefix)
65 GRPCLibConstants.TYPE_TLS_AUTH -> {
66 tlsAuthGrpcServerProperties(prefix)
69 throw BluePrintProcessorException("Grpc type($type) not supported")
74 private fun tokenAuthGrpcServerProperties(prefix: String): TokenAuthGrpcServerProperties {
75 return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcServerProperties::class.java)
78 private fun tlsAuthGrpcServerProperties(prefix: String): TLSAuthGrpcServerProperties {
79 return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcServerProperties::class.java)
82 private fun blueprintGrpcServerService(grpcServerProperties: GrpcServerProperties)
83 : BluePrintGrpcServerService {
84 when (grpcServerProperties) {
85 is TLSAuthGrpcServerProperties -> {
86 return TLSAuthGrpcServerService(grpcServerProperties)
89 throw BluePrintProcessorException("couldn't get grpc client service for properties $grpcServerProperties")
95 /** GRPC Client Lib Property Service */
97 fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
98 val restClientProperties = grpcClientProperties(jsonNode)
99 return blueprintGrpcClientService(restClientProperties)
102 fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
103 val prefix = "${GRPCLibConstants.PROPERTY_GRPC_CLIENT_PREFIX}$selector"
104 val restClientProperties = grpcClientProperties(prefix)
105 return blueprintGrpcClientService(restClientProperties)
109 fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
110 val type = jsonNode.get("type").returnNullIfMissing()?.textValue()
111 ?: BluePrintProcessorException("missing type property")
113 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
114 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
116 GRPCLibConstants.TYPE_TLS_AUTH -> {
117 JacksonUtils.readValue(jsonNode, TLSAuthGrpcClientProperties::class.java)!!
119 GRPCLibConstants.TYPE_BASIC_AUTH -> {
120 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
123 throw BluePrintProcessorException("Grpc type($type) not supported")
128 fun grpcClientProperties(prefix: String): GrpcClientProperties {
129 val type = bluePrintPropertiesService.propertyBeanType(
130 "$prefix.type", String::class.java)
132 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
133 tokenAuthGrpcClientProperties(prefix)
135 GRPCLibConstants.TYPE_TLS_AUTH -> {
136 tlsAuthGrpcClientProperties(prefix)
138 GRPCLibConstants.TYPE_BASIC_AUTH -> {
139 basicAuthGrpcClientProperties(prefix)
142 throw BluePrintProcessorException("Grpc type($type) not supported")
148 fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
149 BluePrintGrpcClientService {
150 return when (grpcClientProperties) {
151 is TokenAuthGrpcClientProperties -> {
152 TokenAuthGrpcClientService(grpcClientProperties)
154 is TLSAuthGrpcClientProperties -> {
155 TLSAuthGrpcClientService(grpcClientProperties)
157 is BasicAuthGrpcClientProperties -> {
158 BasicAuthGrpcClientService(grpcClientProperties)
161 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
166 private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
167 return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
170 private fun tlsAuthGrpcClientProperties(prefix: String): TLSAuthGrpcClientProperties {
171 return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcClientProperties::class.java)
174 private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
175 return bluePrintPropertiesService.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)