idl: Convert python daemons to utilize SchemaHelper.
[sliver-openvswitch.git] / tests / test-ovsdb.py
index dbdd8f8..b0e42a3 100644 (file)
@@ -28,12 +28,14 @@ import ovs.ovsuuid
 import ovs.poller
 import ovs.util
 
+
 def unbox_json(json):
     if type(json) == list and len(json) == 1:
         return json[0]
     else:
         return json
 
+
 def do_default_atoms():
     for type_ in types.ATOMIC_TYPES:
         if type_ == types.VoidType:
@@ -48,6 +50,7 @@ def do_default_atoms():
 
         sys.stdout.write("OK\n")
 
+
 def do_default_data():
     any_errors = False
     for n_min in 0, 1:
@@ -74,21 +77,25 @@ def do_default_data():
     if any_errors:
         sys.exit(1)
 
+
 def do_parse_atomic_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
     atomic_type = types.AtomicType.from_json(type_json)
     print ovs.json.to_string(atomic_type.to_json(), sort_keys=True)
 
+
 def do_parse_base_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
     base_type = types.BaseType.from_json(type_json)
     print ovs.json.to_string(base_type.to_json(), sort_keys=True)
 
+
 def do_parse_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
     type_ = types.Type.from_json(type_json)
     print ovs.json.to_string(type_.to_json(), sort_keys=True)
 
+
 def do_parse_atoms(type_string, *atom_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
     base = types.BaseType.from_json(type_json)
@@ -100,6 +107,7 @@ def do_parse_atoms(type_string, *atom_strings):
         except error.Error, e:
             print unicode(e)
 
+
 def do_parse_data(type_string, *data_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
     type_ = types.Type.from_json(type_json)
@@ -108,6 +116,7 @@ def do_parse_data(type_string, *data_strings):
         datum = data.Datum.from_json(type_, datum_json)
         print ovs.json.to_string(datum.to_json())
 
+
 def do_sort_atoms(type_string, atom_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
     base = types.BaseType.from_json(type_json)
@@ -116,22 +125,26 @@ def do_sort_atoms(type_string, atom_strings):
     print ovs.json.to_string([data.Atom.to_json(atom)
                               for atom in sorted(atoms)])
 
+
 def do_parse_column(name, column_string):
     column_json = unbox_json(ovs.json.from_string(column_string))
     column = ovs.db.schema.ColumnSchema.from_json(column_json, name)
     print ovs.json.to_string(column.to_json(), sort_keys=True)
 
+
 def do_parse_table(name, table_string, default_is_root_string='false'):
     default_is_root = default_is_root_string == 'true'
     table_json = unbox_json(ovs.json.from_string(table_string))
     table = ovs.db.schema.TableSchema.from_json(table_json, name)
     print ovs.json.to_string(table.to_json(default_is_root), sort_keys=True)
 
+
 def do_parse_schema(schema_string):
     schema_json = unbox_json(ovs.json.from_string(schema_string))
     schema = ovs.db.schema.DbSchema.from_json(schema_json)
     print ovs.json.to_string(schema.to_json(), sort_keys=True)
 
+
 def print_idl(idl, step):
     simple = idl.tables["simple"].rows
     l1 = idl.tables["link1"].rows
@@ -176,6 +189,7 @@ def print_idl(idl, step):
         print("%03d: empty" % step)
     sys.stdout.flush()
 
+
 def substitute_uuids(json, symtab):
     if type(json) in [str, unicode]:
         symbol = symtab.get(json)
@@ -190,6 +204,7 @@ def substitute_uuids(json, symtab):
         return d
     return json
 
+
 def parse_uuids(json, symtab):
     if type(json) in [str, unicode] and ovs.ovsuuid.is_valid_string(json):
         name = "#%d#" % len(symtab)
@@ -202,12 +217,14 @@ def parse_uuids(json, symtab):
         for value in json.itervalues():
             parse_uuids(value, symtab)
 
+
 def idltest_find_simple(idl, i):
     for row in idl.tables["simple"].rows.itervalues():
         if row.i == i:
             return row
     return None
 
+
 def idl_set(idl, commands, step):
     txn = ovs.db.idl.Transaction(idl)
     increment = False
@@ -301,9 +318,11 @@ def idl_set(idl, commands, step):
     sys.stdout.write("\n")
     sys.stdout.flush()
 
+
 def do_idl(schema_file, remote, *commands):
-    schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
-    idl = ovs.db.idl.Idl(remote, schema)
+    schema_helper = ovs.db.idl.SchemaHelper(schema_file)
+    schema_helper.register_all()
+    idl = ovs.db.idl.Idl(remote, schema_helper)
 
     if commands:
         error, stream = ovs.stream.Stream.open_block(
@@ -331,7 +350,7 @@ def do_idl(schema_file, remote, *commands):
                 idl.wait(poller)
                 rpc.wait(poller)
                 poller.block()
-                
+
             print_idl(idl, step)
             step += 1
 
@@ -357,6 +376,11 @@ def do_idl(schema_file, remote, *commands):
                 sys.stderr.write("jsonrpc transaction failed: %s"
                                  % os.strerror(error))
                 sys.exit(1)
+            elif reply.error is not None:
+                sys.stderr.write("jsonrpc transaction failed: %s"
+                                 % reply.error)
+                sys.exit(1)
+
             sys.stdout.write("%03d: " % step)
             sys.stdout.flush()
             step += 1
@@ -377,6 +401,7 @@ def do_idl(schema_file, remote, *commands):
     idl.close()
     print("%03d: done" % step)
 
+
 def usage():
     print """\
 %(program_name)s: test utility for Open vSwitch database Python bindings
@@ -419,6 +444,7 @@ The following options are also available:
 """ % {'program_name': ovs.util.PROGRAM_NAME}
     sys.exit(0)
 
+
 def main(argv):
     try:
         options, args = getopt.gnu_getopt(argv[1:], 't:h',
@@ -489,6 +515,7 @@ def main(argv):
 
     func(*args)
 
+
 if __name__ == '__main__':
     try:
         main(sys.argv)