2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19 import kotlinx.coroutines.GlobalScope
20 import kotlinx.coroutines.channels.consumeEach
21 import kotlinx.coroutines.launch
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsType
28 import org.onap.ccsdk.cds.controllerblueprints.core.logger
29 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
31 import org.springframework.boot.context.event.ApplicationReadyEvent
32 import org.springframework.context.event.EventListener
33 import org.springframework.stereotype.Service
34 import java.nio.charset.Charset
36 import java.util.concurrent.Phaser
37 import javax.annotation.PreDestroy
39 @ConditionalOnProperty(
40 name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
44 open class BluePrintProcessingKafkaConsumer(
45 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
46 private val executionServiceHandler: ExecutionServiceHandler
49 val log = logger(BluePrintProcessingKafkaConsumer::class)
51 private val ph = Phaser(1)
53 private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
56 const val CONSUMER_SELECTOR = "self-service-api"
57 const val PRODUCER_SELECTOR = "self-service-api"
60 @EventListener(ApplicationReadyEvent::class)
61 fun setupMessageListener() = GlobalScope.launch {
64 "Setting up message consumer($CONSUMER_SELECTOR)" +
65 "message producer($PRODUCER_SELECTOR)..."
68 /** Get the Message Consumer Service **/
69 blueprintMessageConsumerService = try {
70 bluePrintMessageLibPropertyService
71 .blueprintMessageConsumerService(CONSUMER_SELECTOR)
72 } catch (e: BluePrintProcessorException) {
73 val errorMsg = "Failed creating Kafka consumer message service."
74 throw e.updateErrorMessage(SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
75 "Wrong Kafka selector provided or internal error in Kafka service.")
76 } catch (e: Exception) {
77 throw BluePrintProcessorException("failed to create consumer service ${e.message}")
80 /** Get the Message Producer Service **/
81 val blueprintMessageProducerService = try {
82 bluePrintMessageLibPropertyService
83 .blueprintMessageProducerService(PRODUCER_SELECTOR)
84 } catch (e: BluePrintProcessorException) {
85 val errorMsg = "Failed creating Kafka producer message service."
86 throw e.updateErrorMessage(SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
87 "Wrong Kafka selector provided or internal error in Kafka service.")
88 } catch (e: Exception) {
89 throw BluePrintProcessorException("failed to create producer service ${e.message}")
93 /** Subscribe to the consumer topics */
94 val additionalConfig: MutableMap<String, Any> = hashMapOf()
95 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
96 channel.consumeEach { message ->
100 val key = message.key() ?: UUID.randomUUID().toString()
101 val value = String(message.value(), Charset.defaultCharset())
102 log.trace("Consumed Message : key($key) value($value)")
103 val executionServiceInput = value.jsonAsType<ExecutionServiceInput>()
104 val executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
105 blueprintMessageProducerService.sendMessage(key, executionServiceOutput)
106 } catch (e: Exception) {
107 log.error("failed in processing the consumed message : $message", e)
109 ph.arriveAndDeregister()
114 } catch (e: Exception) {
116 "failed to start message consumer($CONSUMER_SELECTOR) " +
117 "message producer($PRODUCER_SELECTOR) ", e
123 fun shutdownMessageListener() = runBlocking {
126 "Shutting down message consumer($CONSUMER_SELECTOR)" +
127 "message producer($PRODUCER_SELECTOR)..."
129 blueprintMessageConsumerService.shutDown()
130 ph.arriveAndAwaitAdvance()
131 } catch (e: Exception) {
132 log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)