vlib: improvement to automatic core pinning
[vpp.git] / src / vat / json_format.h
1 /*
2  *------------------------------------------------------------------
3  * json_format.h
4  *
5  * Copyright (c) 2015 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #ifndef __JSON_FORMAT_H__
21 #define __JSON_FORMAT_H__
22
23 #include <vppinfra/clib.h>
24 #include <vppinfra/format.h>
25 #ifdef __FreeBSD__
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #endif /* __FreeBSD__ */
30 #include <netinet/ip.h>
31
32 /* JSON value type */
33 typedef enum
34 {
35   VAT_JSON_NONE,
36   VAT_JSON_OBJECT,
37   VAT_JSON_ARRAY,
38   VAT_JSON_STRING,
39   VAT_JSON_REAL,
40   VAT_JSON_UINT,
41   VAT_JSON_INT,
42   VAT_JSON_IPV4,
43   VAT_JSON_IPV6,
44   VAT_JSON_MAX
45 } vat_json_val_type_t;
46
47 typedef struct vat_json_node_s vat_json_node_t;
48 typedef struct vat_json_pair_s vat_json_pair_t;
49
50 /* JSON object structure */
51 struct vat_json_node_s
52 {
53   vat_json_val_type_t type;
54   union
55   {
56     vat_json_pair_t *pairs;
57     vat_json_node_t *array;
58     u8 *string;
59     struct in_addr ip4;
60     struct in6_addr ip6;
61     u64 uint;
62     i64 sint;
63     f64 real;
64   };
65 };
66
67 struct vat_json_pair_s
68 {
69   const char *name;
70   vat_json_node_t value;
71 };
72
73 void vat_json_print (FILE * ofp, vat_json_node_t * node);
74 void vat_json_free (vat_json_node_t * node);
75
76 static_always_inline void
77 vat_json_init_object (vat_json_node_t * json)
78 {
79   json->type = VAT_JSON_OBJECT;
80   json->pairs = NULL;
81 }
82
83 static_always_inline void
84 vat_json_init_array (vat_json_node_t * json)
85 {
86   json->type = VAT_JSON_ARRAY;
87   json->array = NULL;
88 }
89
90 static_always_inline void
91 vat_json_set_string (vat_json_node_t * json, u8 * str)
92 {
93   json->type = VAT_JSON_STRING;
94   json->string = str;
95 }
96
97 static_always_inline void
98 vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
99 {
100   u8 *ns = NULL;
101   int len = strlen ((const char *) str);
102   vec_validate_init_c_string (ns, str, len);
103   vat_json_set_string (json, ns);
104 }
105
106 static_always_inline void
107 vat_json_set_int (vat_json_node_t * json, i64 num)
108 {
109   json->type = VAT_JSON_INT;
110   json->sint = num;
111 }
112
113 static_always_inline void
114 vat_json_set_uint (vat_json_node_t * json, u64 num)
115 {
116   json->type = VAT_JSON_UINT;
117   json->uint = num;
118 }
119
120 static_always_inline void
121 vat_json_set_real (vat_json_node_t * json, f64 real)
122 {
123   json->type = VAT_JSON_REAL;
124   json->real = real;
125 }
126
127 static_always_inline void
128 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
129 {
130   json->type = VAT_JSON_IPV4;
131   json->ip4 = ip4;
132 }
133
134 static_always_inline void
135 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
136 {
137   json->type = VAT_JSON_IPV6;
138   json->ip6 = ip6;
139 }
140
141 static_always_inline vat_json_node_t *
142 vat_json_object_add (vat_json_node_t * json, const char *name)
143 {
144   ASSERT (VAT_JSON_OBJECT == json->type);
145   uword pos = vec_len (json->pairs);
146   vec_validate (json->pairs, pos);
147   json->pairs[pos].name = name;
148   return &json->pairs[pos].value;
149 }
150
151 static_always_inline vat_json_node_t *
152 vat_json_array_add (vat_json_node_t * json)
153 {
154   ASSERT (VAT_JSON_ARRAY == json->type);
155   uword pos = vec_len (json->array);
156   vec_validate (json->array, pos);
157   return &json->array[pos];
158 }
159
160 static_always_inline vat_json_node_t *
161 vat_json_object_add_list (vat_json_node_t * json, const char *name)
162 {
163   vat_json_node_t *array_node = vat_json_object_add (json, name);
164   vat_json_init_array (array_node);
165   return array_node;
166 }
167
168 static_always_inline void
169 vat_json_object_add_string_copy (vat_json_node_t * json,
170                                  const char *name, u8 * str)
171 {
172   vat_json_set_string_copy (vat_json_object_add (json, name), str);
173 }
174
175 static_always_inline void
176 vat_json_object_add_uint (vat_json_node_t * json,
177                           const char *name, u64 number)
178 {
179   vat_json_set_uint (vat_json_object_add (json, name), number);
180 }
181
182 static_always_inline void
183 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
184 {
185   vat_json_set_int (vat_json_object_add (json, name), number);
186 }
187
188 static_always_inline void
189 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
190 {
191   vat_json_set_real (vat_json_object_add (json, name), real);
192 }
193
194 static_always_inline void
195 vat_json_object_add_ip4 (vat_json_node_t * json,
196                          const char *name, struct in_addr ip4)
197 {
198   vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
199 }
200
201 static_always_inline void
202 vat_json_object_add_ip6 (vat_json_node_t * json,
203                          const char *name, struct in6_addr ip6)
204 {
205   vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
206 }
207
208 static_always_inline void
209 vat_json_array_add_int (vat_json_node_t * json, i64 number)
210 {
211   vat_json_set_int (vat_json_array_add (json), number);
212 }
213
214 static_always_inline void
215 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
216 {
217   vat_json_set_uint (vat_json_array_add (json), number);
218 }
219
220 static_always_inline void
221 vat_json_object_add_bytes (vat_json_node_t * json,
222                            const char *name, u8 * array, uword size)
223 {
224   ASSERT (VAT_JSON_OBJECT == json->type);
225   vat_json_node_t *json_array = vat_json_object_add (json, name);
226   vat_json_init_array (json_array);
227   int i;
228   for (i = 0; i < size; i++)
229     {
230       vat_json_array_add_uint (json_array, array[i]);
231     }
232 }
233
234 static_always_inline vat_json_node_t *
235 vat_json_object_get_element (vat_json_node_t * json, const char *name)
236 {
237   int i = 0;
238
239   ASSERT (VAT_JSON_OBJECT == json->type);
240   for (i = 0; i < vec_len (json->pairs); i++)
241     {
242       if (0 == strcmp (json->pairs[i].name, name))
243         {
244           return &json->pairs[i].value;
245         }
246     }
247   return NULL;
248 }
249
250 #endif /* __JSON_FORMAT_H__ */
251
252 /*
253  * fd.io coding-style-patch-verification: ON
254  *
255  * Local Variables:
256  * eval: (c-set-style "gnu")
257  * End:
258  */