Updating README.md
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / BluePrintProcessingKafkaConsumer.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
18
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
35 import java.util.UUID
36 import java.util.concurrent.Phaser
37 import javax.annotation.PreDestroy
38
39 @ConditionalOnProperty(
40     name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
41     havingValue = "true"
42 )
43 @Service
44 open class BluePrintProcessingKafkaConsumer(
45     private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
46     private val executionServiceHandler: ExecutionServiceHandler
47 ) {
48
49     val log = logger(BluePrintProcessingKafkaConsumer::class)
50
51     private val ph = Phaser(1)
52
53     private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
54
55     companion object {
56         const val CONSUMER_SELECTOR = "self-service-api"
57         const val PRODUCER_SELECTOR = "self-service-api"
58     }
59
60     @EventListener(ApplicationReadyEvent::class)
61     fun setupMessageListener() = GlobalScope.launch {
62         try {
63             log.info(
64                 "Setting up message consumer($CONSUMER_SELECTOR)" +
65                         "message producer($PRODUCER_SELECTOR)..."
66             )
67
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}")
78             }
79
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}")
90             }
91
92             launch {
93                 /** Subscribe to the consumer topics */
94                 val additionalConfig: MutableMap<String, Any> = hashMapOf()
95                 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
96                 channel.consumeEach { message ->
97                     launch {
98                         try {
99                             ph.register()
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)
108                         } finally {
109                             ph.arriveAndDeregister()
110                         }
111                     }
112                 }
113             }
114         } catch (e: Exception) {
115             log.error(
116                 "failed to start message consumer($CONSUMER_SELECTOR) " +
117                         "message producer($PRODUCER_SELECTOR) ", e
118             )
119         }
120     }
121
122     @PreDestroy
123     fun shutdownMessageListener() = runBlocking {
124         try {
125             log.info(
126                 "Shutting down message consumer($CONSUMER_SELECTOR)" +
127                         "message producer($PRODUCER_SELECTOR)..."
128             )
129             blueprintMessageConsumerService.shutDown()
130             ph.arriveAndAwaitAdvance()
131         } catch (e: Exception) {
132             log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)
133         }
134     }
135 }