Multiple revisions 400318-400319
[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)
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         msg = stasis_cache_get(ast_endpoint_cache(),
202                 ast_endpoint_snapshot_type(), id);
203         if (!msg) {
204                 return NULL;
205         }
206
207         snapshot = stasis_message_data(msg);
208         ast_assert(snapshot != NULL);
209
210         ao2_ref(snapshot, +1);
211         return snapshot;
212 }
213
214 /*!
215  * \brief Callback extract a unique identity from a snapshot message.
216  *
217  * This identity is unique to the underlying object of the snapshot, such as the
218  * UniqueId field of a channel.
219  *
220  * \param message Message to extract id from.
221  * \return String representing the snapshot's id.
222  * \return \c NULL if the message_type of the message isn't a handled snapshot.
223  * \since 12
224  */
225 static const char *endpoint_snapshot_get_id(struct stasis_message *message)
226 {
227         struct ast_endpoint_snapshot *snapshot;
228
229         if (ast_endpoint_snapshot_type() != stasis_message_type(message)) {
230                 return NULL;
231         }
232
233         snapshot = stasis_message_data(message);
234
235         return snapshot->id;
236 }
237
238
239 struct ast_json *ast_endpoint_snapshot_to_json(
240         const struct ast_endpoint_snapshot *snapshot)
241 {
242         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
243         struct ast_json *channel_array;
244         int i;
245
246         json = ast_json_pack("{s: s, s: s, s: s, s: []}",
247                 "technology", snapshot->tech,
248                 "resource", snapshot->resource,
249                 "state", ast_endpoint_state_to_string(snapshot->state),
250                 "channel_ids");
251
252         if (json == NULL) {
253                 return NULL;
254         }
255
256         if (snapshot->max_channels != -1) {
257                 int res = ast_json_object_set(json, "max_channels",
258                         ast_json_integer_create(snapshot->max_channels));
259                 if (res != 0) {
260                         return NULL;
261                 }
262         }
263
264         channel_array = ast_json_object_get(json, "channel_ids");
265         ast_assert(channel_array != NULL);
266         for (i = 0; i < snapshot->num_channels; ++i) {
267                 int res = ast_json_array_append(channel_array,
268                         ast_json_string_create(snapshot->channel_ids[i]));
269                 if (res != 0) {
270                         return NULL;
271                 }
272         }
273
274         return ast_json_ref(json);
275 }
276
277 static void endpoints_stasis_cleanup(void)
278 {
279         STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_snapshot_type);
280         STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_state_type);
281
282         ao2_cleanup(endpoint_cache_all);
283         endpoint_cache_all = NULL;
284 }
285
286 int ast_endpoint_stasis_init(void)
287 {
288         int res = 0;
289         ast_register_cleanup(endpoints_stasis_cleanup);
290
291         endpoint_cache_all = stasis_cp_all_create("endpoint_topic_all",
292                 endpoint_snapshot_get_id);
293         if (!endpoint_cache_all) {
294                 return -1;
295         }
296
297         res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_snapshot_type);
298         res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_state_type);
299
300         return res;
301 }