Fixing Blueprint Typo's and docs
[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
57         const val CONSUMER_SELECTOR = "self-service-api"
58         const val PRODUCER_SELECTOR = "self-service-api"
59     }
60
61     @EventListener(ApplicationReadyEvent::class)
62     fun setupMessageListener() = GlobalScope.launch {
63         try {
64             log.info(
65                 "Setting up message consumer($CONSUMER_SELECTOR)" +
66                     "message producer($PRODUCER_SELECTOR)..."
67             )
68
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."
78                 )
79             } catch (e: Exception) {
80                 throw BlueprintProcessorException("failed to create consumer service ${e.message}")
81             }
82
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."
92                 )
93             } catch (e: Exception) {
94                 throw BlueprintProcessorException("failed to create producer service ${e.message}")
95             }
96
97             launch {
98                 /** Subscribe to the consumer topics */
99                 val additionalConfig: MutableMap<String, Any> = hashMapOf()
100                 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
101                 channel.consumeEach { message ->
102                     launch {
103                         try {
104                             ph.register()
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)
113                         } finally {
114                             ph.arriveAndDeregister()
115                         }
116                     }
117                 }
118             }
119         } catch (e: Exception) {
120             log.error(
121                 "failed to start message consumer($CONSUMER_SELECTOR) " +
122                     "message producer($PRODUCER_SELECTOR) ",
123                 e
124             )
125         }
126     }
127
128     @PreDestroy
129     fun shutdownMessageListener() = runBlocking {
130         try {
131             log.info(
132                 "Shutting down message consumer($CONSUMER_SELECTOR)" +
133                     "message producer($PRODUCER_SELECTOR)..."
134             )
135             blueprintMessageConsumerService.shutDown()
136             ph.arriveAndAwaitAdvance()
137         } catch (e: Exception) {
138             log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)
139         }
140     }
141 }