Created
May 31, 2017 19:24
-
-
Save otherwiseguy/26c798d6d1aef71db1bb4c937de84930 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import abc | |
import unittest | |
import six | |
### base stuff ### | |
@six.add_metaclass(abc.ABCMeta) | |
class API(object): | |
backend = None | |
@abc.abstractmethod | |
def create_transaction(self): | |
"""Create transaction""" | |
def db_add(self, *args, **kwargs): | |
return backend.db_add(*args, **kwargs) | |
### Backend-specific, schema-agnostic impl stuff ### | |
class IdlDb(API): | |
def db_add(self, *args, **kwargs): | |
return IdlDb | |
class CliDb(API): | |
def db_add(self, *args, **kwargs): | |
return CliDb | |
### Open_vSwitch schema stuff ### | |
@six.add_metaclass(abc.ABCMeta) | |
class VsctlAPI(API): | |
@abc.abstractmethod | |
def add_bridge(self, br): | |
"""Add a bridge""" | |
# how would we do this with single-inheritance? | |
# we need the schema-specific API and the schema-agnostic Db ops | |
# we can't add the schema-agnostic stuff to the VsctlAPI | |
class VsctlIdlImpl(VsctlAPI): | |
backend = IdlDb() | |
def create_transaction(self): | |
return True | |
def add_bridge(self, br): | |
return True | |
class VsctlCliImpl(VsctlAPI): | |
backend = CliDb() | |
def create_transaction(self): | |
return True | |
def add_bridge(self, br): | |
return True | |
### OVN_Northbound schema stuff ### | |
@six.add_metaclass(abc.ABCMeta) | |
class OVNNbAPI(API): | |
@abc.abstractmethod | |
def acl_add(self, **acl_args): | |
"""Add an ACL""" | |
class OvnNbIdlImpl(OVNNbAPI): | |
backend = IdlDb() | |
def create_transaction(self): | |
return True | |
def acl_add(self, **acl_args): | |
return True | |
class OvnNbCliImpl(OVNNbAPI): | |
backend = CliDb() | |
def create_transaction(self): | |
return True | |
def acl_add(self, **acl_args): | |
return True | |
class ApiTest(unittest.TestCase): | |
impl_map = { | |
VsctlIdlImpl: IdlDb, | |
VsctlCliImpl: CliDb, | |
OvnNbIdlImpl: IdlDb, | |
OvnNbCliImpl: CliDb, | |
} | |
def test_impl_db_usage(self): | |
for impl, db in self.impl_map.items(): | |
i = impl() | |
self.assertEqual(i.db_add(), db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment