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):
25 and cType.endswith('*') and
26 (cType == 'char **' or not cType.endswith('**'))):
27 return 'const %s' % cType
31 def cMembers(prefix, columnName, column, const):
35 return [{'name': columnName,
36 'type': 'struct smap ',
39 if type.n_min == 1 and type.n_max == 1:
44 if type.is_optional_pointer():
50 key = {'name': "key_%s" % columnName,
51 'type': constify(type.key.toCType(prefix) + pointer, const),
53 value = {'name': "value_%s" % columnName,
54 'type': constify(type.value.toCType(prefix) + pointer, const),
56 members = [key, value]
58 m = {'name': columnName,
59 'type': constify(type.key.toCType(prefix) + pointer, const),
60 'comment': type.cDeclComment()}
63 if not singleton and not type.is_optional_pointer():
64 members.append({'name': 'n_%s' % columnName,
69 def printCIDLHeader(schemaFile):
70 schema = parseSchema(schemaFile)
71 prefix = schema.idlPrefix
73 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
75 #ifndef %(prefix)sIDL_HEADER
76 #define %(prefix)sIDL_HEADER 1
81 #include "ovsdb-data.h"
82 #include "ovsdb-idl-provider.h"
84 #include "uuid.h"''' % {'prefix': prefix.upper()}
86 for tableName, table in sorted(schema.tables.iteritems()):
87 structName = "%s%s" % (prefix, tableName.lower())
90 print "/* %s table. */" % tableName
91 print "struct %s {" % structName
92 print "\tstruct ovsdb_idl_row header_;"
93 for columnName, column in sorted(table.columns.iteritems()):
94 print "\n\t/* %s column. */" % columnName
95 for member in cMembers(prefix, columnName, column, False):
96 print "\t%(type)s%(name)s;%(comment)s" % member
100 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
101 for columnName in sorted(table.columns)]
102 + ["%s_N_COLUMNS" % structName.upper()])
105 for columnName in table.columns:
106 print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
108 'S': structName.upper(),
110 'C': columnName.upper()}
112 print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
115 const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uuid *);
116 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
117 const struct %(s)s *%(s)s_next(const struct %(s)s *);
118 #define %(S)s_FOR_EACH(ROW, IDL) \\
119 for ((ROW) = %(s)s_first(IDL); \\
121 (ROW) = %(s)s_next(ROW))
122 #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\
123 for ((ROW) = %(s)s_first(IDL); \\
124 (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
127 void %(s)s_init(struct %(s)s *);
128 void %(s)s_delete(const struct %(s)s *);
129 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
130 ''' % {'s': structName, 'S': structName.upper()}
132 for columnName, column in sorted(table.columns.iteritems()):
133 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
136 /* Functions for fetching columns as \"struct ovsdb_datum\"s. (This is
137 rarely useful. More often, it is easier to access columns by using
138 the members of %(s)s directly.) */""" % {'s': structName}
139 for columnName, column in sorted(table.columns.iteritems()):
140 if column.type.value:
141 valueParam = ', enum ovsdb_atomic_type value_type'
144 print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
145 's': structName, 'c': columnName, 'v': valueParam}
148 for columnName, column in sorted(table.columns.iteritems()):
149 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
150 if column.type.is_smap():
151 args = ['const struct smap *']
153 args = ['%(type)s%(name)s' % member for member
154 in cMembers(prefix, columnName, column, True)]
155 print '%s);' % ', '.join(args)
160 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
162 for tableName in schema.tables:
163 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
166 't': tableName.lower(),
167 'T': tableName.upper()}
168 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
170 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
171 print "\nvoid %sinit(void);" % prefix
173 print "\nconst char * %sget_db_version(void);" % prefix
174 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
176 def printEnum(members):
177 if len(members) == 0:
181 for member in members[:-1]:
182 print " %s," % member
183 print " %s" % members[-1]
186 def printCIDLSource(schemaFile):
187 schema = parseSchema(schemaFile)
188 prefix = schema.idlPrefix
190 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
195 #include "ovs-thread.h"
196 #include "ovsdb-data.h"
197 #include "ovsdb-error.h"
201 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
202 enum { sizeof_bool = 1 };
204 enum { sizeof_bool = sizeof(bool) };
208 ''' % schema.idlHeader
211 for tableName, table in sorted(schema.tables.iteritems()):
212 structName = "%s%s" % (prefix, tableName.lower())
214 static struct %(s)s *
215 %(s)s_cast(const struct ovsdb_idl_row *row)
217 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
219 ''' % {'s': structName}
222 for tableName, table in sorted(schema.tables.iteritems()):
223 structName = "%s%s" % (prefix, tableName.lower())
225 print "/* %s table. */" % (tableName)
228 for columnName, column in sorted(table.columns.iteritems()):
231 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
233 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
237 keyVar = "row->key_%s" % columnName
238 valueVar = "row->value_%s" % columnName
240 keyVar = "row->%s" % columnName
246 print " ovs_assert(inited);"
247 print " smap_init(&row->%s);" % columnName
248 print " for (i = 0; i < datum->n; i++) {"
249 print " smap_add(&row->%s," % columnName
250 print " datum->keys[i].string,"
251 print " datum->values[i].string);"
253 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
255 print " ovs_assert(inited);"
256 print " if (datum->n >= 1) {"
257 if not type.key.ref_table:
258 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
260 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())
263 if type.value.ref_table:
264 print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
266 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())
268 print " %s" % type.key.initCDefault(keyVar, type.n_min == 0)
270 print " %s" % type.value.initCDefault(valueVar, type.n_min == 0)
273 if type.n_max != sys.maxint:
274 print " size_t n = MIN(%d, datum->n);" % type.n_max
280 print " ovs_assert(inited);"
281 print " %s = NULL;" % keyVar
283 print " %s = NULL;" % valueVar
284 print " row->n_%s = 0;" % columnName
285 print " for (i = 0; i < %s; i++) {" % nMax
287 if type.key.ref_table:
288 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())
290 refs.append('keyRow')
292 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
293 if type.value and type.value.ref_table:
294 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())
295 valueSrc = "valueRow"
296 refs.append('valueRow')
298 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
300 print " if (%s) {" % ' && '.join(refs)
304 print "%sif (!row->n_%s) {" % (indent, columnName)
306 # Special case for boolean types. This is only here because
307 # sparse does not like the "normal" case ("warning: expression
308 # using sizeof bool").
309 if type.key.type == ovs.db.types.BooleanType:
310 sizeof = "sizeof_bool"
312 sizeof = "sizeof *%s" % keyVar
313 print "%s %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
316 # Special case for boolean types (see above).
317 if type.value.type == ovs.db.types.BooleanType:
318 sizeof = " * sizeof_bool"
320 sizeof = "sizeof *%s" % valueVar
321 print "%s %s = xmalloc(%s * %s);" % (indent, valueVar,
324 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
326 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
327 print "%srow->n_%s++;" % (indent, columnName)
334 for columnName, column in sorted(table.columns.iteritems()):
336 if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
339 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
341 struct %(s)s *row = %(s)s_cast(row_);
343 ovs_assert(inited);''' % {'s': structName, 'c': columnName}
346 print " smap_destroy(&row->%s);" % columnName
349 keyVar = "row->key_%s" % columnName
350 valueVar = "row->value_%s" % columnName
352 keyVar = "row->%s" % columnName
354 print " free(%s);" % keyVar
356 print " free(%s);" % valueVar
361 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
364 }''' % {'s': structName, 'c': columnName}
366 # Generic Row Initialization function.
369 %(s)s_init__(struct ovsdb_idl_row *row)
371 %(s)s_init(%(s)s_cast(row));
372 }""" % {'s': structName}
374 # Row Initialization function.
377 %(s)s_init(struct %(s)s *row)
379 memset(row, 0, sizeof *row); """ % {'s': structName}
380 for columnName, column in sorted(table.columns.iteritems()):
381 if column.type.is_smap():
382 print " smap_init(&row->%s);" % columnName
385 # First, next functions.
388 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
390 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
394 %(s)s_first(const struct ovsdb_idl *idl)
396 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
400 %(s)s_next(const struct %(s)s *row)
402 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
403 }''' % {'s': structName,
406 'T': tableName.upper()}
410 %(s)s_delete(const struct %(s)s *row)
412 ovsdb_idl_txn_delete(&row->header_);
416 %(s)s_insert(struct ovsdb_idl_txn *txn)
418 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
420 ''' % {'s': structName,
423 'T': tableName.upper()}
426 for columnName, column in sorted(table.columns.iteritems()):
429 %(s)s_verify_%(c)s(const struct %(s)s *row)
432 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
433 }''' % {'s': structName,
434 'S': structName.upper(),
436 'C': columnName.upper()}
439 for columnName, column in sorted(table.columns.iteritems()):
440 if column.type.value:
441 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
442 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
443 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
449 /* Returns the %(c)s column's value in 'row' as a struct ovsdb_datum.
450 * This is useful occasionally: for example, ovsdb_datum_find_key() is an
451 * easier and more efficient way to search for a given key than implementing
452 * the same operation on the "cooked" form in 'row'.
454 * 'key_type' must be %(kt)s.%(vc)s
455 * (This helps to avoid silent bugs if someone changes %(c)s's
456 * type without updating the caller.)
458 * The caller must not modify or free the returned value.
460 * Various kinds of changes can invalidate the returned value: modifying
461 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
462 * If the returned value is needed for a long time, it is best to make a copy
463 * of it with ovsdb_datum_clone(). */
464 const struct ovsdb_datum *
465 %(s)s_get_%(c)s(const struct %(s)s *row,
466 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
468 ovs_assert(key_type == %(kt)s);%(vt)s
469 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
470 }""" % {'s': structName, 'c': columnName,
471 'kt': column.type.key.toAtomicType(),
472 'v': valueParam, 'vt': valueType, 'vc': valueComment}
475 for columnName, column in sorted(table.columns.iteritems()):
481 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *smap)
483 struct ovsdb_datum datum;
487 struct smap_node *node;
490 datum.n = smap_count(smap);
491 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
492 datum.values = xmalloc(datum.n * sizeof *datum.values);
495 SMAP_FOR_EACH (node, smap) {
496 datum.keys[i].string = xstrdup(node->key);
497 datum.values[i].string = xstrdup(node->value);
500 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
502 ovsdb_datum_init_empty(&datum);
504 ovsdb_idl_txn_write(&row->header_,
505 &%(s)s_columns[%(S)s_COL_%(C)s],
508 """ % {'s': structName,
509 'S': structName.upper(),
511 'C': columnName.upper()}
516 members = cMembers(prefix, columnName, column, True)
517 keyVar = members[0]['name']
521 valueVar = members[1]['name']
523 nVar = members[2]['name']
526 nVar = members[1]['name']
527 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
528 {'s': structName, 'c': columnName,
529 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
531 print " struct ovsdb_datum datum;"
532 if type.n_min == 1 and type.n_max == 1:
533 print " union ovsdb_atom key;"
535 print " union ovsdb_atom value;"
537 print " ovs_assert(inited);"
538 print " datum.n = 1;"
539 print " datum.keys = &key;"
540 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
542 print " datum.values = &value;"
543 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
545 print " datum.values = NULL;"
546 txn_write_func = "ovsdb_idl_txn_write_clone"
547 elif type.is_optional_pointer():
548 print " union ovsdb_atom key;"
550 print " ovs_assert(inited);"
551 print " if (%s) {" % keyVar
552 print " datum.n = 1;"
553 print " datum.keys = &key;"
554 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
556 print " datum.n = 0;"
557 print " datum.keys = NULL;"
559 print " datum.values = NULL;"
560 txn_write_func = "ovsdb_idl_txn_write_clone"
561 elif type.n_max == 1:
562 print " union ovsdb_atom key;"
564 print " ovs_assert(inited);"
565 print " if (%s) {" % nVar
566 print " datum.n = 1;"
567 print " datum.keys = &key;"
568 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
570 print " datum.n = 0;"
571 print " datum.keys = NULL;"
573 print " datum.values = NULL;"
574 txn_write_func = "ovsdb_idl_txn_write_clone"
578 print " ovs_assert(inited);"
579 print " datum.n = %s;" % nVar
580 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
582 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
584 print " datum.values = NULL;"
585 print " for (i = 0; i < %s; i++) {" % nVar
586 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
588 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
591 valueType = type.value.toAtomicType()
593 valueType = "OVSDB_TYPE_VOID"
594 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
595 type.key.toAtomicType(), valueType)
596 txn_write_func = "ovsdb_idl_txn_write"
597 print " %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
598 % {'f': txn_write_func,
600 'S': structName.upper(),
601 'C': columnName.upper()}
605 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
606 structName, structName.upper())
608 static void\n%s_columns_init(void)
610 struct ovsdb_idl_column *c;\
612 for columnName, column in sorted(table.columns.iteritems()):
613 cs = "%s_col_%s" % (structName, columnName)
614 d = {'cs': cs, 'c': columnName, 's': structName}
620 print " /* Initialize %(cs)s. */" % d
621 print " c = &%(cs)s;" % d
622 print " c->name = \"%(c)s\";" % d
623 print column.type.cInitType(" ", "c->type")
624 print " c->mutable = %s;" % mutable
625 print " c->parse = %(s)s_parse_%(c)s;" % d
626 print " c->unparse = %(s)s_unparse_%(c)s;" % d
631 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
632 for tableName, table in sorted(schema.tables.iteritems()):
633 structName = "%s%s" % (prefix, tableName.lower())
638 print " {\"%s\", %s," % (tableName, is_root)
639 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
640 structName, structName)
641 print " sizeof(struct %s), %s_init__}," % (structName, structName)
645 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
646 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
647 schema.name, prefix, prefix)
650 # global init function
658 assert_single_threaded();
661 for tableName, table in sorted(schema.tables.iteritems()):
662 structName = "%s%s" % (prefix, tableName.lower())
663 print " %s_columns_init();" % structName
667 /* Return the schema version. The caller must not free the returned value. */
669 %sget_db_version(void)
673 """ % (prefix, schema.version)
677 def ovsdb_escape(string):
681 raise ovs.db.error.Error("strings may not contain null bytes")
695 return '\\x%02x' % ord(c)
696 return re.sub(r'["\\\000-\037]', escape, string)
700 %(argv0)s: ovsdb schema compiler
701 usage: %(argv0)s [OPTIONS] COMMAND ARG...
703 The following commands are supported:
704 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
705 c-idl-header IDL print C header file for IDL
706 c-idl-source IDL print C source file for IDL implementation
707 nroff IDL print schema documentation in nroff format
709 The following options are also available:
710 -h, --help display this help message
711 -V, --version display version information\
712 """ % {'argv0': argv0}
715 if __name__ == "__main__":
718 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
722 except getopt.GetoptError, geo:
723 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
726 for key, value in options:
727 if key in ['-h', '--help']:
729 elif key in ['-V', '--version']:
730 print "ovsdb-idlc (Open vSwitch) @VERSION@"
731 elif key in ['-C', '--directory']:
736 optKeys = [key for key, value in options]
739 sys.stderr.write("%s: missing command argument "
740 "(use --help for help)\n" % argv0)
743 commands = {"annotate": (annotateSchema, 2),
744 "c-idl-header": (printCIDLHeader, 1),
745 "c-idl-source": (printCIDLSource, 1)}
747 if not args[0] in commands:
748 sys.stderr.write("%s: unknown command \"%s\" "
749 "(use --help for help)\n" % (argv0, args[0]))
752 func, n_args = commands[args[0]]
753 if len(args) - 1 != n_args:
754 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
756 % (argv0, args[0], n_args, len(args) - 1))
760 except ovs.db.error.Error, e:
761 sys.stderr.write("%s: %s\n" % (argv0, e))