Formatting Code base with ktlint
[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.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
34
35 @ConditionalOnProperty(
36     name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
37     havingValue = "true"
38 )
39 @Service
40 open class BluePrintProcessingKafkaConsumer(
41     private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
42     private val executionServiceHandler: ExecutionServiceHandler
43 ) {
44
45     val log = logger(BluePrintProcessingKafkaConsumer::class)
46
47     private val ph = Phaser(1)
48
49     private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
50
51     companion object {
52         const val CONSUMER_SELECTOR = "self-service-api"
53         const val PRODUCER_SELECTOR = "self-service-api"
54     }
55
56     @EventListener(ApplicationReadyEvent::class)
57     fun setupMessageListener() = runBlocking {
58         try {
59             log.info(
60                 "Setting up message consumer($CONSUMER_SELECTOR) and " +
61                         "message producer($PRODUCER_SELECTOR)..."
62             )
63
64             /** Get the Message Consumer Service **/
65             blueprintMessageConsumerService = try {
66                 bluePrintMessageLibPropertyService
67                     .blueprintMessageConsumerService(CONSUMER_SELECTOR)
68             } catch (e: Exception) {
69                 throw BluePrintProcessorException("failed to create consumer service ${e.message}")
70             }
71
72             /** Get the Message Producer Service **/
73             val blueprintMessageProducerService = try {
74                 bluePrintMessageLibPropertyService
75                     .blueprintMessageProducerService(PRODUCER_SELECTOR)
76             } catch (e: Exception) {
77                 throw BluePrintProcessorException("failed to create producer service ${e.message}")
78             }
79
80             launch {
81                 /** Subscribe to the consumer topics */
82                 val additionalConfig: MutableMap<String, Any> = hashMapOf()
83                 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
84                 channel.consumeEach { message ->
85                     launch {
86                         try {
87                             ph.register()
88                             log.trace("Consumed Message : $message")
89                             val executionServiceInput = message.jsonAsType<ExecutionServiceInput>()
90                             val executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
91                             // TODO("In future, Message publisher configuration vary with respect to request")
92                             /** Send the response message */
93                             blueprintMessageProducerService.sendMessage(executionServiceOutput)
94                         } catch (e: Exception) {
95                             log.error("failed in processing the consumed message : $message", e)
96                         } finally {
97                             ph.arriveAndDeregister()
98                         }
99                     }
100                 }
101             }
102         } catch (e: Exception) {
103             log.error(
104                 "failed to start message consumer($CONSUMER_SELECTOR) and " +
105                         "message producer($PRODUCER_SELECTOR) ", e
106             )
107         }
108     }
109
110     @PreDestroy
111     fun shutdownMessageListener() = runBlocking {
112         try {
113             log.info(
114                 "Shutting down message consumer($CONSUMER_SELECTOR) and " +
115                         "message producer($PRODUCER_SELECTOR)..."
116             )
117             blueprintMessageConsumerService.shutDown()
118             ph.arriveAndAwaitAdvance()
119         } catch (e: Exception) {
120             log.error("failed to shutdown message listener($CONSUMER_SELECTOR)", e)
121         }
122     }
123 }