a9dda7e0c5df5c1a16e4cee8e8f87d9b0a4a2155
[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(name = ["blueprintsprocessor.messageconsumer.self-service-api.kafkaEnable"],
36         havingValue = "true")
37 @Service
38 open class BluePrintProcessingKafkaConsumer(
39         private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
40         private val executionServiceHandler: ExecutionServiceHandler) {
41
42     val log = logger(BluePrintProcessingKafkaConsumer::class)
43
44     private val ph = Phaser(1)
45
46     private lateinit var blueprintMessageConsumerService: BlueprintMessageConsumerService
47
48     companion object {
49         const val CONSUMER_SELECTOR = "self-service-api"
50         const val PRODUCER_SELECTOR = "self-service-api"
51     }
52
53     @EventListener(ApplicationReadyEvent::class)
54     fun setupMessageListener() = runBlocking {
55         try {
56             log.info("Setting up message consumer($CONSUMER_SELECTOR) and " +
57                     "message producer($PRODUCER_SELECTOR)...")
58
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}")
65             }
66
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}")
73             }
74
75             launch {
76                 /** Subscribe to the consumer topics */
77                 val additionalConfig: MutableMap<String, Any> = hashMapOf()
78                 val channel = blueprintMessageConsumerService.subscribe(additionalConfig)
79                 channel.consumeEach { message ->
80                     launch {
81                         try {
82                             ph.register()
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)
91                         }
92                         finally {
93                             ph.arriveAndDeregister()
94                         }
95                     }
96                 }
97             }
98         } catch (e: Exception) {
99             log.error("failed to start message consumer($CONSUMER_SELECTOR) and " +
100                     "message producer($PRODUCER_SELECTOR) ", e)
101         }
102     }
103
104     @PreDestroy
105     fun shutdownMessageListener() = runBlocking {
106         try {
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)
113         }
114     }
115
116 }