|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +/* |
| 13 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 14 | + * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 16 | + * You may not use this file except in compliance with the License. |
| 17 | + * A copy of the License is located at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * or in the "license" file accompanying this file. This file is distributed |
| 22 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 23 | + * express or implied. See the License for the specific language governing |
| 24 | + * permissions and limitations under the License. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.opensearch.commons.notifications |
| 28 | + |
| 29 | +import org.opensearch.action.ActionListener |
| 30 | +import org.opensearch.client.node.NodeClient |
| 31 | +import org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT |
| 32 | +import org.opensearch.commons.notifications.action.CreateNotificationConfigRequest |
| 33 | +import org.opensearch.commons.notifications.action.CreateNotificationConfigResponse |
| 34 | +import org.opensearch.commons.notifications.action.DeleteNotificationConfigRequest |
| 35 | +import org.opensearch.commons.notifications.action.DeleteNotificationConfigResponse |
| 36 | +import org.opensearch.commons.notifications.action.GetFeatureChannelListRequest |
| 37 | +import org.opensearch.commons.notifications.action.GetFeatureChannelListResponse |
| 38 | +import org.opensearch.commons.notifications.action.GetNotificationConfigRequest |
| 39 | +import org.opensearch.commons.notifications.action.GetNotificationConfigResponse |
| 40 | +import org.opensearch.commons.notifications.action.GetNotificationEventRequest |
| 41 | +import org.opensearch.commons.notifications.action.GetNotificationEventResponse |
| 42 | +import org.opensearch.commons.notifications.action.GetPluginFeaturesRequest |
| 43 | +import org.opensearch.commons.notifications.action.GetPluginFeaturesResponse |
| 44 | +import org.opensearch.commons.notifications.action.NotificationsActions.CREATE_NOTIFICATION_CONFIG_ACTION_TYPE |
| 45 | +import org.opensearch.commons.notifications.action.NotificationsActions.DELETE_NOTIFICATION_CONFIG_ACTION_TYPE |
| 46 | +import org.opensearch.commons.notifications.action.NotificationsActions.GET_FEATURE_CHANNEL_LIST_ACTION_TYPE |
| 47 | +import org.opensearch.commons.notifications.action.NotificationsActions.GET_NOTIFICATION_CONFIG_ACTION_TYPE |
| 48 | +import org.opensearch.commons.notifications.action.NotificationsActions.GET_NOTIFICATION_EVENT_ACTION_TYPE |
| 49 | +import org.opensearch.commons.notifications.action.NotificationsActions.GET_PLUGIN_FEATURES_ACTION_TYPE |
| 50 | +import org.opensearch.commons.notifications.action.NotificationsActions.SEND_NOTIFICATION_ACTION_TYPE |
| 51 | +import org.opensearch.commons.notifications.action.NotificationsActions.UPDATE_NOTIFICATION_CONFIG_ACTION_TYPE |
| 52 | +import org.opensearch.commons.notifications.action.SendNotificationRequest |
| 53 | +import org.opensearch.commons.notifications.action.SendNotificationResponse |
| 54 | +import org.opensearch.commons.notifications.action.UpdateNotificationConfigRequest |
| 55 | +import org.opensearch.commons.notifications.action.UpdateNotificationConfigResponse |
| 56 | +import org.opensearch.commons.notifications.model.ChannelMessage |
| 57 | +import org.opensearch.commons.notifications.model.EventSource |
| 58 | +import org.opensearch.commons.utils.SecureClientWrapper |
| 59 | + |
| 60 | +/** |
| 61 | + * All the transport action plugin interfaces for the Notification plugin |
| 62 | + */ |
| 63 | +object NotificationsPluginInterface { |
| 64 | + |
| 65 | + /** |
| 66 | + * Create notification configuration. |
| 67 | + * @param client Node client for making transport action |
| 68 | + * @param request The request object |
| 69 | + * @param listener The listener for getting response |
| 70 | + */ |
| 71 | + fun createNotificationConfig( |
| 72 | + client: NodeClient, |
| 73 | + request: CreateNotificationConfigRequest, |
| 74 | + listener: ActionListener<CreateNotificationConfigResponse> |
| 75 | + ) { |
| 76 | + client.execute( |
| 77 | + CREATE_NOTIFICATION_CONFIG_ACTION_TYPE, |
| 78 | + request, |
| 79 | + listener |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Update notification configuration. |
| 85 | + * @param client Node client for making transport action |
| 86 | + * @param request The request object |
| 87 | + * @param listener The listener for getting response |
| 88 | + */ |
| 89 | + fun updateNotificationConfig( |
| 90 | + client: NodeClient, |
| 91 | + request: UpdateNotificationConfigRequest, |
| 92 | + listener: ActionListener<UpdateNotificationConfigResponse> |
| 93 | + ) { |
| 94 | + client.execute( |
| 95 | + UPDATE_NOTIFICATION_CONFIG_ACTION_TYPE, |
| 96 | + request, |
| 97 | + listener |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Delete notification configuration. |
| 103 | + * @param client Node client for making transport action |
| 104 | + * @param request The request object |
| 105 | + * @param listener The listener for getting response |
| 106 | + */ |
| 107 | + fun deleteNotificationConfig( |
| 108 | + client: NodeClient, |
| 109 | + request: DeleteNotificationConfigRequest, |
| 110 | + listener: ActionListener<DeleteNotificationConfigResponse> |
| 111 | + ) { |
| 112 | + client.execute( |
| 113 | + DELETE_NOTIFICATION_CONFIG_ACTION_TYPE, |
| 114 | + request, |
| 115 | + listener |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get notification configuration. |
| 121 | + * @param client Node client for making transport action |
| 122 | + * @param request The request object |
| 123 | + * @param listener The listener for getting response |
| 124 | + */ |
| 125 | + fun getNotificationConfig( |
| 126 | + client: NodeClient, |
| 127 | + request: GetNotificationConfigRequest, |
| 128 | + listener: ActionListener<GetNotificationConfigResponse> |
| 129 | + ) { |
| 130 | + client.execute( |
| 131 | + GET_NOTIFICATION_CONFIG_ACTION_TYPE, |
| 132 | + request, |
| 133 | + listener |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get notification events. |
| 139 | + * @param client Node client for making transport action |
| 140 | + * @param request The request object |
| 141 | + * @param listener The listener for getting response |
| 142 | + */ |
| 143 | + fun getNotificationEvent( |
| 144 | + client: NodeClient, |
| 145 | + request: GetNotificationEventRequest, |
| 146 | + listener: ActionListener<GetNotificationEventResponse> |
| 147 | + ) { |
| 148 | + client.execute( |
| 149 | + GET_NOTIFICATION_EVENT_ACTION_TYPE, |
| 150 | + request, |
| 151 | + listener |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get notification plugin features. |
| 157 | + * @param client Node client for making transport action |
| 158 | + * @param request The request object |
| 159 | + * @param listener The listener for getting response |
| 160 | + */ |
| 161 | + fun getPluginFeatures( |
| 162 | + client: NodeClient, |
| 163 | + request: GetPluginFeaturesRequest, |
| 164 | + listener: ActionListener<GetPluginFeaturesResponse> |
| 165 | + ) { |
| 166 | + client.execute( |
| 167 | + GET_PLUGIN_FEATURES_ACTION_TYPE, |
| 168 | + request, |
| 169 | + listener |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Get notification channel configuration enabled for a feature. |
| 175 | + * @param client Node client for making transport action |
| 176 | + * @param request The request object |
| 177 | + * @param listener The listener for getting response |
| 178 | + */ |
| 179 | + fun getFeatureChannelList( |
| 180 | + client: NodeClient, |
| 181 | + request: GetFeatureChannelListRequest, |
| 182 | + listener: ActionListener<GetFeatureChannelListResponse> |
| 183 | + ) { |
| 184 | + client.execute( |
| 185 | + GET_FEATURE_CHANNEL_LIST_ACTION_TYPE, |
| 186 | + request, |
| 187 | + listener |
| 188 | + ) |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Send notification API enabled for a feature. No REST API. Internal API only for Inter plugin communication. |
| 193 | + * @param client Node client for making transport action |
| 194 | + * @param eventSource The notification event information |
| 195 | + * @param channelMessage The notification message |
| 196 | + * @param channelIds The list of channel ids to send message to. |
| 197 | + * @param listener The listener for getting response |
| 198 | + */ |
| 199 | + fun sendNotification( |
| 200 | + client: NodeClient, |
| 201 | + eventSource: EventSource, |
| 202 | + channelMessage: ChannelMessage, |
| 203 | + channelIds: List<String>, |
| 204 | + listener: ActionListener<SendNotificationResponse> |
| 205 | + ) { |
| 206 | + val threadContext: String? = |
| 207 | + client.threadPool().threadContext.getTransient<String>(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT) |
| 208 | + val wrapper = SecureClientWrapper(client) // Executing request in privileged mode |
| 209 | + wrapper.execute( |
| 210 | + SEND_NOTIFICATION_ACTION_TYPE, |
| 211 | + SendNotificationRequest(eventSource, channelMessage, channelIds, threadContext), |
| 212 | + listener |
| 213 | + ) |
| 214 | + } |
| 215 | +} |
0 commit comments