Skip to content

Commit

Permalink
python: Implement write support in Python IDL for OVSDB.
Browse files Browse the repository at this point in the history
Until now, the Python bindings for OVSDB have not supported writing to the
database.  Instead, writes had to be done with "ovs-vsctl" subprocesses.
This commit adds write support and brings the Python bindings in line with
the C bindings.

This commit deletes the Python-specific IDL tests in favor of using the
same tests as the C version of the IDL, which now pass with both
implementations.

This commit updates the two users of the Python IDL to use the new write
support.  I tested this updates only by writing unit tests for them,
which appear in upcoming commits.
  • Loading branch information
blp committed Sep 23, 2011
1 parent 7cba02e commit 8cdf034
Show file tree
Hide file tree
Showing 14 changed files with 1,369 additions and 489 deletions.
46 changes: 27 additions & 19 deletions debian/ovs-monitor-ipsec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import socket
import subprocess
import sys

import ovs.dirs
from ovs.db import error
from ovs.db import types
import ovs.util
Expand Down Expand Up @@ -376,7 +377,7 @@ def keep_table_columns(schema, table_name, column_types):
table.columns = new_columns
return table

def monitor_uuid_schema_cb(schema):
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)
Expand Down Expand Up @@ -425,25 +426,27 @@ def update_ipsec(ipsec, interfaces, new_interfaces):
s_log.warning("skipping ipsec config for %s: %s" % (name, msg))

def get_ssl_cert(data):
for ovs_rec in data["Open_vSwitch"].itervalues():
if ovs_rec.ssl.as_list():
ssl_rec = data["SSL"][ovs_rec.ssl.as_scalar()]
return (ssl_rec.certificate.as_scalar(),
ssl_rec.private_key.as_scalar())
for ovs_rec in data["Open_vSwitch"].rows.itervalues():
ssl = ovs_rec.ssl
if ssl and ssl.certificate and ssl.private_key:
return (ssl.certificate, ssl.private_key)

return None

def main(argv):
try:
options, args = getopt.gnu_getopt(
argv[1:], 'h', ['help'] + ovs.daemon.LONG_OPTIONS)
argv[1:], 'h', ['help', 'root-prefix='] + ovs.daemon.LONG_OPTIONS)
except getopt.GetoptError, geo:
sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
sys.exit(1)

for key, value in options:
if key in ['-h', '--help']:
usage()
elif key == '--root-prefix':
global root_prefix
root_prefix = value
elif not ovs.daemon.parse_opt(key, value):
sys.stderr.write("%s: unhandled option %s\n"
% (ovs.util.PROGRAM_NAME, key))
Expand All @@ -455,7 +458,11 @@ def main(argv):
sys.exit(1)

remote = args[0]
idl = ovs.db.idl.Idl(remote, "Open_vSwitch", monitor_uuid_schema_cb)

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)

ovs.daemon.daemonize()

Expand All @@ -469,20 +476,21 @@ def main(argv):
poller.block()
continue

ssl_cert = get_ssl_cert(idl.data)
ssl_cert = get_ssl_cert(idl.tables)

new_interfaces = {}
for rec in idl.data["Interface"].itervalues():
if rec.type.as_scalar() == "ipsec_gre":
name = rec.name.as_scalar()
for rec in idl.tables["Interface"].rows.itervalues():
if rec.type == "ipsec_gre":
name = rec.name
options = rec.options
entry = {
"remote_ip": rec.options.get("remote_ip"),
"local_ip": rec.options.get("local_ip", "0.0.0.0/0"),
"certificate": rec.options.get("certificate"),
"private_key": rec.options.get("private_key"),
"use_ssl_cert": rec.options.get("use_ssl_cert"),
"peer_cert": rec.options.get("peer_cert"),
"psk": rec.options.get("psk") }
"remote_ip": options.get("remote_ip"),
"local_ip": options.get("local_ip", "0.0.0.0/0"),
"certificate": options.get("certificate"),
"private_key": options.get("private_key"),
"use_ssl_cert": options.get("use_ssl_cert"),
"peer_cert": options.get("peer_cert"),
"psk": options.get("psk") }

if entry["peer_cert"] and entry["psk"]:
s_log.warning("both 'peer_cert' and 'psk' defined for %s"
Expand Down
5 changes: 4 additions & 1 deletion ovsdb/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ EXTRA_DIST += \
ovsdb/ovsdb-idlc.in \
ovsdb/ovsdb-idlc.1
DISTCLEANFILES += ovsdb/ovsdb-idlc
SUFFIXES += .ovsidl
SUFFIXES += .ovsidl .ovsschema .py
OVSDB_IDLC = $(run_python) $(srcdir)/ovsdb/ovsdb-idlc.in
.ovsidl.c:
$(OVSDB_IDLC) c-idl-source $< > $@.tmp
mv $@.tmp $@
.ovsidl.h:
$(OVSDB_IDLC) c-idl-header $< > $@.tmp
mv $@.tmp $@
.ovsschema.py:
$(OVSDB_IDLC) python-module $< > $@.tmp
mv $@.tmp $@

EXTRA_DIST += $(OVSIDL_BUILT)
BUILT_SOURCES += $(OVSIDL_BUILT)
Expand Down
21 changes: 18 additions & 3 deletions ovsdb/ovsdb-idlc.in
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ void
print " %s_columns_init();" % structName
print "}"

def print_python_module(schema_file):
schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
print """\
# Generated automatically -- do not modify! -*- buffer-read-only: t -*-

import ovs.db.schema
import ovs.json

__schema_json = \"\"\"
%s
\"\"\"

schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_string(__schema_json))
""" % ovs.json.to_string(schema.to_json(), pretty=True)

def ovsdb_escape(string):
def escape(match):
c = match.group(0)
Expand All @@ -569,8 +584,6 @@ def ovsdb_escape(string):
return '\\x%02x' % ord(c)
return re.sub(r'["\\\000-\037]', escape, string)



def usage():
print """\
%(argv0)s: ovsdb schema compiler
Expand All @@ -580,6 +593,7 @@ The following commands are supported:
annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
c-idl-header IDL print C header file for IDL
c-idl-source IDL print C source file for IDL implementation
python-module IDL print Python module for IDL
nroff IDL print schema documentation in nroff format

The following options are also available:
Expand Down Expand Up @@ -618,7 +632,8 @@ if __name__ == "__main__":

commands = {"annotate": (annotateSchema, 2),
"c-idl-header": (printCIDLHeader, 1),
"c-idl-source": (printCIDLSource, 1)}
"c-idl-source": (printCIDLSource, 1),
"python-module": (print_python_module, 1)}

if not args[0] in commands:
sys.stderr.write("%s: unknown command \"%s\" "
Expand Down
117 changes: 116 additions & 1 deletion python/ovs/db/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def __hash__(self):

@staticmethod
def default(type_):
"""Returns the default value for the given type_, which must be an
instance of ovs.db.types.AtomicType.
The default value for each atomic type is;
- 0, for integer or real atoms.
- False, for a boolean atom.
- "", for a string atom.
- The all-zeros UUID, for a UUID atom."""
return Atom(type_)

def is_default(self):
Expand All @@ -102,12 +114,21 @@ def from_json(base, json, symtab=None):
atom.check_constraints(base)
return atom

@staticmethod
def from_python(base, value):
value = ovs.db.parser.float_to_int(value)
if type(value) in base.type.python_types:
atom = Atom(base.type, value)
else:
raise error.Error("expected %s, got %s" % (base.type, type(value)))
atom.check_constraints(base)
return atom

def check_constraints(self, base):
"""Checks whether 'atom' meets the constraints (if any) defined in
'base' and raises an ovs.db.error.Error if any constraint is violated.
'base' and 'atom' must have the same type.
Checking UUID constraints is deferred to transaction commit time, so
this function does nothing for UUID constraints."""
assert base.type == self.type
Expand Down Expand Up @@ -363,6 +384,9 @@ def as_list(self):
else:
return [k.value for k in self.values.iterkeys()]

def as_dict(self):
return dict(self.values)

def as_scalar(self):
if len(self.values) == 1:
if self.type.is_map():
Expand All @@ -373,6 +397,97 @@ def as_scalar(self):
else:
return None

def to_python(self, uuid_to_row):
"""Returns this datum's value converted into a natural Python
representation of this datum's type, according to the following
rules:
- If the type has exactly one value and it is not a map (that is,
self.type.is_scalar() returns True), then the value is:
* An int or long, for an integer column.
* An int or long or float, for a real column.
* A bool, for a boolean column.
* A str or unicode object, for a string column.
* A uuid.UUID object, for a UUID column without a ref_table.
* An object represented the referenced row, for a UUID column with
a ref_table. (For the Idl, this object will be an ovs.db.idl.Row
object.)
If some error occurs (e.g. the database server's idea of the column
is different from the IDL's idea), then the default value for the
scalar type is used (see Atom.default()).
- Otherwise, if the type is not a map, then the value is a Python list
whose elements have the types described above.
- Otherwise, the type is a map, and the value is a Python dict that
maps from key to value, with key and value types determined as
described above.
'uuid_to_row' must be a function that takes a value and an
ovs.db.types.BaseType and translates UUIDs into row objects."""
if self.type.is_scalar():
value = uuid_to_row(self.as_scalar(), self.type.key)
if value is None:
return self.type.key.default()
else:
return value
elif self.type.is_map():
value = {}
for k, v in self.values.iteritems():
dk = uuid_to_row(k.value, self.type.key)
dv = uuid_to_row(v.value, self.type.value)
if dk is not None and dv is not None:
value[dk] = dv
return value
else:
s = set()
for k in self.values:
dk = uuid_to_row(k.value, self.type.key)
if dk is not None:
s.add(dk)
return sorted(s)

@staticmethod
def from_python(type_, value, row_to_uuid):
"""Returns a new Datum with the given ovs.db.types.Type 'type_'. The
new datum's value is taken from 'value', which must take the form
described as a valid return value from Datum.to_python() for 'type'.
Each scalar value within 'value' is initally passed through
'row_to_uuid', which should convert objects that represent rows (if
any) into uuid.UUID objects and return other data unchanged.
Raises ovs.db.error.Error if 'value' is not in an appropriate form for
'type_'."""
d = {}
if type(value) == dict:
for k, v in value.iteritems():
ka = Atom.from_python(type_.key, row_to_uuid(k))
va = Atom.from_python(type_.value, row_to_uuid(v))
d[ka] = va
elif type(value) in (list, tuple):
for k in value:
ka = Atom.from_python(type_.key, row_to_uuid(k))
d[ka] = None
else:
ka = Atom.from_python(type_.key, row_to_uuid(value))
d[ka] = None

datum = Datum(type_, d)
datum.check_constraints()
if not datum.conforms_to_type():
raise error.Error("%d values when type requires between %d and %d"
% (len(d), type_.n_min, type_.n_max))

return datum

def __getitem__(self, key):
if not isinstance(key, Atom):
key = Atom.new(key)
Expand Down
Loading

0 comments on commit 8cdf034

Please sign in to comment.