|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.commons.alerting |
| 6 | + |
| 7 | +import org.opensearch.action.ActionListener |
| 8 | +import org.opensearch.action.ActionResponse |
| 9 | +import org.opensearch.client.node.NodeClient |
| 10 | +import org.opensearch.common.io.stream.Writeable |
| 11 | +import org.opensearch.commons.alerting.action.AlertingActions |
| 12 | +import org.opensearch.commons.alerting.action.IndexMonitorRequest |
| 13 | +import org.opensearch.commons.alerting.action.IndexMonitorResponse |
| 14 | +import org.opensearch.commons.notifications.action.BaseResponse |
| 15 | +import org.opensearch.commons.utils.recreateObject |
| 16 | + |
| 17 | +/** |
| 18 | + * All the transport action plugin interfaces for the Alerting plugin |
| 19 | + */ |
| 20 | +object AlertingPluginInterface { |
| 21 | + |
| 22 | + /** |
| 23 | + * Index monitor interface. |
| 24 | + * @param client Node client for making transport action |
| 25 | + * @param request The request object |
| 26 | + * @param listener The listener for getting response |
| 27 | + */ |
| 28 | + fun indexMonitor( |
| 29 | + client: NodeClient, |
| 30 | + request: IndexMonitorRequest, |
| 31 | + listener: ActionListener<IndexMonitorResponse> |
| 32 | + ) { |
| 33 | + client.execute( |
| 34 | + AlertingActions.INDEX_MONITOR_ACTION_TYPE, |
| 35 | + request, |
| 36 | + wrapActionListener(listener) { response -> |
| 37 | + recreateObject(response) { |
| 38 | + IndexMonitorResponse( |
| 39 | + it |
| 40 | + ) |
| 41 | + } |
| 42 | + } |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + @Suppress("UNCHECKED_CAST") |
| 47 | + private fun <Response : BaseResponse> wrapActionListener( |
| 48 | + listener: ActionListener<Response>, |
| 49 | + recreate: (Writeable) -> Response |
| 50 | + ): ActionListener<Response> { |
| 51 | + return object : ActionListener<ActionResponse> { |
| 52 | + override fun onResponse(response: ActionResponse) { |
| 53 | + val recreated = response as? Response ?: recreate(response) |
| 54 | + listener.onResponse(recreated) |
| 55 | + } |
| 56 | + |
| 57 | + override fun onFailure(exception: java.lang.Exception) { |
| 58 | + listener.onFailure(exception) |
| 59 | + } |
| 60 | + } as ActionListener<Response> |
| 61 | + } |
| 62 | +} |
0 commit comments