7 sys.path.insert(0, "@abs_top_srcdir@/ovsdb")
8 import simplejson as json
12 class Error(Exception):
13 def __init__(self, msg):
14 Exception.__init__(self)
17 def getMember(json, name, validTypes, description, default=None):
20 if type(member) not in validTypes:
21 raise Error("%s: type mismatch for '%s' member"
22 % (description, name))
26 def mustGetMember(json, name, expectedType, description):
27 member = getMember(json, name, expectedType, description)
29 raise Error("%s: missing '%s' member" % (description, name))
33 def __init__(self, name, comment, tables, idlPrefix, idlHeader):
35 self.comment = comment
37 self.idlPrefix = idlPrefix
38 self.idlHeader = idlHeader
42 name = mustGetMember(json, 'name', [unicode], 'database')
43 comment = getMember(json, 'comment', [unicode], 'database')
44 tablesJson = mustGetMember(json, 'tables', [dict], 'database')
46 for tableName, tableJson in tablesJson.iteritems():
47 tables[tableName] = TableSchema.fromJson(tableJson,
48 "%s table" % tableName)
49 idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database')
50 idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database')
51 return DbSchema(name, comment, tables, idlPrefix, idlHeader)
54 d = {"name": self.name,
56 for name, table in self.tables.iteritems():
57 d["tables"][name] = table.toJson()
58 if self.comment != None:
59 d["comment"] = self.comment
63 def __init__(self, comment, columns):
64 self.comment = comment
65 self.columns = columns
68 def fromJson(json, description):
69 comment = getMember(json, 'comment', [unicode], description)
70 columnsJson = mustGetMember(json, 'columns', [dict], description)
72 for name, json in columnsJson.iteritems():
73 columns[name] = ColumnSchema.fromJson(
74 json, "column %s in %s" % (name, description))
75 return TableSchema(comment, columns)
79 for name, column in self.columns.iteritems():
80 d["columns"][name] = column.toJson()
81 if self.comment != None:
82 d["comment"] = self.comment
86 def __init__(self, comment, type, persistent):
87 self.comment = comment
89 self.persistent = persistent
92 def fromJson(json, description):
93 comment = getMember(json, 'comment', [unicode], description)
94 type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode],
96 'type of %s' % description)
97 ephemeral = getMember(json, 'ephemeral', [bool], description)
98 persistent = ephemeral != True
99 return ColumnSchema(comment, type, persistent)
102 d = {"type": self.type.toJson()}
103 if self.persistent == False:
104 d["ephemeral"] = True
105 if self.comment != None:
106 d["comment"] = self.comment
110 def __init__(self, key, keyRefTable=None, value=None, valueRefTable=None,
113 self.keyRefTable = keyRefTable
115 self.valueRefTable = valueRefTable
120 def fromJson(json, description):
121 if type(json) == unicode:
124 key = mustGetMember(json, 'key', [unicode], description)
125 keyRefTable = getMember(json, 'keyRefTable', [unicode], description)
126 value = getMember(json, 'value', [unicode], description)
127 valueRefTable = getMember(json, 'valueRefTable', [unicode], description)
128 min = getMember(json, 'min', [int], description, 1)
129 max = getMember(json, 'max', [int, unicode], description, 1)
130 return Type(key, keyRefTable, value, valueRefTable, min, max)
133 if self.value == None and self.min == 1 and self.max == 1:
136 d = {"key": self.key}
137 if self.value != None:
138 d["value"] = self.value
146 return self.min == 1 and self.max == 1 and not self.value
148 def isOptional(self):
149 return self.min == 0 and self.max == 1
152 keyName = atomicTypeToEnglish(self.key, self.keyRefTable)
154 valueName = atomicTypeToEnglish(self.value, self.valueRefTable)
157 return atomicTypeToEnglish(self.key, self.keyRefTable)
158 elif self.isOptional():
160 return "optional %s-%s pair" % (keyName, valueName)
162 return "optional %s" % keyName
164 if self.max == "unlimited":
166 quantity = "%d or more " % self.min
170 quantity = "%d to %d " % (self.min, self.max)
172 quantity = "up to %d " % self.max
175 return "map of %s%s-%s pairs" % (quantity, keyName, valueName)
177 return "set of %s%s" % (quantity, keyName)
180 def atomicTypeToEnglish(base, refTable):
181 if base == 'uuid' and refTable:
186 def parseSchema(filename):
187 file = open(filename, "r")
190 if not line.startswith('//'):
192 return DbSchema.fromJson(json.loads(s))
194 def cBaseType(prefix, type, refTable=None):
195 if type == 'uuid' and refTable:
196 return "struct %s%s *" % (prefix, refTable.lower())
198 return {'integer': 'int64_t ',
200 'uuid': 'struct uuid ',
202 'string': 'char *'}[type]
204 def cCopyType(indent, dstVar, dst, src, type, refTable=None):
205 args = {'indent': indent,
209 if type == 'uuid' and refTable:
210 return ("%(indent)s%(dstVar)s = %(src)s;\n" +
211 "%(indent)s%(dst)s = %(src)s->header_.uuid;") % args
212 elif type == 'string':
213 return "%(indent)s%(dstVar)s = %(dst)s = xstrdup(%(src)s);" % args
215 return "%(dstVar)s = %(dst)s = %(src)s;" % args
217 def typeIsOptionalPointer(type):
218 return (type.min == 0 and type.max == 1 and not type.value
219 and (type.key == 'string'
220 or (type.key == 'uuid' and type.keyRefTable)))
222 def cDeclComment(type):
223 if type.min == 1 and type.max == 1 and type.key == "string":
224 return "\t/* Always nonnull. */"
228 def constify(cType, const):
230 and cType.endswith('*') and not cType.endswith('**')
231 and (cType.startswith('struct uuid') or cType.startswith('char'))):
232 return 'const %s' % cType
236 def cMembers(prefix, columnName, column, const):
238 if type.min == 1 and type.max == 1:
243 if typeIsOptionalPointer(type):
249 key = {'name': "key_%s" % columnName,
250 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
252 value = {'name': "value_%s" % columnName,
253 'type': constify(cBaseType(prefix, type.value, type.valueRefTable) + pointer, const),
255 members = [key, value]
257 m = {'name': columnName,
258 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
259 'comment': cDeclComment(type)}
262 if not singleton and not typeIsOptionalPointer(type):
263 members.append({'name': 'n_%s' % columnName,
268 def printCIDLHeader(schema):
269 prefix = schema.idlPrefix
271 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
273 #ifndef %(prefix)sIDL_HEADER
274 #define %(prefix)sIDL_HEADER 1
279 #include "ovsdb-idl-provider.h"
280 #include "uuid.h"''' % {'prefix': prefix.upper()}
281 for tableName, table in schema.tables.iteritems():
283 print "/* %s table. */" % tableName
284 structName = "%s%s" % (prefix, tableName.lower())
285 print "struct %s {" % structName
286 print "\tstruct ovsdb_idl_row header_;"
287 for columnName, column in table.columns.iteritems():
288 print "\n\t/* %s column. */" % columnName
289 for member in cMembers(prefix, columnName, column, False):
290 print "\t%(type)s%(name)s;%(comment)s" % member
294 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
295 const struct %(s)s *%(s)s_next(const struct %(s)s *);
296 #define %(S)s_FOR_EACH(ROW, IDL) for ((ROW) = %(s)s_first(IDL); (ROW); (ROW) = %(s)s_next(ROW))
298 void %(s)s_delete(const struct %(s)s *);
299 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
300 ''' % {'s': structName, 'S': structName.upper()}
302 for columnName, column in table.columns.iteritems():
303 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
306 for columnName, column in table.columns.iteritems():
308 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
309 args = ['%(type)s%(name)s' % member for member
310 in cMembers(prefix, columnName, column, True)]
311 print '%s);' % ', '.join(args)
313 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
314 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
316 def printEnum(members):
317 if len(members) == 0:
321 for member in members[:-1]:
322 print " %s," % member
323 print " %s" % members[-1]
326 def printCIDLSource(schema):
327 prefix = schema.idlPrefix
329 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
334 #include "ovsdb-data.h"''' % schema.idlHeader
337 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in schema.tables] + ["%sN_TABLES" % prefix.upper()])
338 print "\nstatic struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
341 for tableName, table in schema.tables.iteritems():
342 structName = "%s%s" % (prefix, tableName.lower())
344 static struct %(s)s *
345 %(s)s_cast(struct ovsdb_idl_row *row)
347 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
349 ''' % {'s': structName}
352 for tableName, table in schema.tables.iteritems():
353 structName = "%s%s" % (prefix, tableName.lower())
355 if table.comment != None:
356 print "/* %s table (%s). */" % (tableName, table.comment)
358 print "/* %s table. */" % (tableName)
361 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
362 for columnName in table.columns]
363 + ["%s_N_COLUMNS" % structName.upper()])
365 print "\nstatic struct ovsdb_idl_column %s_columns[];" % structName
370 %s_parse(struct ovsdb_idl_row *row_)
372 struct %s *row = %s_cast(row_);
373 const struct ovsdb_datum *datum;
376 memset(row_ + 1, 0, sizeof *row - sizeof *row_);''' % (structName, structName, structName)
379 for columnName, column in table.columns.iteritems():
381 refKey = type.key == "uuid" and type.keyRefTable
382 refValue = type.value == "uuid" and type.valueRefTable
384 print " datum = &row_->old[%s_COL_%s];" % (structName.upper(), columnName.upper())
386 keyVar = "row->key_%s" % columnName
387 valueVar = "row->value_%s" % columnName
389 keyVar = "row->%s" % columnName
392 if (type.min == 1 and type.max == 1) or typeIsOptionalPointer(type):
393 print " if (datum->n >= 1) {"
395 print " %s = datum->keys[0].%s;" % (keyVar, type.key)
397 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())
401 print " %s = datum->values[0].%s;" % (valueVar, type.value)
403 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())
404 if (not typeIsOptionalPointer(type) and
405 (type.key == "string" or type.value == "string")):
407 if type.key == "string":
408 print " %s = \"\";" % keyVar
409 if type.value == "string":
410 print " %s = \"\";" % valueVar
414 if type.max != 'unlimited':
415 nMax = "MIN(%d, datum->n)" % type.max
418 print " for (i = 0; i < %s; i++) {" % nMax
421 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())
423 refs.append('keyRow')
425 keySrc = "datum->keys[i].%s" % type.key
427 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())
428 valueSrc = "valueRow"
429 refs.append('valueRow')
431 valueSrc = "datum->values[i].%s" % type.value
433 print " if (%s) {" % ' && '.join(refs)
437 print "%sif (!row->n_%s) {" % (indent, columnName)
438 print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
440 print "%s %s = xmalloc(%s * sizeof %s);" % (indent, valueVar, nMax, valueVar)
442 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
444 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
445 print "%srow->n_%s++;" % (indent, columnName)
453 for columnName, column in table.columns.iteritems():
455 if (type.min != 1 or type.max != 1) and not typeIsOptionalPointer(type):
459 %s_unparse(struct ovsdb_idl_row *row_)
461 struct %s *row = %s_cast(row_);
462 ''' % (structName, structName, structName)
464 keyVar = "row->key_%s" % columnName
465 valueVar = "row->value_%s" % columnName
467 keyVar = "row->%s" % columnName
469 print " free(%s);" % keyVar
471 print " free(%s);" % valueVar
476 %s_unparse(struct ovsdb_idl_row *row UNUSED)
480 # First, next functions.
483 %(s)s_first(const struct ovsdb_idl *idl)
485 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
489 %(s)s_next(const struct %(s)s *row)
491 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
492 }''' % {'s': structName,
495 'T': tableName.upper()}
499 %(s)s_delete(const struct %(s)s *row_)
501 struct %(s)s *row = (struct %(s)s *) row_;
502 ovsdb_idl_txn_delete(&row->header_);
506 %(s)s_insert(struct ovsdb_idl_txn *txn)
508 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
510 ''' % {'s': structName,
513 'T': tableName.upper()}
516 for columnName, column in table.columns.iteritems():
519 %(s)s_verify_%(c)s(const struct %(s)s *row)
521 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
522 }''' % {'s': structName,
523 'S': structName.upper(),
525 'C': columnName.upper()}
528 for columnName, column in table.columns.iteritems():
531 members = cMembers(prefix, columnName, column, True)
532 keyVar = members[0]['name']
536 valueVar = members[1]['name']
538 nVar = members[2]['name']
541 nVar = members[1]['name']
542 print '%(s)s_set_%(c)s(const struct %(s)s *row_, %(args)s)' % \
543 {'s': structName, 'c': columnName,
544 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
546 print " struct %(s)s *row = (struct %(s)s *) row_;" % {'s': structName}
547 print " struct ovsdb_datum datum;"
548 if type.min == 1 and type.max == 1:
550 print " datum.n = 1;"
551 print " datum.keys = xmalloc(sizeof *datum.keys);"
552 print cCopyType(" ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
554 print " datum.values = xmalloc(sizeof *datum.values);"
555 print cCopyType(" ", "row->%s" % valueVar, "datum.values[0].%s" % type.value, valueVar, type.value, type.valueRefTable)
557 print " datum.values = NULL;"
558 elif typeIsOptionalPointer(type):
560 print " if (%s) {" % keyVar
561 print " datum.n = 1;"
562 print " datum.keys = xmalloc(sizeof *datum.keys);"
563 print cCopyType(" ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
565 print " datum.n = 0;"
566 print " datum.keys = NULL;"
567 print " row->%s = NULL;" % keyVar
569 print " datum.values = NULL;"
573 print " free(row->%s);" % keyVar
574 print " row->%s = %s ? xmalloc(%s * sizeof *row->%s) : NULL;" % (keyVar, nVar, nVar, keyVar)
575 print " row->%s = %s;" % (nVar, nVar)
577 print " free(row->%s);" % valueVar
578 print " row->%s = xmalloc(%s * sizeof *row->%s);" % (valueVar, nVar, valueVar)
579 print " datum.n = %s;" % nVar
580 print " datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar
582 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
584 print " datum.values = NULL;"
585 print " for (i = 0; i < %s; i++) {" % nVar
586 print cCopyType(" ", "row->%s[i]" % keyVar, "datum.keys[i].%s" % type.key, "%s[i]" % keyVar, type.key, type.keyRefTable)
588 print cCopyType(" ", "row->%s[i]" % valueVar, "datum.values[i].%s" % type.value, "%s[i]" % valueVar, type.value, type.valueRefTable)
590 print " ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
592 'S': structName.upper(),
593 'C': columnName.upper()}
597 print "\nstatic struct ovsdb_idl_column %s_columns[%s_N_COLUMNS] = {" % (
598 structName, structName.upper())
599 for columnName, column in table.columns.iteritems():
603 valueTypeName = type.value.upper()
605 valueTypeName = "VOID"
606 if type.max == "unlimited":
610 print " {\"%s\", {OVSDB_TYPE_%s, OVSDB_TYPE_%s, %d, %s}}," % (
611 columnName, type.key.upper(), valueTypeName,
617 print "static struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
618 for tableName, table in schema.tables.iteritems():
619 structName = "%s%s" % (prefix, tableName.lower())
620 print " {\"%s\"," % tableName
621 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
622 structName, structName)
623 print " sizeof(struct %s)," % structName
624 print " %s_parse," % structName
625 print " %s_unparse}," % structName
629 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
630 print " %stable_classes, ARRAY_SIZE(%stable_classes)" % (prefix, prefix)
633 def ovsdb_escape(string):
637 raise Error("strings may not contain null bytes")
651 return '\\x%02x' % ord(c)
652 return re.sub(r'["\\\000-\037]', escape, string)
654 def printOVSDBSchema(schema):
655 json.dump(schema.toJson(), sys.stdout, sort_keys=True, indent=2)
657 def printDoc(schema):
662 for tableName, table in schema.tables.iteritems():
663 title = "%s table" % tableName
666 print '-' * len(title)
670 for columnName, column in table.columns.iteritems():
672 print "%s (%s)" % (columnName, column.type.toEnglish())
674 print "\t%s" % column.comment
678 %(argv0)s: ovsdb schema compiler
679 usage: %(argv0)s [OPTIONS] ACTION SCHEMA
680 where SCHEMA is the ovsdb schema to read (in JSON format).
682 One of the following actions must specified:
683 validate validate schema without taking any other action
684 c-idl-header print C header file for IDL
685 c-idl-source print C source file for IDL implementation
686 ovsdb-schema print ovsdb parseable schema
687 doc print schema documentation
689 The following options are also available:
690 -h, --help display this help message
691 -V, --version display version information\
692 """ % {'argv0': argv0}
695 if __name__ == "__main__":
698 options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
701 except getopt.GetoptError, geo:
702 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
705 optKeys = [key for key, value in options]
706 if '-h' in optKeys or '--help' in optKeys:
708 elif '-V' in optKeys or '--version' in optKeys:
709 print "ovsdb-idlc (Open vSwitch) @VERSION@"
713 sys.stderr.write("%s: exactly two non-option arguments are "
714 "required (use --help for help)\n" % argv0)
717 action, inputFile = args
718 schema = parseSchema(inputFile)
719 if action == 'validate':
721 elif action == 'ovsdb-schema':
722 printOVSDBSchema(schema)
723 elif action == 'c-idl-header':
724 printCIDLHeader(schema)
725 elif action == 'c-idl-source':
726 printCIDLSource(schema)
727 elif action == 'doc':
731 "%s: unknown action '%s' (use --help for help)\n" %
735 sys.stderr.write("%s: %s\n" % (argv0, e.msg))