Convert component functions IT to UT.
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / BluePrintMessageLibPropertyService.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
21 import org.onap.ccsdk.cds.blueprintsprocessor.message.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
24 import org.springframework.stereotype.Service
25
26 @Service(MessageLibConstants.SERVICE_BLUEPRINT_MESSAGE_LIB_PROPERTY)
27 open class BluePrintMessageLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
28
29     fun blueprintMessageProducerService(jsonNode: JsonNode): BlueprintMessageProducerService {
30         val messageClientProperties = messageProducerProperties(jsonNode)
31         return blueprintMessageProducerService(messageClientProperties)
32     }
33
34     fun blueprintMessageProducerService(selector: String): BlueprintMessageProducerService {
35         val prefix = "${MessageLibConstants.PROPERTY_MESSAGE_PRODUCER_PREFIX}$selector"
36         val messageClientProperties = messageProducerProperties(prefix)
37         return blueprintMessageProducerService(messageClientProperties)
38     }
39
40     fun messageProducerProperties(prefix: String): MessageProducerProperties {
41         val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
42         return when (type) {
43             MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
44                 kafkaBasicAuthMessageProducerProperties(prefix)
45             }
46             else -> {
47                 throw BluePrintProcessorException("Message adaptor($type) is not supported")
48             }
49         }
50     }
51
52     fun messageProducerProperties(jsonNode: JsonNode): MessageProducerProperties {
53         val type = jsonNode.get("type").textValue()
54         return when (type) {
55             MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
56                 JacksonUtils.readValue(jsonNode, KafkaBasicAuthMessageProducerProperties::class.java)!!
57             }
58             else -> {
59                 throw BluePrintProcessorException("Message adaptor($type) is not supported")
60             }
61         }
62     }
63
64     private fun blueprintMessageProducerService(MessageProducerProperties: MessageProducerProperties)
65             : BlueprintMessageProducerService {
66
67         when (MessageProducerProperties) {
68             is KafkaBasicAuthMessageProducerProperties -> {
69                 return KafkaBasicAuthMessageProducerService(MessageProducerProperties)
70             }
71             else -> {
72                 throw BluePrintProcessorException("couldn't get Message client service for")
73             }
74         }
75     }
76
77     private fun kafkaBasicAuthMessageProducerProperties(prefix: String): KafkaBasicAuthMessageProducerProperties {
78         return bluePrintPropertiesService.propertyBeanType(
79                 prefix, KafkaBasicAuthMessageProducerProperties::class.java)
80     }
81
82     /** Consumer Property Lib Service Implementation **/
83
84     /** Return Message Consumer Service for [jsonNode] definitions. */
85     fun blueprintMessageConsumerService(jsonNode: JsonNode): BlueprintMessageConsumerService {
86         val messageConsumerProperties = messageConsumerProperties(jsonNode)
87         return blueprintMessageConsumerService(messageConsumerProperties)
88     }
89
90     /** Return Message Consumer Service for [selector] definitions. */
91     fun blueprintMessageConsumerService(selector: String): BlueprintMessageConsumerService {
92         val prefix = "${MessageLibConstants.PROPERTY_MESSAGE_CONSUMER_PREFIX}$selector"
93         val messageClientProperties = messageConsumerProperties(prefix)
94         return blueprintMessageConsumerService(messageClientProperties)
95     }
96
97     /** Return Message Consumer Properties for [prefix] definitions. */
98     fun messageConsumerProperties(prefix: String): MessageConsumerProperties {
99         val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
100         return when (type) {
101             MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
102                 kafkaBasicAuthMessageConsumerProperties(prefix)
103             }
104             else -> {
105                 throw BluePrintProcessorException("Message adaptor($type) is not supported")
106             }
107         }
108     }
109
110     fun messageConsumerProperties(jsonNode: JsonNode): MessageConsumerProperties {
111         val type = jsonNode.get("type").textValue()
112         return when (type) {
113             MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
114                 JacksonUtils.readValue(jsonNode, KafkaBasicAuthMessageConsumerProperties::class.java)!!
115             }
116             else -> {
117                 throw BluePrintProcessorException("Message adaptor($type) is not supported")
118             }
119         }
120     }
121
122     private fun blueprintMessageConsumerService(messageConsumerProperties: MessageConsumerProperties)
123             : BlueprintMessageConsumerService {
124
125         when (messageConsumerProperties) {
126             is KafkaBasicAuthMessageConsumerProperties -> {
127                 return KafkaBasicAuthMessageConsumerService(messageConsumerProperties)
128             }
129             else -> {
130                 throw BluePrintProcessorException("couldn't get Message client service for")
131             }
132         }
133     }
134
135     private fun kafkaBasicAuthMessageConsumerProperties(prefix: String): KafkaBasicAuthMessageConsumerProperties {
136         return bluePrintPropertiesService.propertyBeanType(
137                 prefix, KafkaBasicAuthMessageConsumerProperties::class.java)
138     }
139
140 }