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.BasicAuthGrpcClientProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
24 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GrpcClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GrpcServerProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcClientProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcServerProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcServerProperties
30 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.springframework.stereotype.Service
35 @Service(GRPCLibConstants.SERVICE_BLUEPRINT_GRPC_LIB_PROPERTY)
36 open class BlueprintGrpcLibPropertyService(private var bluePrintPropertiesService: BlueprintPropertiesService) {
38 fun blueprintGrpcServerService(jsonNode: JsonNode): BlueprintGrpcServerService {
39 val grpcServerProperties = grpcServerProperties(jsonNode)
40 return blueprintGrpcServerService(grpcServerProperties)
43 fun blueprintGrpcServerService(selector: String): BlueprintGrpcServerService {
44 val prefix = "${GRPCLibConstants.PROPERTY_GRPC_SERVER_PREFIX}$selector"
45 val grpcServerProperties = grpcServerProperties(prefix)
46 return blueprintGrpcServerService(grpcServerProperties)
49 /** GRPC Server Lib Property Service */
50 fun grpcServerProperties(jsonNode: JsonNode): GrpcServerProperties {
51 return when (val type = jsonNode.get("type").textValue()) {
52 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
53 JacksonUtils.readValue(jsonNode, TokenAuthGrpcServerProperties::class.java)!!
55 GRPCLibConstants.TYPE_TLS_AUTH -> {
56 JacksonUtils.readValue(jsonNode, TLSAuthGrpcServerProperties::class.java)!!
59 throw BlueprintProcessorException("Grpc type($type) not supported")
64 fun grpcServerProperties(prefix: String): GrpcServerProperties {
65 val type = bluePrintPropertiesService.propertyBeanType(
66 "$prefix.type", String::class.java
69 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
70 tokenAuthGrpcServerProperties(prefix)
72 GRPCLibConstants.TYPE_TLS_AUTH -> {
73 tlsAuthGrpcServerProperties(prefix)
76 throw BlueprintProcessorException("Grpc type($type) not supported")
81 private fun tokenAuthGrpcServerProperties(prefix: String): TokenAuthGrpcServerProperties {
82 return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcServerProperties::class.java)
85 private fun tlsAuthGrpcServerProperties(prefix: String): TLSAuthGrpcServerProperties {
86 return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcServerProperties::class.java)
89 private fun blueprintGrpcServerService(grpcServerProperties: GrpcServerProperties):
90 BlueprintGrpcServerService {
91 when (grpcServerProperties) {
92 is TLSAuthGrpcServerProperties -> {
93 return TLSAuthGrpcServerService(grpcServerProperties)
96 throw BlueprintProcessorException("couldn't get grpc client service for properties $grpcServerProperties")
101 /** GRPC Client Lib Property Service */
103 fun blueprintGrpcClientService(jsonNode: JsonNode): BlueprintGrpcClientService {
104 val restClientProperties = grpcClientProperties(jsonNode)
105 return blueprintGrpcClientService(restClientProperties)
108 fun blueprintGrpcClientService(selector: String): BlueprintGrpcClientService {
109 val prefix = "${GRPCLibConstants.PROPERTY_GRPC_CLIENT_PREFIX}$selector"
110 val restClientProperties = grpcClientProperties(prefix)
111 return blueprintGrpcClientService(restClientProperties)
114 fun grpcClientProperties(jsonNode: JsonNode): GrpcClientProperties {
115 val type = jsonNode.get("type").returnNullIfMissing()?.textValue()
116 ?: BlueprintProcessorException("missing type property")
118 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
119 JacksonUtils.readValue(jsonNode, TokenAuthGrpcClientProperties::class.java)!!
121 GRPCLibConstants.TYPE_TLS_AUTH -> {
122 JacksonUtils.readValue(jsonNode, TLSAuthGrpcClientProperties::class.java)!!
124 GRPCLibConstants.TYPE_BASIC_AUTH -> {
125 JacksonUtils.readValue(jsonNode, BasicAuthGrpcClientProperties::class.java)!!
128 throw BlueprintProcessorException("Grpc type($type) not supported")
133 fun grpcClientProperties(prefix: String): GrpcClientProperties {
134 val type = bluePrintPropertiesService.propertyBeanType(
135 "$prefix.type", String::class.java
138 GRPCLibConstants.TYPE_TOKEN_AUTH -> {
139 tokenAuthGrpcClientProperties(prefix)
141 GRPCLibConstants.TYPE_TLS_AUTH -> {
142 tlsAuthGrpcClientProperties(prefix)
144 GRPCLibConstants.TYPE_BASIC_AUTH -> {
145 basicAuthGrpcClientProperties(prefix)
148 throw BlueprintProcessorException("Grpc type($type) not supported")
153 fun blueprintGrpcClientService(grpcClientProperties: GrpcClientProperties):
154 BlueprintGrpcClientService {
155 return when (grpcClientProperties) {
156 is TokenAuthGrpcClientProperties -> {
157 TokenAuthGrpcClientService(grpcClientProperties)
159 is TLSAuthGrpcClientProperties -> {
160 TLSAuthGrpcClientService(grpcClientProperties)
162 is BasicAuthGrpcClientProperties -> {
163 BasicAuthGrpcClientService(grpcClientProperties)
166 throw BlueprintProcessorException("couldn't get grpc service for type(${grpcClientProperties.type})")
171 private fun tokenAuthGrpcClientProperties(prefix: String): TokenAuthGrpcClientProperties {
172 return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthGrpcClientProperties::class.java)
175 private fun tlsAuthGrpcClientProperties(prefix: String): TLSAuthGrpcClientProperties {
176 return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthGrpcClientProperties::class.java)
179 private fun basicAuthGrpcClientProperties(prefix: String): BasicAuthGrpcClientProperties {
180 return bluePrintPropertiesService.propertyBeanType(prefix, BasicAuthGrpcClientProperties::class.java)