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.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
28 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
29 open class BluePrintGrpcLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
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)!!
37 GRPCLibConstants.TYPE_TLS_AUTH -> {
38 JacksonUtils.readValue(jsonNode, TLSAuthGrpcServerProperties::class.java)!!
41 throw BluePrintProcessorException("Grpc type($type) not supported")
46 fun grpcServerProperties(prefix: String): GrpcServerProperties {
47 val type = bluePrintProperties.propertyBeanType(
48 "$prefix.type", String::class.java)
50 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
51 tokenAuthGrpcServerProperties(prefix)
53 GRPCLibConstants.TYPE_TLS_AUTH -> {
54 tlsAuthGrpcServerProperties(prefix)
57 throw BluePrintProcessorException("Grpc type($type) not supported")
62 private fun tokenAuthGrpcServerProperties(prefix: String): TokenAuthGrpcServerProperties {
63 return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcServerProperties::class.java)
66 private fun tlsAuthGrpcServerProperties(prefix: String): TLSAuthGrpcServerProperties {
67 return bluePrintProperties.propertyBeanType(prefix, TLSAuthGrpcServerProperties::class.java)
70 /** GRPC Client Lib Property Service */
72 fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
73 val restClientProperties = grpcClientProperties(jsonNode)
74 return blueprintGrpcClientService(restClientProperties)
77 fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
78 val prefix = "blueprintsprocessor.grpcclient.$selector"
79 val restClientProperties = grpcClientProperties(prefix)
80 return blueprintGrpcClientService(restClientProperties)
84 fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
85 val type = jsonNode.get("type").returnNullIfMissing()?.textValue()
86 ?: BluePrintProcessorException("missing type property")
88 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
89 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
91 GRPCLibConstants.TYPE_TLS_AUTH -> {
92 JacksonUtils.readValue(jsonNode, TLSAuthGrpcClientProperties::class.java)!!
94 GRPCLibConstants.TYPE_BASIC_AUTH -> {
95 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
98 throw BluePrintProcessorException("Grpc type($type) not supported")
103 fun grpcClientProperties(prefix: String): GrpcClientProperties {
104 val type = bluePrintProperties.propertyBeanType(
105 "$prefix.type", String::class.java)
107 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
108 tokenAuthGrpcClientProperties(prefix)
110 GRPCLibConstants.TYPE_TLS_AUTH -> {
111 tlsAuthGrpcClientProperties(prefix)
113 GRPCLibConstants.TYPE_BASIC_AUTH -> {
114 basicAuthGrpcClientProperties(prefix)
117 throw BluePrintProcessorException("Grpc type($type) not supported")
123 fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
124 BluePrintGrpcClientService {
125 return when (grpcClientProperties) {
126 is TokenAuthGrpcClientProperties -> {
127 TokenAuthGrpcClientService(grpcClientProperties)
129 is TLSAuthGrpcClientProperties -> {
130 TLSAuthGrpcClientService(grpcClientProperties)
132 is BasicAuthGrpcClientProperties -> {
133 BasicAuthGrpcClientService(grpcClientProperties)
136 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
141 private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
142 return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
145 private fun tlsAuthGrpcClientProperties(prefix: String): TLSAuthGrpcClientProperties {
146 return bluePrintProperties.propertyBeanType(prefix, TLSAuthGrpcClientProperties::class.java)
149 private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
150 return bluePrintProperties.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)