]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb-idlc.in
raft: Send all missing logs in one single append_request.
[mirror_ovs.git] / ovsdb / ovsdb-idlc.in
1 #! @PYTHON3@
2
3 from __future__ import print_function
4 import getopt
5 import os
6 import re
7 import sys
8
9 import ovs.json
10 import ovs.db.error
11 import ovs.db.schema
12 from ovs.db.types import StringType, IntegerType, RealType
13
14 argv0 = sys.argv[0]
15
16 def parseSchema(filename):
17 return ovs.db.schema.IdlSchema.from_json(ovs.json.from_file(filename))
18
19 def annotateSchema(schemaFile, annotationFile):
20 schemaJson = ovs.json.from_file(schemaFile)
21 exec(compile(open(annotationFile, "rb").read(), annotationFile, 'exec'), globals(), {"s": schemaJson})
22 ovs.json.to_stream(schemaJson, sys.stdout)
23 sys.stdout.write('\n')
24
25 def constify(cType, const):
26 if (const
27 and cType.endswith('*') and
28 (cType == 'char **' or not cType.endswith('**'))):
29 return 'const %s' % cType
30 else:
31 return cType
32
33 def cMembers(prefix, tableName, columnName, column, const, refTable=True):
34 comment = ""
35 type = column.type
36
37 if type.is_smap():
38 comment = """
39 /* Sets the "%(c)s" column's value from the "%(t)s" table in 'row'
40 * to '%(c)s'.
41 *
42 * The caller retains ownership of '%(c)s' and everything in it. */""" \
43 % {'c': columnName,
44 't': tableName}
45 return (comment, [{'name': columnName,
46 'type': 'struct smap ',
47 'comment': ''}])
48
49 comment = """\n/* Sets the "%s" column from the "%s" table in """\
50 """'row' to\n""" % (columnName, tableName)
51
52 if type.n_min == 1 and type.n_max == 1:
53 singleton = True
54 pointer = ''
55 else:
56 singleton = False
57 if type.is_optional_pointer():
58 pointer = ''
59 else:
60 pointer = '*'
61
62
63 if type.value:
64 keyName = "key_%s" % columnName
65 valueName = "value_%s" % columnName
66
67 key = {'name': keyName,
68 'type': constify(type.key.toCType(prefix, refTable) + pointer, const),
69 'comment': ''}
70 value = {'name': valueName,
71 'type': constify(type.value.toCType(prefix, refTable) + pointer, const),
72 'comment': ''}
73
74 if singleton:
75 comment += " * the map with key '%s' and value '%s'\n *" \
76 % (keyName, valueName)
77 else:
78 comment += " * the map with keys '%s' and values '%s'\n *" \
79 % (keyName, valueName)
80 members = [key, value]
81 else:
82 m = {'name': columnName,
83 'type': constify(type.key.toCType(prefix, refTable) + pointer, const),
84 'comment': type.cDeclComment()}
85
86 if singleton:
87 comment += " * '%s'" % columnName
88 else:
89 comment += " * the '%s' set" % columnName
90 members = [m]
91
92 if not singleton and not type.is_optional_pointer():
93 sizeName = "n_%s" % columnName
94
95 comment += " with '%s' entries" % sizeName
96 members.append({'name': sizeName,
97 'type': 'size_t ',
98 'comment': ''})
99
100 comment += ".\n"
101
102 if type.is_optional() and not type.is_optional_pointer():
103 comment += """ *
104 * '%s' may be 0 or 1; if it is 0, then '%s'
105 * may be NULL.\n""" \
106 % ("n_%s" % columnName, columnName)
107
108 if type.is_optional_pointer():
109 comment += """ *
110 * If "%s" is null, the column will be the empty set,
111 * otherwise it will contain the specified value.\n""" % columnName
112
113 if type.constraintsToEnglish():
114 comment += """ *
115 * Argument constraints: %s\n""" \
116 % type.constraintsToEnglish(lambda s : '"%s"' % s)
117
118 comment += " *\n * The caller retains ownership of the arguments. */"
119
120 return (comment, members)
121
122 # This is equivalent to sorted(table.columns.items()), except that the
123 # sorting includes a topological component: if column B has a
124 # dependency on column A, then A will be sorted before B.
125 def sorted_columns(table):
126 input = []
127 for name, column in table.columns.items():
128 dependencies = column.extensions.get('dependencies', [])
129 for d in dependencies:
130 if d not in table.columns:
131 sys.stderr.write("Table %s column %s depends on column %s "
132 "but there is no such column\n"
133 % (table.name, name, d))
134 sys.exit(1)
135 input += [(name, column, set(dependencies))]
136
137 output = []
138 satisfied_dependencies = set()
139 while input:
140 done = []
141 next = []
142 for name, column, dependencies in input:
143 if dependencies <= satisfied_dependencies:
144 done += [(name, column)]
145 else:
146 next += [(name, column, dependencies)]
147
148 if not done:
149 sys.stderr.write("Table %s columns have circular dependencies\n"
150 % table.name)
151 sys.exit(1)
152
153 for name, column in done:
154 satisfied_dependencies.add(name)
155 output += sorted(done)
156 input = next
157 return output
158
159 # If a column name in the schema is a C or C++ keyword, append an underscore
160 # to the column name.
161 def replace_cplusplus_keyword(schema):
162 keywords = {'alignas', 'alignof', 'and', 'and_eq', 'asm', 'auto', 'bitand',
163 'bitor', 'bool', 'break', 'case', 'catch', 'char', 'char16_t',
164 'char32_t', 'class', 'compl', 'concept', 'const', 'const_cast',
165 'constexpr', 'continue', 'decltype', 'default', 'delete', 'do',
166 'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export',
167 'extern', 'false', 'float', 'for', 'friend', 'goto', 'if',
168 'inline', 'int', 'long', 'mutable', 'namespace', 'new',
169 'noexcept', 'not', 'not_eq', 'nullptr', 'operator', 'or',
170 'or_eq', 'private', 'protected', 'public', 'register',
171 'reinterpret_cast', 'requires', 'restrict', 'return', 'short',
172 'signed', 'sizeof', 'static', 'static_assert', 'static_cast',
173 'struct', 'switch', 'template', 'this', 'thread_local',
174 'throw', 'true', 'try', 'typedef', 'typeid', 'typename',
175 'union', 'unsigned', 'using', 'virtual', 'void', 'volatile',
176 'wchar_t', 'while', 'xor', 'xor_eq'}
177
178 for tableName, table in schema.tables.items():
179 for columnName in list(table.columns):
180 if columnName in keywords:
181 table.columns[columnName + '_'] = table.columns.pop(columnName)
182
183 def printCIDLHeader(schemaFile):
184 schema = parseSchema(schemaFile)
185 replace_cplusplus_keyword(schema)
186 prefix = schema.idlPrefix
187 print('''\
188 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
189
190 #ifndef %(prefix)sIDL_HEADER
191 #define %(prefix)sIDL_HEADER 1
192
193 #include <stdbool.h>
194 #include <stddef.h>
195 #include <stdint.h>
196 #include "ovsdb-data.h"
197 #include "ovsdb-idl-provider.h"
198 #include "smap.h"
199 #include "uuid.h"
200
201 #ifdef __cplusplus
202 extern "C" {
203 #endif
204 %(hDecls)s''' % {'prefix': prefix.upper(),
205 'hDecls': schema.hDecls})
206
207 for tableName, table in sorted(schema.tables.items()):
208 structName = "%s%s" % (prefix, tableName.lower())
209
210 print("\f")
211 print("/* %s table. */" % tableName)
212 print("struct %s {" % structName)
213 print("\tstruct ovsdb_idl_row header_;")
214 for columnName, column in sorted_columns(table):
215 print("\n\t/* %s column. */" % columnName)
216 if column.extensions.get("members"):
217 print("\t%s" % column.extensions["members"])
218 continue
219 comment, members = cMembers(prefix, tableName,
220 columnName, column, False)
221 for member in members:
222 print("\t%(type)s%(name)s;%(comment)s" % member)
223 print("};")
224
225 # Column indexes.
226 printEnum("%s_column_id" % structName.lower(), ["%s_COL_%s" % (structName.upper(), columnName.upper())
227 for columnName, column in sorted_columns(table)]
228 + ["%s_N_COLUMNS" % structName.upper()])
229
230 print("")
231 for columnName in table.columns:
232 print("#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
233 's': structName,
234 'S': structName.upper(),
235 'c': columnName,
236 'C': columnName.upper()})
237
238 print("\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper()))
239
240 print('''
241 const struct %(s)s_table *%(s)s_table_get(const struct ovsdb_idl *);
242 const struct %(s)s *%(s)s_table_first(const struct %(s)s_table *);
243
244 #define %(S)s_TABLE_FOR_EACH(ROW, TABLE) \\
245 for ((ROW) = %(s)s_table_first(TABLE); \\
246 (ROW); \\
247 (ROW) = %(s)s_next(ROW))
248 #define %(S)s_TABLE_FOR_EACH_SAFE(ROW, NEXT, TABLE) \\
249 for ((ROW) = %(s)s_table_first(TABLE); \\
250 (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
251 (ROW) = (NEXT))
252
253 const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uuid *);
254 const struct %(s)s *%(s)s_table_get_for_uuid(const struct %(s)s_table *, const struct uuid *);
255 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
256 const struct %(s)s *%(s)s_next(const struct %(s)s *);
257 #define %(S)s_FOR_EACH(ROW, IDL) \\
258 for ((ROW) = %(s)s_first(IDL); \\
259 (ROW); \\
260 (ROW) = %(s)s_next(ROW))
261 #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\
262 for ((ROW) = %(s)s_first(IDL); \\
263 (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
264 (ROW) = (NEXT))
265
266 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *);
267 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change);
268 const struct %(s)s *%(s)s_track_get_first(const struct ovsdb_idl *);
269 const struct %(s)s *%(s)s_track_get_next(const struct %(s)s *);
270 #define %(S)s_FOR_EACH_TRACKED(ROW, IDL) \\
271 for ((ROW) = %(s)s_track_get_first(IDL); \\
272 (ROW); \\
273 (ROW) = %(s)s_track_get_next(ROW))
274
275 const struct %(s)s *%(s)s_table_track_get_first(const struct %(s)s_table *);
276 #define %(S)s_TABLE_FOR_EACH_TRACKED(ROW, TABLE) \\
277 for ((ROW) = %(s)s_table_track_get_first(TABLE); \\
278 (ROW); \\
279 (ROW) = %(s)s_track_get_next(ROW))
280
281
282 /* Returns true if 'row' was inserted since the last change tracking reset. */
283 static inline bool %(s)s_is_new(const struct %(s)s *row)
284 {
285 return %(s)s_row_get_seqno(row, OVSDB_IDL_CHANGE_MODIFY) == 0;
286 }
287
288 /* Returns true if 'row' was deleted since the last change tracking reset. */
289 static inline bool %(s)s_is_deleted(const struct %(s)s *row)
290 {
291 return %(s)s_row_get_seqno(row, OVSDB_IDL_CHANGE_DELETE) > 0;
292 }
293
294 void %(s)s_index_destroy_row(const struct %(s)s *);
295
296 struct %(s)s *%(s)s_index_find(struct ovsdb_idl_index *, const struct %(s)s *);
297
298 int %(s)s_index_compare(
299 struct ovsdb_idl_index *,
300 const struct %(s)s *,
301 const struct %(s)s *);
302 struct ovsdb_idl_cursor %(s)s_cursor_first(struct ovsdb_idl_index *);
303 struct ovsdb_idl_cursor %(s)s_cursor_first_eq(
304 struct ovsdb_idl_index *, const struct %(s)s *);
305 struct ovsdb_idl_cursor %(s)s_cursor_first_ge(
306 struct ovsdb_idl_index *, const struct %(s)s *);
307
308 struct %(s)s *%(s)s_cursor_data(struct ovsdb_idl_cursor *);
309
310 #define %(S)s_FOR_EACH_RANGE(ROW, FROM, TO, INDEX) \\
311 for (struct ovsdb_idl_cursor cursor__ = %(s)s_cursor_first_ge(INDEX, FROM); \\
312 (cursor__.position \\
313 && ((ROW) = %(s)s_cursor_data(&cursor__), \\
314 !(TO) || %(s)s_index_compare(INDEX, ROW, TO) <= 0)); \\
315 ovsdb_idl_cursor_next(&cursor__))
316 #define %(S)s_FOR_EACH_EQUAL(ROW, KEY, INDEX) \\
317 for (struct ovsdb_idl_cursor cursor__ = %(s)s_cursor_first_eq(INDEX, KEY); \\
318 (cursor__.position \\
319 ? ((ROW) = %(s)s_cursor_data(&cursor__), \\
320 ovsdb_idl_cursor_next_eq(&cursor__), \\
321 true) \\
322 : false); \\
323 )
324 #define %(S)s_FOR_EACH_BYINDEX(ROW, INDEX) \\
325 for (struct ovsdb_idl_cursor cursor__ = %(s)s_cursor_first(INDEX); \\
326 (cursor__.position \\
327 ? ((ROW) = %(s)s_cursor_data(&cursor__), \\
328 ovsdb_idl_cursor_next(&cursor__), \\
329 true) \\
330 : false); \\
331 )
332
333 void %(s)s_init(struct %(s)s *);
334 void %(s)s_delete(const struct %(s)s *);
335 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
336 bool %(s)s_is_updated(const struct %(s)s *, enum %(s)s_column_id);
337 ''' % {'s': structName, 'S': structName.upper()})
338
339 for columnName, column in sorted_columns(table):
340 if column.extensions.get('synthetic'):
341 continue
342 print('void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName})
343
344 print("")
345 for columnName, column in sorted_columns(table):
346 if column.extensions.get('synthetic'):
347 continue
348 if column.type.value:
349 valueParam = ', enum ovsdb_atomic_type value_type'
350 else:
351 valueParam = ''
352 print('const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
353 's': structName, 'c': columnName, 'v': valueParam})
354
355 print("")
356 for columnName, column in sorted_columns(table):
357 if column.extensions.get('synthetic'):
358 continue
359 print('void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName}, end=' ')
360 if column.type.is_smap():
361 args = ['const struct smap *']
362 else:
363 comment, members = cMembers(prefix, tableName, columnName,
364 column, True)
365 args = ['%(type)s%(name)s' % member for member in members]
366 print('%s);' % ', '.join(args))
367
368 print("")
369 for columnName, column in sorted_columns(table):
370 if column.extensions.get('synthetic'):
371 continue
372 if column.type.is_map():
373 print('void %(s)s_update_%(c)s_setkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName}, end=' ')
374 print('%(coltype)s, %(valtype)s);' % {'coltype':column.type.key.to_const_c_type(prefix), 'valtype':column.type.value.to_const_c_type(prefix)})
375 print('void %(s)s_update_%(c)s_delkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName}, end=' ')
376 print('%(coltype)s);' % {'coltype':column.type.key.to_const_c_type(prefix)})
377 if column.type.is_set():
378 print('void %(s)s_update_%(c)s_addvalue(const struct %(s)s *, ' % {'s': structName, 'c': columnName}, end=' ')
379 print('%(valtype)s);' % {'valtype':column.type.key.to_const_c_type(prefix)})
380 print('void %(s)s_update_%(c)s_delvalue(const struct %(s)s *, ' % {'s': structName, 'c': columnName}, end=' ')
381 print('%(valtype)s);' % {'valtype':column.type.key.to_const_c_type(prefix)})
382
383 print('void %(s)s_add_clause_%(c)s(struct ovsdb_idl_condition *, enum ovsdb_function function,' % {'s': structName, 'c': columnName}, end=' ')
384 if column.type.is_smap():
385 args = ['const struct smap *']
386 else:
387 comment, members = cMembers(prefix, tableName, columnName,
388 column, True, refTable=False)
389 args = ['%(type)s%(name)s' % member for member in members]
390 print('%s);' % ', '.join(args))
391
392 print('void %(s)s_set_condition(struct ovsdb_idl *, struct ovsdb_idl_condition *);' % {'s': structName})
393
394 print("")
395
396 # Table indexes.
397 print("struct %(s)s *%(s)s_index_init_row(struct ovsdb_idl_index *);" % {'s': structName})
398 print
399 for columnName, column in sorted(table.columns.items()):
400 print('void %(s)s_index_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName})
401 if column.type.is_smap():
402 args = ['const struct smap *']
403 else:
404 comment, members = cMembers(prefix, tableName, columnName,
405 column, True)
406 args = ['%(type)s%(name)s' % member for member in members]
407 print('%s);' % ', '.join(args))
408
409 print
410 printEnum("%stable_id" % prefix.lower(), ["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
411 print("")
412 for tableName in schema.tables:
413 print("#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
414 'p': prefix,
415 'P': prefix.upper(),
416 't': tableName.lower(),
417 'T': tableName.upper()})
418 print("\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper()))
419
420 print("\nextern struct ovsdb_idl_class %sidl_class;" % prefix)
421
422 print("\nconst char * %sget_db_version(void);" % prefix)
423 print('''
424 #ifdef __cplusplus
425 }
426 #endif''')
427 print("\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()})
428
429
430 def printEnum(type, members):
431 if len(members) == 0:
432 return
433
434 print("\nenum %s {" % type)
435 for member in members[:-1]:
436 print(" %s," % member)
437 print(" %s" % members[-1])
438 print("};")
439
440 def printCIDLSource(schemaFile):
441 schema = parseSchema(schemaFile)
442 replace_cplusplus_keyword(schema)
443 prefix = schema.idlPrefix
444 print('''\
445 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
446
447 #include <config.h>
448 #include %(header)s
449 #include <limits.h>
450 #include "ovs-thread.h"
451 #include "ovsdb-data.h"
452 #include "ovsdb-error.h"
453 #include "util.h"
454
455 %(cDecls)s
456
457 ''' % {'header': schema.idlHeader,
458 'cDecls': schema.cDecls})
459
460 # Cast functions.
461 for tableName, table in sorted(schema.tables.items()):
462 structName = "%s%s" % (prefix, tableName.lower())
463 print('''
464 static struct %(s)s *
465 %(s)s_cast(const struct ovsdb_idl_row *row)
466 {
467 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
468 }\
469 ''' % {'s': structName})
470
471
472 for tableName, table in sorted(schema.tables.items()):
473 structName = "%s%s" % (prefix, tableName.lower())
474 print("\f")
475 print("/* %s table. */" % (tableName))
476
477 print('''
478 const struct %(s)s_table *
479 %(s)s_table_get(const struct ovsdb_idl *idl)
480 {
481 return (const struct %(s)s_table *) idl;
482 }
483
484 const struct %(s)s *
485 %(s)s_table_first(const struct %(s)s_table *table)
486 {
487 const struct ovsdb_idl *idl = (const struct ovsdb_idl *) table;
488 return %(s)s_first(idl);
489 }
490
491
492 const struct %(s)s *
493 %(s)s_table_track_get_first(const struct %(s)s_table *table)
494 {
495 const struct ovsdb_idl *idl = (const struct ovsdb_idl *) table;
496 return %(s)s_track_get_first(idl);
497 }
498 ''' % {'s': structName})
499
500 # Parse functions.
501 for columnName, column in sorted_columns(table):
502 if 'parse' in column.extensions:
503 print('''
504 static void
505 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum OVS_UNUSED)
506 {
507 struct %(s)s *row = %(s)s_cast(row_);\
508 ''' % {'s': structName, 'c': columnName})
509 print(column.extensions["parse"])
510 print("}")
511 continue
512 if column.extensions.get('synthetic'):
513 # Synthetic columns aren't parsed from a datum.
514 unused = " OVS_UNUSED"
515 else:
516 unused = ""
517 print('''
518 static void
519 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
520 {
521 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
522 'c': columnName})
523 type = column.type
524 if 'parse' in column.extensions:
525 print(column.extensions["parse"])
526 print("}")
527 continue
528 if type.value:
529 keyVar = "row->key_%s" % columnName
530 valueVar = "row->value_%s" % columnName
531 else:
532 keyVar = "row->%s" % columnName
533 valueVar = None
534
535 if type.is_smap():
536 print(" smap_init(&row->%s);" % columnName)
537 print(" for (size_t i = 0; i < datum->n; i++) {")
538 print(" smap_add(&row->%s," % columnName)
539 print(" datum->keys[i].string,")
540 print(" datum->values[i].string);")
541 print(" }")
542 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
543 print("")
544 print(" if (datum->n >= 1) {")
545 if not type.key.ref_table:
546 print(" %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string()))
547 else:
548 print(" %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->keys[0].uuid));" % (keyVar, prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower()))
549
550 if valueVar:
551 if not type.value.ref_table:
552 print(" %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string()))
553 else:
554 print(" %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->values[0].uuid));" % (valueVar, prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower()))
555 print(" } else {")
556 print(" %s" % type.key.initCDefault(keyVar, type.n_min == 0))
557 if valueVar:
558 print(" %s" % type.value.initCDefault(valueVar, type.n_min == 0))
559 print(" }")
560 else:
561 if type.n_max != sys.maxsize:
562 print(" size_t n = MIN(%d, datum->n);" % type.n_max)
563 nMax = "n"
564 else:
565 nMax = "datum->n"
566 print(" %s = NULL;" % keyVar)
567 if valueVar:
568 print(" %s = NULL;" % valueVar)
569 print(" row->n_%s = 0;" % columnName)
570 print(" for (size_t i = 0; i < %s; i++) {" % nMax)
571 if type.key.ref_table:
572 print("""\
573 struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->keys[i].uuid));
574 if (!keyRow) {
575 continue;
576 }\
577 """ % (prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower()))
578 keySrc = "keyRow"
579 else:
580 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
581 if type.value and type.value.ref_table:
582 print("""\
583 struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->values[i].uuid));
584 if (!valueRow) {
585 continue;
586 }\
587 """ % (prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower()))
588 valueSrc = "valueRow"
589 elif valueVar:
590 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
591 print(" if (!row->n_%s) {" % (columnName))
592
593 print(" %s = xmalloc(%s * sizeof *%s);" % (
594 keyVar, nMax, keyVar))
595 if valueVar:
596 print(" %s = xmalloc(%s * sizeof *%s);" % (
597 valueVar, nMax, valueVar))
598 print(" }")
599 print(" %s[row->n_%s] = %s;" % (keyVar, columnName, keySrc))
600 if valueVar:
601 print(" %s[row->n_%s] = %s;" % (valueVar, columnName, valueSrc))
602 print(" row->n_%s++;" % columnName)
603 print(" }")
604 print("}")
605
606 # Unparse functions.
607 for columnName, column in sorted_columns(table):
608 type = column.type
609 if (type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer()) or 'unparse' in column.extensions:
610 print('''
611 static void
612 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
613 {
614 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
615 'c': columnName})
616 if 'unparse' in column.extensions:
617 print(column.extensions["unparse"])
618 elif type.is_smap():
619 print(" smap_destroy(&row->%s);" % columnName)
620 else:
621 if type.value:
622 keyVar = "row->key_%s" % columnName
623 valueVar = "row->value_%s" % columnName
624 else:
625 keyVar = "row->%s" % columnName
626 valueVar = None
627 print(" free(%s);" % keyVar)
628 if valueVar:
629 print(" free(%s);" % valueVar)
630 print('}')
631 else:
632 print('''
633 static void
634 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
635 {
636 /* Nothing to do. */
637 }''' % {'s': structName, 'c': columnName})
638
639 # Generic Row Initialization function.
640 print("""
641 static void
642 %(s)s_init__(struct ovsdb_idl_row *row)
643 {
644 %(s)s_init(%(s)s_cast(row));
645 }""" % {'s': structName})
646
647 # Row Initialization function.
648 print("""
649 /* Clears the contents of 'row' in table "%(t)s". */
650 void
651 %(s)s_init(struct %(s)s *row)
652 {
653 memset(row, 0, sizeof *row); """ % {'s': structName, 't': tableName})
654 for columnName, column in sorted_columns(table):
655 if column.type.is_smap():
656 print(" smap_init(&row->%s);" % columnName)
657 elif (column.type.n_min == 1 and
658 column.type.n_max == 1 and
659 column.type.key.type == ovs.db.types.StringType and
660 not column.type.value):
661 print(" row->%s = \"\";" % columnName)
662 print("}")
663
664 # First, next functions.
665 print('''
666 /* Searches table "%(t)s" in 'idl' for a row with UUID 'uuid'. Returns
667 * a pointer to the row if there is one, otherwise a null pointer. */
668 const struct %(s)s *
669 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
670 {
671 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_%(tl)s, uuid));
672 }
673
674 /* Searches table "%(t)s" for a row with UUID 'uuid'. Returns
675 * a pointer to the row if there is one, otherwise a null pointer. */
676 const struct %(s)s *
677 %(s)s_table_get_for_uuid(const struct %(s)s_table *table, const struct uuid *uuid)
678 {
679 const struct ovsdb_idl *idl = (const struct ovsdb_idl *) table;
680 return %(s)s_get_for_uuid(idl, uuid);
681 }
682
683 /* Returns a row in table "%(t)s" in 'idl', or a null pointer if that
684 * table is empty.
685 *
686 * Database tables are internally maintained as hash tables, so adding or
687 * removing rows while traversing the same table can cause some rows to be
688 * visited twice or not at apply. */
689 const struct %(s)s *
690 %(s)s_first(const struct ovsdb_idl *idl)
691 {
692 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_%(tl)s));
693 }
694
695 /* Returns a row following 'row' within its table, or a null pointer if 'row'
696 * is the last row in its table. */
697 const struct %(s)s *
698 %(s)s_next(const struct %(s)s *row)
699 {
700 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
701 }
702
703 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *idl)
704 {
705 return ovsdb_idl_table_get_seqno(idl, &%(p)stable_%(tl)s);
706 }
707
708 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change)
709 {
710 return ovsdb_idl_row_get_seqno(&row->header_, change);
711 }
712
713 const struct %(s)s *
714 %(s)s_track_get_first(const struct ovsdb_idl *idl)
715 {
716 return %(s)s_cast(ovsdb_idl_track_get_first(idl, &%(p)stable_%(tl)s));
717 }
718
719 const struct %(s)s
720 *%(s)s_track_get_next(const struct %(s)s *row)
721 {
722 return %(s)s_cast(ovsdb_idl_track_get_next(&row->header_));
723 }''' % {'s': structName,
724 'p': prefix,
725 'P': prefix.upper(),
726 't': tableName,
727 'tl': tableName.lower(),
728 'T': tableName.upper()})
729
730 print('''
731
732 /* Deletes 'row' from table "%(t)s". 'row' may be freed, so it must not be
733 * accessed afterward.
734 *
735 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
736 void
737 %(s)s_delete(const struct %(s)s *row)
738 {
739 ovsdb_idl_txn_delete(&row->header_);
740 }
741
742 /* Inserts and returns a new row in the table "%(t)s" in the database
743 * with open transaction 'txn'.
744 *
745 * The new row is assigned a randomly generated provisional UUID.
746 * ovsdb-server will assign a different UUID when 'txn' is committed,
747 * but the IDL will replace any uses of the provisional UUID in the
748 * data to be to be committed by the UUID assigned by ovsdb-server. */
749 struct %(s)s *
750 %(s)s_insert(struct ovsdb_idl_txn *txn)
751 {
752 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_%(tl)s, NULL));
753 }
754
755 bool
756 %(s)s_is_updated(const struct %(s)s *row, enum %(s)s_column_id column)
757 {
758 return ovsdb_idl_track_is_updated(&row->header_, &%(s)s_columns[column]);
759 }''' % {'s': structName,
760 'p': prefix,
761 'P': prefix.upper(),
762 't': tableName,
763 'tl': tableName.lower(),
764 'T': tableName.upper()})
765
766 # Verify functions.
767 for columnName, column in sorted_columns(table):
768 if column.extensions.get('synthetic'):
769 continue
770 print('''
771 /* Causes the original contents of column "%(c)s" in 'row' to be
772 * verified as a prerequisite to completing the transaction. That is, if
773 * "%(c)s" in 'row' changed (or if 'row' was deleted) between the
774 * time that the IDL originally read its contents and the time that the
775 * transaction aborts and ovsdb_idl_txn_commit() returns TXN_TRY_AGAIN.
776 *
777 * The intention is that, to ensure that no transaction commits based on dirty
778 * reads, an application should call this function any time "%(c)s" is
779 * read as part of a read-modify-write operation.
780 *
781 * In some cases this function reduces to a no-op, because the current value
782 * of "%(c)s" is already known:
783 *
784 * - If 'row' is a row created by the current transaction (returned by
785 * %(s)s_insert()).
786 *
787 * - If "%(c)s" has already been modified (with
788 * %(s)s_set_%(c)s()) within the current transaction.
789 *
790 * Because of the latter property, always call this function *before*
791 * %(s)s_set_%(c)s() for a given read-modify-write.
792 *
793 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
794 void
795 %(s)s_verify_%(c)s(const struct %(s)s *row)
796 {
797 ovsdb_idl_txn_verify(&row->header_, &%(s)s_col_%(c)s);
798 }''' % {'s': structName,
799 'S': structName.upper(),
800 'c': columnName,
801 'C': columnName.upper()})
802
803 # Get functions.
804 for columnName, column in sorted_columns(table):
805 if column.extensions.get('synthetic'):
806 continue
807 if column.type.value:
808 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
809 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
810 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
811 else:
812 valueParam = ''
813 valueType = ''
814 valueComment = ''
815 print("""
816 /* Returns the "%(c)s" column's value from the "%(t)s" table in 'row'
817 * as a struct ovsdb_datum. This is useful occasionally: for example,
818 * ovsdb_datum_find_key() is an easier and more efficient way to search
819 * for a given key than implementing the same operation on the "cooked"
820 * form in 'row'.
821 *
822 * 'key_type' must be %(kt)s.%(vc)s
823 * (This helps to avoid silent bugs if someone changes %(c)s's
824 * type without updating the caller.)
825 *
826 * The caller must not modify or free the returned value.
827 *
828 * Various kinds of changes can invalidate the returned value: modifying
829 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
830 * If the returned value is needed for a long time, it is best to make a copy
831 * of it with ovsdb_datum_clone().
832 *
833 * This function is rarely useful, since it is easier to access the value
834 * directly through the "%(c)s" member in %(s)s. */
835 const struct ovsdb_datum *
836 %(s)s_get_%(c)s(const struct %(s)s *row,
837 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
838 {
839 ovs_assert(key_type == %(kt)s);%(vt)s
840 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
841 }""" % {'t': tableName, 's': structName, 'c': columnName,
842 'kt': column.type.key.toAtomicType(),
843 'v': valueParam, 'vt': valueType, 'vc': valueComment})
844
845 # Set functions.
846 for columnName, column in sorted_columns(table):
847 if column.extensions.get('synthetic'):
848 continue
849 type = column.type
850
851 comment, members = cMembers(prefix, tableName, columnName,
852 column, True)
853
854 if type.is_smap():
855 print(comment)
856 print("""void
857 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
858 {
859 struct ovsdb_datum datum;
860
861 if (%(c)s) {
862 ovsdb_datum_from_smap(&datum, %(c)s);
863 } else {
864 ovsdb_datum_init_empty(&datum);
865 }
866 ovsdb_idl_txn_write(&row->header_,
867 &%(s)s_col_%(c)s,
868 &datum);
869 }
870 """ % {'t': tableName,
871 's': structName,
872 'S': structName.upper(),
873 'c': columnName,
874 'C': columnName.upper()})
875 continue
876
877 keyVar = members[0]['name']
878 nVar = None
879 valueVar = None
880 if type.value:
881 valueVar = members[1]['name']
882 if len(members) > 2:
883 nVar = members[2]['name']
884 else:
885 if len(members) > 1:
886 nVar = members[1]['name']
887
888 print(comment)
889 print("""\
890 void
891 %(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)
892 {
893 struct ovsdb_datum datum;""" % {'s': structName,
894 'c': columnName,
895 'args': ', '.join(['%(type)s%(name)s'
896 % m for m in members])})
897 if type.n_min == 1 and type.n_max == 1:
898 print(" union ovsdb_atom key;")
899 if type.value:
900 print(" union ovsdb_atom value;")
901 print("")
902 print(" datum.n = 1;")
903 print(" datum.keys = &key;")
904 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar))
905 if type.value:
906 print(" datum.values = &value;")
907 print(" "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar))
908 else:
909 print(" datum.values = NULL;")
910 txn_write_func = "ovsdb_idl_txn_write_clone"
911 elif type.is_optional_pointer():
912 print(" union ovsdb_atom key;")
913 print("")
914 print(" if (%s) {" % keyVar)
915 print(" datum.n = 1;")
916 print(" datum.keys = &key;")
917 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar))
918 print(" } else {")
919 print(" datum.n = 0;")
920 print(" datum.keys = NULL;")
921 print(" }")
922 print(" datum.values = NULL;")
923 txn_write_func = "ovsdb_idl_txn_write_clone"
924 elif type.n_max == 1:
925 print(" union ovsdb_atom key;")
926 print("")
927 print(" if (%s) {" % nVar)
928 print(" datum.n = 1;")
929 print(" datum.keys = &key;")
930 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar))
931 print(" } else {")
932 print(" datum.n = 0;")
933 print(" datum.keys = NULL;")
934 print(" }")
935 print(" datum.values = NULL;")
936 txn_write_func = "ovsdb_idl_txn_write_clone"
937 else:
938 print("")
939 print(" datum.n = %s;" % nVar)
940 print(" datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar))
941 if type.value:
942 print(" datum.values = xmalloc(%s * sizeof *datum.values);" % nVar)
943 else:
944 print(" datum.values = NULL;")
945 print(" for (size_t i = 0; i < %s; i++) {" % nVar)
946 print(" " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar))
947 if type.value:
948 print(" " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar))
949 print(" }")
950 if type.value:
951 valueType = type.value.toAtomicType()
952 else:
953 valueType = "OVSDB_TYPE_VOID"
954 txn_write_func = "ovsdb_idl_txn_write"
955 print(" %(f)s(&row->header_, &%(s)s_col_%(c)s, &datum);" \
956 % {'f': txn_write_func,
957 's': structName,
958 'S': structName.upper(),
959 'c': columnName})
960 print("}")
961 # Update/Delete of partial map column functions
962 for columnName, column in sorted_columns(table):
963 if column.extensions.get('synthetic'):
964 continue
965 type = column.type
966 if type.is_map():
967 print('''
968 /* Sets an element of the "%(c)s" map column from the "%(t)s" table in 'row'
969 * to 'new_value' given the key value 'new_key'.
970 *
971 */
972 void
973 %(s)s_update_%(c)s_setkey(const struct %(s)s *row, %(coltype)snew_key, %(valtype)snew_value)
974 {
975 struct ovsdb_datum *datum;
976
977 datum = xmalloc(sizeof *datum);
978 datum->n = 1;
979 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
980 datum->values = xmalloc(datum->n * sizeof *datum->values);
981 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.to_const_c_type(prefix),
982 'valtype':column.type.value.to_const_c_type(prefix), 'S': structName.upper(),
983 'C': columnName.upper(), 't': tableName})
984
985 print(" "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "new_key"))
986 print(" "+ type.value.copyCValue("datum->values[0].%s" % type.value.type.to_string(), "new_value"))
987 print('''
988 ovsdb_idl_txn_write_partial_map(&row->header_,
989 &%(s)s_col_%(c)s,
990 datum);
991 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
992 'valtype':column.type.value.to_const_c_type(prefix), 'S': structName.upper()})
993 print('''
994 /* Deletes an element of the "%(c)s" map column from the "%(t)s" table in 'row'
995 * given the key value 'delete_key'.
996 *
997 */
998 void
999 %(s)s_update_%(c)s_delkey(const struct %(s)s *row, %(coltype)sdelete_key)
1000 {
1001 struct ovsdb_datum *datum;
1002
1003 datum = xmalloc(sizeof *datum);
1004 datum->n = 1;
1005 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1006 datum->values = NULL;
1007 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.to_const_c_type(prefix),
1008 'valtype':column.type.value.to_const_c_type(prefix), 'S': structName.upper(),
1009 'C': columnName.upper(), 't': tableName})
1010
1011 print(" "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "delete_key"))
1012 print('''
1013 ovsdb_idl_txn_delete_partial_map(&row->header_,
1014 &%(s)s_col_%(c)s,
1015 datum);
1016 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
1017 'valtype':column.type.value.to_const_c_type(prefix), 'S': structName.upper()})
1018 # End Update/Delete of partial maps
1019 # Update/Delete of partial set column functions
1020 if type.is_set():
1021 print('''
1022 /* Adds the value 'new_value' to the "%(c)s" set column from the "%(t)s" table
1023 * in 'row'.
1024 *
1025 */
1026 void
1027 %(s)s_update_%(c)s_addvalue(const struct %(s)s *row, %(valtype)snew_value)
1028 {
1029 struct ovsdb_datum *datum;
1030
1031 datum = xmalloc(sizeof *datum);
1032 datum->n = 1;
1033 datum->keys = xmalloc(datum->n * sizeof *datum->values);
1034 datum->values = NULL;
1035 ''' % {'s': structName, 'c': columnName,
1036 'valtype':column.type.key.to_const_c_type(prefix), 't': tableName})
1037
1038 print(" "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "new_value"))
1039 print('''
1040 ovsdb_idl_txn_write_partial_set(&row->header_,
1041 &%(s)s_col_%(c)s,
1042 datum);
1043 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
1044 'valtype':column.type.key.to_const_c_type(prefix), 'S': structName.upper()})
1045 print('''
1046 /* Deletes the value 'delete_value' from the "%(c)s" set column from the
1047 * "%(t)s" table in 'row'.
1048 *
1049 */
1050 void
1051 %(s)s_update_%(c)s_delvalue(const struct %(s)s *row, %(valtype)sdelete_value)
1052 {
1053 struct ovsdb_datum *datum;
1054
1055 datum = xmalloc(sizeof *datum);
1056 datum->n = 1;
1057 datum->keys = xmalloc(datum->n * sizeof *datum->values);
1058 datum->values = NULL;
1059 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.to_const_c_type(prefix),
1060 'valtype':column.type.key.to_const_c_type(prefix), 'S': structName.upper(),
1061 'C': columnName.upper(), 't': tableName})
1062
1063 print(" "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "delete_value"))
1064 print('''
1065 ovsdb_idl_txn_delete_partial_set(&row->header_,
1066 &%(s)s_col_%(c)s,
1067 datum);
1068 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
1069 'valtype':column.type.key.to_const_c_type(prefix), 'S': structName.upper()})
1070 # End Update/Delete of partial set
1071
1072 # Add clause functions.
1073 for columnName, column in sorted_columns(table):
1074 if column.extensions.get('synthetic'):
1075 continue
1076 type = column.type
1077
1078 comment, members = cMembers(prefix, tableName, columnName,
1079 column, True, refTable=False)
1080
1081 if type.is_smap():
1082 print(comment)
1083 print("""void
1084 %(s)s_add_clause_%(c)s(struct ovsdb_idl_condition *cond, enum ovsdb_function function, const struct smap *%(c)s)
1085 {
1086 struct ovsdb_datum datum;
1087
1088 if (%(c)s) {
1089 ovsdb_datum_from_smap(&datum, %(c)s);
1090 } else {
1091 ovsdb_datum_init_empty(&datum);
1092 }
1093
1094 ovsdb_idl_condition_add_clause(cond,
1095 function,
1096 &%(s)s_col_%(c)s,
1097 &datum);
1098
1099 ovsdb_datum_destroy(&datum, &%(s)s_col_%(c)s.type);
1100 }
1101 """ % {'t': tableName,
1102 'tl': tableName.lower(),
1103 'T': tableName.upper(),
1104 'p': prefix,
1105 'P': prefix.upper(),
1106 's': structName,
1107 'S': structName.upper(),
1108 'c': columnName})
1109 continue
1110
1111 keyVar = members[0]['name']
1112 nVar = None
1113 valueVar = None
1114 if type.value:
1115 valueVar = members[1]['name']
1116 if len(members) > 2:
1117 nVar = members[2]['name']
1118 else:
1119 if len(members) > 1:
1120 nVar = members[1]['name']
1121
1122 print(comment)
1123 print('void')
1124 print('%(s)s_add_clause_%(c)s(struct ovsdb_idl_condition *cond, enum ovsdb_function function, %(args)s)' % \
1125 {'s': structName, 'c': columnName,
1126 'args': ', '.join(['%(type)s%(name)s' % m for m in members])})
1127 print("{")
1128 print(" struct ovsdb_datum datum;")
1129 free = []
1130 if type.n_min == 1 and type.n_max == 1:
1131 print(" union ovsdb_atom key;")
1132 if type.value:
1133 print(" union ovsdb_atom value;")
1134 print("")
1135 print(" datum.n = 1;")
1136 print(" datum.keys = &key;")
1137 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar, refTable=False))
1138 if type.value:
1139 print(" datum.values = &value;")
1140 print(" "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar, refTable=False))
1141 else:
1142 print(" datum.values = NULL;")
1143 elif type.is_optional_pointer():
1144 print(" union ovsdb_atom key;")
1145 print("")
1146 print(" if (%s) {" % keyVar)
1147 print(" datum.n = 1;")
1148 print(" datum.keys = &key;")
1149 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar, refTable=False))
1150 print(" } else {")
1151 print(" datum.n = 0;")
1152 print(" datum.keys = NULL;")
1153 print(" }")
1154 print(" datum.values = NULL;")
1155 elif type.n_max == 1:
1156 print(" union ovsdb_atom key;")
1157 print("")
1158 print(" if (%s) {" % nVar)
1159 print(" datum.n = 1;")
1160 print(" datum.keys = &key;")
1161 print(" " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar, refTable=False))
1162 print(" } else {")
1163 print(" datum.n = 0;")
1164 print(" datum.keys = NULL;")
1165 print(" }")
1166 print(" datum.values = NULL;")
1167 else:
1168 print(" datum.n = %s;" % nVar)
1169 print(" datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar))
1170 free += ['datum.keys']
1171 if type.value:
1172 print(" datum.values = xmalloc(%s * sizeof *datum.values);" % nVar)
1173 free += ['datum.values']
1174 else:
1175 print(" datum.values = NULL;")
1176 print(" for (size_t i = 0; i < %s; i++) {" % nVar)
1177 print(" " + type.key.assign_c_value_casting_away_const("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar, refTable=False))
1178 if type.value:
1179 print(" " + type.value.assign_c_value_casting_away_const("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar, refTable=False))
1180 print(" }")
1181 if type.value:
1182 valueType = type.value.toAtomicType()
1183 else:
1184 valueType = "OVSDB_TYPE_VOID"
1185 print(" ovsdb_datum_sort_unique(&datum, %s, %s);" % (
1186 type.key.toAtomicType(), valueType))
1187
1188 print(""" ovsdb_idl_condition_add_clause(cond,
1189 function,
1190 &%(s)s_col_%(c)s,
1191 &datum);\
1192 """ % {'tl': tableName.lower(),
1193 'T': tableName.upper(),
1194 'p': prefix,
1195 'P': prefix.upper(),
1196 's': structName,
1197 'S': structName.upper(),
1198 'c': columnName})
1199 for var in free:
1200 print(" free(%s);" % var)
1201 print("}")
1202
1203 # Index table related functions
1204 print("""
1205 /* Destroy 'row' of kind "%(t)s". The row must have been
1206 * created with ovsdb_idl_index_init_row.
1207 */
1208 void
1209 %(s)s_index_destroy_row(const struct %(s)s *row)
1210 {
1211 ovsdb_idl_index_destroy_row(&row->header_);
1212 }
1213 """ % { 's' : structName, 't': tableName })
1214 print("""
1215 /* Creates a new row of kind "%(t)s". */
1216 struct %(s)s *
1217 %(s)s_index_init_row(struct ovsdb_idl_index *index)
1218 {
1219 ovs_assert(index->table->class_ == &%(p)stable_%(tl)s);
1220 return ALIGNED_CAST(struct %(s)s *, ovsdb_idl_index_init_row(index));
1221 }
1222
1223 struct %(s)s *
1224 %(s)s_index_find(struct ovsdb_idl_index *index, const struct %(s)s *target)
1225 {
1226 ovs_assert(index->table->class_ == &%(p)stable_%(tl)s);
1227 return %(s)s_cast(ovsdb_idl_index_find(index, &target->header_));
1228 }
1229
1230 /* Compares 'a' to 'b' and returns a strcmp()-type result. */
1231 int
1232 %(s)s_index_compare(
1233 struct ovsdb_idl_index *index,
1234 const struct %(s)s *a,
1235 const struct %(s)s *b)
1236 {
1237 return ovsdb_idl_index_compare(index, &a->header_, &b->header_);
1238 }
1239
1240 struct ovsdb_idl_cursor
1241 %(s)s_cursor_first(struct ovsdb_idl_index *index)
1242 {
1243 ovs_assert(index->table->class_ == &%(p)stable_%(tl)s);
1244 return ovsdb_idl_cursor_first(index);
1245 }
1246
1247 struct ovsdb_idl_cursor
1248 %(s)s_cursor_first_eq(
1249 struct ovsdb_idl_index *index, const struct %(s)s *target)
1250 {
1251 ovs_assert(index->table->class_ == &%(p)stable_%(tl)s);
1252 return ovsdb_idl_cursor_first_eq(index, &target->header_);
1253 }
1254
1255 struct ovsdb_idl_cursor
1256 %(s)s_cursor_first_ge(
1257 struct ovsdb_idl_index *index, const struct %(s)s *target)
1258 {
1259 ovs_assert(index->table->class_ == &%(p)stable_%(tl)s);
1260 return ovsdb_idl_cursor_first_ge(index, &target->header_);
1261 }
1262
1263 struct %(s)s *
1264 %(s)s_cursor_data(struct ovsdb_idl_cursor *cursor)
1265 {
1266 return %(s)s_cast(ovsdb_idl_cursor_data(cursor));
1267 }
1268 """ % {'s': structName,
1269 'c': columnName,
1270 't': tableName,
1271 'tl': tableName.lower(),
1272 'p': prefix})
1273 # Indexes Set functions
1274 for columnName, column in sorted(table.columns.items()):
1275 type = column.type
1276
1277 comment, members = cMembers(prefix, tableName, columnName,
1278 column, True)
1279
1280 if type.is_smap():
1281 print(comment)
1282 print("""void
1283 %(s)s_index_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
1284 {
1285 struct ovsdb_datum *datum = xmalloc(sizeof(struct ovsdb_datum));
1286
1287 if (%(c)s) {
1288 struct smap_node *node;
1289 size_t i;
1290
1291 datum->n = smap_count(%(c)s);
1292 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1293 datum->values = xmalloc(datum->n * sizeof *datum->values);
1294
1295 i = 0;
1296 SMAP_FOR_EACH (node, %(c)s) {
1297 datum->keys[i].string = node->key;
1298 datum->values[i].string = node->value;
1299 i++;
1300 }
1301 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1302 } else {
1303 ovsdb_datum_init_empty(datum);
1304 }
1305 ovsdb_idl_index_write(CONST_CAST(struct ovsdb_idl_row *, &row->header_),
1306 &%(s)s_columns[%(S)s_COL_%(C)s],
1307 datum,
1308 &%(p)stable_classes[%(P)sTABLE_%(T)s]);
1309 }
1310 """ % {'t': tableName,
1311 'p': prefix,
1312 'P': prefix.upper(),
1313 's': structName,
1314 'S': structName.upper(),
1315 'c': columnName,
1316 'C': columnName.upper(),
1317 't': tableName,
1318 'T': tableName.upper()})
1319 continue
1320
1321 keyVar = members[0]['name']
1322 nVar = None
1323 valueVar = None
1324 if type.value:
1325 valueVar = members[1]['name']
1326 if len(members) > 2:
1327 nVar = members[2]['name']
1328 else:
1329 if len(members) > 1:
1330 nVar = members[1]['name']
1331
1332 print(comment)
1333 print('void')
1334 print('%(s)s_index_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
1335 {'s': structName, 'c': columnName,
1336 'args': ', '.join(['%(type)s%(name)s' % m for m in members])})
1337 print("{")
1338 print(" struct ovsdb_datum datum;")
1339 if type.n_min == 1 and type.n_max == 1:
1340 print(" union ovsdb_atom *key = xmalloc(sizeof(union ovsdb_atom));")
1341 if type.value:
1342 print(" union ovsdb_atom *value = xmalloc(sizeof(union ovsdb_atom));")
1343 print()
1344 print(" datum.n = 1;")
1345 print(" datum.keys = key;")
1346 print(" " + type.key.assign_c_value_casting_away_const("key->%s" % type.key.type.to_string(), keyVar))
1347 if type.value:
1348 print(" datum.values = value;")
1349 print(" "+ type.value.assign_c_value_casting_away_const("value->%s" % type.value.type.to_string(), valueVar))
1350 else:
1351 print(" datum.values = NULL;")
1352 txn_write_func = "ovsdb_idl_index_write"
1353 elif type.is_optional_pointer():
1354 print(" union ovsdb_atom *key = xmalloc(sizeof (union ovsdb_atom));")
1355 print()
1356 print(" if (%s) {" % keyVar)
1357 print(" datum.n = 1;")
1358 print(" datum.keys = key;")
1359 print(" " + type.key.assign_c_value_casting_away_const("key->%s" % type.key.type.to_string(), keyVar))
1360 print(" } else {")
1361 print(" datum.n = 0;")
1362 print(" datum.keys = NULL;")
1363 print(" }")
1364 print(" datum.values = NULL;")
1365 txn_write_func = "ovsdb_idl_index_write"
1366 elif type.n_max == 1:
1367 print(" union ovsdb_atom *key = xmalloc(sizeof(union ovsdb_atom));")
1368 print()
1369 print(" if (%s) {" % nVar)
1370 print(" datum.n = 1;")
1371 print(" datum.keys = key;")
1372 print(" " + type.key.assign_c_value_casting_away_const("key->%s" % type.key.type.to_string(), "*" + keyVar))
1373 print(" } else {")
1374 print(" datum.n = 0;")
1375 print(" datum.keys = NULL;")
1376 print(" }")
1377 print(" datum.values = NULL;")
1378 txn_write_func = "ovsdb_idl_index_write"
1379 else:
1380 print(" size_t i;")
1381 print()
1382 print(" datum.n = %s;" % nVar)
1383 print(" datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar))
1384 if type.value:
1385 print(" datum.values = xmalloc(%s * sizeof *datum.values);" % nVar)
1386 else:
1387 print(" datum.values = NULL;")
1388 print(" for (i = 0; i < %s; i++) {" % nVar)
1389 print(" " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar))
1390 if type.value:
1391 print(" " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar))
1392 print(" }")
1393 if type.value:
1394 valueType = type.value.toAtomicType()
1395 else:
1396 valueType = "OVSDB_TYPE_VOID"
1397 print(" ovsdb_datum_sort_unique(&datum, %s, %s);" % (
1398 type.key.toAtomicType(), valueType))
1399 txn_write_func = "ovsdb_idl_index_write"
1400 print(" %(f)s(CONST_CAST(struct ovsdb_idl_row *, &row->header_), &%(s)s_columns[ %(S)s_COL_%(C)s ], &datum, &%(p)stable_classes[%(P)sTABLE_%(T)s]);" \
1401 % {'f': txn_write_func,
1402 's': structName,
1403 'S': structName.upper(),
1404 'C': columnName.upper(),
1405 'p': prefix,
1406 'P': prefix.upper(),
1407 't': tableName,
1408 'T': tableName.upper()})
1409 print("}")
1410 # End Index table related functions
1411
1412 # Table columns.
1413 print("\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
1414 structName, structName.upper()))
1415 print("""
1416 void
1417 %(s)s_set_condition(struct ovsdb_idl *idl, struct ovsdb_idl_condition *condition)
1418 {
1419 ovsdb_idl_set_condition(idl, &%(p)stable_%(tl)s, condition);
1420 }""" % {'p': prefix,
1421 's': structName,
1422 'tl': tableName.lower()})
1423
1424 # Table columns.
1425 for columnName, column in sorted_columns(table):
1426 prereqs = []
1427 x = column.type.cInitType("%s_col_%s" % (tableName, columnName), prereqs)
1428 if prereqs:
1429 print('\n'.join(prereqs))
1430 print("\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS] = {" % (
1431 structName, structName.upper()))
1432 for columnName, column in sorted_columns(table):
1433 if column.mutable:
1434 mutable = "true"
1435 else:
1436 mutable = "false"
1437 if column.extensions.get("synthetic"):
1438 synthetic = "true"
1439 else:
1440 synthetic = "false"
1441 type_init = '\n'.join(" " + x
1442 for x in column.type.cInitType("%s_col_%s" % (tableName, columnName), prereqs))
1443 print("""\
1444 [%(P)s%(T)s_COL_%(C)s] = {
1445 .name = "%(column_name_in_schema)s",
1446 .type = {
1447 %(type)s
1448 },
1449 .is_mutable = %(mutable)s,
1450 .is_synthetic = %(synthetic)s,
1451 .parse = %(s)s_parse_%(c)s,
1452 .unparse = %(s)s_unparse_%(c)s,
1453 },\n""" % {'P': prefix.upper(),
1454 'T': tableName.upper(),
1455 'c': columnName,
1456 'C': columnName.upper(),
1457 's': structName,
1458 'mutable': mutable,
1459 'synthetic': synthetic,
1460 'type': type_init,
1461 'column_name_in_schema': column.name})
1462 print("};")
1463
1464 # Table classes.
1465 print("\f")
1466 print("struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper()))
1467 for tableName, table in sorted(schema.tables.items()):
1468 structName = "%s%s" % (prefix, tableName.lower())
1469 if table.is_root:
1470 is_root = "true"
1471 else:
1472 is_root = "false"
1473 if table.max_rows == 1:
1474 is_singleton = "true"
1475 else:
1476 is_singleton = "false"
1477 print(" {\"%s\", %s, %s," % (tableName, is_root, is_singleton))
1478 print(" %s_columns, ARRAY_SIZE(%s_columns)," % (
1479 structName, structName))
1480 print(" sizeof(struct %s), %s_init__}," % (structName, structName))
1481 print("};")
1482
1483 # IDL class.
1484 print("\nstruct ovsdb_idl_class %sidl_class = {" % prefix)
1485 print(" \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
1486 schema.name, prefix, prefix))
1487 print("};")
1488
1489 print("""
1490 /* Return the schema version. The caller must not free the returned value. */
1491 const char *
1492 %sget_db_version(void)
1493 {
1494 return "%s";
1495 }
1496 """ % (prefix, schema.version))
1497
1498
1499
1500 def ovsdb_escape(string):
1501 def escape(match):
1502 c = match.group(0)
1503 if c == '\0':
1504 raise ovs.db.error.Error("strings may not contain null bytes")
1505 elif c == '\\':
1506 return '\\\\'
1507 elif c == '\n':
1508 return '\\n'
1509 elif c == '\r':
1510 return '\\r'
1511 elif c == '\t':
1512 return '\\t'
1513 elif c == '\b':
1514 return '\\b'
1515 elif c == '\a':
1516 return '\\a'
1517 else:
1518 return '\\x%02x' % ord(c)
1519 return re.sub(r'["\\\000-\037]', escape, string)
1520
1521 def usage():
1522 print("""\
1523 %(argv0)s: ovsdb schema compiler
1524 usage: %(argv0)s [OPTIONS] COMMAND ARG...
1525
1526 The following commands are supported:
1527 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
1528 c-idl-header IDL print C header file for IDL
1529 c-idl-source IDL print C source file for IDL implementation
1530
1531 The following options are also available:
1532 -h, --help display this help message
1533 -V, --version display version information\
1534 """ % {'argv0': argv0})
1535 sys.exit(0)
1536
1537 if __name__ == "__main__":
1538 try:
1539 try:
1540 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
1541 ['directory',
1542 'help',
1543 'version'])
1544 except getopt.GetoptError as geo:
1545 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
1546 sys.exit(1)
1547
1548 for key, value in options:
1549 if key in ['-h', '--help']:
1550 usage()
1551 elif key in ['-V', '--version']:
1552 print("ovsdb-idlc (Open vSwitch) @VERSION@")
1553 elif key in ['-C', '--directory']:
1554 os.chdir(value)
1555 else:
1556 sys.exit(0)
1557
1558 optKeys = [key for key, value in options]
1559
1560 if not args:
1561 sys.stderr.write("%s: missing command argument "
1562 "(use --help for help)\n" % argv0)
1563 sys.exit(1)
1564
1565 commands = {"annotate": (annotateSchema, 2),
1566 "c-idl-header": (printCIDLHeader, 1),
1567 "c-idl-source": (printCIDLSource, 1)}
1568
1569 if not args[0] in commands:
1570 sys.stderr.write("%s: unknown command \"%s\" "
1571 "(use --help for help)\n" % (argv0, args[0]))
1572 sys.exit(1)
1573
1574 func, n_args = commands[args[0]]
1575 if len(args) - 1 != n_args:
1576 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
1577 "provided\n"
1578 % (argv0, args[0], n_args, len(args) - 1))
1579 sys.exit(1)
1580
1581 func(*args[1:])
1582 except ovs.db.error.Error as e:
1583 sys.stderr.write("%s: %s\n" % (argv0, e))
1584 sys.exit(1)
1585
1586 # Local variables:
1587 # mode: python
1588 # End: