Beispiel #1
0
def test_build_result():
    pm = PathMap()
    pm.create(RESULT_ELEMENT_MAP)
    result = build_result(RESULT_1, pm)

    assert_equal(result['interface']['phys-address'], 'd2:0e:4c:e6:9a:25')
    assert_equal(result['interface']['learning-mode'], 1)
    assert_equal(result['interface']['id'], 100)
    assert_equal(result['interface']['if-index'], 40)
    assert_equal(result['interface']['enabled'], 0)
    assert_equal(result['interface']['name'], 'br100')
def test_list_item_multiple_elements():
    '''list path mapping - item level, multiple items present, details for elements'''
    pm = PathMap()
    # same as above, use direct access to verify member content
    pm.create({"level1":[{"level2":"level3-value", "content":{}}]})
    pm.mapnode({"level1":[{"level2":"level3-value", "content":{}}]}).meta = "list"
    assert_equal(pm.metadata({"level1":[{"level2":"level3-value", "content":{}}]}), "list")
    assert_equal(pm.mapnode({"level1":[{"level2":"level3-value", "content":{}}]}), {})
    assert_equal(pm.mapnode({"level1":[{"level2":"level3-value"}]})["level2"], "level3-value")
    assert_equal(pm.mapnode({"level1":[{"level2":"level3-value"}]})["content"], {})
def test_list_item_complex_map ():
    '''list path mapping - item level, multiple items present'''
    # same as above, just a more complex use case where there are multiple list
    # items
    pm = PathMap()
    # basic set
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1"}]}, 1), 1)
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2"}]}, 2), 2)
    # basic get 1
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1"}]}), 1)
    # basic get 2
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2"}]}), 2)
    # basic failed get
    assert_is_none(pm.metadata({"level1":[{}]}))
    # basic inherited get
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1", "content":{}}]}), 1)
    # basic inherited get
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2", "content":{}}]}), 2)
    # wrong key failed get
    assert_is_none(pm.metadata({"level1":[{"level2":"level3-value"}]}))
    # wrong key look in depth failed get
    assert_is_none(pm.metadata({"level1":[{"level2":"level3-value", "content":{}}]}))
    def __init__(self):
        BaseService.__init__(self)
        self.methods = {
            'read': self.read,
            'put': self.put,
            'merge': self.merge,
            'delete': self.delete,
            'exists': self.exists,
            'commit': self.commit,
            'cancel': self.cancel,
            'txid': self.txid,
            'error': self.error
        }
        for (key, parser) in MAP:
            cps_utils.add_attr_type(key, parser)

        self._pathmap = PathMap()
        self._tx = {}
        self._rtx = Transaction()
        # set default handler
        self._pathmap.metadata({}, Handler())
        for (fr_e, to_e) in REMAP:
            self._pathmap.metadata(
                fr_e, Handler(rewrite=True, strip_path=fr_e, add_path=to_e))
def test_list_item_path():
    '''list path mapping - item level'''
    pm = PathMap()
    # basic set to a list item
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value"}]}, 1), 1)
    # basic get for a list item
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value"}]}), 1)
    # basic get - nothing set at the to list level, we have messed only with
    # elements
    assert_is_none(pm.metadata({"level1":[{}]}))
    # basic inherited get
    # we have set metadata for {"level1":[{"level2":"level2-value"}]} to 1, subelements
    # inherit so a path under this will have 1 until we change to otherwise
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value", "content":{}}]}), 1)
    # wrong key failed get
    assert_is_none(pm.metadata({"level1":[{"level2":"level3-value"}]}))
    # wrong key look in depth failed get
    assert_is_none(pm.metadata({"level1":[{"level2":"level3-value", "content":{}}]}))
def test_list_path():
    '''Basic list path mapping'''
    pm = PathMap()
    # basic set of metadata for whole list
    assert_equal(pm.metadata({"level1":[{}]}, 1), 1)
    # basic get of metadata for whole list
    assert_equal(pm.metadata({"level1":[{}]}), 1)
    # basic failed get - for whole list
    assert_is_none(pm.metadata({"level1-wrong":[{}]}))
    # basic inherited get - we have something set for whole list, 
    # an element should inherit it
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value"}]}), 1)
def test_container_path():
    '''Basic container path mapping'''
    pm = PathMap()
    # basic set - simple two level path, get back the metadata upon creation
    # set to 1
    assert_equal(pm.metadata({"level1":{"level2":{}}}, 1), 1)
    # basic get - double check the metadata for same path a step later
    # set to 1 in previous step
    assert_equal(pm.metadata({"level1":{"level2":{}}}), 1)
    # basic failed get
    # we have no higher level inherited value and we give a wrong path - should
    # get none
    assert_is_none(pm.metadata({"level1":{"level2-wrong":{}}}))
    # basic inherited get
    # we now try a "wrong" (actually inexistent path) under a level where we
    # have something set
    import sys
    sys.stderr.write("EXPORT {} DATA {}".format(pm.export(), pm.to_data()))
    assert_equal(pm.metadata({"level1":{"level2":{"level3":{}}}}), 1)
Beispiel #8
0
class Listener(object):
    '''A `JSON-RPC 2.0` openswitch notification mapper service.'''
    def __init__(self, notify):
        self._pathmap = PathMap()
        self._notify = notify  # notify callback

    def addpath(self, path):
        '''Add a path and a method to use in a JSON RPC Notification'''
        handler = self._pathmap.mapnode(path)
        if handler is None:
            handler = self._pathmap.metadata(path, Handler(path, self._notify))
        else:
            handler = self._pathmap.metadata(path)
        handler.inc_refcount()
        return handler.topic()

    def delpath(self, path):
        '''Add a path and a method to use in a JSON RPC Notification'''
        handler = self._pathmap.mapnode(path)
        if handler is None:
            return
        self._pathmap.metadata(path).dec_refcount()
def test_list_item_complex_map_take_2 ():
    '''list path mapping - item level, multiple items present, metadata on list'''
    pm = PathMap()
    # same as above, just a more complex use case where there are multiple list
    # items and inheritance into the mix
    assert_equal(pm.metadata({"level1":[{}]}, "list"), "list")
    assert_equal(pm.metadata({"level1":[{}]}), "list")
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1"}]}, 1), 1)
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2"}]}, 2), 2)
    # basic get 1
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1"}]}), 1)
    # basic get 2
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2"}]}), 2)
    # basic failed get
    # basic inherited get
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value1", "content":{}}]}), 1)
    # basic inherited get
    assert_equal(pm.metadata({"level1":[{"level2":"level2-value2", "content":{}}]}), 2)
    # wrong key failed get
    assert_equal(pm.metadata({"level1":[{"level2":"level3-value"}]}), "list")
    # wrong key look in depth failed get
    assert_equal(pm.metadata({"level1":[{"level2":"level3-value", "content":{}}]}), "list")
Beispiel #10
0
 def __init__(self, notify):
     self._pathmap = PathMap()
     self._notify = notify  # notify callback
class Service(BaseService):
    '''A `JSON-RPC 2.0` openswitch rpc/transaction mapper service.'''
    def __init__(self):
        BaseService.__init__(self)
        self.methods = {
            'read': self.read,
            'put': self.put,
            'merge': self.merge,
            'delete': self.delete,
            'exists': self.exists,
            'commit': self.commit,
            'cancel': self.cancel,
            'txid': self.txid,
            'error': self.error
        }
        for (key, parser) in MAP:
            cps_utils.add_attr_type(key, parser)

        self._pathmap = PathMap()
        self._tx = {}
        self._rtx = Transaction()
        # set default handler
        self._pathmap.metadata({}, Handler())
        for (fr_e, to_e) in REMAP:
            self._pathmap.metadata(
                fr_e, Handler(rewrite=True, strip_path=fr_e, add_path=to_e))

    def txid(self):
        '''Allocate a new TXID'''
        uu4 = str(uuid.uuid4())
        while self._tx.get(uu4) is not None:
            uu4 = str(uuid.uuid4())
        self._tx[uu4] = Transaction()
        return uu4

    def read(self, store, entity, path):
        '''Read'''
        # we ignore store and entity for the moment
        handler = self._pathmap.metadata(path)
        return handler.read(self._rtx, path)

    def exists(self, store, entity, path):
        '''Read'''
        # we ignore store and entity for the moment
        handler = self._pathmap.metadata(path)
        return handler.exists(self._rtx, path)

    def put(self, txid, store, entity, path, data):
        '''Put'''
        # we ignore store and entity for the moment
        handler = self._pathmap.metadata(path)
        handler.put(self._tx[txid], path, data)

    def merge(self, txid, store, entity, path, data):
        '''Merge'''
        # we ignore store and entity for the moment
        handler = self._pathmap.metadata(path)
        handler.merge(self._tx[txid], path, data)

    def delete(self, txid, store, entity, path):
        '''Delete'''
        # we ignore store and entity for the moment
        handler = self._pathmap.metadata(path)
        handler.delete(self._tx[txid], path)

    def commit(self, txid):
        '''Commit tx'''
        return self._tx[txid].commit()

    def cancel(self, txid):
        '''Delete transaction - effectively cancel it'''
        del self._tx[txid]
        return True

    def error(self, txid):
        '''Extended Error - not yet implemented'''
        raise NotImplementedError("Extended Error not implemented")