2 * Copyright © 2019 IBM.
3 * Modifications Copyright © 2021 Bell Canada.
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.selfservice.api
20 import kotlinx.coroutines.GlobalScope
21 import kotlinx.coroutines.channels.consumeEach
22 import kotlinx.coroutines.launch
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsType
29 import org.onap.ccsdk.cds.controllerblueprints.core.logger
30 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
31 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
32 import org.springframework.boot.context.event.ApplicationReadyEvent
33 import org.springframework.context.event.EventListener
34 import org.springframework.stereotype.Service
35 import java.nio.charset.Charset
37 import java.util.concurrent.Phaser
38 import javax.annotation.PreDestroy
40 @ConditionalOnProperty(
41 name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
45 open class BlueprintProcessingKafkaConsumer(
46 private val bluePrintMessageLibPropertyService: BlueprintMessageLibPropertyService,
47 private val executionServiceHandler: ExecutionServiceHandler
50 val log = logger(BlueprintProcessingKafkaConsumer::class)
52 private val ph = Phaser(1)
54 private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
58 const val CONSUMER_SELECTOR = "self-service-api"
59 const val PRODUCER_SELECTOR = "self-service-api"
62 @EventListener(ApplicationReadyEvent::class)
63 fun setupMessageListener() = GlobalScope.launch {
66 "Setting up message consumer($CONSUMER_SELECTOR)" +
67 "message producer($PRODUCER_SELECTOR)..."
70 /** Get the Message Consumer Service **/
71 blueprintMessageConsumerService = try {
72 bluePrintMessageLibPropertyService
73 .blueprintMessageConsumerService(CONSUMER_SELECTOR)
74 } catch (e: BlueprintProcessorException) {
75 val errorMsg = "Failed creating Kafka consumer message service."
76 throw e.updateErrorMessage(
77 SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
78 "Wrong Kafka selector provided or internal error in Kafka service."
80 } catch (e: Exception) {
81 throw BlueprintProcessorException("failed to create consumer service ${e.message}")
84 /** Get the Message Producer Service **/
85 val blueprintMessageProducerService = try {
86 bluePrintMessageLibPropertyService
87 .blueprintMessageProducerService(PRODUCER_SELECTOR)
88 } catch (e: BlueprintProcessorException) {
89 val errorMsg = "Failed creating Kafka producer message service."
90 throw e.updateErrorMessage(
91 SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
92 "Wrong Kafka selector provided or internal error in Kafka service."
94 } catch (e: Exception) {
95 throw BlueprintProcessorException("failed to create producer service ${e.message}")
99 /** Subscribe to the consumer topics */
100 val additionalConfig: MutableMap<String, Any> = hashMapOf()
101 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
102 channel.consumeEach { message ->
106 val key = message.key() ?: UUID.randomUUID().toString()
107 val value = String(message.value(), Charset.defaultCharset())
108 val executionServiceInput = value.jsonAsType<ExecutionServiceInput>()
110 "Consumed Message : topic(${message.topic()}) " +
111 "partition(${message.partition()}) " +
112 "leaderEpoch(${message.leaderEpoch().get()}) " +
113 "offset(${message.offset()}) " +
114 "key(${message.key()}) " +
115 "CBA(${executionServiceInput.actionIdentifiers.blueprintName}/${executionServiceInput.actionIdentifiers.blueprintVersion}/${executionServiceInput.actionIdentifiers.actionName})"
117 val executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
118 blueprintMessageProducerService.sendMessage(key, executionServiceOutput)
119 } catch (e: Exception) {
120 log.error("failed in processing the consumed message : $message", e)
122 ph.arriveAndDeregister()
127 } catch (e: Exception) {
129 "failed to start message consumer($CONSUMER_SELECTOR) " +
130 "message producer($PRODUCER_SELECTOR) ",
137 fun shutdownMessageListener() = runBlocking {
140 "Shutting down message consumer($CONSUMER_SELECTOR)" +
141 "message producer($PRODUCER_SELECTOR)..."
143 blueprintMessageConsumerService.shutDown()
144 ph.arriveAndAwaitAdvance()
145 } catch (e: Exception) {
146 log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)