def create(line): if len(line) < 1: raise InvalidInput( 'invalid input: create [module| netif <module-oid> | hostif <module-oid>] [<attr-name>:<attr-value> ]...' ) object_type = line[0] line.pop(0) module_id = 0 if object_type in ['netif', 'hostif']: if len(line) < 1: print( 'invalid input: create [module| netif <module-oid> | hostif <module-oid>] [<attr-name>:<attr-value> ]...' ) return try: module_id = int(line[0], 0) except ValueError as e: raise InvalidInput(f'invalid module oid: {e}') line.pop(0) attrs = [tuple(attr.split(':')) for attr in line] try: print('oid: 0x{:x}'.format( self.client.create(object_type, attrs, module_id))) except TAIException as e: print('err: {} (code: {:x})'.format(e.msg, e.code))
def monitor(args): if len(args) == 0: args = ['notify'] #TODO default value handling if len(args) != 1: raise InvalidInput('usage: monitor <name>') l = self.client.list_attribute_metadata() def cb(res): for attr in res.attrs: a = [ v.short_name for v in l if v.attr_id == attr.attr_id ] if len(a) == 1: if JSON_OUTPUT: print(json.dumps({"name": a[0], "value": json.loads(attr.value)})) else: print('{} | {}'.format(a[0], attr.value)) elif len(a) == 0: if JSON_OUTPUT: print(json.dumps({"name": attr.attr_id, "value": json.loads(attr.value)})) else: print('0x{:x} | {}'.format(attr.attr_id, attr.value)) else: print('error: more than one metadata matched for id 0x{:x}: {}'.format(attr.attr_id, a)) try: self.client.monitor(args[0], cb, json=JSON_OUTPUT) print() except TAIException as e: print('err: {} (code: {:x})'.format(e.msg, e.code))
def set(args): if len(args) != 2: raise InvalidInput('usage: set <name> <value>') try: self.client.set(args[0], args[1]) except TAIException as e: print('err: {} (code: {:x})'.format(e.msg, e.code))
def get(args): if len(args) != 1: raise InvalidInput('usage: get <name>') try: print(self.client.get(args[0], json=JSON_OUTPUT)) except TAIException as e: print('err: {} (code {:x})'.format(e.msg, e.code))
def remove(line): if len(line) != 1: raise InvalidInput('usage: remove <oid>') try: self.client.remove(int(line[0], 0)) except TAIException as e: print('err: {} (code: {:x})'.format(e.msg, e.code))
def _list(line): if len(line) != 0: raise InvalidInput('usage: list') for k, m in self.client.list().items(): stroid = lambda oid : '0x{:08x}'.format(oid) print('module:', k, stroid(m.oid) if oid else 'not present') for v in m.hostifs: print(' hostif:', v.index, stroid(v.oid)) for v in m.netifs: print(' netif:', v.index, stroid(v.oid))
def list(args): if len(args) > 1 or (len(args) == 1 and args[0] not in ['simple']): raise InvalidInput('usage: list-attr [simple]') simple = len(args) == 1 if simple: for m in self.client.list_attribute_metadata(): print(m.short_name) else: d = [] for m in self.client.list_attribute_metadata(): d.append([m.short_name, 'ro' if m.is_readonly else 'r/w', m.usage, 'custom' if m.attr_id > TAI_ATTR_CUSTOM_RANGE_START else 'official']) print(tabulate(d, headers=['name', 'type', 'value', 'range']))
def set_log_level(line): if len(line) != 1: raise InvalidInput('usage: log-level <level>') self.client.set_log_level(line[0])
def module(line): if len(line) != 1: raise InvalidInput('usage: module <name>') return Module(self.client.get_module(line[0]), line[0], self)
def hostif(line): if len(line) != 1: raise InvalidInput('usage: hostif <index>') return HostIf(self.client.get_hostif(int(line[0])), line[0], self)
def netif(line): if len(line) != 1: raise InvalidInput('usage: netif <index>') return NetIf(self.client.get_netif(int(line[0])), line[0], self)