mirror of
https://github.com/openvswitch/ovs
synced 2025-10-19 14:37:21 +00:00
ovsdb-idlc: Add infrastructure for IDL schema extensions.
An IDL schema is an OVSDB schema with some extra stuff in it. So far, all of the extras have been at the top level. This commit makes it possible for IDL schemas to have extra information at the table and column levels as long as it is in an "extensions" member. No extensions are actually supported yet. Signed-off-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
@@ -32,7 +32,7 @@ def _check_id(name, json):
|
||||
class DbSchema(object):
|
||||
"""Schema for an OVSDB database."""
|
||||
|
||||
def __init__(self, name, version, tables):
|
||||
def __init__(self, name, version, tables, allow_extensions=False):
|
||||
self.name = name
|
||||
self.version = version
|
||||
self.tables = tables
|
||||
@@ -64,7 +64,7 @@ class DbSchema(object):
|
||||
return n_root
|
||||
|
||||
@staticmethod
|
||||
def from_json(json):
|
||||
def from_json(json, allow_extensions=False):
|
||||
parser = ovs.db.parser.Parser(json, "database schema")
|
||||
name = parser.get("name", ['id'])
|
||||
version = parser.get_optional("version", six.string_types)
|
||||
@@ -80,7 +80,8 @@ class DbSchema(object):
|
||||
tables = {}
|
||||
for tableName, tableJson in six.iteritems(tablesJson):
|
||||
_check_id(tableName, json)
|
||||
tables[tableName] = TableSchema.from_json(tableJson, tableName)
|
||||
tables[tableName] = TableSchema.from_json(tableJson, tableName,
|
||||
allow_extensions)
|
||||
|
||||
return DbSchema(name, version, tables)
|
||||
|
||||
@@ -146,7 +147,7 @@ class IdlSchema(DbSchema):
|
||||
del subjson["idlHeader"]
|
||||
subjson.pop("cDecls", None)
|
||||
subjson.pop("hDecls", None)
|
||||
schema = DbSchema.from_json(subjson)
|
||||
schema = DbSchema.from_json(subjson, allow_extensions=True)
|
||||
|
||||
return IdlSchema(schema.name, schema.version, schema.tables,
|
||||
idlPrefix, idlHeader, cDecls, hDecls)
|
||||
@@ -173,22 +174,27 @@ def column_set_from_json(json, columns):
|
||||
|
||||
class TableSchema(object):
|
||||
def __init__(self, name, columns, mutable=True, max_rows=sys.maxsize,
|
||||
is_root=True, indexes=[]):
|
||||
is_root=True, indexes=[], extensions={}):
|
||||
self.name = name
|
||||
self.columns = columns
|
||||
self.mutable = mutable
|
||||
self.max_rows = max_rows
|
||||
self.is_root = is_root
|
||||
self.indexes = indexes
|
||||
self.extensions = extensions
|
||||
|
||||
@staticmethod
|
||||
def from_json(json, name):
|
||||
def from_json(json, name, allow_extensions=False):
|
||||
parser = ovs.db.parser.Parser(json, "table schema for table %s" % name)
|
||||
columns_json = parser.get("columns", [dict])
|
||||
mutable = parser.get_optional("mutable", [bool], True)
|
||||
max_rows = parser.get_optional("maxRows", [int])
|
||||
is_root = parser.get_optional("isRoot", [bool], False)
|
||||
indexes_json = parser.get_optional("indexes", [list], [])
|
||||
if allow_extensions:
|
||||
extensions = parser.get_optional("extensions", [dict], {})
|
||||
else:
|
||||
extensions = {}
|
||||
parser.finish()
|
||||
|
||||
if max_rows is None:
|
||||
@@ -203,7 +209,8 @@ class TableSchema(object):
|
||||
for column_name, column_json in six.iteritems(columns_json):
|
||||
_check_id(column_name, json)
|
||||
columns[column_name] = ColumnSchema.from_json(column_json,
|
||||
column_name)
|
||||
column_name,
|
||||
allow_extensions)
|
||||
|
||||
indexes = []
|
||||
for index_json in indexes_json:
|
||||
@@ -218,7 +225,8 @@ class TableSchema(object):
|
||||
"not be indexed" % column.name, json)
|
||||
indexes.append(index)
|
||||
|
||||
return TableSchema(name, columns, mutable, max_rows, is_root, indexes)
|
||||
return TableSchema(name, columns, mutable, max_rows, is_root, indexes,
|
||||
extensions)
|
||||
|
||||
def to_json(self, default_is_root=False):
|
||||
"""Returns this table schema serialized into JSON.
|
||||
@@ -255,21 +263,26 @@ class TableSchema(object):
|
||||
|
||||
|
||||
class ColumnSchema(object):
|
||||
def __init__(self, name, mutable, persistent, type_):
|
||||
def __init__(self, name, mutable, persistent, type_, extensions={}):
|
||||
self.name = name
|
||||
self.mutable = mutable
|
||||
self.persistent = persistent
|
||||
self.type = type_
|
||||
self.unique = False
|
||||
self.extensions = extensions
|
||||
|
||||
@staticmethod
|
||||
def from_json(json, name):
|
||||
def from_json(json, name, allow_extensions=False):
|
||||
parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
|
||||
mutable = parser.get_optional("mutable", [bool], True)
|
||||
ephemeral = parser.get_optional("ephemeral", [bool], False)
|
||||
_types = list(six.string_types)
|
||||
_types.extend([dict])
|
||||
type_ = ovs.db.types.Type.from_json(parser.get("type", _types))
|
||||
if allow_extensions:
|
||||
extensions = parser.get_optional("extensions", [dict], {})
|
||||
else:
|
||||
extensions = {}
|
||||
parser.finish()
|
||||
|
||||
if not mutable and (type_.key.is_weak_ref()
|
||||
@@ -278,7 +291,7 @@ class ColumnSchema(object):
|
||||
# rows are deleted, then the weak reference needs to change.
|
||||
mutable = True
|
||||
|
||||
return ColumnSchema(name, mutable, not ephemeral, type_)
|
||||
return ColumnSchema(name, mutable, not ephemeral, type_, extensions)
|
||||
|
||||
def to_json(self):
|
||||
json = {"type": self.type.to_json()}
|
||||
@@ -286,4 +299,6 @@ class ColumnSchema(object):
|
||||
json["mutable"] = False
|
||||
if not self.persistent:
|
||||
json["ephemeral"] = True
|
||||
if self.extensions:
|
||||
json["extensions"] = self.extensions
|
||||
return json
|
||||
|
Reference in New Issue
Block a user