2
0
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:
Ben Pfaff
2016-10-07 13:35:29 -07:00
parent 0ea866bbc0
commit 00386a6921
2 changed files with 38 additions and 11 deletions

View File

@@ -1,4 +1,9 @@
.\" -*- nroff -*- .\" -*- nroff -*-
.de IQ
. br
. ns
. IP "\\$1"
..
.TH ovsdb\-idlc 1 "November 2009" "Open vSwitch" "Open vSwitch Manual" .TH ovsdb\-idlc 1 "November 2009" "Open vSwitch" "Open vSwitch Manual"
.ds PN ovsdb\-idlc .ds PN ovsdb\-idlc
. .
@@ -45,6 +50,13 @@ These optional members may specify arbitrary code to include in the
generated \fB.c\fR or \fB.h\fR file, respectively, in each case just generated \fB.c\fR or \fB.h\fR file, respectively, in each case just
after the \fB#include\fR directives in those files. after the \fB#include\fR directives in those files.
. .
.IP "\fB""\fBextensions\fR"" member of <table-schema>"
.IQ "\fB""\fBextensions\fR"" member of <column-schema>"
This member is optional. If specified, it is an object whose contents
describes extensions to the OVSDB schema language, for the purpose of
specifying interpretation by the IDL.
No extensions are supported yet.
.
.SS "Commands" .SS "Commands"
.IP "\fBannotate\fI schema annotations\fR" .IP "\fBannotate\fI schema annotations\fR"
Reads \fIschema\fR, which should be a file in JSON format (ordinarily Reads \fIschema\fR, which should be a file in JSON format (ordinarily

View File

@@ -32,7 +32,7 @@ def _check_id(name, json):
class DbSchema(object): class DbSchema(object):
"""Schema for an OVSDB database.""" """Schema for an OVSDB database."""
def __init__(self, name, version, tables): def __init__(self, name, version, tables, allow_extensions=False):
self.name = name self.name = name
self.version = version self.version = version
self.tables = tables self.tables = tables
@@ -64,7 +64,7 @@ class DbSchema(object):
return n_root return n_root
@staticmethod @staticmethod
def from_json(json): def from_json(json, allow_extensions=False):
parser = ovs.db.parser.Parser(json, "database schema") parser = ovs.db.parser.Parser(json, "database schema")
name = parser.get("name", ['id']) name = parser.get("name", ['id'])
version = parser.get_optional("version", six.string_types) version = parser.get_optional("version", six.string_types)
@@ -80,7 +80,8 @@ class DbSchema(object):
tables = {} tables = {}
for tableName, tableJson in six.iteritems(tablesJson): for tableName, tableJson in six.iteritems(tablesJson):
_check_id(tableName, json) _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) return DbSchema(name, version, tables)
@@ -146,7 +147,7 @@ class IdlSchema(DbSchema):
del subjson["idlHeader"] del subjson["idlHeader"]
subjson.pop("cDecls", None) subjson.pop("cDecls", None)
subjson.pop("hDecls", 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, return IdlSchema(schema.name, schema.version, schema.tables,
idlPrefix, idlHeader, cDecls, hDecls) idlPrefix, idlHeader, cDecls, hDecls)
@@ -173,22 +174,27 @@ def column_set_from_json(json, columns):
class TableSchema(object): class TableSchema(object):
def __init__(self, name, columns, mutable=True, max_rows=sys.maxsize, def __init__(self, name, columns, mutable=True, max_rows=sys.maxsize,
is_root=True, indexes=[]): is_root=True, indexes=[], extensions={}):
self.name = name self.name = name
self.columns = columns self.columns = columns
self.mutable = mutable self.mutable = mutable
self.max_rows = max_rows self.max_rows = max_rows
self.is_root = is_root self.is_root = is_root
self.indexes = indexes self.indexes = indexes
self.extensions = extensions
@staticmethod @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) parser = ovs.db.parser.Parser(json, "table schema for table %s" % name)
columns_json = parser.get("columns", [dict]) columns_json = parser.get("columns", [dict])
mutable = parser.get_optional("mutable", [bool], True) mutable = parser.get_optional("mutable", [bool], True)
max_rows = parser.get_optional("maxRows", [int]) max_rows = parser.get_optional("maxRows", [int])
is_root = parser.get_optional("isRoot", [bool], False) is_root = parser.get_optional("isRoot", [bool], False)
indexes_json = parser.get_optional("indexes", [list], []) indexes_json = parser.get_optional("indexes", [list], [])
if allow_extensions:
extensions = parser.get_optional("extensions", [dict], {})
else:
extensions = {}
parser.finish() parser.finish()
if max_rows is None: if max_rows is None:
@@ -203,7 +209,8 @@ class TableSchema(object):
for column_name, column_json in six.iteritems(columns_json): for column_name, column_json in six.iteritems(columns_json):
_check_id(column_name, json) _check_id(column_name, json)
columns[column_name] = ColumnSchema.from_json(column_json, columns[column_name] = ColumnSchema.from_json(column_json,
column_name) column_name,
allow_extensions)
indexes = [] indexes = []
for index_json in indexes_json: for index_json in indexes_json:
@@ -218,7 +225,8 @@ class TableSchema(object):
"not be indexed" % column.name, json) "not be indexed" % column.name, json)
indexes.append(index) 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): def to_json(self, default_is_root=False):
"""Returns this table schema serialized into JSON. """Returns this table schema serialized into JSON.
@@ -255,21 +263,26 @@ class TableSchema(object):
class ColumnSchema(object): class ColumnSchema(object):
def __init__(self, name, mutable, persistent, type_): def __init__(self, name, mutable, persistent, type_, extensions={}):
self.name = name self.name = name
self.mutable = mutable self.mutable = mutable
self.persistent = persistent self.persistent = persistent
self.type = type_ self.type = type_
self.unique = False self.unique = False
self.extensions = extensions
@staticmethod @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) parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
mutable = parser.get_optional("mutable", [bool], True) mutable = parser.get_optional("mutable", [bool], True)
ephemeral = parser.get_optional("ephemeral", [bool], False) ephemeral = parser.get_optional("ephemeral", [bool], False)
_types = list(six.string_types) _types = list(six.string_types)
_types.extend([dict]) _types.extend([dict])
type_ = ovs.db.types.Type.from_json(parser.get("type", _types)) type_ = ovs.db.types.Type.from_json(parser.get("type", _types))
if allow_extensions:
extensions = parser.get_optional("extensions", [dict], {})
else:
extensions = {}
parser.finish() parser.finish()
if not mutable and (type_.key.is_weak_ref() 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. # rows are deleted, then the weak reference needs to change.
mutable = True mutable = True
return ColumnSchema(name, mutable, not ephemeral, type_) return ColumnSchema(name, mutable, not ephemeral, type_, extensions)
def to_json(self): def to_json(self):
json = {"type": self.type.to_json()} json = {"type": self.type.to_json()}
@@ -286,4 +299,6 @@ class ColumnSchema(object):
json["mutable"] = False json["mutable"] = False
if not self.persistent: if not self.persistent:
json["ephemeral"] = True json["ephemeral"] = True
if self.extensions:
json["extensions"] = self.extensions
return json return json