19af8c052b8ee3198af38d33111b7ede1f5e2a43
[trust_router.git] / mon / tr_mon_resp_encode.c
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <talloc.h>
36 #include <jansson.h>
37
38 #include <tr_mon.h>
39
40 /* Helper for encoding. Adds a newly allocated JSON object to
41  * jobj. If the allocation or setting fails, returns NULL after
42  * cleaning up. */
43 #define object_set_or_free_and_return(jobj, tmp_jval, key, jval)  \
44   do {                                                            \
45     (tmp_jval) = (jval);                                          \
46     if ( (tmp_jval) == NULL) {                                    \
47       json_decref(jobj);                                          \
48       return NULL;                                                \
49     }                                                             \
50     if (json_object_set_new((jobj), (key), (tmp_jval)) == -1) {   \
51       json_decref(tmp_jval);                                      \
52       json_decref(jobj);                                          \
53       return NULL;                                                \
54     }                                                             \
55   } while(0)
56
57 /**
58  * Encode a monitoring response as a JSON object
59  *
60  * Caller must ensure json_decref() is used to free the return value.
61  *
62  * @param resp response to encode
63  * @return response as a newly allocated JSON object
64  */
65 json_t *tr_mon_resp_encode(TR_MON_RESP *resp)
66 {
67   json_t *resp_json = NULL;
68   json_t *jval = NULL;
69   const char *cmd_str = NULL;
70
71   /* Get a JSON object */
72   resp_json = json_object();
73   if (resp_json == NULL)
74     return NULL;
75
76   /* Add properties, cleaning up and returning NULL on failure */
77   object_set_or_free_and_return(resp_json, jval, "code",    json_integer(resp->code));
78   object_set_or_free_and_return(resp_json, jval, "message", tr_name_to_json_string(resp->message));
79
80   /* If we have a payload, add it */
81   if (resp->payload) {
82     cmd_str = cmd_to_string(resp->req->command); // key for the response payload
83     object_set_or_free_and_return(resp_json, jval, cmd_str, resp->payload);
84   }
85
86   return resp_json;
87 }