aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/commons/message-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/message/service/BlueprintMessageLibPropertyService.kt
blob: cb0bc322569346352a1ae2d1bf0394db53a0b35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *  Copyright © 2019 IBM.
 *  Modifications Copyright © 2018-2021 AT&T, Bell Canada Intellectual Property
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.message.service

import com.fasterxml.jackson.databind.JsonNode
import io.micrometer.core.instrument.MeterRegistry
import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaBasicAuthMessageConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaBasicAuthMessageProducerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaScramSslAuthMessageConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaScramSslAuthMessageProducerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaSslAuthMessageConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaSslAuthMessageProducerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsScramSslAuthConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsSslAuthConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageLibConstants
import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageProducerProperties
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.springframework.stereotype.Service

@Service(MessageLibConstants.SERVICE_BLUEPRINT_MESSAGE_LIB_PROPERTY)
open class BlueprintMessageLibPropertyService(
    private var bluePrintPropertiesService: BlueprintPropertiesService,
    private val meterRegistry: MeterRegistry
) {

    fun blueprintMessageProducerService(jsonNode: JsonNode): BlueprintMessageProducerService {
        val messageClientProperties = messageProducerProperties(jsonNode)
        return KafkaMessageProducerService(messageClientProperties, meterRegistry)
    }

    fun blueprintMessageProducerService(selector: String): BlueprintMessageProducerService {
        val prefix = "${MessageLibConstants.PROPERTY_MESSAGE_PRODUCER_PREFIX}$selector"
        val messageClientProperties = messageProducerProperties(prefix)
        return KafkaMessageProducerService(messageClientProperties, meterRegistry)
    }

    fun messageProducerProperties(prefix: String): MessageProducerProperties {
        val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
        return when (type) {
            MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaBasicAuthMessageProducerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaSslAuthMessageProducerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_SCRAM_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaScramSslAuthMessageProducerProperties::class.java
                )
            }
            else -> {
                throw BlueprintProcessorException("Message adaptor($type) is not supported")
            }
        }
    }

    fun messageProducerProperties(jsonNode: JsonNode): MessageProducerProperties {
        val type = jsonNode.get("type").textValue()
        return when (type) {
            MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaBasicAuthMessageProducerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaSslAuthMessageProducerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_SCRAM_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaScramSslAuthMessageProducerProperties::class.java)!!
            }
            else -> {
                throw BlueprintProcessorException("Message adaptor($type) is not supported")
            }
        }
    }

    /** Consumer Property Lib Service Implementation **/

    /** Return Message Consumer Service for [jsonNode] definitions. */
    fun blueprintMessageConsumerService(jsonNode: JsonNode): BlueprintMessageConsumerService {
        val messageConsumerProperties = messageConsumerProperties(jsonNode)
        return blueprintMessageConsumerService(messageConsumerProperties)
    }

    /** Return Message Consumer Service for [selector] definitions. */
    fun blueprintMessageConsumerService(selector: String): BlueprintMessageConsumerService {
        val prefix = "${MessageLibConstants.PROPERTY_MESSAGE_CONSUMER_PREFIX}$selector"
        val messageClientProperties = messageConsumerProperties(prefix)
        return blueprintMessageConsumerService(messageClientProperties)
    }

    /** Return Message Consumer Properties for [prefix] definitions. */
    fun messageConsumerProperties(prefix: String): MessageConsumerProperties {
        val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
        return when (type) {
            /** Message Consumer */
            MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaBasicAuthMessageConsumerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaSslAuthMessageConsumerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_SCRAM_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaScramSslAuthMessageConsumerProperties::class.java
                )
            }
            /** Stream Consumer */
            MessageLibConstants.TYPE_KAFKA_STREAMS_BASIC_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaStreamsBasicAuthConsumerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_STREAMS_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaStreamsSslAuthConsumerProperties::class.java
                )
            }
            MessageLibConstants.TYPE_KAFKA_STREAMS_SCRAM_SSL_AUTH -> {
                bluePrintPropertiesService.propertyBeanType(
                    prefix, KafkaStreamsScramSslAuthConsumerProperties::class.java
                )
            }
            else -> {
                throw BlueprintProcessorException("Message adaptor($type) is not supported")
            }
        }
    }

    fun messageConsumerProperties(jsonNode: JsonNode): MessageConsumerProperties {
        val type = jsonNode.get("type").textValue()
        return when (type) {
            /** Message Consumer */
            MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaBasicAuthMessageConsumerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaSslAuthMessageConsumerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_SCRAM_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaScramSslAuthMessageConsumerProperties::class.java)!!
            }
            /** Stream Consumer */
            MessageLibConstants.TYPE_KAFKA_STREAMS_BASIC_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaStreamsBasicAuthConsumerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_STREAMS_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaStreamsSslAuthConsumerProperties::class.java)!!
            }
            MessageLibConstants.TYPE_KAFKA_STREAMS_SCRAM_SSL_AUTH -> {
                JacksonUtils.readValue(jsonNode, KafkaStreamsScramSslAuthConsumerProperties::class.java)!!
            }
            else -> {
                throw BlueprintProcessorException("Message adaptor($type) is not supported")
            }
        }
    }

    private fun blueprintMessageConsumerService(messageConsumerProperties: MessageConsumerProperties):
        BlueprintMessageConsumerService {

            when (messageConsumerProperties.type) {
                /** Message Consumer */
                MessageLibConstants.TYPE_KAFKA_BASIC_AUTH -> {
                    return KafkaMessageConsumerService(
                        messageConsumerProperties as KafkaBasicAuthMessageConsumerProperties,
                        meterRegistry
                    )
                }
                MessageLibConstants.TYPE_KAFKA_SSL_AUTH -> {
                    return KafkaMessageConsumerService(
                        messageConsumerProperties as KafkaSslAuthMessageConsumerProperties,
                        meterRegistry
                    )
                }
                MessageLibConstants.TYPE_KAFKA_SCRAM_SSL_AUTH -> {
                    return KafkaMessageConsumerService(
                        messageConsumerProperties as KafkaScramSslAuthMessageConsumerProperties,
                        meterRegistry
                    )
                }
                /** Stream Consumer */
                MessageLibConstants.TYPE_KAFKA_STREAMS_BASIC_AUTH -> {
                    return KafkaStreamsConsumerService(
                        messageConsumerProperties as KafkaStreamsBasicAuthConsumerProperties
                    )
                }
                MessageLibConstants.TYPE_KAFKA_STREAMS_SSL_AUTH -> {
                    return KafkaStreamsConsumerService(
                        messageConsumerProperties as KafkaStreamsSslAuthConsumerProperties
                    )
                }
                MessageLibConstants.TYPE_KAFKA_STREAMS_SCRAM_SSL_AUTH -> {
                    return KafkaStreamsConsumerService(
                        messageConsumerProperties as KafkaStreamsScramSslAuthConsumerProperties
                    )
                }
                else -> {
                    throw BlueprintProcessorException("couldn't get message client service for ${messageConsumerProperties.type}")
                }
            }
        }
}