8 sys.path.insert(0, "@abs_top_srcdir@/ovsdb")
9 import simplejson as json
13 class Error(Exception):
14 def __init__(self, msg):
15 Exception.__init__(self)
18 def getMember(json, name, validTypes, description, default=None):
21 if type(member) not in validTypes:
22 raise Error("%s: type mismatch for '%s' member"
23 % (description, name))
27 def mustGetMember(json, name, expectedType, description):
28 member = getMember(json, name, expectedType, description)
30 raise Error("%s: missing '%s' member" % (description, name))
34 def __init__(self, name, comment, tables, idlPrefix, idlHeader):
36 self.comment = comment
38 self.idlPrefix = idlPrefix
39 self.idlHeader = idlHeader
43 name = mustGetMember(json, 'name', [unicode], 'database')
44 comment = getMember(json, 'comment', [unicode], 'database')
45 tablesJson = mustGetMember(json, 'tables', [dict], 'database')
47 for tableName, tableJson in tablesJson.iteritems():
48 tables[tableName] = TableSchema.fromJson(tableJson,
49 "%s table" % tableName)
50 idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database')
51 idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database')
52 return DbSchema(name, comment, tables, idlPrefix, idlHeader)
55 d = {"name": self.name,
57 for name, table in self.tables.iteritems():
58 d["tables"][name] = table.toJson()
59 if self.comment != None:
60 d["comment"] = self.comment
64 def __init__(self, comment, columns):
65 self.comment = comment
66 self.columns = columns
69 def fromJson(json, description):
70 comment = getMember(json, 'comment', [unicode], description)
71 columnsJson = mustGetMember(json, 'columns', [dict], description)
73 for name, json in columnsJson.iteritems():
74 columns[name] = ColumnSchema.fromJson(
75 json, "column %s in %s" % (name, description))
76 return TableSchema(comment, columns)
80 for name, column in self.columns.iteritems():
81 d["columns"][name] = column.toJson()
82 if self.comment != None:
83 d["comment"] = self.comment
87 def __init__(self, comment, type, persistent):
88 self.comment = comment
90 self.persistent = persistent
93 def fromJson(json, description):
94 comment = getMember(json, 'comment', [unicode], description)
95 type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode],
97 'type of %s' % description)
98 ephemeral = getMember(json, 'ephemeral', [bool], description)
99 persistent = ephemeral != True
100 return ColumnSchema(comment, type, persistent)
103 d = {"type": self.type.toJson()}
104 if self.persistent == False:
105 d["ephemeral"] = True
106 if self.comment != None:
107 d["comment"] = self.comment
111 def __init__(self, key, keyRefTable=None, value=None, valueRefTable=None,
114 self.keyRefTable = keyRefTable
116 self.valueRefTable = valueRefTable
121 def fromJson(json, description):
122 if type(json) == unicode:
125 key = mustGetMember(json, 'key', [unicode], description)
126 keyRefTable = getMember(json, 'keyRefTable', [unicode], description)
127 value = getMember(json, 'value', [unicode], description)
128 valueRefTable = getMember(json, 'valueRefTable', [unicode], description)
129 min = getMember(json, 'min', [int], description, 1)
130 max = getMember(json, 'max', [int, unicode], description, 1)
131 return Type(key, keyRefTable, value, valueRefTable, min, max)
134 if self.value == None and self.min == 1 and self.max == 1:
137 d = {"key": self.key}
138 if self.value != None:
139 d["value"] = self.value
147 return self.min == 1 and self.max == 1 and not self.value
149 def isOptional(self):
150 return self.min == 0 and self.max == 1
153 keyName = atomicTypeToEnglish(self.key, self.keyRefTable)
155 valueName = atomicTypeToEnglish(self.value, self.valueRefTable)
158 return atomicTypeToEnglish(self.key, self.keyRefTable)
159 elif self.isOptional():
161 return "optional %s-%s pair" % (keyName, valueName)
163 return "optional %s" % keyName
165 if self.max == "unlimited":
167 quantity = "%d or more " % self.min
171 quantity = "%d to %d " % (self.min, self.max)
173 quantity = "up to %d " % self.max
176 return "map of %s%s-%s pairs" % (quantity, keyName, valueName)
178 return "set of %s%s" % (quantity, keyName)
181 def atomicTypeToEnglish(base, refTable):
182 if base == 'uuid' and refTable:
187 def parseSchema(filename):
188 return DbSchema.fromJson(json.load(open(filename, "r")))
190 def annotateSchema(schemaFile, annotationFile):
191 schemaJson = json.load(open(schemaFile, "r"))
192 execfile(annotationFile, globals(), {"s": schemaJson})
193 json.dump(schemaJson, sys.stdout)
195 def cBaseType(prefix, type, refTable=None):
196 if type == 'uuid' and refTable:
197 return "struct %s%s *" % (prefix, refTable.lower())
199 return {'integer': 'int64_t ',
201 'uuid': 'struct uuid ',
203 'string': 'char *'}[type]
205 def cCopyType(indent, dst, src, type, refTable=None):
206 args = {'indent': indent,
209 if type == 'uuid' and refTable:
210 return ("%(indent)s%(dst)s = %(src)s->header_.uuid;") % args
211 elif type == 'string':
212 return "%(indent)s%(dst)s = xstrdup(%(src)s);" % args
214 return "%(indent)s%(dst)s = %(src)s;" % args
216 def typeIsOptionalPointer(type):
217 return (type.min == 0 and type.max == 1 and not type.value
218 and (type.key == 'string'
219 or (type.key == 'uuid' and type.keyRefTable)))
221 def cDeclComment(type):
222 if type.min == 1 and type.max == 1 and type.key == "string":
223 return "\t/* Always nonnull. */"
227 def cInitDefault(var, type, refTable, isOptional):
228 if type == 'uuid' and refTable:
229 return "%s = NULL;" % var
230 elif type == 'string' and not isOptional:
231 return "%s = \"\";" % var
233 return {'integer': '%s = 0;',
235 'uuid': 'uuid_zero(&%s);',
236 'boolean': '%s = false;',
237 'string': '%s = NULL;'}[type] % var
239 def constify(cType, const):
241 and cType.endswith('*') and not cType.endswith('**')
242 and (cType.startswith('struct uuid') or cType.startswith('char'))):
243 return 'const %s' % cType
247 def cMembers(prefix, columnName, column, const):
249 if type.min == 1 and type.max == 1:
254 if typeIsOptionalPointer(type):
260 key = {'name': "key_%s" % columnName,
261 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
263 value = {'name': "value_%s" % columnName,
264 'type': constify(cBaseType(prefix, type.value, type.valueRefTable) + pointer, const),
266 members = [key, value]
268 m = {'name': columnName,
269 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
270 'comment': cDeclComment(type)}
273 if not singleton and not typeIsOptionalPointer(type):
274 members.append({'name': 'n_%s' % columnName,
279 def printCIDLHeader(schemaFile):
280 schema = parseSchema(schemaFile)
281 prefix = schema.idlPrefix
283 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
285 #ifndef %(prefix)sIDL_HEADER
286 #define %(prefix)sIDL_HEADER 1
291 #include "ovsdb-idl-provider.h"
292 #include "uuid.h"''' % {'prefix': prefix.upper()}
294 for tableName, table in schema.tables.iteritems():
295 structName = "%s%s" % (prefix, tableName.lower())
298 print "/* %s table. */" % tableName
299 print "struct %s {" % structName
300 print "\tstruct ovsdb_idl_row header_;"
301 for columnName, column in table.columns.iteritems():
302 print "\n\t/* %s column. */" % columnName
303 for member in cMembers(prefix, columnName, column, False):
304 print "\t%(type)s%(name)s;%(comment)s" % member
308 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
309 for columnName in table.columns]
310 + ["%s_N_COLUMNS" % structName.upper()])
313 for columnName in table.columns:
314 print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
316 'S': structName.upper(),
318 'C': columnName.upper()}
320 print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
323 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
324 const struct %(s)s *%(s)s_next(const struct %(s)s *);
325 #define %(S)s_FOR_EACH(ROW, IDL) for ((ROW) = %(s)s_first(IDL); (ROW); (ROW) = %(s)s_next(ROW))
327 void %(s)s_delete(const struct %(s)s *);
328 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
329 ''' % {'s': structName, 'S': structName.upper()}
331 for columnName, column in table.columns.iteritems():
332 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
335 for columnName, column in table.columns.iteritems():
337 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
338 args = ['%(type)s%(name)s' % member for member
339 in cMembers(prefix, columnName, column, True)]
340 print '%s);' % ', '.join(args)
343 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in schema.tables] + ["%sN_TABLES" % prefix.upper()])
345 for tableName in schema.tables:
346 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
349 't': tableName.lower(),
350 'T': tableName.upper()}
351 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
353 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
354 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
356 def printEnum(members):
357 if len(members) == 0:
361 for member in members[:-1]:
362 print " %s," % member
363 print " %s" % members[-1]
366 def printCIDLSource(schemaFile):
367 schema = parseSchema(schemaFile)
368 prefix = schema.idlPrefix
370 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
375 #include "ovsdb-data.h"''' % schema.idlHeader
378 for tableName, table in schema.tables.iteritems():
379 structName = "%s%s" % (prefix, tableName.lower())
381 static struct %(s)s *
382 %(s)s_cast(const struct ovsdb_idl_row *row)
384 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
386 ''' % {'s': structName}
389 for tableName, table in schema.tables.iteritems():
390 structName = "%s%s" % (prefix, tableName.lower())
392 if table.comment != None:
393 print "/* %s table (%s). */" % (tableName, table.comment)
395 print "/* %s table. */" % (tableName)
398 for columnName, column in table.columns.iteritems():
401 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
403 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
407 refKey = type.key == "uuid" and type.keyRefTable
408 refValue = type.value == "uuid" and type.valueRefTable
410 keyVar = "row->key_%s" % columnName
411 valueVar = "row->value_%s" % columnName
413 keyVar = "row->%s" % columnName
416 if ((type.min == 1 and type.max == 1) or
417 typeIsOptionalPointer(type)):
419 print " if (datum->n >= 1) {"
421 print " %s = datum->keys[0].%s;" % (keyVar, type.key)
423 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[0].uuid));" % (keyVar, prefix, type.keyRefTable.lower(), prefix, prefix.upper(), type.keyRefTable.upper())
427 print " %s = datum->values[0].%s;" % (valueVar, type.value)
429 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[0].uuid));" % (valueVar, prefix, type.valueRefTable.lower(), prefix, prefix.upper(), type.valueRefTable.upper())
431 print " %s" % cInitDefault(keyVar, type.key, type.keyRefTable, type.min == 0)
433 print " %s" % cInitDefault(valueVar, type.value, type.valueRefTable, type.min == 0)
436 if type.max != 'unlimited':
437 print " size_t n = MIN(%d, datum->n);" % type.max
443 print " %s = NULL;" % keyVar
445 print " %s = NULL;" % valueVar
446 print " row->n_%s = 0;" % columnName
447 print " for (i = 0; i < %s; i++) {" % nMax
450 print " struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[i].uuid));" % (prefix, type.keyRefTable.lower(), prefix, type.keyRefTable.lower(), prefix, prefix.upper(), type.keyRefTable.upper())
452 refs.append('keyRow')
454 keySrc = "datum->keys[i].%s" % type.key
456 print " struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[i].uuid));" % (prefix, type.valueRefTable.lower(), prefix, type.valueRefTable.lower(), prefix, prefix.upper(), type.valueRefTable.upper())
457 valueSrc = "valueRow"
458 refs.append('valueRow')
460 valueSrc = "datum->values[i].%s" % type.value
462 print " if (%s) {" % ' && '.join(refs)
466 print "%sif (!row->n_%s) {" % (indent, columnName)
467 print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
469 print "%s %s = xmalloc(%s * sizeof %s);" % (indent, valueVar, nMax, valueVar)
471 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
473 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
474 print "%srow->n_%s++;" % (indent, columnName)
481 for columnName, column in table.columns.iteritems():
483 if (type.min != 1 or type.max != 1) and not typeIsOptionalPointer(type):
486 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
488 struct %(s)s *row = %(s)s_cast(row_);
489 ''' % {'s': structName, 'c': columnName}
491 keyVar = "row->key_%s" % columnName
492 valueVar = "row->value_%s" % columnName
494 keyVar = "row->%s" % columnName
496 print " free(%s);" % keyVar
498 print " free(%s);" % valueVar
503 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row UNUSED)
506 }''' % {'s': structName, 'c': columnName}
508 # First, next functions.
511 %(s)s_first(const struct ovsdb_idl *idl)
513 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
517 %(s)s_next(const struct %(s)s *row)
519 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
520 }''' % {'s': structName,
523 'T': tableName.upper()}
527 %(s)s_delete(const struct %(s)s *row)
529 ovsdb_idl_txn_delete(&row->header_);
533 %(s)s_insert(struct ovsdb_idl_txn *txn)
535 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
537 ''' % {'s': structName,
540 'T': tableName.upper()}
543 for columnName, column in table.columns.iteritems():
546 %(s)s_verify_%(c)s(const struct %(s)s *row)
548 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
549 }''' % {'s': structName,
550 'S': structName.upper(),
552 'C': columnName.upper()}
555 for columnName, column in table.columns.iteritems():
558 members = cMembers(prefix, columnName, column, True)
559 keyVar = members[0]['name']
563 valueVar = members[1]['name']
565 nVar = members[2]['name']
568 nVar = members[1]['name']
569 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
570 {'s': structName, 'c': columnName,
571 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
573 print " struct ovsdb_datum datum;"
574 if type.min == 1 and type.max == 1:
576 print " datum.n = 1;"
577 print " datum.keys = xmalloc(sizeof *datum.keys);"
578 print cCopyType(" ", "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
580 print " datum.values = xmalloc(sizeof *datum.values);"
581 print cCopyType(" ", "datum.values[0].%s" % type.value, valueVar, type.value, type.valueRefTable)
583 print " datum.values = NULL;"
584 elif typeIsOptionalPointer(type):
586 print " if (%s) {" % keyVar
587 print " datum.n = 1;"
588 print " datum.keys = xmalloc(sizeof *datum.keys);"
589 print cCopyType(" ", "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
591 print " datum.n = 0;"
592 print " datum.keys = NULL;"
594 print " datum.values = NULL;"
598 print " datum.n = %s;" % nVar
599 print " datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar
601 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
603 print " datum.values = NULL;"
604 print " for (i = 0; i < %s; i++) {" % nVar
605 print cCopyType(" ", "datum.keys[i].%s" % type.key, "%s[i]" % keyVar, type.key, type.keyRefTable)
607 print cCopyType(" ", "datum.values[i].%s" % type.value, "%s[i]" % valueVar, type.value, type.valueRefTable)
609 print " ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
611 'S': structName.upper(),
612 'C': columnName.upper()}
616 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS] = {" % (
617 structName, structName.upper())
618 for columnName, column in table.columns.iteritems():
622 valueTypeName = type.value.upper()
624 valueTypeName = "VOID"
625 if type.max == "unlimited":
631 {OVSDB_TYPE_%(kt)s, OVSDB_TYPE_%(vt)s, %(min)s, %(max)s},
633 %(s)s_unparse_%(c)s},""" % {'c': columnName,
635 'kt': type.key.upper(),
643 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
644 for tableName, table in schema.tables.iteritems():
645 structName = "%s%s" % (prefix, tableName.lower())
646 print " {\"%s\"," % tableName
647 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
648 structName, structName)
649 print " sizeof(struct %s)}," % structName
653 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
654 print " %stable_classes, ARRAY_SIZE(%stable_classes)" % (prefix, prefix)
657 def ovsdb_escape(string):
661 raise Error("strings may not contain null bytes")
675 return '\\x%02x' % ord(c)
676 return re.sub(r'["\\\000-\037]', escape, string)
678 def printDoc(schemaFile):
679 schema = parseSchema(schemaFile)
684 for tableName, table in sorted(schema.tables.iteritems()):
685 title = "%s table" % tableName
688 print '-' * len(title)
692 for columnName, column in sorted(table.columns.iteritems()):
694 print "%s (%s)" % (columnName, column.type.toEnglish())
696 print "\t%s" % column.comment
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 doc IDL print schema documentation
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),
746 "doc": (printDoc, 1)}
748 if not args[0] in commands:
749 sys.stderr.write("%s: unknown command \"%s\" "
750 "(use --help for help)\n" % (argv0, args[0]))
753 func, n_args = commands[args[0]]
754 if len(args) - 1 != n_args:
755 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
757 % (argv0, args[0], n_args, len(args) - 1))
762 sys.stderr.write("%s: %s\n" % (argv0, e.msg))