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, dstVar, dst, src, type, refTable=None):
206 args = {'indent': indent,
210 if type == 'uuid' and refTable:
211 return ("%(indent)s%(dstVar)s = %(src)s;\n" +
212 "%(indent)s%(dst)s = %(src)s->header_.uuid;") % args
213 elif type == 'string':
214 return "%(indent)s%(dstVar)s = %(dst)s = xstrdup(%(src)s);" % args
216 return "%(dstVar)s = %(dst)s = %(src)s;" % args
218 def typeIsOptionalPointer(type):
219 return (type.min == 0 and type.max == 1 and not type.value
220 and (type.key == 'string'
221 or (type.key == 'uuid' and type.keyRefTable)))
223 def cDeclComment(type):
224 if type.min == 1 and type.max == 1 and type.key == "string":
225 return "\t/* Always nonnull. */"
229 def constify(cType, const):
231 and cType.endswith('*') and not cType.endswith('**')
232 and (cType.startswith('struct uuid') or cType.startswith('char'))):
233 return 'const %s' % cType
237 def cMembers(prefix, columnName, column, const):
239 if type.min == 1 and type.max == 1:
244 if typeIsOptionalPointer(type):
250 key = {'name': "key_%s" % columnName,
251 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
253 value = {'name': "value_%s" % columnName,
254 'type': constify(cBaseType(prefix, type.value, type.valueRefTable) + pointer, const),
256 members = [key, value]
258 m = {'name': columnName,
259 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
260 'comment': cDeclComment(type)}
263 if not singleton and not typeIsOptionalPointer(type):
264 members.append({'name': 'n_%s' % columnName,
269 def printCIDLHeader(schemaFile):
270 schema = parseSchema(schemaFile)
271 prefix = schema.idlPrefix
273 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
275 #ifndef %(prefix)sIDL_HEADER
276 #define %(prefix)sIDL_HEADER 1
281 #include "ovsdb-idl-provider.h"
282 #include "uuid.h"''' % {'prefix': prefix.upper()}
283 for tableName, table in schema.tables.iteritems():
285 print "/* %s table. */" % tableName
286 structName = "%s%s" % (prefix, tableName.lower())
287 print "struct %s {" % structName
288 print "\tstruct ovsdb_idl_row header_;"
289 for columnName, column in table.columns.iteritems():
290 print "\n\t/* %s column. */" % columnName
291 for member in cMembers(prefix, columnName, column, False):
292 print "\t%(type)s%(name)s;%(comment)s" % member
296 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
297 const struct %(s)s *%(s)s_next(const struct %(s)s *);
298 #define %(S)s_FOR_EACH(ROW, IDL) for ((ROW) = %(s)s_first(IDL); (ROW); (ROW) = %(s)s_next(ROW))
300 void %(s)s_delete(const struct %(s)s *);
301 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
302 ''' % {'s': structName, 'S': structName.upper()}
304 for columnName, column in table.columns.iteritems():
305 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
308 for columnName, column in table.columns.iteritems():
310 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
311 args = ['%(type)s%(name)s' % member for member
312 in cMembers(prefix, columnName, column, True)]
313 print '%s);' % ', '.join(args)
315 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
316 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
318 def printEnum(members):
319 if len(members) == 0:
323 for member in members[:-1]:
324 print " %s," % member
325 print " %s" % members[-1]
328 def printCIDLSource(schemaFile):
329 schema = parseSchema(schemaFile)
330 prefix = schema.idlPrefix
332 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
337 #include "ovsdb-data.h"''' % schema.idlHeader
340 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in schema.tables] + ["%sN_TABLES" % prefix.upper()])
341 print "\nstatic struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
344 for tableName, table in schema.tables.iteritems():
345 structName = "%s%s" % (prefix, tableName.lower())
347 static struct %(s)s *
348 %(s)s_cast(struct ovsdb_idl_row *row)
350 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
352 ''' % {'s': structName}
355 for tableName, table in schema.tables.iteritems():
356 structName = "%s%s" % (prefix, tableName.lower())
358 if table.comment != None:
359 print "/* %s table (%s). */" % (tableName, table.comment)
361 print "/* %s table. */" % (tableName)
364 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
365 for columnName in table.columns]
366 + ["%s_N_COLUMNS" % structName.upper()])
368 print "\nstatic struct ovsdb_idl_column %s_columns[];" % structName
373 %s_parse(struct ovsdb_idl_row *row_)
375 struct %s *row = %s_cast(row_);
376 const struct ovsdb_datum *datum;
379 memset(row_ + 1, 0, sizeof *row - sizeof *row_);''' % (structName, structName, structName)
382 for columnName, column in table.columns.iteritems():
384 refKey = type.key == "uuid" and type.keyRefTable
385 refValue = type.value == "uuid" and type.valueRefTable
387 print " datum = &row_->old[%s_COL_%s];" % (structName.upper(), columnName.upper())
389 keyVar = "row->key_%s" % columnName
390 valueVar = "row->value_%s" % columnName
392 keyVar = "row->%s" % columnName
395 if (type.min == 1 and type.max == 1) or typeIsOptionalPointer(type):
396 print " if (datum->n >= 1) {"
398 print " %s = datum->keys[0].%s;" % (keyVar, type.key)
400 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())
404 print " %s = datum->values[0].%s;" % (valueVar, type.value)
406 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())
407 if (not typeIsOptionalPointer(type) and
408 (type.key == "string" or type.value == "string")):
410 if type.key == "string":
411 print " %s = \"\";" % keyVar
412 if type.value == "string":
413 print " %s = \"\";" % valueVar
417 if type.max != 'unlimited':
418 nMax = "MIN(%d, datum->n)" % type.max
421 print " for (i = 0; i < %s; i++) {" % nMax
424 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())
426 refs.append('keyRow')
428 keySrc = "datum->keys[i].%s" % type.key
430 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())
431 valueSrc = "valueRow"
432 refs.append('valueRow')
434 valueSrc = "datum->values[i].%s" % type.value
436 print " if (%s) {" % ' && '.join(refs)
440 print "%sif (!row->n_%s) {" % (indent, columnName)
441 print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
443 print "%s %s = xmalloc(%s * sizeof %s);" % (indent, valueVar, nMax, valueVar)
445 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
447 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
448 print "%srow->n_%s++;" % (indent, columnName)
456 for columnName, column in table.columns.iteritems():
458 if (type.min != 1 or type.max != 1) and not typeIsOptionalPointer(type):
462 %s_unparse(struct ovsdb_idl_row *row_)
464 struct %s *row = %s_cast(row_);
465 ''' % (structName, structName, structName)
467 keyVar = "row->key_%s" % columnName
468 valueVar = "row->value_%s" % columnName
470 keyVar = "row->%s" % columnName
472 print " free(%s);" % keyVar
474 print " free(%s);" % valueVar
479 %s_unparse(struct ovsdb_idl_row *row UNUSED)
483 # First, next functions.
486 %(s)s_first(const struct ovsdb_idl *idl)
488 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
492 %(s)s_next(const struct %(s)s *row)
494 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
495 }''' % {'s': structName,
498 'T': tableName.upper()}
502 %(s)s_delete(const struct %(s)s *row_)
504 struct %(s)s *row = (struct %(s)s *) row_;
505 ovsdb_idl_txn_delete(&row->header_);
509 %(s)s_insert(struct ovsdb_idl_txn *txn)
511 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
513 ''' % {'s': structName,
516 'T': tableName.upper()}
519 for columnName, column in table.columns.iteritems():
522 %(s)s_verify_%(c)s(const struct %(s)s *row)
524 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
525 }''' % {'s': structName,
526 'S': structName.upper(),
528 'C': columnName.upper()}
531 for columnName, column in table.columns.iteritems():
534 members = cMembers(prefix, columnName, column, True)
535 keyVar = members[0]['name']
539 valueVar = members[1]['name']
541 nVar = members[2]['name']
544 nVar = members[1]['name']
545 print '%(s)s_set_%(c)s(const struct %(s)s *row_, %(args)s)' % \
546 {'s': structName, 'c': columnName,
547 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
549 print " struct %(s)s *row = (struct %(s)s *) row_;" % {'s': structName}
550 print " struct ovsdb_datum datum;"
551 if type.min == 1 and type.max == 1:
553 print " datum.n = 1;"
554 print " datum.keys = xmalloc(sizeof *datum.keys);"
555 print cCopyType(" ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
557 print " datum.values = xmalloc(sizeof *datum.values);"
558 print cCopyType(" ", "row->%s" % valueVar, "datum.values[0].%s" % type.value, valueVar, type.value, type.valueRefTable)
560 print " datum.values = NULL;"
561 elif typeIsOptionalPointer(type):
563 print " if (%s) {" % keyVar
564 print " datum.n = 1;"
565 print " datum.keys = xmalloc(sizeof *datum.keys);"
566 print cCopyType(" ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
568 print " datum.n = 0;"
569 print " datum.keys = NULL;"
570 print " row->%s = NULL;" % keyVar
572 print " datum.values = NULL;"
576 print " free(row->%s);" % keyVar
577 print " row->%s = %s ? xmalloc(%s * sizeof *row->%s) : NULL;" % (keyVar, nVar, nVar, keyVar)
578 print " row->%s = %s;" % (nVar, nVar)
580 print " free(row->%s);" % valueVar
581 print " row->%s = xmalloc(%s * sizeof *row->%s);" % (valueVar, nVar, valueVar)
582 print " datum.n = %s;" % nVar
583 print " datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar
585 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
587 print " datum.values = NULL;"
588 print " for (i = 0; i < %s; i++) {" % nVar
589 print cCopyType(" ", "row->%s[i]" % keyVar, "datum.keys[i].%s" % type.key, "%s[i]" % keyVar, type.key, type.keyRefTable)
591 print cCopyType(" ", "row->%s[i]" % valueVar, "datum.values[i].%s" % type.value, "%s[i]" % valueVar, type.value, type.valueRefTable)
593 print " ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
595 'S': structName.upper(),
596 'C': columnName.upper()}
600 print "\nstatic struct ovsdb_idl_column %s_columns[%s_N_COLUMNS] = {" % (
601 structName, structName.upper())
602 for columnName, column in table.columns.iteritems():
606 valueTypeName = type.value.upper()
608 valueTypeName = "VOID"
609 if type.max == "unlimited":
613 print " {\"%s\", {OVSDB_TYPE_%s, OVSDB_TYPE_%s, %d, %s}}," % (
614 columnName, type.key.upper(), valueTypeName,
620 print "static struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
621 for tableName, table in schema.tables.iteritems():
622 structName = "%s%s" % (prefix, tableName.lower())
623 print " {\"%s\"," % tableName
624 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
625 structName, structName)
626 print " sizeof(struct %s)," % structName
627 print " %s_parse," % structName
628 print " %s_unparse}," % structName
632 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
633 print " %stable_classes, ARRAY_SIZE(%stable_classes)" % (prefix, prefix)
636 def ovsdb_escape(string):
640 raise Error("strings may not contain null bytes")
654 return '\\x%02x' % ord(c)
655 return re.sub(r'["\\\000-\037]', escape, string)
657 def printDoc(schemaFile):
658 schema = parseSchema(schemaFile)
663 for tableName, table in sorted(schema.tables.iteritems()):
664 title = "%s table" % tableName
667 print '-' * len(title)
671 for columnName, column in sorted(table.columns.iteritems()):
673 print "%s (%s)" % (columnName, column.type.toEnglish())
675 print "\t%s" % column.comment
679 %(argv0)s: ovsdb schema compiler
680 usage: %(argv0)s [OPTIONS] COMMAND ARG...
682 The following commands are supported:
683 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
684 c-idl-header IDL print C header file for IDL
685 c-idl-source IDL print C source file for IDL implementation
686 doc IDL print schema documentation
688 The following options are also available:
689 -h, --help display this help message
690 -V, --version display version information\
691 """ % {'argv0': argv0}
694 if __name__ == "__main__":
697 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
701 except getopt.GetoptError, geo:
702 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
705 for key, value in options:
706 if key in ['-h', '--help']:
708 elif key in ['-V', '--version']:
709 print "ovsdb-idlc (Open vSwitch) @VERSION@"
710 elif key in ['-C', '--directory']:
715 optKeys = [key for key, value in options]
718 sys.stderr.write("%s: missing command argument "
719 "(use --help for help)\n" % argv0)
722 commands = {"annotate": (annotateSchema, 2),
723 "c-idl-header": (printCIDLHeader, 1),
724 "c-idl-source": (printCIDLSource, 1),
725 "doc": (printDoc, 1)}
727 if not args[0] in commands:
728 sys.stderr.write("%s: unknown command \"%s\" "
729 "(use --help for help)\n" % (argv0, args[0]))
732 func, n_args = commands[args[0]]
733 if len(args) - 1 != n_args:
734 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
736 % (argv0, args[0], n_args, len(args) - 1))
741 sys.stderr.write("%s: %s\n" % (argv0, e.msg))