Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ovsdb / ovsdb-dot.in
1 #! @PYTHON@
2
3 from datetime import date
4 import ovs.db.error
5 import ovs.db.schema
6 import getopt
7 import os
8 import re
9 import sys
10
11 argv0 = sys.argv[0]
12
13 def printEdge(tableName, baseType, label):
14     if baseType.ref_table:
15         options = {}
16         options['label'] = '"%s"' % label
17         if baseType.ref_type == 'weak':
18             options['constraint'] = 'false'
19             options['style'] = 'dotted'
20         print "\t%s -> %s [%s];" % (
21             tableName,
22             baseType.ref_table,
23             ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))
24
25 def schemaToDot(schemaFile):
26     schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schemaFile))
27
28     print "digraph %s {" % schema.name
29     print '\tsize="6.5,4";'
30     print '\tmargin="0";'
31     print "\tnode [shape=box];"
32     print "\tedge [dir=none, arrowhead=none, arrowtail=none];"
33     for tableName, table in schema.tables.iteritems():
34         options = {}
35         if table.is_root:
36             options['style'] = 'bold'
37         print "\t%s [%s];" % (
38             tableName,
39             ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))
40         for columnName, column in table.columns.iteritems():
41             if column.type.value:
42                 printEdge(tableName, column.type.key, "%s key" % columnName)
43                 printEdge(tableName, column.type.value, "%s value" % columnName)
44             else:
45                 printEdge(tableName, column.type.key, columnName)
46     print "}";
47
48 def usage():
49     print """\
50 %(argv0)s: compiles ovsdb schemas to graphviz format
51 Prints a .dot file that "dot" can render to an entity-relationship diagram
52 usage: %(argv0)s [OPTIONS] SCHEMA
53 where SCHEMA is an OVSDB schema in JSON format
54
55 The following options are also available:
56   -h, --help                  display this help message
57   -V, --version               display version information\
58 """ % {'argv0': argv0}
59     sys.exit(0)
60
61 if __name__ == "__main__":
62     try:
63         try:
64             options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
65                                               ['help', 'version'])
66         except getopt.GetoptError, geo:
67             sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
68             sys.exit(1)
69
70         for key, value in options:
71             if key in ['-h', '--help']:
72                 usage()
73             elif key in ['-V', '--version']:
74                 print "ovsdb-dot (Open vSwitch) @VERSION@"
75             else:
76                 sys.exit(0)
77
78         if len(args) != 1:
79             sys.stderr.write("%s: exactly 1 non-option argument required "
80                              "(use --help for help)\n" % argv0)
81             sys.exit(1)
82
83         schemaToDot(args[0])
84
85     except ovs.db.error.Error, e:
86         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
87         sys.exit(1)
88
89 # Local variables:
90 # mode: python
91 # End: