def updateinner(): self._reqid += 1 reqid = ('testobjectdb', self._reqid) for m in callAPI(self.apiroutine, 'objectdb', 'get', {'key': key, 'requestid': reqid}): yield m portobj = self.apiroutine.retvalue with watch_context([key], [portobj], reqid, self.apiroutine): if portobj is not None: @updater def write_status(portobj): if portobj is None: raise ValueError('Already deleted') if not hasattr(portobj, 'owner'): portobj.owner = self._ownerid portobj.status = 'READY' return [portobj] else: raise ValueError('Already managed') try: for m in callAPI(self.apiroutine, 'objectdb', 'transact', {'keys': [portobj.getkey()], 'updater': write_status}): yield m except ValueError: pass else: for m in portobj.waitif(self.apiroutine, lambda x: x.isdeleted() or hasattr(x, 'owner')): yield m self._logger.info('Port managed: %r', dump(portobj)) while True: for m in portobj.waitif(self.apiroutine, lambda x: True, True): yield m if portobj.isdeleted(): self._logger.info('Port deleted: %r', dump(portobj)) break else: self._logger.info('Port updated: %r', dump(portobj))
async def _dumpkeys(self, keys, filter=None): self._reqid += 1 reqid = ('virtualrouter', self._reqid) with request_context(reqid, self.app_routine): retobjs = await call_api(self.app_routine,'objectdb','mget',{'keys':keys,'requestid':reqid}) if filter is None: return [dump(o) for o in retobjs] else: return [dump(o) for o in retobjs if o is not None and all(getattr(o, k, None) == v for k, v in filter.items())]
def listvirtualrouters(self,id=None,**kwargs): "list virtual routers info" if id: routerkey = [VRouter.default_key()] for m in self._getkeys([routerkey]): yield m retobj = self.app_routine.retvalue if all(getattr(retobj,k,None) == v for k,v in kwargs.items()): self.app_routine.retvalue = dump(retobj) else: self.app_routine.retvalue = [] else: # we get all router from vrouterset index routersetkey = VRouterSet.default_key() self._reqid = +1 reqid = ("virtualrouter",self._reqid) def set_walker(key,set,walk,save): for weakobj in set.dataset(): routerkey = weakobj.getkey() try: router = walk(routerkey) except KeyError: pass else: if all(getattr(router,k,None) == v for k,v in kwargs.items()): save(routerkey) def walker_func(set_func): def walker(key,obj,walk,save): if obj is None: return set_walker(key,set_func(obj),walk,save) return walker for m in callAPI(self.app_routine,"objectdb","walk", {"keys":[routersetkey],"walkerdict":{routersetkey:walker_func(lambda x:x.set)}, "requestid":reqid}): yield m keys ,values = self.app_routine.retvalue with watch_context(keys,values,reqid,self.app_routine): self.app_routine.retvalue = [dump(r) for r in values]
def _dumpkeys(self, keys): self._reqid += 1 reqid = ('testobjectdb', self._reqid) for m in callAPI(self.apiroutine, 'objectdb', 'mget', {'keys': keys, 'requestid': reqid}): yield m retobjs = self.apiroutine.retvalue with watch_context(keys, retobjs, reqid, self.apiroutine): self.apiroutine.retvalue = [dump(v) for v in retobjs]
async def _dumpkeys(self, keys): self._reqid += 1 reqid = ('testobjectdb', self._reqid) with request_context(reqid, self.apiroutine): retobjs = await call_api(self.apiroutine, 'objectdb', 'mget', { 'keys': keys, 'requestid': reqid }) return [dump(v) for v in retobjs]
def listrouterinterfaces(self, id, **kwargs): """ Query router ports from a virtual router :param id: virtual router ID :param \*\*kwargs: customized filters on router interfaces :return: a list of dictionaries each stands for a matched router interface """ if not id: raise ValueError(" must special router id") routerkey = VRouter.default_key(id) self._reqid = +1 reqid = ("virtualrouter", self._reqid) def set_walker(key, interfaces, walk, save): for weakobj in interfaces.dataset(): vpkey = weakobj.getkey() try: virtualport = walk(vpkey) except KeyError: pass else: if all( getattr(virtualport, k, None) == v for k, v in kwargs.items()): save(vpkey) def walk_func(filter_func): def walker(key, obj, walk, save): if obj is None: return set_walker(key, filter_func(obj), walk, save) return walker for m in callAPI( self.app_routine, "objectdb", "walk", { "keys": [routerkey], "walkerdict": { routerkey: walk_func(lambda x: x.interfaces) }, "requestid": reqid }): yield m keys, values = self.app_routine.retvalue with watch_context(keys, values, reqid, self.app_routine): self.app_routine.retvalue = [dump(r) for r in values]
async def listvirtualrouters(self, id: (str, None) = None, **kwargs): """ Query virtual router :param id: if specified, only return virtual router with this ID :param \*\*kwargs: customized filter :return: a list of dictionaries each stands for a matched virtual router. """ if id: return await self._dumpone(VRouter.default_key(id), kwargs) else: # we get all router from vrouterset index routersetkey = VRouterSet.default_key() self._reqid += 1 reqid = ("virtualrouter",self._reqid) def set_walker(key,set,walk,save): for weakobj in set.dataset(): routerkey = weakobj.getkey() try: router = walk(routerkey) except KeyError: pass else: if all(getattr(router,k,None) == v for k,v in kwargs.items()): save(routerkey) def walker_func(set_func): def walker(key,obj,walk,save): if obj is None: return set_walker(key,set_func(obj),walk,save) return walker with request_context(reqid, self.app_routine): _, values = await call_api(self.app_routine,"objectdb","walk", {"keys":[routersetkey],"walkerdict":{routersetkey:walker_func(lambda x:x.set)}, "requestid":reqid}) return [dump(r) for r in values]
async def getlogicalnetworks(self, id=None, physicalnetwork=None, **kwargs): def set_walker(key, set, walk, save): if set is None: return for o in set.dataset(): key = o.getkey() try: net = walk(key) except KeyError: pass else: for k, v in kwargs.items(): if getattr(net, k, None) != v: break else: save(key) def walker_func(set_func): def walker(key, obj, walk, save): if obj is None: return set_walker(key, set_func(obj), walk, save) return walker if id is not None: self._reqid += 1 reqid = ('testobjectdb', self._reqid) with request_context(reqid, self.apiroutine): result = await call_api(self.apiroutine, 'objectdb', 'get', { 'key': LogicalNetwork.default_key(id), 'requestid': reqid }) if result is None: return [] if physicalnetwork is not None and physicalnetwork != result.physicalnetwork.id: return [] for k, v in kwargs.items(): if getattr(result, k, None) != v: return [] return [dump(result)] elif physicalnetwork is not None: self._reqid += 1 reqid = ('testobjectdb', self._reqid) pm_key = PhysicalNetworkMap.default_key(physicalnetwork) with request_context(reqid, self.apiroutine): keys, result = await call_api( self.apiroutine, 'objectdb', 'walk', { 'keys': [pm_key], 'walkerdict': { pm_key: walker_func(lambda x: x.networks) }, 'requestid': reqid }) return [dump(r) for r in result] else: self._reqid += 1 reqid = ('testobjectdb', self._reqid) ns_key = LogicalNetworkSet.default_key() with request_context(reqid, self.apiroutine): keys, result = await call_api( self.apiroutine, 'objectdb', 'walk', { 'keys': [ns_key], 'walkerdict': { ns_key: walker_func(lambda x: x.set) }, 'requestid': reqid }) return [dump(r) for r in result]
def getlogicalnetworks(self, id = None, physicalnetwork = None, **kwargs): def set_walker(key, set, walk, save): if set is None: return for o in set.dataset(): key = o.getkey() try: net = walk(key) except KeyError: pass else: for k,v in kwargs.items(): if getattr(net, k, None) != v: break else: save(key) def walker_func(set_func): def walker(key, obj, walk, save): if obj is None: return set_walker(key, set_func(obj), walk, save) return walker if id is not None: self._reqid += 1 reqid = ('testobjectdb', self._reqid) for m in callAPI(self.apiroutine, 'objectdb', 'get', {'key' : LogicalNetwork.default_key(id), 'requestid': reqid}): yield m result = self.apiroutine.retvalue with watch_context([LogicalNetwork.default_key(id)], [result], reqid, self.apiroutine): if result is None: self.apiroutine.retvalue = [] return if physicalnetwork is not None and physicalnetwork != result.physicalnetwork.id: self.apiroutine.retvalue = [] return for k,v in kwargs.items(): if getattr(result, k, None) != v: self.apiroutine.retvalue = [] return self.apiroutine.retvalue = [dump(result)] elif physicalnetwork is not None: self._reqid += 1 reqid = ('testobjectdb', self._reqid) pm_key = PhysicalNetworkMap.default_key(physicalnetwork) for m in callAPI(self.apiroutine, 'objectdb', 'walk', {'keys': [pm_key], 'walkerdict': {pm_key: walker_func(lambda x: x.networks)}, 'requestid': reqid}): yield m keys, result = self.apiroutine.retvalue with watch_context(keys, result, reqid, self.apiroutine): self.apiroutine.retvalue = [dump(r) for r in result] else: self._reqid += 1 reqid = ('testobjectdb', self._reqid) ns_key = LogicalNetworkSet.default_key() for m in callAPI(self.apiroutine, 'objectdb', 'walk', {'keys': [ns_key], 'walkerdict': {ns_key: walker_func(lambda x: x.set)}, 'requestid': reqid}): yield m keys, result = self.apiroutine.retvalue with watch_context(keys, result, reqid, self.apiroutine): self.apiroutine.retvalue = [dump(r) for r in result]