0ab38c6bd0f3b187e453496a43f44db44b739f42
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *  Modifications Copyright © 2021 Bell Canada.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
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
36 import java.util.UUID
37 import java.util.concurrent.Phaser
38 import javax.annotation.PreDestroy
39
40 @ConditionalOnProperty(
41     name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
42     havingValue = "true"
43 )
44 @Service
45 open class BlueprintProcessingKafkaConsumer(
46     private val bluePrintMessageLibPropertyService: BlueprintMessageLibPropertyService,
47     private val executionServiceHandler: ExecutionServiceHandler
48 ) {
49
50     val log = logger(BlueprintProcessingKafkaConsumer::class)
51
52     private val ph = Phaser(1)
53
54     private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
55
56     companion object {
57
58         const val CONSUMER_SELECTOR = "self-service-api"
59         const val PRODUCER_SELECTOR = "self-service-api"
60     }
61
62     @EventListener(ApplicationReadyEvent::class)
63     fun setupMessageListener() = GlobalScope.launch {
64         try {
65             log.info(
66                 "Setting up message consumer($CONSUMER_SELECTOR)" +
67                     "message producer($PRODUCER_SELECTOR)..."
68             )
69
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."
79                 )
80             } catch (e: Exception) {
81                 throw BlueprintProcessorException("failed to create consumer service ${e.message}")
82             }
83
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."
93                 )
94             } catch (e: Exception) {
95                 throw BlueprintProcessorException("failed to create producer service ${e.message}")
96             }
97
98             launch {
99                 /** Subscribe to the consumer topics */
100                 val additionalConfig: MutableMap<String, Any> = hashMapOf()
101                 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
102                 channel.consumeEach { message ->
103                     launch {
104                         try {
105                             ph.register()
106                             val key = message.key() ?: UUID.randomUUID().toString()
107                             val value = String(message.value(), Charset.defaultCharset())
108                             val executionServiceInput = value.jsonAsType<ExecutionServiceInput>()
109                             log.info(
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})"
116                             )
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)
121                         } finally {
122                             ph.arriveAndDeregister()
123                         }
124                     }
125                 }
126             }
127         } catch (e: Exception) {
128             log.error(
129                 "failed to start message consumer($CONSUMER_SELECTOR) " +
130                     "message producer($PRODUCER_SELECTOR) ",
131                 e
132             )
133         }
134     }
135
136     @PreDestroy
137     fun shutdownMessageListener() = runBlocking {
138         try {
139             log.info(
140                 "Shutting down message consumer($CONSUMER_SELECTOR)" +
141                     "message producer($PRODUCER_SELECTOR)..."
142             )
143             blueprintMessageConsumerService.shutDown()
144             ph.arriveAndAwaitAdvance()
145         } catch (e: Exception) {
146             log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)
147         }
148     }
149 }