def test_normalize_meta_invalid_3(self): try: runner = ysanity.Runner() MetaService.normalize_meta(None, None) except YPYServiceError as err: expected_msg = "'capabilities' and 'entity' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised')
def _bind_to_object(payload, entity, capabilities, pretty_p='|-'): active_deviation_tables = MetaService.get_active_deviation_tables(capabilities, entity) payload = payload_convert(payload) if payload is None: return entity rt = etree.fromstring(payload).getroottree().getroot() curr_rt = get_root(rt, entity._common_path, _yang_ns._namespaces) XmlDecoder._bind_to_object_helper(curr_rt, entity, active_deviation_tables, pretty_p='|-')
def _bind_to_object_helper(root, entity, deviation_tables={}, pretty_p='|-'): if root is None: return MetaService.inject_imeta(entity, deviation_tables) entity_members = entity.i_meta.meta_info_class_members for member in entity_members: module_name = member.module_name nmsp = _yang_ns._namespaces[module_name] tag_name = '{' + nmsp + '}' + member.name rt = [rt for rt in root.getchildren() if rt.tag == tag_name] if rt == []: continue if member.mtype == ATTRIBUTE: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_type(rt, member, entity) elif member.mtype == REFERENCE_LEAFLIST: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_list_type(rt, member, entity) elif (member.mtype == REFERENCE_CLASS): instance = entity.__dict__[member.presentation_name] if instance is None: instance = get_class_instance(member.pmodule_name, member.clazz_name) entity.__dict__[member.presentation_name] = instance instance.parent = entity XmlDecoder._bind_to_object_helper(rt[0], instance, deviation_tables, pretty_p + '-|') elif member.mtype == REFERENCE_LIST: instance = entity.__dict__[member.presentation_name] if instance is None: instance = [] entity.__dict__[member] = instance module = importlib.import_module(member.pmodule_name) for rtchild in rt: # get nested class child = get_class_instance(member.pmodule_name, member.clazz_name) child.parent = entity instance.append(child) XmlDecoder._bind_to_object_helper(rtchild, child, deviation_tables, pretty_p + '-l') elif member.mtype == REFERENCE_IDENTITY_CLASS: instance = XmlDecoder._bind_to_identity_helper(rt[0], member, entity) entity.__dict__[member.presentation_name] = instance elif member.mtype == REFERENCE_ENUM_CLASS: entity.__dict__[member.presentation_name] = XmlDecoder._bind_to_enum_helper(rt[0], member) elif member.mtype == REFERENCE_BITS: entity.__dict__[member.presentation_name] = XmlDecoder._bind_to_bits_helper(rt[0], member, entity) elif member.mtype == REFERENCE_UNION: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_union_type_helper(rt, member, entity)
def _bind_to_object(payload, top_entity, capabilities, pretty_p='|-'): active_deviation_tables = MetaService.get_active_deviation_tables( capabilities, top_entity) payload = payload_convert(payload) if payload is None: return top_entity rt = etree.fromstring(payload.encode('utf-8')).getroottree().getroot() curr_rt = get_root(rt, top_entity, _yang_ns._namespaces) try: XmlDecoder._bind_to_object_helper(curr_rt, top_entity, active_deviation_tables, pretty_p='|-') except Exception as e: e.payload = payload raise e
def _bind_to_object(payload, top_entity, capabilities, pretty_p='|-'): active_deviation_tables = MetaService.get_active_deviation_tables( capabilities, top_entity) if hasattr(top_entity, 'parent') and top_entity.parent is not None and XmlDecoder( )._is_rpc_reply(top_entity.parent): prefix = top_entity._meta_info().module_name NSMAP = _yang_ns._namespaces payload = payload_convert(payload, NSMAP[prefix], 'output') else: payload = payload_convert(payload, '', '') if payload is None: return top_entity rt = etree.fromstring(payload.encode('utf-8')).getroottree().getroot() curr_rt = get_root(rt, top_entity, _yang_ns._namespaces) try: XmlDecoder._bind_to_object_helper(curr_rt, top_entity, active_deviation_tables, pretty_p='|-') except Exception as e: e.payload = payload raise e
def _bind_to_object_helper(root, entity, deviation_tables={}, pretty_p='|-'): if root is None: return MetaService.inject_imeta(entity, deviation_tables) entity_members = entity.i_meta.meta_info_class_members for member in entity_members: module_name = member.module_name nmsp = _yang_ns._namespaces[module_name] tag_name = '{' + nmsp + '}' + member.name rt = [rt for rt in root.getchildren() if rt.tag == tag_name] if rt == []: continue if member.mtype == ATTRIBUTE: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_type(rt, member, entity) elif member.mtype == REFERENCE_LEAFLIST: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_list_type(rt, member, entity) elif (member.mtype == REFERENCE_CLASS): instance = entity.__dict__[member.presentation_name] if instance is None: exec_import = 'from ' + member.pmodule_name + ' import ' + member.clazz_name.split('.')[0] exec exec_import eval_getinstance = member.clazz_name + '()' instance = eval(eval_getinstance) entity.__dict__[member.presentation_name] = instance instance.parent = entity XmlDecoder._bind_to_object_helper(rt[0], instance, deviation_tables, pretty_p + '-|') elif member.mtype == REFERENCE_LIST: instance = entity.__dict__[member.presentation_name] if instance is None: instance = [] entity.__dict__[member] = instance exec_import = 'from ' + member.pmodule_name + ' import ' + member.clazz_name.split('.')[0] exec exec_import for rtchild in rt: child = eval(member.clazz_name + '()') child.parent = entity instance.append(child) XmlDecoder._bind_to_object_helper(rtchild, child, deviation_tables, pretty_p + '-l') elif member.mtype == REFERENCE_IDENTITY_CLASS: identity_mod_name = None identity_name = None if ':' in rt[0].text: (prefix, identity_name) = rt[0].text.split(':') identity_ns = rt[0].nsmap[prefix] identity_mod_name = None # find the module name corresponding to this identity_ns for mod_name in _yang_ns._namespaces: if identity_ns == _yang_ns._namespaces[mod_name]: identity_mod_name = mod_name break else: identity_mod_name = entity.i_meta.module_name identity_name = rt[0].text py_mod_name = None if not (identity_mod_name, identity_name) in _yang_ns._identity_map: # this is a hack on some platforms the identity mod_name is not available sp_logger = logging.getLogger('ydk.providers.NetconfServiceProvider') sp_logger.error('Could not find identity tuple (%s, %s) in identity_map, trying secondary mechanism', identity_mod_name, identity_name) identities_with_name = [(x, y) for (x, y) in _yang_ns._identity_map if y == identity_name] if len(identities_with_name) == 0: sp_logger.error('Secondary mechanism failed no identity with name %s found', identity_name) elif len(identities_with_name) > 1: sp_logger.error('Secondary mechanism failed more than one identity with name %s found', identity_name) else: (py_mod_name, identity_clazz_name) = _yang_ns._identity_map[identities_with_name[0]] sp_logger.debug('Secondary mechanism succeeded identity with name %s found in module %s', identity_name, py_mod_name) else: (py_mod_name, identity_clazz_name) = _yang_ns._identity_map[(identity_mod_name, identity_name)] if py_mod_name is not None: exec_import = 'from ' + py_mod_name + ' import ' + identity_clazz_name exec exec_import eval_getinstance = identity_clazz_name + '()' instance = eval(eval_getinstance) entity.__dict__[member.presentation_name] = instance elif member.mtype == REFERENCE_ENUM_CLASS: exec_import = 'from ' + member.pmodule_name + ' import ' + member.clazz_name.split('.')[0] exec exec_import # first get the enum_class meta_info = eval('%s._meta_info()' % member.clazz_name) enum_literal_key = rt[0].text if enum_literal_key not in meta_info.literal_map: sp_logger = logging.getLogger('ydk.providers.NetconfServiceProvider') values = ','.join(meta_info.literal_map) sp_logger.error('Cannot find enum literal with name %s in enum clazz %s(%s) trying with different case', enum_literal_key, member.clazz_name, values) # hack change the case and check if enum_literal_key.upper() in meta_info.literal_map: sp_logger.debug('Found literal using secondary mechanism') enum_literal = meta_info.literal_map[enum_literal_key.upper()] entity.__dict__[member.presentation_name] = eval('%s.%s' % (member.clazz_name, enum_literal)) elif enum_literal_key.lower() in meta_info.literal_map: sp_logger.debug('Found literal using secondary mechanism') enum_literal = meta_info.literal_map[enum_literal_key.lower()] entity.__dict__[member.presentation_name] = eval('%s.%s' % (member.clazz_name, enum_literal)) else: enum_literal = meta_info.literal_map[enum_literal_key] entity.__dict__[member.presentation_name] = eval('%s.%s' % (member.clazz_name, enum_literal)) elif member.mtype == REFERENCE_BITS: exec_import = 'from ' + member.pmodule_name + ' import ' + member.clazz_name.split('.')[0] exec exec_import keys = rt[0].text.split(" ") for key in keys: entity.__dict__[member.presentation_name][key] = True elif member.mtype == REFERENCE_UNION: entity.__dict__[member.presentation_name] = XmlDecoder._to_real_union_type_helper(rt, member, entity)
def _bind_to_object_helper(root, entity, deviation_tables={}, pretty_p='|-'): if root is None: return MetaService.inject_imeta(entity, deviation_tables) entity_members = entity.i_meta.meta_info_class_members for member in entity_members: module_name = member.module_name nmsp = _yang_ns._namespaces[module_name] tag_name = '{' + nmsp + '}' + member.name rt = [rt for rt in root.getchildren() if rt.tag == tag_name] if rt == []: continue if member.mtype == ATTRIBUTE: entity.__dict__[ member.presentation_name] = XmlDecoder._to_real_type( rt, member, entity) elif member.mtype == REFERENCE_LEAFLIST: entity.__dict__[ member.presentation_name] = XmlDecoder._to_real_list_type( rt, member, entity) elif member.mtype == REFERENCE_CLASS: instance = entity.__dict__[member.presentation_name] if instance is None: instance = get_class_instance(member.pmodule_name, member.clazz_name) entity.__dict__[member.presentation_name] = instance instance.parent = entity XmlDecoder._bind_to_object_helper(rt[0], instance, deviation_tables, pretty_p + '-|') elif member.mtype == REFERENCE_LIST: instance = entity.__dict__[member.presentation_name] if instance is None: instance = [] entity.__dict__[member] = instance importlib.import_module(member.pmodule_name) for rtchild in rt: # get nested class child = get_class_instance(member.pmodule_name, member.clazz_name) child.parent = entity instance.append(child) XmlDecoder._bind_to_object_helper(rtchild, child, deviation_tables, pretty_p + '-l') elif member.mtype == REFERENCE_IDENTITY_CLASS: instance = XmlDecoder._bind_to_identity_helper( rt[0], member, entity) entity.__dict__[member.presentation_name] = instance elif member.mtype == REFERENCE_ENUM_CLASS: entity.__dict__[ member. presentation_name] = XmlDecoder._bind_to_enum_helper( rt[0], member) elif member.mtype == REFERENCE_BITS: entity.__dict__[ member. presentation_name] = XmlDecoder._bind_to_bits_helper( rt[0], member, entity) elif member.mtype == REFERENCE_UNION: entity.__dict__[ member. presentation_name] = XmlDecoder._to_real_union_type_helper( rt, member, entity) elif member.mtype == ANYXML_CLASS: for rtchild in rt: for ch in rtchild: namespace = ch.tag.split('}')[0][1:] prefix = ch.tag.split('}')[1] child = XmlDecoder().get_top_container_for_namespace( namespace, prefix) entity.__dict__[child._meta_info().yang_name.replace( '-', '_')] = child child.parent = entity XmlDecoder._bind_to_object_helper( ch, child, deviation_tables, pretty_p + '-l')