idl: Convert python daemons to utilize SchemaHelper.
authorEthan Jackson <ethan@nicira.com>
Sat, 3 Mar 2012 01:50:59 +0000 (17:50 -0800)
committerEthan Jackson <ethan@nicira.com>
Fri, 9 Mar 2012 21:37:39 +0000 (13:37 -0800)
The recently added SchemaHelper class significantly simplifies IDL
instantiation in Python.  This commit converts all users of the old
method to the new method, and removes support for the old method.

Signed-off-by: Ethan Jackson <ethan@nicira.com>
debian/ovs-monitor-ipsec
python/ovs/db/idl.py
tests/test-ovsdb.py
xenserver/usr_share_openvswitch_scripts_ovs-xapi-sync

index e89e709..87a1491 100755 (executable)
@@ -352,49 +352,6 @@ class IPsec:
             self.entries.remove(remote_ip)
 
 
-def keep_table_columns(schema, table_name, column_types):
-    table = schema.tables.get(table_name)
-    if not table:
-        raise error.Error("schema has no %s table" % table_name)
-
-    new_columns = {}
-    for column_name, column_type in column_types.iteritems():
-        column = table.columns.get(column_name)
-        if not column:
-            raise error.Error("%s table schema lacks %s column"
-                              % (table_name, column_name))
-        if column.type != column_type:
-            raise error.Error("%s column in %s table has type \"%s\", "
-                              "expected type \"%s\""
-                              % (column_name, table_name,
-                                 column.type.toEnglish(),
-                                 column_type.toEnglish()))
-        new_columns[column_name] = column
-    table.columns = new_columns
-    return table
-
-
-def prune_schema(schema):
-    string_type = types.Type(types.BaseType(types.StringType))
-    optional_ssl_type = types.Type(types.BaseType(types.UuidType,
-        ref_table_name='SSL'), None, 0, 1)
-    string_map_type = types.Type(types.BaseType(types.StringType),
-                                 types.BaseType(types.StringType),
-                                 0, sys.maxint)
-
-    new_tables = {}
-    new_tables["Interface"] = keep_table_columns(
-        schema, "Interface", {"name": string_type,
-                              "type": string_type,
-                              "options": string_map_type})
-    new_tables["Open_vSwitch"] = keep_table_columns(
-        schema, "Open_vSwitch", {"ssl": optional_ssl_type})
-    new_tables["SSL"] = keep_table_columns(
-        schema, "SSL", {"certificate": string_type,
-                        "private_key": string_type})
-    schema.tables = new_tables
-
-
 def update_ipsec(ipsec, interfaces, new_interfaces):
     for name, vals in interfaces.iteritems():
         if name not in new_interfaces:
@@ -448,10 +405,11 @@ def main():
         root_prefix = args.root_prefix
 
     remote = args.database
-    schema_file = "%s/vswitch.ovsschema" % ovs.dirs.PKGDATADIR
-    schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
-    prune_schema(schema)
-    idl = ovs.db.idl.Idl(remote, schema)
+    schema_helper = ovs.db.idl.SchemaHelper()
+    schema_helper.register_columns("Interface", ["name", "type", "options"])
+    schema_helper.register_columns("Open_vSwitch", ["ssl"])
+    schema_helper.register_columns("SSL", ["certificate", "private_key"])
+    idl = ovs.db.idl.Idl(remote, schema_helper)
 
     ovs.daemon.daemonize()
 
index b33d9f8..5639120 100644 (file)
@@ -93,8 +93,8 @@ class Idl:
 
         The IDL uses and modifies 'schema' directly."""
 
-        if isinstance(schema, SchemaHelper):
-            schema = schema.get_idl_schema()
+        assert isinstance(schema, SchemaHelper)
+        schema = schema.get_idl_schema()
 
         self.tables = schema.tables
         self._db = schema
@@ -1100,6 +1100,7 @@ class SchemaHelper(object):
 
         self.schema_location = location
         self._tables = {}
+        self._all = False
 
     def register_columns(self, table, columns):
         """Registers interest in the given 'columns' of 'table'.  Future calls
@@ -1117,6 +1118,10 @@ class SchemaHelper(object):
         columns = set(columns) | self._tables.get(table, set())
         self._tables[table] = columns
 
+    def register_all(self):
+        """Registers interest in every column of every table."""
+        self._all = True
+
     def get_idl_schema(self):
         """Gets a schema appropriate for the creation of an 'ovs.db.id.IDL'
         object based on columns registered using the register_columns()
@@ -1124,12 +1129,14 @@ class SchemaHelper(object):
 
         schema = ovs.db.schema.DbSchema.from_json(
             ovs.json.from_file(self.schema_location))
-        schema_tables = {}
-        for table, columns in self._tables.iteritems():
-            schema_tables[table] = (
-                self._keep_table_columns(schema, table, columns))
 
-        schema.tables = schema_tables
+        if not self._all:
+            schema_tables = {}
+            for table, columns in self._tables.iteritems():
+                schema_tables[table] = (
+                    self._keep_table_columns(schema, table, columns))
+
+            schema.tables = schema_tables
         return schema
 
     def _keep_table_columns(self, schema, table_name, columns):
index 5f3cb99..b0e42a3 100644 (file)
@@ -320,8 +320,9 @@ def idl_set(idl, commands, step):
 
 
 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(
index eea319a..7c78251 100755 (executable)
@@ -205,32 +205,6 @@ def update_in_band_mgmt(row):
         row.other_config = other_config
 
 
-def keep_table_columns(schema, table_name, columns):
-    table = schema.tables.get(table_name)
-    if not table:
-        raise error.Error("schema has no %s table" % table_name)
-
-    new_columns = {}
-    for column_name in columns:
-        column = table.columns.get(column_name)
-        if not column:
-            raise error.Error("%s table schema lacks %s column"
-                              % (table_name, column_name))
-        new_columns[column_name] = column
-    table.columns = new_columns
-    return table
-
-
-def prune_schema(schema):
-    new_tables = {}
-    new_tables["Bridge"] = keep_table_columns(
-        schema, "Bridge", ("name", "external_ids", "other_config",
-                           "fail_mode"))
-    new_tables["Interface"] = keep_table_columns(
-        schema, "Interface", ("name", "external_ids"))
-    schema.tables = new_tables
-
-
 def main():
     global flush_cache
 
@@ -248,10 +222,11 @@ def main():
     ovs.daemon.handle_args(args)
 
     remote = args.database
-    schema_file = "%s/vswitch.ovsschema" % ovs.dirs.PKGDATADIR
-    schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
-    prune_schema(schema)
-    idl = ovs.db.idl.Idl(remote, schema)
+    schema_helper = ovs.db.idl.SchemaHelper()
+    schema_helper.register_columns("Bridge", ["name", "external_ids",
+                                              "other_config", "fail_mode"])
+    schema_helper.register_columns("Interface", ["name", "external_ids"])
+    idl = ovs.db.idl.Idl(remote, schema_helper)
 
     ovs.daemon.daemonize()