api: fromjson/tojson enum flag support
[vpp.git] / src / vat2 / jsonconvert.c
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vppinfra/cJSON.h>
17 #include <vnet/ethernet/mac_address.h>
18 #include <vnet/ip/ip6_packet.h>
19 #include <vnet/ip/ip_format_fns.h>
20 #include <vpp/api/types.h>
21 #include "jsonconvert.h"
22
23 #define _(T)                                    \
24 int vl_api_ ##T## _fromjson(cJSON *o, T *d)     \
25 {                                               \
26     if (!cJSON_IsNumber(o)) return -1;          \
27     memcpy(d, &o->valueint, sizeof(T));         \
28     return 0;                                   \
29 }
30   foreach_vat2_fromjson
31 #undef _
32
33 int vl_api_bool_fromjson(cJSON *o, bool *d)
34 {
35     if (!cJSON_IsBool(o)) return -1;
36     *d = o->valueint ? true : false;
37     return 0;
38 }
39
40 int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
41 {
42     unformat_input_t input;
43     char *p = cJSON_GetStringValue(o);
44     unformat_init_string (&input, p, strlen(p));
45     unformat(&input, "0x%U", unformat_hex_string, s);
46     return 0;
47 }
48
49 u8 *
50 u8string_fromjson(cJSON *o, char *fieldname)
51 {
52     u8 *s = 0;
53     unformat_input_t input;
54     cJSON *item = cJSON_GetObjectItem(o, fieldname);
55     if (!item) {
56         printf("Illegal JSON, no such fieldname %s\n", fieldname);
57         return 0;
58     }
59
60     char *p = cJSON_GetStringValue(item);
61     unformat_init_string (&input, p, strlen(p));
62     unformat(&input, "0x%U", unformat_hex_string, &s);
63     return s;
64 }
65
66 int
67 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
68 {
69     u8 *s = u8string_fromjson(o, fieldname);
70     if (!s) return 0;
71     memcpy(data, s, vec_len(s));
72     vec_free(s);
73     return 0;
74 }
75
76 /* Parse an IP4 address %d.%d.%d.%d. */
77 uword
78 unformat_ip4_address (unformat_input_t * input, va_list * args)
79 {
80   u8 *result = va_arg (*args, u8 *);
81   unsigned a[4];
82
83   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
84     return 0;
85
86   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
87     return 0;
88
89   result[0] = a[0];
90   result[1] = a[1];
91   result[2] = a[2];
92   result[3] = a[3];
93
94   return 1;
95 }
96
97 /* Parse an IP6 address. */
98 uword
99 unformat_ip6_address (unformat_input_t * input, va_list * args)
100 {
101   ip6_address_t *result = va_arg (*args, ip6_address_t *);
102   u16 hex_quads[8];
103   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
104   uword c, n_colon, double_colon_index;
105
106   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
107   double_colon_index = ARRAY_LEN (hex_quads);
108   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
109     {
110       hex_digit = 16;
111       if (c >= '0' && c <= '9')
112         hex_digit = c - '0';
113       else if (c >= 'a' && c <= 'f')
114         hex_digit = c + 10 - 'a';
115       else if (c >= 'A' && c <= 'F')
116         hex_digit = c + 10 - 'A';
117       else if (c == ':' && n_colon < 2)
118         n_colon++;
119       else
120         {
121           unformat_put_input (input);
122           break;
123         }
124
125       /* Too many hex quads. */
126       if (n_hex_quads >= ARRAY_LEN (hex_quads))
127         return 0;
128
129       if (hex_digit < 16)
130         {
131           hex_quad = (hex_quad << 4) | hex_digit;
132
133           /* Hex quad must fit in 16 bits. */
134           if (n_hex_digits >= 4)
135             return 0;
136
137           n_colon = 0;
138           n_hex_digits++;
139         }
140
141       /* Save position of :: */
142       if (n_colon == 2)
143         {
144           /* More than one :: ? */
145           if (double_colon_index < ARRAY_LEN (hex_quads))
146             return 0;
147           double_colon_index = n_hex_quads;
148         }
149
150       if (n_colon > 0 && n_hex_digits > 0)
151         {
152           hex_quads[n_hex_quads++] = hex_quad;
153           hex_quad = 0;
154           n_hex_digits = 0;
155         }
156     }
157
158   if (n_hex_digits > 0)
159     hex_quads[n_hex_quads++] = hex_quad;
160
161
162   {
163     word i;
164
165     /* Expand :: to appropriate number of zero hex quads. */
166     if (double_colon_index < ARRAY_LEN (hex_quads))
167       {
168         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
169
170         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
171           hex_quads[n_zero + i] = hex_quads[i];
172
173         for (i = 0; i < n_zero; i++)
174           {
175             ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
176             hex_quads[double_colon_index + i] = 0;
177           }
178
179         n_hex_quads = ARRAY_LEN (hex_quads);
180       }
181
182     /* Too few hex quads given. */
183     if (n_hex_quads < ARRAY_LEN (hex_quads))
184       return 0;
185
186     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
187       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
188
189     return 1;
190   }
191 }
192
193 u8 *
194 format_ip6_address (u8 * s, va_list * args)
195 {
196   ip6_address_t *a = va_arg (*args, ip6_address_t *);
197   u32 max_zero_run = 0, this_zero_run = 0;
198   int max_zero_run_index = -1, this_zero_run_index = 0;
199   int in_zero_run = 0, i;
200   int last_double_colon = 0;
201
202   /* Ugh, this is a pain. Scan forward looking for runs of 0's */
203   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
204     {
205       if (a->as_u16[i] == 0)
206         {
207           if (in_zero_run)
208             this_zero_run++;
209           else
210             {
211               in_zero_run = 1;
212               this_zero_run = 1;
213               this_zero_run_index = i;
214             }
215         }
216       else
217         {
218           if (in_zero_run)
219             {
220               /* offer to compress the biggest run of > 1 zero */
221               if (this_zero_run > max_zero_run && this_zero_run > 1)
222                 {
223                   max_zero_run_index = this_zero_run_index;
224                   max_zero_run = this_zero_run;
225                 }
226             }
227           in_zero_run = 0;
228           this_zero_run = 0;
229         }
230     }
231
232   if (in_zero_run)
233     {
234       if (this_zero_run > max_zero_run && this_zero_run > 1)
235         {
236           max_zero_run_index = this_zero_run_index;
237           max_zero_run = this_zero_run;
238         }
239     }
240
241   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
242     {
243       if (i == max_zero_run_index)
244         {
245           s = format (s, "::");
246           i += max_zero_run - 1;
247           last_double_colon = 1;
248         }
249       else
250         {
251           s = format (s, "%s%x",
252                       (last_double_colon || i == 0) ? "" : ":",
253                       clib_net_to_host_u16 (a->as_u16[i]));
254           last_double_colon = 0;
255         }
256     }
257
258   return s;
259 }
260
261 void *vl_api_ip4_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_address_t *a)
262 {
263     unformat_input_t input;
264     char *p = cJSON_GetStringValue(o);
265     if (!p) return 0;
266     unformat_init_string (&input, p, strlen(p));
267     unformat(&input, "%U", unformat_ip4_address, a);
268     return mp;
269 }
270
271 void *vl_api_ip4_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_prefix_t *a)
272 {
273     unformat_input_t input;
274     char *p = cJSON_GetStringValue(o);
275     if (!p) return 0;
276     unformat_init_string (&input, p, strlen(p));
277     unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
278     return mp;
279 }
280
281 void *vl_api_ip4_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_prefix_t *a)
282 {
283   return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
284 }
285 void *vl_api_ip6_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_address_t *a)
286 {
287     unformat_input_t input;
288     char *p = cJSON_GetStringValue(o);
289     if (!p) return 0;
290     unformat_init_string (&input, p, strlen(p));
291     unformat(&input, "%U", unformat_ip6_address, a);
292     return mp;
293 }
294
295 void *vl_api_ip6_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_prefix_t *a)
296 {
297   unformat_input_t input;
298   char *p = cJSON_GetStringValue(o);
299   if (!p) return 0;
300   unformat_init_string (&input, p, strlen(p));
301   unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
302   return mp;
303 }
304
305 void *vl_api_ip6_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_prefix_t *a)
306 {
307   return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
308 }
309
310 void *vl_api_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_address_t *a)
311 {
312   unformat_input_t input;
313
314   char *p = cJSON_GetStringValue(o);
315   if (!p) return 0;
316   unformat_init_string (&input, p, strlen(p));
317   if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
318     a->af = ADDRESS_IP4;
319   else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
320     a->af = ADDRESS_IP6;
321   else
322     return (0);
323   return mp;
324 }
325
326 void *vl_api_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_prefix_t *a)
327 {
328   unformat_input_t input;
329
330   char *p = cJSON_GetStringValue(o);
331
332   if (!p) return 0;
333   unformat_init_string (&input, p, strlen(p));
334   int plen;
335   if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
336     a->address.af = ADDRESS_IP4;
337   else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
338     a->address.af = ADDRESS_IP6;
339   else
340     return (0);
341   a->len = plen;
342   return mp;
343 }
344
345 void *vl_api_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_prefix_t *a)
346 {
347   return vl_api_prefix_t_fromjson(mp, len, o, a);
348 }
349
350 uword
351 unformat_mac_address (unformat_input_t * input, va_list * args)
352 {
353   mac_address_t *mac = va_arg (*args, mac_address_t *);
354   u32 i, a[3];
355
356   if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
357                 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
358                 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
359     return (1);
360   else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
361     {
362       for (i = 0; i < ARRAY_LEN (a); i++)
363         if (a[i] >= (1 << 16))
364           return 0;
365
366       mac->bytes[0] = (a[0] >> 8) & 0xff;
367       mac->bytes[1] = (a[0] >> 0) & 0xff;
368       mac->bytes[2] = (a[1] >> 8) & 0xff;
369       mac->bytes[3] = (a[1] >> 0) & 0xff;
370       mac->bytes[4] = (a[2] >> 8) & 0xff;
371       mac->bytes[5] = (a[2] >> 0) & 0xff;
372
373       return (1);
374     }
375   return (0);
376 }
377
378 void *vl_api_mac_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_mac_address_t *a)
379 {
380   unformat_input_t input;
381
382   char *p = cJSON_GetStringValue(o);
383   unformat_init_string (&input, p, strlen(p));
384   unformat(&input, "%U", unformat_mac_address, a);
385   return mp;
386 }
387
388 /* Format an IP4 address. */
389 u8 *
390 format_ip4_address (u8 * s, va_list * args)
391 {
392   u8 *a = va_arg (*args, u8 *);
393   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
394 }
395
396 int
397 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
398 {
399   /* copy without nul terminator */
400   u32 len = strlen (buf);
401   if (len > 0)
402     clib_memcpy_fast (str->buf, buf, len);
403   str->length = htonl (len);
404   return len + sizeof (u32);
405 }
406
407 u8 *
408 format_vl_api_interface_index_t (u8 *s, va_list *args)
409 {
410   u32 *a = va_arg (*args, u32 *);
411   return format (s, "%u", *a);
412 }
413
414 uword
415 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
416 {
417     u32 *a = va_arg (*args, u32 *);
418
419     if (!unformat (input, "%u", a))
420         return 0;
421     return 1;
422 }
423
424 void
425 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
426 {
427
428     if (astr == 0) return;
429     u32 length = clib_net_to_host_u32 (astr->length);
430
431     char *cstr = malloc(length + 1);
432     memcpy(cstr, astr->buf, length);
433     cstr[length] = '\0';
434     cJSON_AddStringToObject(object, name, cstr);
435     free(cstr);
436 }
437
438 u8 *
439 format_vl_api_timestamp_t(u8 * s, va_list * args)
440 {
441     f64 timestamp = va_arg (*args, f64);
442     struct tm *tm;
443     word msec;
444
445     time_t t = timestamp;
446     tm = gmtime (&t);
447     msec = 1e6 * (timestamp - t);
448     return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
449                    1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
450                    tm->tm_sec, msec);
451 }
452
453 u8 *
454 format_vl_api_timedelta_t(u8 * s, va_list * args)
455 {
456     return format_vl_api_timestamp_t(s, args);
457 }
458
459 uword
460 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
461 {
462     return 0;
463 }
464
465 uword
466 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
467 {
468     return 0;
469 }
470 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
471 {
472     return 0;
473 }
474 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
475 {
476     return 0;
477 }
478
479 cJSON *
480 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
481   u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
482   cJSON *o = cJSON_CreateString((char *)s);
483   vec_free(s);
484   return o;
485 }
486 cJSON *
487 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
488   u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
489   cJSON *o = cJSON_CreateString((char *)s);
490   vec_free(s);
491   return o;
492 }
493 cJSON *
494 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
495   u8 *s = format(0, "%U", format_vl_api_address_t, a);
496   cJSON *o = cJSON_CreateString((char *)s);
497   vec_free(s);
498   return o;
499 }
500 u8 *
501 format_vl_api_mac_address_t (u8 * s, va_list * args)
502 {
503   const mac_address_t *mac = va_arg (*args, mac_address_t *);
504
505   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
506                  mac->bytes[0], mac->bytes[1], mac->bytes[2],
507                  mac->bytes[3], mac->bytes[4], mac->bytes[5]);
508 }
509 #define _(T)                                                \
510   cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) {   \
511   u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a);      \
512   cJSON *o = cJSON_CreateString((char *)s);                 \
513   vec_free(s);                                              \
514   return o;                                                 \
515   }
516 foreach_vat2_tojson
517 #undef _