Add missing k8s-rb-instance-release-name.json
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / dmaap-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / dmaap / BluePrintDmaapClientService.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.cds.blueprintsprocessor.dmaap
22
23 import com.att.nsa.mr.client.MRBatchingPublisher
24 import com.att.nsa.mr.client.MRPublisher
25 import org.slf4j.LoggerFactory
26 import java.io.IOException
27 import java.util.concurrent.TimeUnit
28
29 /**
30  * Abstraction of DMAAP client services that could form DMAAP client from the
31  * properties provided. This abstraction also provides a mechanism to send
32  * messages with the given partition in a session and closing the same.
33  */
34 interface BluePrintDmaapClientService {
35
36     /**
37      * Static variable for logging.
38      */
39     companion object {
40
41         var log = LoggerFactory.getLogger(
42             BluePrintDmaapClientService::class.java
43         )!!
44     }
45
46     /**
47      * Returns the properly constructed DMAAP client with the type.
48      */
49     fun getDmaapClient(): MutableList<MRBatchingPublisher>
50
51     /**
52      * Sends messages to the sessions created by the information provided from
53      * application.properties and event.properties file
54      */
55     fun sendMessage(msgs: Collection<String>): Boolean {
56         var success = true
57         val clients = getDmaapClient()
58         val dmaapMsgs = mutableListOf<MRPublisher.message>()
59         for (m in msgs) {
60             dmaapMsgs.add(MRPublisher.message("1", m))
61         }
62         log.info("Sending messages to the DMAAP Server")
63         for (client in clients) {
64             try {
65                 client.send(dmaapMsgs)
66             } catch (e: IOException) {
67                 success = false
68                 log.error(e.message, e)
69             }
70         }
71         return success
72     }
73
74     /**
75      * Sends message to the sessions created by the information provided from
76      * application.properties and event.properties file
77      */
78     fun sendMessage(msg: String): Boolean {
79         val msgs = mutableListOf<String>()
80         msgs.add(msg)
81         return sendMessage(msgs)
82     }
83
84     /**
85      * Closes the opened session that was used for sending messages.
86      */
87     fun close(timeout: Long): MutableList<MutableList<MRPublisher.message>>? {
88         log.debug("Closing the DMAAP producer clients")
89         var msgs: MutableList<MutableList<MRPublisher.message>> =
90             mutableListOf()
91         val clients = getDmaapClient()
92         for (client in clients) {
93             try {
94                 var ms = client.close(timeout, TimeUnit.SECONDS)
95                 msgs.add(ms)
96             } catch (e: IOException) {
97                 log.warn(
98                     "Unable to cleanly close the connection from the " +
99                         "client $client",
100                     e
101                 )
102             }
103         }
104         return msgs
105     }
106 }