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
57 const val CONSUMER_SELECTOR = "self-service-api"
58 const val PRODUCER_SELECTOR = "self-service-api"
61 @EventListener(ApplicationReadyEvent::class)
62 fun setupMessageListener() = GlobalScope.launch {
65 "Setting up message consumer($CONSUMER_SELECTOR)" +
66 "message producer($PRODUCER_SELECTOR)..."
69 /** Get the Message Consumer Service **/
70 blueprintMessageConsumerService = try {
71 bluePrintMessageLibPropertyService
72 .blueprintMessageConsumerService(CONSUMER_SELECTOR)
73 } catch (e: BlueprintProcessorException) {
74 val errorMsg = "Failed creating Kafka consumer message service."
75 throw e.updateErrorMessage(
76 SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
77 "Wrong Kafka selector provided or internal error in Kafka service."
79 } catch (e: Exception) {
80 throw BlueprintProcessorException("failed to create consumer service ${e.message}")
83 /** Get the Message Producer Service **/
84 val blueprintMessageProducerService = try {
85 bluePrintMessageLibPropertyService
86 .blueprintMessageProducerService(PRODUCER_SELECTOR)
87 } catch (e: BlueprintProcessorException) {
88 val errorMsg = "Failed creating Kafka producer message service."
89 throw e.updateErrorMessage(
90 SelfServiceApiDomains.SELF_SERVICE_API, errorMsg,
91 "Wrong Kafka selector provided or internal error in Kafka service."
93 } catch (e: Exception) {
94 throw BlueprintProcessorException("failed to create producer service ${e.message}")
98 /** Subscribe to the consumer topics */
99 val additionalConfig: MutableMap<String, Any> = hashMapOf()
100 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
101 channel.consumeEach { message ->
105 val key = message.key() ?: UUID.randomUUID().toString()
106 val value = String(message.value(), Charset.defaultCharset())
107 log.trace("Consumed Message : key($key) value($value)")
108 val executionServiceInput = value.jsonAsType<ExecutionServiceInput>()
109 val executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
110 blueprintMessageProducerService.sendMessage(key, executionServiceOutput)
111 } catch (e: Exception) {
112 log.error("failed in processing the consumed message : $message", e)
114 ph.arriveAndDeregister()
119 } catch (e: Exception) {
121 "failed to start message consumer($CONSUMER_SELECTOR) " +
122 "message producer($PRODUCER_SELECTOR) ",
129 fun shutdownMessageListener() = runBlocking {
132 "Shutting down message consumer($CONSUMER_SELECTOR)" +
133 "message producer($PRODUCER_SELECTOR)..."
135 blueprintMessageConsumerService.shutDown()
136 ph.arriveAndAwaitAdvance()
137 } catch (e: Exception) {
138 log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)