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.channels.consumeEach
20 import kotlinx.coroutines.launch
21 import kotlinx.coroutines.runBlocking
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
23 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsType
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
29 import org.springframework.boot.context.event.ApplicationReadyEvent
30 import org.springframework.context.event.EventListener
31 import org.springframework.stereotype.Service
32 import java.util.concurrent.Phaser
33 import javax.annotation.PreDestroy
35 @ConditionalOnProperty(name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
38 open class BluePrintProcessingKafkaConsumer(
39 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
40 private val executionServiceHandler: ExecutionServiceHandler) {
42 val log = logger(BluePrintProcessingKafkaConsumer::class)
44 private val ph = Phaser(1)
46 private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
49 const val CONSUMER_SELECTOR = "self-service-api"
50 const val PRODUCER_SELECTOR = "self-service-api"
53 @EventListener(ApplicationReadyEvent::class)
54 fun setupMessageListener() = runBlocking {
56 log.info("Setting up message consumer($CONSUMER_SELECTOR) and " +
57 "message producer($PRODUCER_SELECTOR)...")
59 /** Get the Message Consumer Service **/
60 blueprintMessageConsumerService = try {
61 bluePrintMessageLibPropertyService
62 .blueprintMessageConsumerService(CONSUMER_SELECTOR)
63 } catch (e: Exception) {
64 throw BluePrintProcessorException("failed to create consumer service ${e.message}")
67 /** Get the Message Producer Service **/
68 val blueprintMessageProducerService = try {
69 bluePrintMessageLibPropertyService
70 .blueprintMessageProducerService(PRODUCER_SELECTOR)
71 } catch (e: Exception) {
72 throw BluePrintProcessorException("failed to create producer service ${e.message}")
76 /** Subscribe to the consumer topics */
77 val additionalConfig: MutableMap<String, Any> = hashMapOf()
78 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
79 channel.consumeEach { message ->
83 log.trace("Consumed Message : $message")
84 val executionServiceInput = message.jsonAsType<ExecutionServiceInput>()
85 val executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
86 //TODO("In future, Message publisher configuration vary with respect to request")
87 /** Send the response message */
88 blueprintMessageProducerService.sendMessage(executionServiceOutput)
89 } catch (e: Exception) {
90 log.error("failed in processing the consumed message : $message", e)
93 ph.arriveAndDeregister()
98 } catch (e: Exception) {
99 log.error("failed to start message consumer($CONSUMER_SELECTOR) and " +
100 "message producer($PRODUCER_SELECTOR) ", e)
105 fun shutdownMessageListener() = runBlocking {
107 log.info("Shutting down message consumer($CONSUMER_SELECTOR) and " +
108 "message producer($PRODUCER_SELECTOR)...")
109 blueprintMessageConsumerService.shutDown()
110 ph.arriveAndAwaitAdvance()
111 } catch (e: Exception) {
112 log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)