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