api: vat2 and json autogeneration for api messages
[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 (a->af == ADDRESS_IP4)
318     unformat(&input, "%U", unformat_ip4_address, &a->un.ip4);
319   else if (a->af == ADDRESS_IP6)
320     unformat(&input, "%U", unformat_ip6_address, &a->un.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   if (!p) return 0;
332   unformat_init_string (&input, p, strlen(p));
333   if (a->address.af == ADDRESS_IP4)
334     unformat(&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &a->len);
335   else if (a->address.af == ADDRESS_IP6)
336     unformat(&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &a->len);
337   else
338     return 0;
339   return mp;
340 }
341
342 void *vl_api_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_prefix_t *a)
343 {
344   return vl_api_prefix_t_fromjson(mp, len, o, a);
345 }
346
347 uword
348 unformat_mac_address (unformat_input_t * input, va_list * args)
349 {
350   mac_address_t *mac = va_arg (*args, mac_address_t *);
351   u32 i, a[3];
352
353   if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
354                 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
355                 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
356     return (1);
357   else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
358     {
359       for (i = 0; i < ARRAY_LEN (a); i++)
360         if (a[i] >= (1 << 16))
361           return 0;
362
363       mac->bytes[0] = (a[0] >> 8) & 0xff;
364       mac->bytes[1] = (a[0] >> 0) & 0xff;
365       mac->bytes[2] = (a[1] >> 8) & 0xff;
366       mac->bytes[3] = (a[1] >> 0) & 0xff;
367       mac->bytes[4] = (a[2] >> 8) & 0xff;
368       mac->bytes[5] = (a[2] >> 0) & 0xff;
369
370       return (1);
371     }
372   return (0);
373 }
374
375 void *vl_api_mac_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_mac_address_t *a)
376 {
377   unformat_input_t input;
378
379   char *p = cJSON_GetStringValue(o);
380   unformat_init_string (&input, p, strlen(p));
381   unformat(&input, "%U", unformat_mac_address, a);
382   return mp;
383 }
384
385 /* Format an IP4 address. */
386 u8 *
387 format_ip4_address (u8 * s, va_list * args)
388 {
389   u8 *a = va_arg (*args, u8 *);
390   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
391 }
392
393 int
394 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
395 {
396   /* copy without nul terminator */
397   u32 len = strlen (buf);
398   if (len > 0)
399     clib_memcpy_fast (str->buf, buf, len);
400   str->length = htonl (len);
401   return len + sizeof (u32);
402 }
403
404 u8 *
405 format_vl_api_interface_index_t (u8 *s, va_list *args)
406 {
407   u32 *a = va_arg (*args, u32 *);
408   return format (s, "%u", *a);
409 }
410
411 uword
412 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
413 {
414     u32 *a = va_arg (*args, u32 *);
415
416     if (!unformat (input, "%u", a))
417         return 0;
418     return 1;
419 }
420
421 void
422 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
423 {
424
425     if (astr == 0) return;
426     u32 length = clib_net_to_host_u32 (astr->length);
427
428     char *cstr = malloc(length + 1);
429     memcpy(cstr, astr->buf, length);
430     cstr[length] = '\0';
431     cJSON_AddStringToObject(object, name, cstr);
432     free(cstr);
433 }
434
435 u8 *
436 format_vl_api_timestamp_t(u8 * s, va_list * args)
437 {
438     f64 timestamp = va_arg (*args, f64);
439     struct tm *tm;
440     word msec;
441
442     time_t t = timestamp;
443     tm = gmtime (&t);
444     msec = 1e6 * (timestamp - t);
445     return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
446                    1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
447                    tm->tm_sec, msec);
448 }
449
450 u8 *
451 format_vl_api_timedelta_t(u8 * s, va_list * args)
452 {
453     return format_vl_api_timestamp_t(s, args);
454 }
455
456 uword
457 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
458 {
459     return 0;
460 }
461
462 uword
463 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
464 {
465     return 0;
466 }
467 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
468 {
469     return 0;
470 }
471 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
472 {
473     return 0;
474 }
475
476 cJSON *
477 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
478   u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
479   cJSON *o = cJSON_CreateString((char *)s);
480   vec_free(s);
481   return o;
482 }
483 cJSON *
484 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
485   u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
486   cJSON *o = cJSON_CreateString((char *)s);
487   vec_free(s);
488   return o;
489 }
490 cJSON *
491 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
492   u8 *s = format(0, "%U", format_vl_api_address_t, a);
493   cJSON *o = cJSON_CreateString((char *)s);
494   vec_free(s);
495   return o;
496 }
497 u8 *
498 format_vl_api_mac_address_t (u8 * s, va_list * args)
499 {
500   const mac_address_t *mac = va_arg (*args, mac_address_t *);
501
502   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
503                  mac->bytes[0], mac->bytes[1], mac->bytes[2],
504                  mac->bytes[3], mac->bytes[4], mac->bytes[5]);
505 }
506 #define _(T)                                                \
507   cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) {   \
508   u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a);      \
509   cJSON *o = cJSON_CreateString((char *)s);                 \
510   vec_free(s);                                              \
511   return o;                                                 \
512   }
513 foreach_vat2_tojson
514 #undef _