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_column_id" % structName.lower(), ["%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 /* Returns true if 'row' was inserted since the last change tracking reset. */
190 static inline bool %(s)s_is_new(const struct %(s)s *row)
192 return %(s)s_row_get_seqno(row, OVSDB_IDL_CHANGE_MODIFY) == 0;
195 /* Returns true if 'row' was deleted since the last change tracking reset. */
196 static inline bool %(s)s_is_deleted(const struct %(s)s *row)
198 return %(s)s_row_get_seqno(row, OVSDB_IDL_CHANGE_DELETE) > 0;
201 void %(s)s_init(struct %(s)s *);
202 void %(s)s_delete(const struct %(s)s *);
203 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
204 bool %(s)s_is_updated(const struct %(s)s *, enum %(s)s_column_id);
205 ''' % {'s': structName, 'S': structName.upper()}
207 for columnName, column in sorted(table.columns.iteritems()):
208 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
211 for columnName, column in sorted(table.columns.iteritems()):
212 if column.type.value:
213 valueParam = ', enum ovsdb_atomic_type value_type'
216 print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
217 's': structName, 'c': columnName, 'v': valueParam}
220 for columnName, column in sorted(table.columns.iteritems()):
221 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
222 if column.type.is_smap():
223 args = ['const struct smap *']
225 comment, members = cMembers(prefix, tableName, columnName,
227 args = ['%(type)s%(name)s' % member for member in members]
228 print '%s);' % ', '.join(args)
231 for columnName, column in sorted(table.columns.iteritems()):
232 if column.type.is_map():
233 print 'void %(s)s_update_%(c)s_setkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName},
234 print '%(coltype)s, %(valtype)s);' % {'coltype':column.type.key.toCType(prefix), 'valtype':column.type.value.toCType(prefix)}
235 print 'void %(s)s_update_%(c)s_delkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName},
236 print '%(coltype)s);' % {'coltype':column.type.key.toCType(prefix)},
238 print 'void %(s)s_add_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function,' % {'s': structName, 'c': columnName},
239 if column.type.is_smap():
240 args = ['const struct smap *']
242 comment, members = cMembers(prefix, tableName, columnName,
244 args = ['%(type)s%(name)s' % member for member in members]
245 print '%s);' % ', '.join(args)
247 print 'void %s_add_clause_true(struct ovsdb_idl *idl);' % structName
248 print 'void %s_add_clause_false(struct ovsdb_idl *idl);' % structName
251 for columnName, column in sorted(table.columns.iteritems()):
252 print 'void %(s)s_remove_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function,' % {'s': structName, 'c': columnName},
253 if column.type.is_smap():
254 args = ['const struct smap *']
256 comment, members = cMembers(prefix, tableName, columnName,
258 args = ['%(type)s%(name)s' % member for member in members]
259 print '%s);' % ', '.join(args)
261 print 'void %s_remove_clause_true(struct ovsdb_idl *idl);' % structName
262 print 'void %s_remove_clause_false(struct ovsdb_idl *idl);' % structName
267 printEnum("%stable_id" % prefix.lower(), ["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
269 for tableName in schema.tables:
270 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
273 't': tableName.lower(),
274 'T': tableName.upper()}
275 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
277 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
278 print "\nvoid %sinit(void);" % prefix
280 print "\nconst char * %sget_db_version(void);" % prefix
281 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
283 def printEnum(type, members):
284 if len(members) == 0:
287 print "\nenum %s {" % type
288 for member in members[:-1]:
289 print " %s," % member
290 print " %s" % members[-1]
293 def printCIDLSource(schemaFile):
294 schema = parseSchema(schemaFile)
295 prefix = schema.idlPrefix
297 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
302 #include "ovs-thread.h"
303 #include "ovsdb-data.h"
304 #include "ovsdb-error.h"
308 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
309 enum { sizeof_bool = 1 };
311 enum { sizeof_bool = sizeof(bool) };
315 ''' % schema.idlHeader
318 for tableName, table in sorted(schema.tables.iteritems()):
319 structName = "%s%s" % (prefix, tableName.lower())
321 static struct %(s)s *
322 %(s)s_cast(const struct ovsdb_idl_row *row)
324 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
326 ''' % {'s': structName}
329 for tableName, table in sorted(schema.tables.iteritems()):
330 structName = "%s%s" % (prefix, tableName.lower())
332 print "/* %s table. */" % (tableName)
335 for columnName, column in sorted(table.columns.iteritems()):
338 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
340 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
344 keyVar = "row->key_%s" % columnName
345 valueVar = "row->value_%s" % columnName
347 keyVar = "row->%s" % columnName
353 print " ovs_assert(inited);"
354 print " smap_init(&row->%s);" % columnName
355 print " for (i = 0; i < datum->n; i++) {"
356 print " smap_add(&row->%s," % columnName
357 print " datum->keys[i].string,"
358 print " datum->values[i].string);"
360 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
362 print " ovs_assert(inited);"
363 print " if (datum->n >= 1) {"
364 if not type.key.ref_table:
365 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
367 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())
370 if type.value.ref_table:
371 print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
373 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())
375 print " %s" % type.key.initCDefault(keyVar, type.n_min == 0)
377 print " %s" % type.value.initCDefault(valueVar, type.n_min == 0)
380 if type.n_max != sys.maxint:
381 print " size_t n = MIN(%d, datum->n);" % type.n_max
387 print " ovs_assert(inited);"
388 print " %s = NULL;" % keyVar
390 print " %s = NULL;" % valueVar
391 print " row->n_%s = 0;" % columnName
392 print " for (i = 0; i < %s; i++) {" % nMax
394 if type.key.ref_table:
395 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())
397 refs.append('keyRow')
399 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
400 if type.value and type.value.ref_table:
401 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())
402 valueSrc = "valueRow"
403 refs.append('valueRow')
405 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
407 print " if (%s) {" % ' && '.join(refs)
411 print "%sif (!row->n_%s) {" % (indent, columnName)
413 # Special case for boolean types. This is only here because
414 # sparse does not like the "normal" case ("warning: expression
415 # using sizeof bool").
416 if type.key.type == ovs.db.types.BooleanType:
417 sizeof = "sizeof_bool"
419 sizeof = "sizeof *%s" % keyVar
420 print "%s %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
423 # Special case for boolean types (see above).
424 if type.value.type == ovs.db.types.BooleanType:
425 sizeof = " * sizeof_bool"
427 sizeof = "sizeof *%s" % valueVar
428 print "%s %s = xmalloc(%s * %s);" % (indent, valueVar,
431 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
433 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
434 print "%srow->n_%s++;" % (indent, columnName)
441 for columnName, column in sorted(table.columns.iteritems()):
443 if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
446 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
448 struct %(s)s *row = %(s)s_cast(row_);
450 ovs_assert(inited);''' % {'s': structName, 'c': columnName}
453 print " smap_destroy(&row->%s);" % columnName
456 keyVar = "row->key_%s" % columnName
457 valueVar = "row->value_%s" % columnName
459 keyVar = "row->%s" % columnName
461 print " free(%s);" % keyVar
463 print " free(%s);" % valueVar
468 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
471 }''' % {'s': structName, 'c': columnName}
473 # Generic Row Initialization function.
476 %(s)s_init__(struct ovsdb_idl_row *row)
478 %(s)s_init(%(s)s_cast(row));
479 }""" % {'s': structName}
481 # Row Initialization function.
483 /* Clears the contents of 'row' in table "%(t)s". */
485 %(s)s_init(struct %(s)s *row)
487 memset(row, 0, sizeof *row); """ % {'s': structName, 't': tableName}
488 for columnName, column in sorted(table.columns.iteritems()):
489 if column.type.is_smap():
490 print " smap_init(&row->%s);" % columnName
493 # First, next functions.
495 /* Searches table "%(t)s" in 'idl' for a row with UUID 'uuid'. Returns
496 * a pointer to the row if there is one, otherwise a null pointer. */
498 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
500 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
503 /* Returns a row in table "%(t)s" in 'idl', or a null pointer if that
506 * Database tables are internally maintained as hash tables, so adding or
507 * removing rows while traversing the same table can cause some rows to be
508 * visited twice or not at apply. */
510 %(s)s_first(const struct ovsdb_idl *idl)
512 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
515 /* Returns a row following 'row' within its table, or a null pointer if 'row'
516 * is the last row in its table. */
518 %(s)s_next(const struct %(s)s *row)
520 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
523 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *idl)
525 return ovsdb_idl_table_get_seqno(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]);
528 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change)
530 return ovsdb_idl_row_get_seqno(&row->header_, change);
534 %(s)s_track_get_first(const struct ovsdb_idl *idl)
536 return %(s)s_cast(ovsdb_idl_track_get_first(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
540 *%(s)s_track_get_next(const struct %(s)s *row)
542 return %(s)s_cast(ovsdb_idl_track_get_next(&row->header_));
543 }''' % {'s': structName,
547 'T': tableName.upper()}
551 /* Deletes 'row' from table "%(t)s". 'row' may be freed, so it must not be
552 * accessed afterward.
554 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
556 %(s)s_delete(const struct %(s)s *row)
558 ovsdb_idl_txn_delete(&row->header_);
561 /* Inserts and returns a new row in the table "%(t)s" in the database
562 * with open transaction 'txn'.
564 * The new row is assigned a randomly generated provisional UUID.
565 * ovsdb-server will assign a different UUID when 'txn' is committed,
566 * but the IDL will replace any uses of the provisional UUID in the
567 * data to be to be committed by the UUID assigned by ovsdb-server. */
569 %(s)s_insert(struct ovsdb_idl_txn *txn)
571 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
575 %(s)s_is_updated(const struct %(s)s *row, enum %(s)s_column_id column)
577 return ovsdb_idl_track_is_updated(&row->header_, &%(s)s_columns[column]);
578 }''' % {'s': structName,
582 'T': tableName.upper()}
585 for columnName, column in sorted(table.columns.iteritems()):
587 /* Causes the original contents of column "%(c)s" in 'row' to be
588 * verified as a prerequisite to completing the transaction. That is, if
589 * "%(c)s" in 'row' changed (or if 'row' was deleted) between the
590 * time that the IDL originally read its contents and the time that the
591 * transaction commits, then the transaction aborts and ovsdb_idl_txn_commit()
592 * returns TXN_AGAIN_WAIT or TXN_AGAIN_NOW (depending on whether the database
593 * change has already been received).
595 * The intention is that, to ensure that no transaction commits based on dirty
596 * reads, an application should call this function any time "%(c)s" is
597 * read as part of a read-modify-write operation.
599 * In some cases this function reduces to a no-op, because the current value
600 * of "%(c)s" is already known:
602 * - If 'row' is a row created by the current transaction (returned by
605 * - If "%(c)s" has already been modified (with
606 * %(s)s_set_%(c)s()) within the current transaction.
608 * Because of the latter property, always call this function *before*
609 * %(s)s_set_%(c)s() for a given read-modify-write.
611 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
613 %(s)s_verify_%(c)s(const struct %(s)s *row)
616 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
617 }''' % {'s': structName,
618 'S': structName.upper(),
620 'C': columnName.upper()}
623 for columnName, column in sorted(table.columns.iteritems()):
624 if column.type.value:
625 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
626 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
627 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
633 /* Returns the "%(c)s" column's value from the "%(t)s" table in 'row'
634 * as a struct ovsdb_datum. This is useful occasionally: for example,
635 * ovsdb_datum_find_key() is an easier and more efficient way to search
636 * for a given key than implementing the same operation on the "cooked"
639 * 'key_type' must be %(kt)s.%(vc)s
640 * (This helps to avoid silent bugs if someone changes %(c)s's
641 * type without updating the caller.)
643 * The caller must not modify or free the returned value.
645 * Various kinds of changes can invalidate the returned value: modifying
646 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
647 * If the returned value is needed for a long time, it is best to make a copy
648 * of it with ovsdb_datum_clone().
650 * This function is rarely useful, since it is easier to access the value
651 * directly through the "%(c)s" member in %(s)s. */
652 const struct ovsdb_datum *
653 %(s)s_get_%(c)s(const struct %(s)s *row,
654 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
656 ovs_assert(key_type == %(kt)s);%(vt)s
657 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
658 }""" % {'t': tableName, 's': structName, 'c': columnName,
659 'kt': column.type.key.toAtomicType(),
660 'v': valueParam, 'vt': valueType, 'vc': valueComment}
663 for columnName, column in sorted(table.columns.iteritems()):
666 comment, members = cMembers(prefix, tableName, columnName,
672 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
674 struct ovsdb_datum datum;
678 struct smap_node *node;
681 datum.n = smap_count(%(c)s);
682 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
683 datum.values = xmalloc(datum.n * sizeof *datum.values);
686 SMAP_FOR_EACH (node, %(c)s) {
687 datum.keys[i].string = xstrdup(node->key);
688 datum.values[i].string = xstrdup(node->value);
691 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
693 ovsdb_datum_init_empty(&datum);
695 ovsdb_idl_txn_write(&row->header_,
696 &%(s)s_columns[%(S)s_COL_%(C)s],
699 """ % {'t': tableName,
701 'S': structName.upper(),
703 'C': columnName.upper()}
706 keyVar = members[0]['name']
710 valueVar = members[1]['name']
712 nVar = members[2]['name']
715 nVar = members[1]['name']
719 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
720 {'s': structName, 'c': columnName,
721 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
723 print " struct ovsdb_datum datum;"
724 if type.n_min == 1 and type.n_max == 1:
725 print " union ovsdb_atom key;"
727 print " union ovsdb_atom value;"
729 print " ovs_assert(inited);"
730 print " datum.n = 1;"
731 print " datum.keys = &key;"
732 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
734 print " datum.values = &value;"
735 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
737 print " datum.values = NULL;"
738 txn_write_func = "ovsdb_idl_txn_write_clone"
739 elif type.is_optional_pointer():
740 print " union ovsdb_atom key;"
742 print " ovs_assert(inited);"
743 print " if (%s) {" % keyVar
744 print " datum.n = 1;"
745 print " datum.keys = &key;"
746 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
748 print " datum.n = 0;"
749 print " datum.keys = NULL;"
751 print " datum.values = NULL;"
752 txn_write_func = "ovsdb_idl_txn_write_clone"
753 elif type.n_max == 1:
754 print " union ovsdb_atom key;"
756 print " ovs_assert(inited);"
757 print " if (%s) {" % nVar
758 print " datum.n = 1;"
759 print " datum.keys = &key;"
760 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
762 print " datum.n = 0;"
763 print " datum.keys = NULL;"
765 print " datum.values = NULL;"
766 txn_write_func = "ovsdb_idl_txn_write_clone"
770 print " ovs_assert(inited);"
771 print " datum.n = %s;" % nVar
772 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
774 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
776 print " datum.values = NULL;"
777 print " for (i = 0; i < %s; i++) {" % nVar
778 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
780 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
783 valueType = type.value.toAtomicType()
785 valueType = "OVSDB_TYPE_VOID"
786 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
787 type.key.toAtomicType(), valueType)
788 txn_write_func = "ovsdb_idl_txn_write"
789 print " %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
790 % {'f': txn_write_func,
792 'S': structName.upper(),
793 'C': columnName.upper()}
795 # Update/Delete of partial map column functions
796 for columnName, column in sorted(table.columns.iteritems()):
800 /* Sets an element of the "%(c)s" map column from the "%(t)s" table in 'row'
801 * to 'new_value' given the key value 'new_key'.
805 %(s)s_update_%(c)s_setkey(const struct %(s)s *row, %(coltype)snew_key, %(valtype)snew_value)
807 struct ovsdb_datum *datum;
811 datum = xmalloc(sizeof *datum);
813 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
814 datum->values = xmalloc(datum->n * sizeof *datum->values);
815 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
816 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
817 'C': columnName.upper(), 't': tableName}
819 print " "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "new_key")
820 print " "+ type.value.copyCValue("datum->values[0].%s" % type.value.type.to_string(), "new_value")
822 ovsdb_idl_txn_write_partial_map(&row->header_,
823 &%(s)s_columns[%(S)s_COL_%(C)s],
825 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
826 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
827 'C': columnName.upper()}
829 /* Deletes an element of the "%(c)s" map column from the "%(t)s" table in 'row'
830 * given the key value 'delete_key'.
834 %(s)s_update_%(c)s_delkey(const struct %(s)s *row, %(coltype)sdelete_key)
836 struct ovsdb_datum *datum;
840 datum = xmalloc(sizeof *datum);
842 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
843 datum->values = NULL;
844 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
845 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
846 'C': columnName.upper(), 't': tableName}
848 print " "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "delete_key")
850 ovsdb_idl_txn_delete_partial_map(&row->header_,
851 &%(s)s_columns[%(S)s_COL_%(C)s],
853 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
854 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
855 'C': columnName.upper()}
856 # End Update/Delete of partial maps
858 # Add clause functions.
859 for columnName, column in sorted(table.columns.iteritems()):
862 comment, members = cMembers(prefix, tableName, columnName,
868 %(s)s_add_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function, const struct smap *%(c)s)
870 struct ovsdb_datum datum;
874 struct smap_node *node;
877 datum.n = smap_count(%(c)s);
878 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
879 datum.values = xmalloc(datum.n * sizeof *datum.values);
882 SMAP_FOR_EACH (node, %(c)s) {
883 datum.keys[i].string = xstrdup(node->key);
884 datum.values[i].string = xstrdup(node->value);
887 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
889 ovsdb_datum_init_empty(&datum);
892 ovsdb_idl_condition_add_clause(idl,
893 &%(p)stable_classes[%(P)sTABLE_%(T)s],
895 &%(s)s_columns[%(S)s_COL_%(C)s],
898 """ % {'t': tableName,
899 'T': tableName.upper(),
903 'S': structName.upper(),
905 'C': columnName.upper()}
908 keyVar = members[0]['name']
912 valueVar = members[1]['name']
914 nVar = members[2]['name']
917 nVar = members[1]['name']
921 print '%(s)s_add_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function, %(args)s)' % \
922 {'s': structName, 'c': columnName,
923 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
925 print " struct ovsdb_datum datum;"
926 if type.n_min == 1 and type.n_max == 1:
927 print " union ovsdb_atom key;"
929 print " union ovsdb_atom value;"
931 print " ovs_assert(inited);"
932 print " datum.n = 1;"
933 print " datum.keys = &key;"
934 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
936 print " datum.values = &value;"
937 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
939 print " datum.values = NULL;"
940 elif type.is_optional_pointer():
941 print " union ovsdb_atom key;"
943 print " ovs_assert(inited);"
944 print " if (%s) {" % keyVar
945 print " datum.n = 1;"
946 print " datum.keys = &key;"
947 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
949 print " datum.n = 0;"
950 print " datum.keys = NULL;"
952 print " datum.values = NULL;"
953 elif type.n_max == 1:
954 print " union ovsdb_atom key;"
956 print " ovs_assert(inited);"
957 print " if (%s) {" % nVar
958 print " datum.n = 1;"
959 print " datum.keys = &key;"
960 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
962 print " datum.n = 0;"
963 print " datum.keys = NULL;"
965 print " datum.values = NULL;"
969 print " ovs_assert(inited);"
970 print " datum.n = %s;" % nVar
971 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
973 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
975 print " datum.values = NULL;"
976 print " for (i = 0; i < %s; i++) {" % nVar
977 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
979 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
982 valueType = type.value.toAtomicType()
984 valueType = "OVSDB_TYPE_VOID"
985 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
986 type.key.toAtomicType(), valueType)
988 print""" ovsdb_idl_condition_add_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s],
990 &%(s)s_columns[%(S)s_COL_%(C)s],
992 }""" % {'t': tableName,
993 'T': tableName.upper(),
997 'S': structName.upper(),
999 'C': columnName.upper()}
1002 %(s)s_add_clause_false(struct ovsdb_idl *idl)
1004 struct ovsdb_datum datum;
1006 ovsdb_datum_init_empty(&datum);
1007 ovsdb_idl_condition_add_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], OVSDB_F_FALSE, NULL, &datum);
1008 }""" % {'s': structName,
1009 'T': tableName.upper(),
1011 'P': prefix.upper()}
1014 %(s)s_add_clause_true(struct ovsdb_idl *idl)
1016 struct ovsdb_datum datum;
1018 ovsdb_datum_init_empty(&datum);
1019 ovsdb_idl_condition_add_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], OVSDB_F_TRUE, NULL, &datum);
1020 }""" % {'s': structName,
1021 'T': tableName.upper(),
1023 'P': prefix.upper()}
1025 # Remove clause functions.
1026 for columnName, column in sorted(table.columns.iteritems()):
1029 comment, members = cMembers(prefix, tableName, columnName,
1035 %(s)s_remove_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function, const struct smap *%(c)s)
1037 struct ovsdb_datum datum;
1041 struct smap_node *node;
1044 datum.n = smap_count(%(c)s);
1045 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
1046 datum.values = xmalloc(datum.n * sizeof *datum.values);
1049 SMAP_FOR_EACH (node, %(c)s) {
1050 datum.keys[i].string = xstrdup(node->key);
1051 datum.values[i].string = xstrdup(node->value);
1054 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1056 ovsdb_datum_init_empty(&datum);
1059 ovsdb_idl_condition_remove_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s],
1061 &%(s)s_columns[%(S)s_COL_%(C)s],
1064 """ % {'t': tableName,
1065 'T': tableName.upper(),
1067 'P': prefix.upper(),
1069 'S': structName.upper(),
1071 'C': columnName.upper()}
1074 keyVar = members[0]['name']
1078 valueVar = members[1]['name']
1079 if len(members) > 2:
1080 nVar = members[2]['name']
1082 if len(members) > 1:
1083 nVar = members[1]['name']
1087 print '%(s)s_remove_clause_%(c)s(struct ovsdb_idl *idl, enum ovsdb_function function, %(args)s)' % \
1088 {'s': structName, 'c': columnName,
1089 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
1091 print " struct ovsdb_datum datum;"
1092 if type.n_min == 1 and type.n_max == 1:
1093 print " union ovsdb_atom key;"
1095 print " union ovsdb_atom value;"
1097 print " ovs_assert(inited);"
1098 print " datum.n = 1;"
1099 print " datum.keys = &key;"
1100 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
1102 print " datum.values = &value;"
1103 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
1105 print " datum.values = NULL;"
1106 elif type.is_optional_pointer():
1107 print " union ovsdb_atom key;"
1109 print " ovs_assert(inited);"
1110 print " if (%s) {" % keyVar
1111 print " datum.n = 1;"
1112 print " datum.keys = &key;"
1113 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
1115 print " datum.n = 0;"
1116 print " datum.keys = NULL;"
1118 print " datum.values = NULL;"
1119 elif type.n_max == 1:
1120 print " union ovsdb_atom key;"
1122 print " ovs_assert(inited);"
1123 print " if (%s) {" % nVar
1124 print " datum.n = 1;"
1125 print " datum.keys = &key;"
1126 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
1128 print " datum.n = 0;"
1129 print " datum.keys = NULL;"
1131 print " datum.values = NULL;"
1135 print " ovs_assert(inited);"
1136 print " datum.n = %s;" % nVar
1137 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
1139 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
1141 print " datum.values = NULL;"
1142 print " for (i = 0; i < %s; i++) {" % nVar
1143 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
1145 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
1148 valueType = type.value.toAtomicType()
1150 valueType = "OVSDB_TYPE_VOID"
1151 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
1152 type.key.toAtomicType(), valueType)
1154 print""" ovsdb_idl_condition_remove_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s],
1156 &%(s)s_columns[%(S)s_COL_%(C)s],
1158 }""" % {'t': tableName,
1159 'T': tableName.upper(),
1161 'P': prefix.upper(),
1163 'S': structName.upper(),
1165 'C': columnName.upper()}
1168 %(s)s_remove_clause_false(struct ovsdb_idl *idl)
1170 struct ovsdb_datum datum;
1172 ovsdb_datum_init_empty(&datum);
1173 ovsdb_idl_condition_remove_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], OVSDB_F_FALSE, NULL, &datum);
1174 }""" % {'s': structName,
1175 'T': tableName.upper(),
1177 'P': prefix.upper()}
1180 %(s)s_remove_clause_true(struct ovsdb_idl *idl)
1182 struct ovsdb_datum datum;
1184 ovsdb_datum_init_empty(&datum);
1185 ovsdb_idl_condition_remove_clause(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], OVSDB_F_TRUE, NULL, &datum);
1186 }""" % {'s': structName,
1187 'T': tableName.upper(),
1189 'P': prefix.upper(),}
1192 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
1193 structName, structName.upper())
1195 static void\n%s_columns_init(void)
1197 struct ovsdb_idl_column *c;\
1199 for columnName, column in sorted(table.columns.iteritems()):
1200 cs = "%s_col_%s" % (structName, columnName)
1201 d = {'cs': cs, 'c': columnName, 's': structName}
1207 print " /* Initialize %(cs)s. */" % d
1208 print " c = &%(cs)s;" % d
1209 print " c->name = \"%(c)s\";" % d
1210 print column.type.cInitType(" ", "c->type")
1211 print " c->mutable = %s;" % mutable
1212 print " c->parse = %(s)s_parse_%(c)s;" % d
1213 print " c->unparse = %(s)s_unparse_%(c)s;" % d
1218 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
1219 for tableName, table in sorted(schema.tables.iteritems()):
1220 structName = "%s%s" % (prefix, tableName.lower())
1225 print " {\"%s\", %s," % (tableName, is_root)
1226 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
1227 structName, structName)
1228 print " sizeof(struct %s), %s_init__}," % (structName, structName)
1232 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
1233 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
1234 schema.name, prefix, prefix)
1237 # global init function
1245 assert_single_threaded();
1248 for tableName, table in sorted(schema.tables.iteritems()):
1249 structName = "%s%s" % (prefix, tableName.lower())
1250 print " %s_columns_init();" % structName
1254 /* Return the schema version. The caller must not free the returned value. */
1256 %sget_db_version(void)
1260 """ % (prefix, schema.version)
1264 def ovsdb_escape(string):
1268 raise ovs.db.error.Error("strings may not contain null bytes")
1282 return '\\x%02x' % ord(c)
1283 return re.sub(r'["\\\000-\037]', escape, string)
1287 %(argv0)s: ovsdb schema compiler
1288 usage: %(argv0)s [OPTIONS] COMMAND ARG...
1290 The following commands are supported:
1291 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
1292 c-idl-header IDL print C header file for IDL
1293 c-idl-source IDL print C source file for IDL implementation
1294 nroff IDL print schema documentation in nroff format
1296 The following options are also available:
1297 -h, --help display this help message
1298 -V, --version display version information\
1299 """ % {'argv0': argv0}
1302 if __name__ == "__main__":
1305 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
1309 except getopt.GetoptError, geo:
1310 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
1313 for key, value in options:
1314 if key in ['-h', '--help']:
1316 elif key in ['-V', '--version']:
1317 print "ovsdb-idlc (Open vSwitch) @VERSION@"
1318 elif key in ['-C', '--directory']:
1323 optKeys = [key for key, value in options]
1326 sys.stderr.write("%s: missing command argument "
1327 "(use --help for help)\n" % argv0)
1330 commands = {"annotate": (annotateSchema, 2),
1331 "c-idl-header": (printCIDLHeader, 1),
1332 "c-idl-source": (printCIDLSource, 1)}
1334 if not args[0] in commands:
1335 sys.stderr.write("%s: unknown command \"%s\" "
1336 "(use --help for help)\n" % (argv0, args[0]))
1339 func, n_args = commands[args[0]]
1340 if len(args) - 1 != n_args:
1341 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
1343 % (argv0, args[0], n_args, len(args) - 1))
1347 except ovs.db.error.Error, e:
1348 sys.stderr.write("%s: %s\n" % (argv0, e))