2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
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
29 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
30 open class BluePrintGrpcLibPropertyService(private var bluePrintProperties: BluePrintProperties) {
32 fun blueprintGrpcClientService(jsonNode: JsonNode): BluePrintGrpcClientService {
33 val restClientProperties = grpcClientProperties(jsonNode)
34 return blueprintGrpcClientService(restClientProperties)
37 fun blueprintGrpcClientService(selector: String): BluePrintGrpcClientService {
38 val prefix = "blueprintsprocessor.grpcclient.$selector"
39 val restClientProperties = grpcClientProperties(prefix)
40 return blueprintGrpcClientService(restClientProperties)
44 fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
45 val type = jsonNode.get("type").textValue()
47 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
48 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
50 GRPCLibConstants.TYPE_BASIC_AUTH -> {
51 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
54 throw BluePrintProcessorException("Grpc type($type) not supported")
59 fun grpcClientProperties(prefix: String): GrpcClientProperties {
60 val type = bluePrintProperties.propertyBeanType(
61 "$prefix.type", String::class.java)
63 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
64 tokenAuthGrpcClientProperties(prefix)
66 GRPCLibConstants.TYPE_BASIC_AUTH -> {
67 basicAuthGrpcClientProperties(prefix)
70 throw BluePrintProcessorException("Grpc type($type) not supported")
76 private fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
77 BluePrintGrpcClientService {
78 when (grpcClientProperties) {
79 is TokenAuthGrpcClientProperties -> {
80 return TokenAuthGrpcClientService(grpcClientProperties)
82 is BasicAuthGrpcClientProperties -> {
83 return BasicAuthGrpcClientService(grpcClientProperties)
86 throw BluePrintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
91 private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
92 return bluePrintProperties.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
95 private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
96 return bluePrintProperties.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)