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, tableName, columnName, column, const):
37 /* Sets the "%(c)s" column's value from the "%(t)s" table in 'row'
40 * The caller retains ownership of '%(c)s' and everything in it. */""" \
43 return (comment, [{'name': columnName,
44 'type': 'struct smap ',
47 comment = """\n/* Sets the "%s" column from the "%s" table in """\
48 """'row' to\n""" % (columnName, tableName)
50 if type.n_min == 1 and type.n_max == 1:
55 if type.is_optional_pointer():
62 keyName = "key_%s" % columnName
63 valueName = "value_%s" % columnName
65 key = {'name': keyName,
66 'type': constify(type.key.toCType(prefix) + pointer, const),
68 value = {'name': valueName,
69 'type': constify(type.value.toCType(prefix) + pointer, const),
73 comment += " * the map with key '%s' and value '%s'\n *" \
74 % (keyName, valueName)
76 comment += " * the map with keys '%s' and values '%s'\n *" \
77 % (keyName, valueName)
78 members = [key, value]
80 m = {'name': columnName,
81 'type': constify(type.key.toCType(prefix) + pointer, const),
82 'comment': type.cDeclComment()}
85 comment += " * '%s'" % columnName
87 comment += " * the '%s' set" % columnName
90 if not singleton and not type.is_optional_pointer():
91 sizeName = "n_%s" % columnName
93 comment += " with '%s' entries" % sizeName
94 members.append({'name': sizeName,
100 if type.is_optional() and not type.is_optional_pointer():
102 * '%s' may be 0 or 1; if it is 0, then '%s'
103 * may be NULL.\n""" \
104 % ("n_%s" % columnName, columnName)
106 if type.is_optional_pointer():
108 * If "%s" is null, the column will be the empty set,
109 * otherwise it will contain the specified value.\n""" % columnName
111 if type.constraintsToEnglish():
113 * Argument constraints: %s\n""" \
114 % type.constraintsToEnglish(lambda s : '"%s"' % s)
116 comment += " *\n * The caller retains ownership of the arguments. */"
118 return (comment, members)
120 def printCIDLHeader(schemaFile):
121 schema = parseSchema(schemaFile)
122 prefix = schema.idlPrefix
124 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
126 #ifndef %(prefix)sIDL_HEADER
127 #define %(prefix)sIDL_HEADER 1
132 #include "ovsdb-data.h"
133 #include "ovsdb-idl-provider.h"
135 #include "uuid.h"''' % {'prefix': prefix.upper()}
137 for tableName, table in sorted(schema.tables.iteritems()):
138 structName = "%s%s" % (prefix, tableName.lower())
141 print "/* %s table. */" % tableName
142 print "struct %s {" % structName
143 print "\tstruct ovsdb_idl_row header_;"
144 for columnName, column in sorted(table.columns.iteritems()):
145 print "\n\t/* %s column. */" % columnName
146 comment, members = cMembers(prefix, tableName,
147 columnName, column, False)
148 for member in members:
149 print "\t%(type)s%(name)s;%(comment)s" % member
153 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
154 for columnName in sorted(table.columns)]
155 + ["%s_N_COLUMNS" % structName.upper()])
158 for columnName in table.columns:
159 print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
161 'S': structName.upper(),
163 'C': columnName.upper()}
165 print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
168 const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uuid *);
169 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
170 const struct %(s)s *%(s)s_next(const struct %(s)s *);
171 #define %(S)s_FOR_EACH(ROW, IDL) \\
172 for ((ROW) = %(s)s_first(IDL); \\
174 (ROW) = %(s)s_next(ROW))
175 #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\
176 for ((ROW) = %(s)s_first(IDL); \\
177 (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
180 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *);
181 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change);
182 const struct %(s)s *%(s)s_track_get_first(const struct ovsdb_idl *);
183 const struct %(s)s *%(s)s_track_get_next(const struct %(s)s *);
184 #define %(S)s_FOR_EACH_TRACKED(ROW, IDL) \\
185 for ((ROW) = %(s)s_track_get_first(IDL); \\
187 (ROW) = %(s)s_track_get_next(ROW))
189 void %(s)s_init(struct %(s)s *);
190 void %(s)s_delete(const struct %(s)s *);
191 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
192 ''' % {'s': structName, 'S': structName.upper()}
194 for columnName, column in sorted(table.columns.iteritems()):
195 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
198 for columnName, column in sorted(table.columns.iteritems()):
199 if column.type.value:
200 valueParam = ', enum ovsdb_atomic_type value_type'
203 print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
204 's': structName, 'c': columnName, 'v': valueParam}
207 for columnName, column in sorted(table.columns.iteritems()):
208 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
209 if column.type.is_smap():
210 args = ['const struct smap *']
212 comment, members = cMembers(prefix, tableName, columnName,
214 args = ['%(type)s%(name)s' % member for member in members]
215 print '%s);' % ', '.join(args)
220 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
222 for tableName in schema.tables:
223 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
226 't': tableName.lower(),
227 'T': tableName.upper()}
228 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
230 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
231 print "\nvoid %sinit(void);" % prefix
233 print "\nconst char * %sget_db_version(void);" % prefix
234 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
236 def printEnum(members):
237 if len(members) == 0:
241 for member in members[:-1]:
242 print " %s," % member
243 print " %s" % members[-1]
246 def printCIDLSource(schemaFile):
247 schema = parseSchema(schemaFile)
248 prefix = schema.idlPrefix
250 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
255 #include "ovs-thread.h"
256 #include "ovsdb-data.h"
257 #include "ovsdb-error.h"
261 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
262 enum { sizeof_bool = 1 };
264 enum { sizeof_bool = sizeof(bool) };
268 ''' % schema.idlHeader
271 for tableName, table in sorted(schema.tables.iteritems()):
272 structName = "%s%s" % (prefix, tableName.lower())
274 static struct %(s)s *
275 %(s)s_cast(const struct ovsdb_idl_row *row)
277 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
279 ''' % {'s': structName}
282 for tableName, table in sorted(schema.tables.iteritems()):
283 structName = "%s%s" % (prefix, tableName.lower())
285 print "/* %s table. */" % (tableName)
288 for columnName, column in sorted(table.columns.iteritems()):
291 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
293 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
297 keyVar = "row->key_%s" % columnName
298 valueVar = "row->value_%s" % columnName
300 keyVar = "row->%s" % columnName
306 print " ovs_assert(inited);"
307 print " smap_init(&row->%s);" % columnName
308 print " for (i = 0; i < datum->n; i++) {"
309 print " smap_add(&row->%s," % columnName
310 print " datum->keys[i].string,"
311 print " datum->values[i].string);"
313 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
315 print " ovs_assert(inited);"
316 print " if (datum->n >= 1) {"
317 if not type.key.ref_table:
318 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
320 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())
323 if type.value.ref_table:
324 print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
326 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())
328 print " %s" % type.key.initCDefault(keyVar, type.n_min == 0)
330 print " %s" % type.value.initCDefault(valueVar, type.n_min == 0)
333 if type.n_max != sys.maxint:
334 print " size_t n = MIN(%d, datum->n);" % type.n_max
340 print " ovs_assert(inited);"
341 print " %s = NULL;" % keyVar
343 print " %s = NULL;" % valueVar
344 print " row->n_%s = 0;" % columnName
345 print " for (i = 0; i < %s; i++) {" % nMax
347 if type.key.ref_table:
348 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())
350 refs.append('keyRow')
352 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
353 if type.value and type.value.ref_table:
354 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())
355 valueSrc = "valueRow"
356 refs.append('valueRow')
358 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
360 print " if (%s) {" % ' && '.join(refs)
364 print "%sif (!row->n_%s) {" % (indent, columnName)
366 # Special case for boolean types. This is only here because
367 # sparse does not like the "normal" case ("warning: expression
368 # using sizeof bool").
369 if type.key.type == ovs.db.types.BooleanType:
370 sizeof = "sizeof_bool"
372 sizeof = "sizeof *%s" % keyVar
373 print "%s %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
376 # Special case for boolean types (see above).
377 if type.value.type == ovs.db.types.BooleanType:
378 sizeof = " * sizeof_bool"
380 sizeof = "sizeof *%s" % valueVar
381 print "%s %s = xmalloc(%s * %s);" % (indent, valueVar,
384 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
386 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
387 print "%srow->n_%s++;" % (indent, columnName)
394 for columnName, column in sorted(table.columns.iteritems()):
396 if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
399 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
401 struct %(s)s *row = %(s)s_cast(row_);
403 ovs_assert(inited);''' % {'s': structName, 'c': columnName}
406 print " smap_destroy(&row->%s);" % columnName
409 keyVar = "row->key_%s" % columnName
410 valueVar = "row->value_%s" % columnName
412 keyVar = "row->%s" % columnName
414 print " free(%s);" % keyVar
416 print " free(%s);" % valueVar
421 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
424 }''' % {'s': structName, 'c': columnName}
426 # Generic Row Initialization function.
429 %(s)s_init__(struct ovsdb_idl_row *row)
431 %(s)s_init(%(s)s_cast(row));
432 }""" % {'s': structName}
434 # Row Initialization function.
436 /* Clears the contents of 'row' in table "%(t)s". */
438 %(s)s_init(struct %(s)s *row)
440 memset(row, 0, sizeof *row); """ % {'s': structName, 't': tableName}
441 for columnName, column in sorted(table.columns.iteritems()):
442 if column.type.is_smap():
443 print " smap_init(&row->%s);" % columnName
446 # First, next functions.
448 /* Searches table "%(t)s" in 'idl' for a row with UUID 'uuid'. Returns
449 * a pointer to the row if there is one, otherwise a null pointer. */
451 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
453 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
456 /* Returns a row in table "%(t)s" in 'idl', or a null pointer if that
459 * Database tables are internally maintained as hash tables, so adding or
460 * removing rows while traversing the same table can cause some rows to be
461 * visited twice or not at apply. */
463 %(s)s_first(const struct ovsdb_idl *idl)
465 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
468 /* Returns a row following 'row' within its table, or a null pointer if 'row'
469 * is the last row in its table. */
471 %(s)s_next(const struct %(s)s *row)
473 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
476 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *idl)
478 return ovsdb_idl_table_get_seqno(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]);
481 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change)
483 return ovsdb_idl_row_get_seqno(&row->header_, change);
487 %(s)s_track_get_first(const struct ovsdb_idl *idl)
489 return %(s)s_cast(ovsdb_idl_track_get_first(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
493 *%(s)s_track_get_next(const struct %(s)s *row)
495 return %(s)s_cast(ovsdb_idl_track_get_next(&row->header_));
496 }''' % {'s': structName,
500 'T': tableName.upper()}
504 /* Deletes 'row' from table "%(t)s". 'row' may be freed, so it must not be
505 * accessed afterward.
507 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
509 %(s)s_delete(const struct %(s)s *row)
511 ovsdb_idl_txn_delete(&row->header_);
514 /* Inserts and returns a new row in the table "%(t)s" in the database
515 * with open transaction 'txn'.
517 * The new row is assigned a randomly generated provisional UUID.
518 * ovsdb-server will assign a different UUID when 'txn' is committed,
519 * but the IDL will replace any uses of the provisional UUID in the
520 * data to be to be committed by the UUID assigned by ovsdb-server. */
522 %(s)s_insert(struct ovsdb_idl_txn *txn)
524 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
525 }''' % {'s': structName,
529 'T': tableName.upper()}
532 for columnName, column in sorted(table.columns.iteritems()):
534 /* Causes the original contents of column "%(c)s" in 'row' to be
535 * verified as a prerequisite to completing the transaction. That is, if
536 * "%(c)s" in 'row' changed (or if 'row' was deleted) between the
537 * time that the IDL originally read its contents and the time that the
538 * transaction commits, then the transaction aborts and ovsdb_idl_txn_commit()
539 * returns TXN_AGAIN_WAIT or TXN_AGAIN_NOW (depending on whether the database
540 * change has already been received).
542 * The intention is that, to ensure that no transaction commits based on dirty
543 * reads, an application should call this function any time "%(c)s" is
544 * read as part of a read-modify-write operation.
546 * In some cases this function reduces to a no-op, because the current value
547 * of "%(c)s" is already known:
549 * - If 'row' is a row created by the current transaction (returned by
552 * - If "%(c)s" has already been modified (with
553 * %(s)s_set_%(c)s()) within the current transaction.
555 * Because of the latter property, always call this function *before*
556 * %(s)s_set_%(c)s() for a given read-modify-write.
558 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
560 %(s)s_verify_%(c)s(const struct %(s)s *row)
563 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
564 }''' % {'s': structName,
565 'S': structName.upper(),
567 'C': columnName.upper()}
570 for columnName, column in sorted(table.columns.iteritems()):
571 if column.type.value:
572 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
573 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
574 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
580 /* Returns the "%(c)s" column's value from the "%(t)s" table in 'row'
581 * as a struct ovsdb_datum. This is useful occasionally: for example,
582 * ovsdb_datum_find_key() is an easier and more efficient way to search
583 * for a given key than implementing the same operation on the "cooked"
586 * 'key_type' must be %(kt)s.%(vc)s
587 * (This helps to avoid silent bugs if someone changes %(c)s's
588 * type without updating the caller.)
590 * The caller must not modify or free the returned value.
592 * Various kinds of changes can invalidate the returned value: modifying
593 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
594 * If the returned value is needed for a long time, it is best to make a copy
595 * of it with ovsdb_datum_clone().
597 * This function is rarely useful, since it is easier to access the value
598 * directly through the "%(c)s" member in %(s)s. */
599 const struct ovsdb_datum *
600 %(s)s_get_%(c)s(const struct %(s)s *row,
601 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
603 ovs_assert(key_type == %(kt)s);%(vt)s
604 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
605 }""" % {'t': tableName, 's': structName, 'c': columnName,
606 'kt': column.type.key.toAtomicType(),
607 'v': valueParam, 'vt': valueType, 'vc': valueComment}
610 for columnName, column in sorted(table.columns.iteritems()):
613 comment, members = cMembers(prefix, tableName, columnName,
619 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
621 struct ovsdb_datum datum;
625 struct smap_node *node;
628 datum.n = smap_count(%(c)s);
629 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
630 datum.values = xmalloc(datum.n * sizeof *datum.values);
633 SMAP_FOR_EACH (node, %(c)s) {
634 datum.keys[i].string = xstrdup(node->key);
635 datum.values[i].string = xstrdup(node->value);
638 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
640 ovsdb_datum_init_empty(&datum);
642 ovsdb_idl_txn_write(&row->header_,
643 &%(s)s_columns[%(S)s_COL_%(C)s],
646 """ % {'t': tableName,
648 'S': structName.upper(),
650 'C': columnName.upper()}
653 keyVar = members[0]['name']
657 valueVar = members[1]['name']
659 nVar = members[2]['name']
662 nVar = members[1]['name']
666 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
667 {'s': structName, 'c': columnName,
668 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
670 print " struct ovsdb_datum datum;"
671 if type.n_min == 1 and type.n_max == 1:
672 print " union ovsdb_atom key;"
674 print " union ovsdb_atom value;"
676 print " ovs_assert(inited);"
677 print " datum.n = 1;"
678 print " datum.keys = &key;"
679 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
681 print " datum.values = &value;"
682 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
684 print " datum.values = NULL;"
685 txn_write_func = "ovsdb_idl_txn_write_clone"
686 elif type.is_optional_pointer():
687 print " union ovsdb_atom key;"
689 print " ovs_assert(inited);"
690 print " if (%s) {" % keyVar
691 print " datum.n = 1;"
692 print " datum.keys = &key;"
693 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
695 print " datum.n = 0;"
696 print " datum.keys = NULL;"
698 print " datum.values = NULL;"
699 txn_write_func = "ovsdb_idl_txn_write_clone"
700 elif type.n_max == 1:
701 print " union ovsdb_atom key;"
703 print " ovs_assert(inited);"
704 print " if (%s) {" % nVar
705 print " datum.n = 1;"
706 print " datum.keys = &key;"
707 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
709 print " datum.n = 0;"
710 print " datum.keys = NULL;"
712 print " datum.values = NULL;"
713 txn_write_func = "ovsdb_idl_txn_write_clone"
717 print " ovs_assert(inited);"
718 print " datum.n = %s;" % nVar
719 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
721 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
723 print " datum.values = NULL;"
724 print " for (i = 0; i < %s; i++) {" % nVar
725 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
727 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
730 valueType = type.value.toAtomicType()
732 valueType = "OVSDB_TYPE_VOID"
733 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
734 type.key.toAtomicType(), valueType)
735 txn_write_func = "ovsdb_idl_txn_write"
736 print " %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
737 % {'f': txn_write_func,
739 'S': structName.upper(),
740 'C': columnName.upper()}
744 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
745 structName, structName.upper())
747 static void\n%s_columns_init(void)
749 struct ovsdb_idl_column *c;\
751 for columnName, column in sorted(table.columns.iteritems()):
752 cs = "%s_col_%s" % (structName, columnName)
753 d = {'cs': cs, 'c': columnName, 's': structName}
759 print " /* Initialize %(cs)s. */" % d
760 print " c = &%(cs)s;" % d
761 print " c->name = \"%(c)s\";" % d
762 print column.type.cInitType(" ", "c->type")
763 print " c->mutable = %s;" % mutable
764 print " c->parse = %(s)s_parse_%(c)s;" % d
765 print " c->unparse = %(s)s_unparse_%(c)s;" % d
770 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
771 for tableName, table in sorted(schema.tables.iteritems()):
772 structName = "%s%s" % (prefix, tableName.lower())
777 print " {\"%s\", %s," % (tableName, is_root)
778 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
779 structName, structName)
780 print " sizeof(struct %s), %s_init__}," % (structName, structName)
784 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
785 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
786 schema.name, prefix, prefix)
789 # global init function
797 assert_single_threaded();
800 for tableName, table in sorted(schema.tables.iteritems()):
801 structName = "%s%s" % (prefix, tableName.lower())
802 print " %s_columns_init();" % structName
806 /* Return the schema version. The caller must not free the returned value. */
808 %sget_db_version(void)
812 """ % (prefix, schema.version)
816 def ovsdb_escape(string):
820 raise ovs.db.error.Error("strings may not contain null bytes")
834 return '\\x%02x' % ord(c)
835 return re.sub(r'["\\\000-\037]', escape, string)
839 %(argv0)s: ovsdb schema compiler
840 usage: %(argv0)s [OPTIONS] COMMAND ARG...
842 The following commands are supported:
843 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
844 c-idl-header IDL print C header file for IDL
845 c-idl-source IDL print C source file for IDL implementation
846 nroff IDL print schema documentation in nroff format
848 The following options are also available:
849 -h, --help display this help message
850 -V, --version display version information\
851 """ % {'argv0': argv0}
854 if __name__ == "__main__":
857 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
861 except getopt.GetoptError, geo:
862 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
865 for key, value in options:
866 if key in ['-h', '--help']:
868 elif key in ['-V', '--version']:
869 print "ovsdb-idlc (Open vSwitch) @VERSION@"
870 elif key in ['-C', '--directory']:
875 optKeys = [key for key, value in options]
878 sys.stderr.write("%s: missing command argument "
879 "(use --help for help)\n" % argv0)
882 commands = {"annotate": (annotateSchema, 2),
883 "c-idl-header": (printCIDLHeader, 1),
884 "c-idl-source": (printCIDLSource, 1)}
886 if not args[0] in commands:
887 sys.stderr.write("%s: unknown command \"%s\" "
888 "(use --help for help)\n" % (argv0, args[0]))
891 func, n_args = commands[args[0]]
892 if len(args) - 1 != n_args:
893 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
895 % (argv0, args[0], n_args, len(args) - 1))
899 except ovs.db.error.Error, e:
900 sys.stderr.write("%s: %s\n" % (argv0, e))