4a183784c092a452c09dd23ae60892171da73e6c
[asterisk/asterisk.git] / main / stasis_endpoints.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Stasis endpoint API.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/astobj2.h"
35 #include "asterisk/stasis.h"
36 #include "asterisk/stasis_endpoints.h"
37
38 /*** DOCUMENTATION
39         <managerEvent language="en_US" name="PeerStatus">
40                 <managerEventInstance class="EVENT_FLAG_SYSTEM">
41                         <synopsis>Raised when the state of a peer changes.</synopsis>
42                         <syntax>
43                                 <parameter name="ChannelType">
44                                         <para>The channel technology of the peer.</para>
45                                 </parameter>
46                                 <parameter name="Peer">
47                                         <para>The name of the peer (including channel technology).</para>
48                                 </parameter>
49                                 <parameter name="PeerStatus">
50                                         <para>New status of the peer.</para>
51                                         <enumlist>
52                                                 <enum name="Unknown"/>
53                                                 <enum name="Registered"/>
54                                                 <enum name="Unregistered"/>
55                                                 <enum name="Rejected"/>
56                                                 <enum name="Lagged"/>
57                                         </enumlist>
58                                 </parameter>
59                                 <parameter name="Cause">
60                                         <para>The reason the status has changed.</para>
61                                 </parameter>
62                                 <parameter name="Address">
63                                         <para>New address of the peer.</para>
64                                 </parameter>
65                                 <parameter name="Port">
66                                         <para>New port for the peer.</para>
67                                 </parameter>
68                                 <parameter name="Time">
69                                         <para>Time it takes to reach the peer and receive a response.</para>
70                                 </parameter>
71                         </syntax>
72                 </managerEventInstance>
73         </managerEvent>
74 ***/
75
76 static struct stasis_cp_all *endpoint_cache_all;
77
78 struct stasis_cp_all *ast_endpoint_cache_all(void)
79 {
80         return endpoint_cache_all;
81 }
82
83 struct stasis_cache *ast_endpoint_cache(void)
84 {
85         return stasis_cp_all_cache(endpoint_cache_all);
86 }
87
88 struct stasis_topic *ast_endpoint_topic_all(void)
89 {
90         return stasis_cp_all_topic(endpoint_cache_all);
91 }
92
93 struct stasis_topic *ast_endpoint_topic_all_cached(void)
94 {
95         return stasis_cp_all_topic_cached(endpoint_cache_all);
96 }
97
98 static struct ast_manager_event_blob *peerstatus_to_ami(struct stasis_message *msg);
99
100 STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_snapshot_type);
101 STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_state_type,
102         .to_ami = peerstatus_to_ami,
103 );
104
105 static struct ast_manager_event_blob *peerstatus_to_ami(struct stasis_message *msg)
106 {
107         struct ast_endpoint_blob *obj = stasis_message_data(msg);
108         RAII_VAR(struct ast_str *, peerstatus_event_string, ast_str_create(64), ast_free);
109         const char *value;
110
111         /* peer_status is the only *required* thing */
112         if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "peer_status")))) {
113                 return NULL;
114         }
115         ast_str_append(&peerstatus_event_string, 0, "PeerStatus: %s\r\n", value);
116
117         if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "cause")))) {
118                 ast_str_append(&peerstatus_event_string, 0, "Cause: %s\r\n", value);
119         }
120         if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "address")))) {
121                 ast_str_append(&peerstatus_event_string, 0, "Address: %s\r\n", value);
122         }
123         if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "port")))) {
124                 ast_str_append(&peerstatus_event_string, 0, "Port: %s\r\n", value);
125         }
126         if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "time")))) {
127                 ast_str_append(&peerstatus_event_string, 0, "Time: %s\r\n", value);
128         }
129
130         return ast_manager_event_blob_create(EVENT_FLAG_SYSTEM, "PeerStatus",
131                 "ChannelType: %s\r\n"
132                 "Peer: %s/%s\r\n"
133                 "%s",
134                 obj->snapshot->tech,
135                 obj->snapshot->tech,
136                 obj->snapshot->resource,
137                 ast_str_buffer(peerstatus_event_string));
138 }
139
140 static void endpoint_blob_dtor(void *obj)
141 {
142         struct ast_endpoint_blob *event = obj;
143         ao2_cleanup(event->snapshot);
144         ast_json_unref(event->blob);
145 }
146
147 struct stasis_message *ast_endpoint_blob_create(struct ast_endpoint *endpoint,
148         struct stasis_message_type *type, struct ast_json *blob)
149 {
150         RAII_VAR(struct ast_endpoint_blob *, obj, NULL, ao2_cleanup);
151         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
152
153         if (!blob) {
154                 blob = ast_json_null();
155         }
156
157         if (!(obj = ao2_alloc(sizeof(*obj), endpoint_blob_dtor))) {
158                 return NULL;
159         }
160
161         if (endpoint) {
162                 if (!(obj->snapshot = ast_endpoint_snapshot_create(endpoint))) {
163                         return NULL;
164                 }
165         }
166
167         obj->blob = ast_json_ref(blob);
168
169         if (!(msg = stasis_message_create(type, obj))) {
170                 return NULL;
171         }
172
173         ao2_ref(msg, +1);
174         return msg;
175 }
176
177 void ast_endpoint_blob_publish(struct ast_endpoint *endpoint, struct stasis_message_type *type,
178         struct ast_json *blob)
179 {
180         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
181         if (blob) {
182                 message = ast_endpoint_blob_create(endpoint, type, blob);
183         }
184         if (message) {
185                 stasis_publish(ast_endpoint_topic(endpoint), message);
186         }
187 }
188
189 struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech,
190         const char *name, unsigned int guaranteed)
191 {
192         RAII_VAR(char *, id, NULL, ast_free);
193         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
194         struct ast_endpoint_snapshot *snapshot;
195
196         ast_asprintf(&id, "%s/%s", tech, name);
197         if (!id) {
198                 return NULL;
199         }
200
201         if (guaranteed) {
202                 stasis_topic_wait(ast_endpoint_topic_all_cached());
203         }
204
205         msg = stasis_cache_get(ast_endpoint_cache(),
206                 ast_endpoint_snapshot_type(), id);
207         if (!msg) {
208                 return NULL;
209         }
210
211         snapshot = stasis_message_data(msg);
212         ast_assert(snapshot != NULL);
213
214         ao2_ref(snapshot, +1);
215         return snapshot;
216 }
217
218 /*!
219  * \brief Callback extract a unique identity from a snapshot message.
220  *
221  * This identity is unique to the underlying object of the snapshot, such as the
222  * UniqueId field of a channel.
223  *
224  * \param message Message to extract id from.
225  * \return String representing the snapshot's id.
226  * \return \c NULL if the message_type of the message isn't a handled snapshot.
227  * \since 12
228  */
229 static const char *endpoint_snapshot_get_id(struct stasis_message *message)
230 {
231         struct ast_endpoint_snapshot *snapshot;
232
233         if (ast_endpoint_snapshot_type() != stasis_message_type(message)) {
234                 return NULL;
235         }
236
237         snapshot = stasis_message_data(message);
238
239         return snapshot->id;
240 }
241
242
243 struct ast_json *ast_endpoint_snapshot_to_json(
244         const struct ast_endpoint_snapshot *snapshot)
245 {
246         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
247         struct ast_json *channel_array;
248         int i;
249
250         json = ast_json_pack("{s: s, s: s, s: s, s: []}",
251                 "technology", snapshot->tech,
252                 "resource", snapshot->resource,
253                 "state", ast_endpoint_state_to_string(snapshot->state),
254                 "channel_ids");
255
256         if (json == NULL) {
257                 return NULL;
258         }
259
260         if (snapshot->max_channels != -1) {
261                 int res = ast_json_object_set(json, "max_channels",
262                         ast_json_integer_create(snapshot->max_channels));
263                 if (res != 0) {
264                         return NULL;
265                 }
266         }
267
268         channel_array = ast_json_object_get(json, "channel_ids");
269         ast_assert(channel_array != NULL);
270         for (i = 0; i < snapshot->num_channels; ++i) {
271                 int res = ast_json_array_append(channel_array,
272                         ast_json_string_create(snapshot->channel_ids[i]));
273                 if (res != 0) {
274                         return NULL;
275                 }
276         }
277
278         return ast_json_ref(json);
279 }
280
281 static void endpoints_stasis_cleanup(void)
282 {
283         STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_snapshot_type);
284         STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_state_type);
285
286         ao2_cleanup(endpoint_cache_all);
287         endpoint_cache_all = NULL;
288 }
289
290 int ast_endpoint_stasis_init(void)
291 {
292         int res = 0;
293         ast_register_cleanup(endpoints_stasis_cleanup);
294
295         endpoint_cache_all = stasis_cp_all_create("endpoint_topic_all",
296                 endpoint_snapshot_get_id);
297         if (!endpoint_cache_all) {
298                 return -1;
299         }
300
301         res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_snapshot_type);
302         res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_state_type);
303
304         return res;
305 }