14 def parseSchema(filename):
15 return ovs.db.schema.IdlSchema.from_json(ovs.json.from_file(filename))
17 def annotateSchema(schemaFile, annotationFile):
18 schemaJson = ovs.json.from_file(schemaFile)
19 execfile(annotationFile, globals(), {"s": schemaJson})
20 ovs.json.to_stream(schemaJson, sys.stdout)
21 sys.stdout.write('\n')
23 def constify(cType, const):
24 if (const and cType.endswith('*') and not cType.endswith('**')):
25 return 'const %s' % cType
29 def cMembers(prefix, columnName, column, const):
33 return [{'name': columnName,
34 'type': 'struct smap ',
37 if type.n_min == 1 and type.n_max == 1:
42 if type.is_optional_pointer():
48 key = {'name': "key_%s" % columnName,
49 'type': constify(type.key.toCType(prefix) + pointer, const),
51 value = {'name': "value_%s" % columnName,
52 'type': constify(type.value.toCType(prefix) + pointer, const),
54 members = [key, value]
56 m = {'name': columnName,
57 'type': constify(type.key.toCType(prefix) + pointer, const),
58 'comment': type.cDeclComment()}
61 if not singleton and not type.is_optional_pointer():
62 members.append({'name': 'n_%s' % columnName,
67 def printCIDLHeader(schemaFile):
68 schema = parseSchema(schemaFile)
69 prefix = schema.idlPrefix
71 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
73 #ifndef %(prefix)sIDL_HEADER
74 #define %(prefix)sIDL_HEADER 1
79 #include "ovsdb-data.h"
80 #include "ovsdb-idl-provider.h"
82 #include "uuid.h"''' % {'prefix': prefix.upper()}
84 for tableName, table in sorted(schema.tables.iteritems()):
85 structName = "%s%s" % (prefix, tableName.lower())
88 print "/* %s table. */" % tableName
89 print "struct %s {" % structName
90 print "\tstruct ovsdb_idl_row header_;"
91 for columnName, column in sorted(table.columns.iteritems()):
92 print "\n\t/* %s column. */" % columnName
93 for member in cMembers(prefix, columnName, column, False):
94 print "\t%(type)s%(name)s;%(comment)s" % member
98 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
99 for columnName in sorted(table.columns)]
100 + ["%s_N_COLUMNS" % structName.upper()])
103 for columnName in table.columns:
104 print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
106 'S': structName.upper(),
108 'C': columnName.upper()}
110 print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
113 const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uuid *);
114 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
115 const struct %(s)s *%(s)s_next(const struct %(s)s *);
116 #define %(S)s_FOR_EACH(ROW, IDL) \\
117 for ((ROW) = %(s)s_first(IDL); \\
119 (ROW) = %(s)s_next(ROW))
120 #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\
121 for ((ROW) = %(s)s_first(IDL); \\
122 (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
125 void %(s)s_init(struct %(s)s *);
126 void %(s)s_delete(const struct %(s)s *);
127 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
128 ''' % {'s': structName, 'S': structName.upper()}
130 for columnName, column in sorted(table.columns.iteritems()):
131 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
134 /* Functions for fetching columns as \"struct ovsdb_datum\"s. (This is
135 rarely useful. More often, it is easier to access columns by using
136 the members of %(s)s directly.) */""" % {'s': structName}
137 for columnName, column in sorted(table.columns.iteritems()):
138 if column.type.value:
139 valueParam = ', enum ovsdb_atomic_type value_type'
142 print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
143 's': structName, 'c': columnName, 'v': valueParam}
146 for columnName, column in sorted(table.columns.iteritems()):
147 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
148 if column.type.is_smap():
149 args = ['const struct smap *']
151 args = ['%(type)s%(name)s' % member for member
152 in cMembers(prefix, columnName, column, True)]
153 print '%s);' % ', '.join(args)
158 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
160 for tableName in schema.tables:
161 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
164 't': tableName.lower(),
165 'T': tableName.upper()}
166 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
168 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
169 print "\nvoid %sinit(void);" % prefix
171 print "\nconst char * %sget_db_version(void);" % prefix
172 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
174 def printEnum(members):
175 if len(members) == 0:
179 for member in members[:-1]:
180 print " %s," % member
181 print " %s" % members[-1]
184 def printCIDLSource(schemaFile):
185 schema = parseSchema(schemaFile)
186 prefix = schema.idlPrefix
188 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
193 #include "ovs-thread.h"
194 #include "ovsdb-data.h"
195 #include "ovsdb-error.h"
199 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
200 enum { sizeof_bool = 1 };
202 enum { sizeof_bool = sizeof(bool) };
206 ''' % schema.idlHeader
209 for tableName, table in sorted(schema.tables.iteritems()):
210 structName = "%s%s" % (prefix, tableName.lower())
212 static struct %(s)s *
213 %(s)s_cast(const struct ovsdb_idl_row *row)
215 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
217 ''' % {'s': structName}
220 for tableName, table in sorted(schema.tables.iteritems()):
221 structName = "%s%s" % (prefix, tableName.lower())
223 print "/* %s table. */" % (tableName)
226 for columnName, column in sorted(table.columns.iteritems()):
229 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
231 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
235 keyVar = "row->key_%s" % columnName
236 valueVar = "row->value_%s" % columnName
238 keyVar = "row->%s" % columnName
244 print " ovs_assert(inited);"
245 print " smap_init(&row->%s);" % columnName
246 print " for (i = 0; i < datum->n; i++) {"
247 print " smap_add(&row->%s," % columnName
248 print " datum->keys[i].string,"
249 print " datum->values[i].string);"
251 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
253 print " ovs_assert(inited);"
254 print " if (datum->n >= 1) {"
255 if not type.key.ref_table:
256 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
258 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[0].uuid));" % (keyVar, prefix, type.key.ref_table.name.lower(), prefix, prefix.upper(), type.key.ref_table.name.upper())
261 if type.value.ref_table:
262 print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
264 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[0].uuid));" % (valueVar, prefix, type.value.ref_table.name.lower(), prefix, prefix.upper(), type.value.ref_table.name.upper())
266 print " %s" % type.key.initCDefault(keyVar, type.n_min == 0)
268 print " %s" % type.value.initCDefault(valueVar, type.n_min == 0)
271 if type.n_max != sys.maxint:
272 print " size_t n = MIN(%d, datum->n);" % type.n_max
278 print " ovs_assert(inited);"
279 print " %s = NULL;" % keyVar
281 print " %s = NULL;" % valueVar
282 print " row->n_%s = 0;" % columnName
283 print " for (i = 0; i < %s; i++) {" % nMax
285 if type.key.ref_table:
286 print " struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[i].uuid));" % (prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower(), prefix, prefix.upper(), type.key.ref_table.name.upper())
288 refs.append('keyRow')
290 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
291 if type.value and type.value.ref_table:
292 print " struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[i].uuid));" % (prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower(), prefix, prefix.upper(), type.value.ref_table.name.upper())
293 valueSrc = "valueRow"
294 refs.append('valueRow')
296 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
298 print " if (%s) {" % ' && '.join(refs)
302 print "%sif (!row->n_%s) {" % (indent, columnName)
304 # Special case for boolean types. This is only here because
305 # sparse does not like the "normal" case ("warning: expression
306 # using sizeof bool").
307 if type.key.type == ovs.db.types.BooleanType:
308 sizeof = "sizeof_bool"
310 sizeof = "sizeof *%s" % keyVar
311 print "%s %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
314 # Special case for boolean types (see above).
315 if type.value.type == ovs.db.types.BooleanType:
316 sizeof = " * sizeof_bool"
318 sizeof = "sizeof *%s" % valueVar
319 print "%s %s = xmalloc(%s * %s);" % (indent, valueVar,
322 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
324 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
325 print "%srow->n_%s++;" % (indent, columnName)
332 for columnName, column in sorted(table.columns.iteritems()):
334 if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
337 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
339 struct %(s)s *row = %(s)s_cast(row_);
341 ovs_assert(inited);''' % {'s': structName, 'c': columnName}
344 print " smap_destroy(&row->%s);" % columnName
347 keyVar = "row->key_%s" % columnName
348 valueVar = "row->value_%s" % columnName
350 keyVar = "row->%s" % columnName
352 print " free(%s);" % keyVar
354 print " free(%s);" % valueVar
359 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
362 }''' % {'s': structName, 'c': columnName}
364 # Generic Row Initialization function.
367 %(s)s_init__(struct ovsdb_idl_row *row)
369 %(s)s_init(%(s)s_cast(row));
370 }""" % {'s': structName}
372 # Row Initialization function.
375 %(s)s_init(struct %(s)s *row)
377 memset(row, 0, sizeof *row); """ % {'s': structName}
378 for columnName, column in sorted(table.columns.iteritems()):
379 if column.type.is_smap():
380 print " smap_init(&row->%s);" % columnName
383 # First, next functions.
386 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
388 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
392 %(s)s_first(const struct ovsdb_idl *idl)
394 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
398 %(s)s_next(const struct %(s)s *row)
400 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
401 }''' % {'s': structName,
404 'T': tableName.upper()}
408 %(s)s_delete(const struct %(s)s *row)
410 ovsdb_idl_txn_delete(&row->header_);
414 %(s)s_insert(struct ovsdb_idl_txn *txn)
416 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
418 ''' % {'s': structName,
421 'T': tableName.upper()}
424 for columnName, column in sorted(table.columns.iteritems()):
427 %(s)s_verify_%(c)s(const struct %(s)s *row)
430 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
431 }''' % {'s': structName,
432 'S': structName.upper(),
434 'C': columnName.upper()}
437 for columnName, column in sorted(table.columns.iteritems()):
438 if column.type.value:
439 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
440 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
441 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
447 /* Returns the %(c)s column's value in 'row' as a struct ovsdb_datum.
448 * This is useful occasionally: for example, ovsdb_datum_find_key() is an
449 * easier and more efficient way to search for a given key than implementing
450 * the same operation on the "cooked" form in 'row'.
452 * 'key_type' must be %(kt)s.%(vc)s
453 * (This helps to avoid silent bugs if someone changes %(c)s's
454 * type without updating the caller.)
456 * The caller must not modify or free the returned value.
458 * Various kinds of changes can invalidate the returned value: modifying
459 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
460 * If the returned value is needed for a long time, it is best to make a copy
461 * of it with ovsdb_datum_clone(). */
462 const struct ovsdb_datum *
463 %(s)s_get_%(c)s(const struct %(s)s *row,
464 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
466 ovs_assert(key_type == %(kt)s);%(vt)s
467 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
468 }""" % {'s': structName, 'c': columnName,
469 'kt': column.type.key.toAtomicType(),
470 'v': valueParam, 'vt': valueType, 'vc': valueComment}
473 for columnName, column in sorted(table.columns.iteritems()):
479 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *smap)
481 struct ovsdb_datum datum;
485 struct smap_node *node;
488 datum.n = smap_count(smap);
489 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
490 datum.values = xmalloc(datum.n * sizeof *datum.values);
493 SMAP_FOR_EACH (node, smap) {
494 datum.keys[i].string = xstrdup(node->key);
495 datum.values[i].string = xstrdup(node->value);
498 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
500 ovsdb_datum_init_empty(&datum);
502 ovsdb_idl_txn_write(&row->header_,
503 &%(s)s_columns[%(S)s_COL_%(C)s],
506 """ % {'s': structName,
507 'S': structName.upper(),
509 'C': columnName.upper()}
514 members = cMembers(prefix, columnName, column, True)
515 keyVar = members[0]['name']
519 valueVar = members[1]['name']
521 nVar = members[2]['name']
524 nVar = members[1]['name']
525 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
526 {'s': structName, 'c': columnName,
527 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
529 print " struct ovsdb_datum datum;"
530 if type.n_min == 1 and type.n_max == 1:
531 print " union ovsdb_atom key;"
533 print " union ovsdb_atom value;"
535 print " ovs_assert(inited);"
536 print " datum.n = 1;"
537 print " datum.keys = &key;"
538 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
540 print " datum.values = &value;"
541 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
543 print " datum.values = NULL;"
544 txn_write_func = "ovsdb_idl_txn_write_clone"
545 elif type.is_optional_pointer():
546 print " union ovsdb_atom key;"
548 print " ovs_assert(inited);"
549 print " if (%s) {" % keyVar
550 print " datum.n = 1;"
551 print " datum.keys = &key;"
552 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
554 print " datum.n = 0;"
555 print " datum.keys = NULL;"
557 print " datum.values = NULL;"
558 txn_write_func = "ovsdb_idl_txn_write_clone"
559 elif type.n_max == 1:
560 print " union ovsdb_atom key;"
562 print " ovs_assert(inited);"
563 print " if (%s) {" % nVar
564 print " datum.n = 1;"
565 print " datum.keys = &key;"
566 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
568 print " datum.n = 0;"
569 print " datum.keys = NULL;"
571 print " datum.values = NULL;"
572 txn_write_func = "ovsdb_idl_txn_write_clone"
576 print " ovs_assert(inited);"
577 print " datum.n = %s;" % nVar
578 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
580 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
582 print " datum.values = NULL;"
583 print " for (i = 0; i < %s; i++) {" % nVar
584 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
586 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
589 valueType = type.value.toAtomicType()
591 valueType = "OVSDB_TYPE_VOID"
592 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
593 type.key.toAtomicType(), valueType)
594 txn_write_func = "ovsdb_idl_txn_write"
595 print " %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
596 % {'f': txn_write_func,
598 'S': structName.upper(),
599 'C': columnName.upper()}
603 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
604 structName, structName.upper())
606 static void\n%s_columns_init(void)
608 struct ovsdb_idl_column *c;\
610 for columnName, column in sorted(table.columns.iteritems()):
611 cs = "%s_col_%s" % (structName, columnName)
612 d = {'cs': cs, 'c': columnName, 's': structName}
618 print " /* Initialize %(cs)s. */" % d
619 print " c = &%(cs)s;" % d
620 print " c->name = \"%(c)s\";" % d
621 print column.type.cInitType(" ", "c->type")
622 print " c->mutable = %s;" % mutable
623 print " c->parse = %(s)s_parse_%(c)s;" % d
624 print " c->unparse = %(s)s_unparse_%(c)s;" % d
629 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
630 for tableName, table in sorted(schema.tables.iteritems()):
631 structName = "%s%s" % (prefix, tableName.lower())
636 print " {\"%s\", %s," % (tableName, is_root)
637 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
638 structName, structName)
639 print " sizeof(struct %s), %s_init__}," % (structName, structName)
643 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
644 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
645 schema.name, prefix, prefix)
648 # global init function
656 assert_single_threaded();
659 for tableName, table in sorted(schema.tables.iteritems()):
660 structName = "%s%s" % (prefix, tableName.lower())
661 print " %s_columns_init();" % structName
665 /* Return the schema version. The caller must not free the returned value. */
667 %sget_db_version(void)
671 """ % (prefix, schema.version)
675 def ovsdb_escape(string):
679 raise ovs.db.error.Error("strings may not contain null bytes")
693 return '\\x%02x' % ord(c)
694 return re.sub(r'["\\\000-\037]', escape, string)
698 %(argv0)s: ovsdb schema compiler
699 usage: %(argv0)s [OPTIONS] COMMAND ARG...
701 The following commands are supported:
702 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
703 c-idl-header IDL print C header file for IDL
704 c-idl-source IDL print C source file for IDL implementation
705 nroff IDL print schema documentation in nroff format
707 The following options are also available:
708 -h, --help display this help message
709 -V, --version display version information\
710 """ % {'argv0': argv0}
713 if __name__ == "__main__":
716 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
720 except getopt.GetoptError, geo:
721 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
724 for key, value in options:
725 if key in ['-h', '--help']:
727 elif key in ['-V', '--version']:
728 print "ovsdb-idlc (Open vSwitch) @VERSION@"
729 elif key in ['-C', '--directory']:
734 optKeys = [key for key, value in options]
737 sys.stderr.write("%s: missing command argument "
738 "(use --help for help)\n" % argv0)
741 commands = {"annotate": (annotateSchema, 2),
742 "c-idl-header": (printCIDLHeader, 1),
743 "c-idl-source": (printCIDLSource, 1)}
745 if not args[0] in commands:
746 sys.stderr.write("%s: unknown command \"%s\" "
747 "(use --help for help)\n" % (argv0, args[0]))
750 func, n_args = commands[args[0]]
751 if len(args) - 1 != n_args:
752 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
754 % (argv0, args[0], n_args, len(args) - 1))
758 except ovs.db.error.Error, e:
759 sys.stderr.write("%s: %s\n" % (argv0, e))