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 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 bool %(s)s_is_updated(const struct %(s)s *, enum %(s)s_column_id);
193 ''' % {'s': structName, 'S': structName.upper()}
195 for columnName, column in sorted(table.columns.iteritems()):
196 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
199 for columnName, column in sorted(table.columns.iteritems()):
200 if column.type.value:
201 valueParam = ', enum ovsdb_atomic_type value_type'
204 print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
205 's': structName, 'c': columnName, 'v': valueParam}
208 for columnName, column in sorted(table.columns.iteritems()):
209 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
210 if column.type.is_smap():
211 args = ['const struct smap *']
213 comment, members = cMembers(prefix, tableName, columnName,
215 args = ['%(type)s%(name)s' % member for member in members]
216 print '%s);' % ', '.join(args)
219 for columnName, column in sorted(table.columns.iteritems()):
220 if column.type.is_map():
221 print 'void %(s)s_update_%(c)s_setkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName},
222 print '%(coltype)s, %(valtype)s);' % {'coltype':column.type.key.toCType(prefix), 'valtype':column.type.value.toCType(prefix)}
223 print 'void %(s)s_update_%(c)s_delkey(const struct %(s)s *, ' % {'s': structName, 'c': columnName},
224 print '%(coltype)s);' % {'coltype':column.type.key.toCType(prefix)}
228 printEnum("%stable_id" % prefix.lower(), ["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
230 for tableName in schema.tables:
231 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
234 't': tableName.lower(),
235 'T': tableName.upper()}
236 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
238 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
239 print "\nvoid %sinit(void);" % prefix
241 print "\nconst char * %sget_db_version(void);" % prefix
242 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
244 def printEnum(type, members):
245 if len(members) == 0:
248 print "\nenum %s {" % type
249 for member in members[:-1]:
250 print " %s," % member
251 print " %s" % members[-1]
254 def printCIDLSource(schemaFile):
255 schema = parseSchema(schemaFile)
256 prefix = schema.idlPrefix
258 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
263 #include "ovs-thread.h"
264 #include "ovsdb-data.h"
265 #include "ovsdb-error.h"
269 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
270 enum { sizeof_bool = 1 };
272 enum { sizeof_bool = sizeof(bool) };
276 ''' % schema.idlHeader
279 for tableName, table in sorted(schema.tables.iteritems()):
280 structName = "%s%s" % (prefix, tableName.lower())
282 static struct %(s)s *
283 %(s)s_cast(const struct ovsdb_idl_row *row)
285 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
287 ''' % {'s': structName}
290 for tableName, table in sorted(schema.tables.iteritems()):
291 structName = "%s%s" % (prefix, tableName.lower())
293 print "/* %s table. */" % (tableName)
296 for columnName, column in sorted(table.columns.iteritems()):
299 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
301 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
305 keyVar = "row->key_%s" % columnName
306 valueVar = "row->value_%s" % columnName
308 keyVar = "row->%s" % columnName
314 print " ovs_assert(inited);"
315 print " smap_init(&row->%s);" % columnName
316 print " for (i = 0; i < datum->n; i++) {"
317 print " smap_add(&row->%s," % columnName
318 print " datum->keys[i].string,"
319 print " datum->values[i].string);"
321 elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
323 print " ovs_assert(inited);"
324 print " if (datum->n >= 1) {"
325 if not type.key.ref_table:
326 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
328 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())
331 if type.value.ref_table:
332 print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
334 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())
336 print " %s" % type.key.initCDefault(keyVar, type.n_min == 0)
338 print " %s" % type.value.initCDefault(valueVar, type.n_min == 0)
341 if type.n_max != sys.maxint:
342 print " size_t n = MIN(%d, datum->n);" % type.n_max
348 print " ovs_assert(inited);"
349 print " %s = NULL;" % keyVar
351 print " %s = NULL;" % valueVar
352 print " row->n_%s = 0;" % columnName
353 print " for (i = 0; i < %s; i++) {" % nMax
355 if type.key.ref_table:
356 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())
358 refs.append('keyRow')
360 keySrc = "datum->keys[i].%s" % type.key.type.to_string()
361 if type.value and type.value.ref_table:
362 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())
363 valueSrc = "valueRow"
364 refs.append('valueRow')
366 valueSrc = "datum->values[i].%s" % type.value.type.to_string()
368 print " if (%s) {" % ' && '.join(refs)
372 print "%sif (!row->n_%s) {" % (indent, columnName)
374 # Special case for boolean types. This is only here because
375 # sparse does not like the "normal" case ("warning: expression
376 # using sizeof bool").
377 if type.key.type == ovs.db.types.BooleanType:
378 sizeof = "sizeof_bool"
380 sizeof = "sizeof *%s" % keyVar
381 print "%s %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
384 # Special case for boolean types (see above).
385 if type.value.type == ovs.db.types.BooleanType:
386 sizeof = " * sizeof_bool"
388 sizeof = "sizeof *%s" % valueVar
389 print "%s %s = xmalloc(%s * %s);" % (indent, valueVar,
392 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
394 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
395 print "%srow->n_%s++;" % (indent, columnName)
402 for columnName, column in sorted(table.columns.iteritems()):
404 if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
407 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
409 struct %(s)s *row = %(s)s_cast(row_);
411 ovs_assert(inited);''' % {'s': structName, 'c': columnName}
414 print " smap_destroy(&row->%s);" % columnName
417 keyVar = "row->key_%s" % columnName
418 valueVar = "row->value_%s" % columnName
420 keyVar = "row->%s" % columnName
422 print " free(%s);" % keyVar
424 print " free(%s);" % valueVar
429 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
432 }''' % {'s': structName, 'c': columnName}
434 # Generic Row Initialization function.
437 %(s)s_init__(struct ovsdb_idl_row *row)
439 %(s)s_init(%(s)s_cast(row));
440 }""" % {'s': structName}
442 # Row Initialization function.
444 /* Clears the contents of 'row' in table "%(t)s". */
446 %(s)s_init(struct %(s)s *row)
448 memset(row, 0, sizeof *row); """ % {'s': structName, 't': tableName}
449 for columnName, column in sorted(table.columns.iteritems()):
450 if column.type.is_smap():
451 print " smap_init(&row->%s);" % columnName
454 # First, next functions.
456 /* Searches table "%(t)s" in 'idl' for a row with UUID 'uuid'. Returns
457 * a pointer to the row if there is one, otherwise a null pointer. */
459 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
461 return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
464 /* Returns a row in table "%(t)s" in 'idl', or a null pointer if that
467 * Database tables are internally maintained as hash tables, so adding or
468 * removing rows while traversing the same table can cause some rows to be
469 * visited twice or not at apply. */
471 %(s)s_first(const struct ovsdb_idl *idl)
473 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
476 /* Returns a row following 'row' within its table, or a null pointer if 'row'
477 * is the last row in its table. */
479 %(s)s_next(const struct %(s)s *row)
481 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
484 unsigned int %(s)s_get_seqno(const struct ovsdb_idl *idl)
486 return ovsdb_idl_table_get_seqno(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]);
489 unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change)
491 return ovsdb_idl_row_get_seqno(&row->header_, change);
495 %(s)s_track_get_first(const struct ovsdb_idl *idl)
497 return %(s)s_cast(ovsdb_idl_track_get_first(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
501 *%(s)s_track_get_next(const struct %(s)s *row)
503 return %(s)s_cast(ovsdb_idl_track_get_next(&row->header_));
504 }''' % {'s': structName,
508 'T': tableName.upper()}
512 /* Deletes 'row' from table "%(t)s". 'row' may be freed, so it must not be
513 * accessed afterward.
515 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
517 %(s)s_delete(const struct %(s)s *row)
519 ovsdb_idl_txn_delete(&row->header_);
522 /* Inserts and returns a new row in the table "%(t)s" in the database
523 * with open transaction 'txn'.
525 * The new row is assigned a randomly generated provisional UUID.
526 * ovsdb-server will assign a different UUID when 'txn' is committed,
527 * but the IDL will replace any uses of the provisional UUID in the
528 * data to be to be committed by the UUID assigned by ovsdb-server. */
530 %(s)s_insert(struct ovsdb_idl_txn *txn)
532 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
536 %(s)s_is_updated(const struct %(s)s *row, enum %(s)s_column_id column)
538 return ovsdb_idl_track_is_updated(&row->header_, &%(s)s_columns[column]);
539 }''' % {'s': structName,
543 'T': tableName.upper()}
546 for columnName, column in sorted(table.columns.iteritems()):
548 /* Causes the original contents of column "%(c)s" in 'row' to be
549 * verified as a prerequisite to completing the transaction. That is, if
550 * "%(c)s" in 'row' changed (or if 'row' was deleted) between the
551 * time that the IDL originally read its contents and the time that the
552 * transaction commits, then the transaction aborts and ovsdb_idl_txn_commit()
553 * returns TXN_AGAIN_WAIT or TXN_AGAIN_NOW (depending on whether the database
554 * change has already been received).
556 * The intention is that, to ensure that no transaction commits based on dirty
557 * reads, an application should call this function any time "%(c)s" is
558 * read as part of a read-modify-write operation.
560 * In some cases this function reduces to a no-op, because the current value
561 * of "%(c)s" is already known:
563 * - If 'row' is a row created by the current transaction (returned by
566 * - If "%(c)s" has already been modified (with
567 * %(s)s_set_%(c)s()) within the current transaction.
569 * Because of the latter property, always call this function *before*
570 * %(s)s_set_%(c)s() for a given read-modify-write.
572 * The caller must have started a transaction with ovsdb_idl_txn_create(). */
574 %(s)s_verify_%(c)s(const struct %(s)s *row)
577 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
578 }''' % {'s': structName,
579 'S': structName.upper(),
581 'C': columnName.upper()}
584 for columnName, column in sorted(table.columns.iteritems()):
585 if column.type.value:
586 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
587 valueType = '\n ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
588 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
594 /* Returns the "%(c)s" column's value from the "%(t)s" table in 'row'
595 * as a struct ovsdb_datum. This is useful occasionally: for example,
596 * ovsdb_datum_find_key() is an easier and more efficient way to search
597 * for a given key than implementing the same operation on the "cooked"
600 * 'key_type' must be %(kt)s.%(vc)s
601 * (This helps to avoid silent bugs if someone changes %(c)s's
602 * type without updating the caller.)
604 * The caller must not modify or free the returned value.
606 * Various kinds of changes can invalidate the returned value: modifying
607 * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
608 * If the returned value is needed for a long time, it is best to make a copy
609 * of it with ovsdb_datum_clone().
611 * This function is rarely useful, since it is easier to access the value
612 * directly through the "%(c)s" member in %(s)s. */
613 const struct ovsdb_datum *
614 %(s)s_get_%(c)s(const struct %(s)s *row,
615 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
617 ovs_assert(key_type == %(kt)s);%(vt)s
618 return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
619 }""" % {'t': tableName, 's': structName, 'c': columnName,
620 'kt': column.type.key.toAtomicType(),
621 'v': valueParam, 'vt': valueType, 'vc': valueComment}
624 for columnName, column in sorted(table.columns.iteritems()):
627 comment, members = cMembers(prefix, tableName, columnName,
633 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
635 struct ovsdb_datum datum;
639 struct smap_node *node;
642 datum.n = smap_count(%(c)s);
643 datum.keys = xmalloc(datum.n * sizeof *datum.keys);
644 datum.values = xmalloc(datum.n * sizeof *datum.values);
647 SMAP_FOR_EACH (node, %(c)s) {
648 datum.keys[i].string = xstrdup(node->key);
649 datum.values[i].string = xstrdup(node->value);
652 ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
654 ovsdb_datum_init_empty(&datum);
656 ovsdb_idl_txn_write(&row->header_,
657 &%(s)s_columns[%(S)s_COL_%(C)s],
660 """ % {'t': tableName,
662 'S': structName.upper(),
664 'C': columnName.upper()}
667 keyVar = members[0]['name']
671 valueVar = members[1]['name']
673 nVar = members[2]['name']
676 nVar = members[1]['name']
680 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
681 {'s': structName, 'c': columnName,
682 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
684 print " struct ovsdb_datum datum;"
685 if type.n_min == 1 and type.n_max == 1:
686 print " union ovsdb_atom key;"
688 print " union ovsdb_atom value;"
690 print " ovs_assert(inited);"
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.values = &value;"
696 print " "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
698 print " datum.values = NULL;"
699 txn_write_func = "ovsdb_idl_txn_write_clone"
700 elif type.is_optional_pointer():
701 print " union ovsdb_atom key;"
703 print " ovs_assert(inited);"
704 print " if (%s) {" % keyVar
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"
714 elif type.n_max == 1:
715 print " union ovsdb_atom key;"
717 print " ovs_assert(inited);"
718 print " if (%s) {" % nVar
719 print " datum.n = 1;"
720 print " datum.keys = &key;"
721 print " " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
723 print " datum.n = 0;"
724 print " datum.keys = NULL;"
726 print " datum.values = NULL;"
727 txn_write_func = "ovsdb_idl_txn_write_clone"
731 print " ovs_assert(inited);"
732 print " datum.n = %s;" % nVar
733 print " datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
735 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
737 print " datum.values = NULL;"
738 print " for (i = 0; i < %s; i++) {" % nVar
739 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
741 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
744 valueType = type.value.toAtomicType()
746 valueType = "OVSDB_TYPE_VOID"
747 print " ovsdb_datum_sort_unique(&datum, %s, %s);" % (
748 type.key.toAtomicType(), valueType)
749 txn_write_func = "ovsdb_idl_txn_write"
750 print " %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
751 % {'f': txn_write_func,
753 'S': structName.upper(),
754 'C': columnName.upper()}
756 # Update/Delete of partial map column functions
757 for columnName, column in sorted(table.columns.iteritems()):
761 /* Sets an element of the "%(c)s" map column from the "%(t)s" table in 'row'
762 * to 'new_value' given the key value 'new_key'.
766 %(s)s_update_%(c)s_setkey(const struct %(s)s *row, %(coltype)snew_key, %(valtype)snew_value)
768 struct ovsdb_datum *datum;
772 datum = xmalloc(sizeof *datum);
774 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
775 datum->values = xmalloc(datum->n * sizeof *datum->values);
776 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
777 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
778 'C': columnName.upper(), 't': tableName}
780 print " "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "new_key")
781 print " "+ type.value.copyCValue("datum->values[0].%s" % type.value.type.to_string(), "new_value")
783 ovsdb_idl_txn_write_partial_map(&row->header_,
784 &%(s)s_columns[%(S)s_COL_%(C)s],
786 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
787 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
788 'C': columnName.upper()}
790 /* Deletes an element of the "%(c)s" map column from the "%(t)s" table in 'row'
791 * given the key value 'delete_key'.
795 %(s)s_update_%(c)s_delkey(const struct %(s)s *row, %(coltype)sdelete_key)
797 struct ovsdb_datum *datum;
801 datum = xmalloc(sizeof *datum);
803 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
804 datum->values = NULL;
805 ''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
806 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
807 'C': columnName.upper(), 't': tableName}
809 print " "+ type.key.copyCValue("datum->keys[0].%s" % type.key.type.to_string(), "delete_key")
811 ovsdb_idl_txn_delete_partial_map(&row->header_,
812 &%(s)s_columns[%(S)s_COL_%(C)s],
814 }''' % {'s': structName, 'c': columnName,'coltype':column.type.key.toCType(prefix),
815 'valtype':column.type.value.toCType(prefix), 'S': structName.upper(),
816 'C': columnName.upper()}
817 # End Update/Delete of partial maps
820 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
821 structName, structName.upper())
823 static void\n%s_columns_init(void)
825 struct ovsdb_idl_column *c;\
827 for columnName, column in sorted(table.columns.iteritems()):
828 cs = "%s_col_%s" % (structName, columnName)
829 d = {'cs': cs, 'c': columnName, 's': structName}
835 print " /* Initialize %(cs)s. */" % d
836 print " c = &%(cs)s;" % d
837 print " c->name = \"%(c)s\";" % d
838 print column.type.cInitType(" ", "c->type")
839 print " c->mutable = %s;" % mutable
840 print " c->parse = %(s)s_parse_%(c)s;" % d
841 print " c->unparse = %(s)s_unparse_%(c)s;" % d
846 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
847 for tableName, table in sorted(schema.tables.iteritems()):
848 structName = "%s%s" % (prefix, tableName.lower())
853 print " {\"%s\", %s," % (tableName, is_root)
854 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
855 structName, structName)
856 print " sizeof(struct %s), %s_init__}," % (structName, structName)
860 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
861 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
862 schema.name, prefix, prefix)
865 # global init function
873 assert_single_threaded();
876 for tableName, table in sorted(schema.tables.iteritems()):
877 structName = "%s%s" % (prefix, tableName.lower())
878 print " %s_columns_init();" % structName
882 /* Return the schema version. The caller must not free the returned value. */
884 %sget_db_version(void)
888 """ % (prefix, schema.version)
892 def ovsdb_escape(string):
896 raise ovs.db.error.Error("strings may not contain null bytes")
910 return '\\x%02x' % ord(c)
911 return re.sub(r'["\\\000-\037]', escape, string)
915 %(argv0)s: ovsdb schema compiler
916 usage: %(argv0)s [OPTIONS] COMMAND ARG...
918 The following commands are supported:
919 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
920 c-idl-header IDL print C header file for IDL
921 c-idl-source IDL print C source file for IDL implementation
922 nroff IDL print schema documentation in nroff format
924 The following options are also available:
925 -h, --help display this help message
926 -V, --version display version information\
927 """ % {'argv0': argv0}
930 if __name__ == "__main__":
933 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
937 except getopt.GetoptError, geo:
938 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
941 for key, value in options:
942 if key in ['-h', '--help']:
944 elif key in ['-V', '--version']:
945 print "ovsdb-idlc (Open vSwitch) @VERSION@"
946 elif key in ['-C', '--directory']:
951 optKeys = [key for key, value in options]
954 sys.stderr.write("%s: missing command argument "
955 "(use --help for help)\n" % argv0)
958 commands = {"annotate": (annotateSchema, 2),
959 "c-idl-header": (printCIDLHeader, 1),
960 "c-idl-source": (printCIDLSource, 1)}
962 if not args[0] in commands:
963 sys.stderr.write("%s: unknown command \"%s\" "
964 "(use --help for help)\n" % (argv0, args[0]))
967 func, n_args = commands[args[0]]
968 if len(args) - 1 != n_args:
969 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
971 % (argv0, args[0], n_args, len(args) - 1))
975 except ovs.db.error.Error, e:
976 sys.stderr.write("%s: %s\n" % (argv0, e))