Move internal config parser to a separate file
[trust_router.git] / common / tr_config_internal.c
1 /*
2  * Copyright (c) 2012-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 #include <tr_debug.h>
38 #include <tr_config.h>
39 #include <tr_cfgwatch.h>
40
41 /**
42  * Parse internal configuration JSON
43  *
44  * @param trc configuration structure to fill in
45  * @param jint internal configuration JSON object
46  * @return TR_CFG_SUCCESS or an error code
47  */
48 TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint)
49 {
50   json_t *jmtd = NULL;
51   json_t *jtidsp = NULL;
52   json_t *jtrpsp = NULL;
53   json_t *jhname = NULL;
54   json_t *jlog = NULL;
55   json_t *jconthres = NULL;
56   json_t *jlogthres = NULL;
57   json_t *jcfgpoll = NULL;
58   json_t *jcfgsettle = NULL;
59   json_t *jroutesweep = NULL;
60   json_t *jrouteupdate = NULL;
61   json_t *jtidreq_timeout = NULL;
62   json_t *jtidresp_numer = NULL;
63   json_t *jtidresp_denom = NULL;
64   json_t *jrouteconnect = NULL;
65
66   if ((!trc) || (!jint))
67     return TR_CFG_BAD_PARAMS;
68
69   if (NULL == trc->internal) {
70     if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
71       return TR_CFG_NOMEM;
72   }
73
74   if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
75     if (json_is_number(jmtd)) {
76       trc->internal->max_tree_depth = json_integer_value(jmtd);
77     } else {
78       tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
79       return TR_CFG_NOPARSE;
80     }
81   } else {
82     /* If not configured, use the default */
83     trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
84   }
85   if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
86     if (json_is_number(jtidsp)) {
87       trc->internal->tids_port = json_integer_value(jtidsp);
88     } else {
89       tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
90       return TR_CFG_NOPARSE;
91     }
92   } else {
93     /* If not configured, use the default */
94     trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
95   }
96   if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
97     if (json_is_number(jtrpsp)) {
98       trc->internal->trps_port = json_integer_value(jtrpsp);
99     } else {
100       tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
101       return TR_CFG_NOPARSE;
102     }
103   } else {
104     /* If not configured, use the default */
105     trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
106   }
107   if (NULL != (jhname = json_object_get(jint, "hostname"))) {
108     if (json_is_string(jhname)) {
109       trc->internal->hostname = talloc_strdup(trc->internal, json_string_value(jhname));
110     } else {
111       tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
112       return TR_CFG_NOPARSE;
113     }
114   }
115   if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
116     if (json_is_number(jcfgpoll)) {
117       trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
118     } else {
119       tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
120       return TR_CFG_NOPARSE;
121     }
122   } else {
123     trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
124   }
125
126   if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
127     if (json_is_number(jcfgsettle)) {
128       trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
129     } else {
130       tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
131       return TR_CFG_NOPARSE;
132     }
133   } else {
134     trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
135   }
136
137   if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
138     if (json_is_number(jrouteconnect)) {
139       trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
140     } else {
141       tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
142       return TR_CFG_NOPARSE;
143     }
144   } else {
145     /* if not configured, use the default */
146     trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
147   }
148
149   if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
150     if (json_is_number(jroutesweep)) {
151       trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
152     } else {
153       tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
154       return TR_CFG_NOPARSE;
155     }
156   } else {
157     /* if not configured, use the default */
158     trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
159   }
160
161   if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
162     if (json_is_number(jrouteupdate)) {
163       trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
164     } else {
165       tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
166       return TR_CFG_NOPARSE;
167     }
168   } else {
169     /* if not configured, use the default */
170     trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
171   }
172
173   if (NULL != (jtidreq_timeout = json_object_get(jint, "tid_request_timeout"))) {
174     if (json_is_number(jtidreq_timeout)) {
175       trc->internal->tid_req_timeout = json_integer_value(jtidreq_timeout);
176     } else {
177       tr_debug("tr_cfg_parse_internal: Parsing error, tid_request_timeout is not a number.");
178       return TR_CFG_NOPARSE;
179     }
180   } else {
181     /* if not configured, use the default */
182     trc->internal->tid_req_timeout=TR_DEFAULT_TID_REQ_TIMEOUT;
183   }
184
185   if (NULL != (jtidresp_numer = json_object_get(jint, "tid_response_numerator"))) {
186     if (json_is_number(jtidresp_numer)) {
187       trc->internal->tid_resp_numer = json_integer_value(jtidresp_numer);
188     } else {
189       tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_numerator is not a number.");
190       return TR_CFG_NOPARSE;
191     }
192   } else {
193     /* if not configured, use the default */
194     trc->internal->tid_resp_numer=TR_DEFAULT_TID_RESP_NUMER;
195   }
196
197   if (NULL != (jtidresp_denom = json_object_get(jint, "tid_response_denominator"))) {
198     if (json_is_number(jtidresp_denom)) {
199       trc->internal->tid_resp_denom = json_integer_value(jtidresp_denom);
200     } else {
201       tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_denominator is not a number.");
202       return TR_CFG_NOPARSE;
203     }
204   } else {
205     /* if not configured, use the default */
206     trc->internal->tid_resp_denom=TR_DEFAULT_TID_RESP_DENOM;
207   }
208
209   if (NULL != (jlog = json_object_get(jint, "logging"))) {
210     if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
211       if (json_is_string(jlogthres)) {
212         trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
213       } else {
214         tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
215         return TR_CFG_NOPARSE;
216       }
217     } else {
218       /* If not configured, use the default */
219       trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
220     }
221
222     if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
223       if (json_is_string(jconthres)) {
224         trc->internal->console_threshold = str2sev(json_string_value(jconthres));
225       } else {
226         tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
227         return TR_CFG_NOPARSE;
228       }
229     } else {
230       /* If not configured, use the default */
231       trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
232     }
233   } else {
234     /* If not configured, use the default */
235     trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
236     trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
237   }
238
239   tr_debug("tr_cfg_parse_internal: Internal config parsed.");
240   return TR_CFG_SUCCESS;
241 }