Code

proto: Added sdb_proto_marshal_data.
[sysdb.git] / src / core / store_json.c
1 /*
2  * SysDB - src/core/store_json.c
3  * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * This module implements JSON support.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "utils/error.h"
40 #include <assert.h>
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <string.h>
46 /*
47  * private data types
48  */
50 struct sdb_store_json_formatter {
51         sdb_strbuf_t *buf;
53         /* The context describes the state of the formatter through
54          * the path pointing to the current object */
55         int context[8];
56         size_t current;
58         /* The current host context when processing non-host objects */
59         sdb_store_obj_t *current_host;
61         int type;
62         int flags;
63 };
65 /*
66  * private helper functions
67  */
69 static void
70 escape_string(const char *src, char *dest)
71 {
72         size_t i = 1;
73         dest[0] = '"';
74         for ( ; *src; ++src) {
75                 char c = *src;
76                 if ((c == '"') || (c == '\\') || iscntrl((int)c)) {
77                         dest[i] = '\\';
78                         ++i;
79                 }
80                 switch (c) {
81                         case '\a': dest[i] = 'a'; break;
82                         case '\b': dest[i] = 'b'; break;
83                         case '\t': dest[i] = 't'; break;
84                         case '\n': dest[i] = 'n'; break;
85                         case '\v': dest[i] = 'v'; break;
86                         case '\f': dest[i] = 'f'; break;
87                         case '\r': dest[i] = 'r'; break;
88                         default: dest[i] = c; break;
89                 }
90                 ++i;
91         }
92         dest[i] = '"';
93         dest[i + 1] = '\0';
94 } /* escape_string */
96 static int
97 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
98 {
99         char time_str[64];
100         char interval_str[64];
101         char name[2 * strlen(SDB_OBJ(obj)->name) + 3];
102         size_t i;
104         assert(f && obj);
106         if ((f->type != SDB_HOST) && (f->type == obj->type)) {
107                 /* create the host for the current entry first */
108                 assert(obj->parent && (obj->parent->type == SDB_HOST));
109                 if (f->current_host != obj->parent) {
110                         json_emit(f, obj->parent);
111                         sdb_strbuf_append(f->buf, ", \"%ss\": [",
112                                         SDB_STORE_TYPE_TO_NAME(obj->type));
113                         f->current_host = obj->parent;
114                 }
115         }
117         escape_string(SDB_OBJ(obj)->name, name);
118         sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
119         if (obj->type == SDB_ATTRIBUTE) {
120                 char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
121                 char val[2 * sizeof(tmp) + 3];
122                 if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
123                                         SDB_DOUBLE_QUOTED) < 0)
124                         snprintf(tmp, sizeof(tmp), "<error>");
126                 if (tmp[0] == '"') {
127                         /* a string; escape_string handles quoting */
128                         tmp[strlen(tmp) - 1] = '\0';
129                         escape_string(tmp + 1, val);
130                         sdb_strbuf_append(f->buf, "\"value\": %s, ", val);
131                 }
132                 else
133                         sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
134         }
135         else if (obj->type == SDB_METRIC) {
136                 if (METRIC(obj)->store.type != NULL)
137                         sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
138                 else
139                         sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
140         }
142         /* TODO: make time and interval formats configurable */
143         if (! sdb_strftime(time_str, sizeof(time_str),
144                                 "%F %T %z", obj->last_update))
145                 snprintf(time_str, sizeof(time_str), "<error>");
146         time_str[sizeof(time_str) - 1] = '\0';
148         if (! sdb_strfinterval(interval_str, sizeof(interval_str),
149                                 obj->interval))
150                 snprintf(interval_str, sizeof(interval_str), "<error>");
151         interval_str[sizeof(interval_str) - 1] = '\0';
153         sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
154                         "\"update_interval\": \"%s\", \"backends\": [",
155                         time_str, interval_str);
157         for (i = 0; i < obj->backends_num; ++i) {
158                 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
159                 if (i < obj->backends_num - 1)
160                         sdb_strbuf_append(f->buf, ",");
161         }
162         sdb_strbuf_append(f->buf, "]");
163         return 0;
164 } /* json_emit */
166 /*
167  * public API
168  */
170 sdb_store_json_formatter_t *
171 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
173         sdb_store_json_formatter_t *f;
175         if (! buf)
176                 return NULL;
178         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
179                 return NULL;
181         f = calloc(1, sizeof(*f));
182         if (! f)
183                 return NULL;
185         f->buf = buf;
186         f->context[0] = 0;
187         f->current = 0;
189         f->current_host = NULL;
191         f->type = type;
192         f->flags = flags;
193         return f;
194 } /* sdb_store_json_formatter */
196 int
197 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
199         if ((! f) || (! obj))
200                 return -1;
202         if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
203                 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
204                                 "during %s JSON serialization",
205                                 SDB_STORE_TYPE_TO_NAME(f->type));
206                 return -1;
207         }
209         /* first top-level object */
210         if (! f->context[0]) {
211                 if (obj->type != f->type) {
212                         sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
213                                         "as the first element during %s JSON serialization",
214                                         SDB_STORE_TYPE_TO_NAME(obj->type),
215                                         SDB_STORE_TYPE_TO_NAME(f->type));
216                         return -1;
217                 }
218                 if (f->flags & SDB_WANT_ARRAY)
219                         sdb_strbuf_append(f->buf, "[");
220                 assert(f->current == 0);
221                 f->context[0] = obj->type;
222                 return json_emit(f, obj);
223         }
225         if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
226                 /* new entry of a previous type or a new type on the same level;
227                  * rewind to the right state */
228                 while (f->current > 0) {
229                         if (f->context[f->current] == obj->type)
230                                 break;
231                         sdb_strbuf_append(f->buf, "}]");
232                         --f->current;
233                 }
234         }
235         if ((obj->type == f->type) && (f->type != SDB_HOST))
236                 if (obj->parent != f->current_host)
237                         sdb_strbuf_append(f->buf, "}]");
239         if (obj->type == f->context[f->current]) {
240                 /* new entry of the same type */
241                 sdb_strbuf_append(f->buf, "},");
242         }
243         else if ((f->context[f->current] == SDB_HOST)
244                         || (obj->type == SDB_ATTRIBUTE)) {
245                 assert(obj->type != SDB_HOST);
246                 /* all object types may be children of a host;
247                  * attributes may be children of any type */
248                 sdb_strbuf_append(f->buf, ", \"%ss\": [",
249                                 SDB_STORE_TYPE_TO_NAME(obj->type));
250                 ++f->current;
251         }
252         else {
253                 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
254                                 "on level %zu during JSON serialization",
255                                 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
256                 return -1;
257         }
259         json_emit(f, obj);
260         assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
261         f->context[f->current] = obj->type;
262         return 0;
263 } /* sdb_store_json_emit */
265 int
266 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
267                 sdb_store_matcher_t *filter)
269         sdb_avltree_t *trees[] = { NULL, NULL, NULL };
270         size_t i;
272         if (sdb_store_json_emit(f, obj))
273                 return -1;
275         if (obj->type == SDB_HOST) {
276                 trees[0] = HOST(obj)->attributes;
277                 trees[1] = HOST(obj)->metrics;
278                 trees[2] = HOST(obj)->services;
279         }
280         else if (obj->type == SDB_SERVICE)
281                 trees[0] = SVC(obj)->attributes;
282         else if (obj->type == SDB_METRIC)
283                 trees[0] = METRIC(obj)->attributes;
284         else if (obj->type == SDB_ATTRIBUTE)
285                 return 0;
286         else
287                 return -1;
289         for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
290                 sdb_avltree_iter_t *iter;
292                 if (! trees[i])
293                         continue;
295                 iter = sdb_avltree_get_iter(trees[i]);
296                 while (sdb_avltree_iter_has_next(iter)) {
297                         sdb_store_obj_t *child;
298                         child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
300                         if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
301                                 continue;
303                         if (sdb_store_json_emit_full(f, child, filter)) {
304                                 sdb_avltree_iter_destroy(iter);
305                                 return -1;
306                         }
307                 }
308                 sdb_avltree_iter_destroy(iter);
309         }
310         return 0;
311 } /* sdb_store_json_emit_full */
313 int
314 sdb_store_json_finish(sdb_store_json_formatter_t *f)
316         if (! f)
317                 return -1;
319         if (! f->context[0]) {
320                 /* no content */
321                 if (f->flags & SDB_WANT_ARRAY)
322                         sdb_strbuf_append(f->buf, "[]");
323                 return 0;
324         }
326         while (f->current > 0) {
327                 sdb_strbuf_append(f->buf, "}]");
328                 --f->current;
329         }
330         if (f->context[0] != SDB_HOST) {
331                 assert(f->type != SDB_HOST);
332                 sdb_strbuf_append(f->buf, "}]}");
333         }
334         else
335                 sdb_strbuf_append(f->buf, "}");
337         if (f->flags & SDB_WANT_ARRAY)
338                 sdb_strbuf_append(f->buf, "]");
339         return 0;
340 } /* sdb_store_json_finish */
342 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */