def _perform_read_filter_check(self, read_filter): if read_filter is None: self.crudLogger.error('Passed in a none filter') raise YPYError('Filter cannot be None') if not isinstance(read_filter, YList) and not hasattr( read_filter, '_meta_info'): self.crudLogger.error('Illegal filter type passed in for read') raise YPYError('Illegal filter')
def __init__(self, **kwargs): if(len(kwargs) == 0): raise YPYError('Codec type is required') codec_type = '' for key, val in kwargs.iteritems(): if key == 'type': codec_type = val if codec_type == 'xml': self.encoder = XmlEncoder() self.decoder = XmlDecoder() else: raise YPYError('Codec type "{0}" not yet supported'.format(codec_type))
def is_config(self): ''' Returns True if this instance represents config data else returns False ''' if self.parent is None: raise YPYError( 'Parent reference is needed to determine if entity has configuration data' ) return self.parent.is_config()
def _create_top_level_entity_from_read_filter(self, read_filter): non_list_filter = read_filter while isinstance(non_list_filter, YList): non_list_filter = non_list_filter.parent if non_list_filter is None: self.crud_logger.error( 'Cannot determine hierarchy for entity. Please set the parent reference' ) raise YPYError( 'Cannot determine hierarchy for entity. Please set the parent reference' ) top_entity_meta_info = non_list_filter._meta_info() while hasattr(top_entity_meta_info, 'parent') and top_entity_meta_info.parent is not None: # need to find the member that has top_entity_meta_info = top_entity_meta_info.parent exec_import = 'from ' + top_entity_meta_info.pmodule_name + ' import ' + top_entity_meta_info.name.split( '.')[0] exec exec_import entity = eval(top_entity_meta_info.name + '()') return entity
def decode(self, payload, read_filter): # In order to figure out which fields are the # ones we are interested find the field list entity = self._create_top_level_entity_from_read_filter(read_filter) XmlDecoder._bind_to_object(payload, entity, self._nc_manager.server_capabilities) # drill down to figure out the field access expression # that matches the entity or entities to be returned # not the argument passed in as a filter might have # incomplete key paths, in which case what is returned # will be the entity whose common path can be determined suffix_field_names = [] current_entity = read_filter if isinstance(current_entity, YList): suffix_field_names.append(current_entity.name) current_entity = current_entity.parent current = entity yang_nodes = [] yang_nodes.extend( [s for s in current_entity._common_path.split('/') if ':' in s]) yang_nodes = yang_nodes[1:] for seg in yang_nodes: if '[' in seg: seg = seg.split('[')[0] _, yang_node_name = seg.split(':') found = False for member in current._meta_info().meta_info_class_members: if member.name == yang_node_name: found = True current = eval('current.%s' % member.presentation_name) if current is None: return None if isinstance(current, YList): if len(current) == 0: return None if len(current) > 2: return current if len(current) == 1: current = current[0] break if not found: self.crud_logger.error( 'Error determing what needs to be returned') raise YPYError('Error determining what needs to be returned') for field in suffix_field_names: current = eval('current.%s' % field) return current
def decode(self, payload, read_filter): if read_filter is None: return XmlDecoder().decode(payload) # In order to figure out which fields are the # ones we are interested find the field list entity = self._create_top_level_entity_from_read_filter(read_filter) XmlDecoder._bind_to_object(payload, entity, self._nc_manager.server_capabilities) # drill down to figure out the field access expression # that matches the entity or entities to be returned # not the argument passed in as a filter might have # incomplete key paths, in which case what is returned # will be the entity whose common path can be determined current = entity current_entity = read_filter current_meta = current_entity.i_meta yang_nodes = [] while hasattr(current_meta, 'parent'): yang_nodes.append(current_meta.yang_name) current_meta = current_meta.parent if current_meta: yang_nodes.append(current_meta.yang_name) yang_nodes = list(reversed(yang_nodes)) yang_nodes = yang_nodes[1:] for yang_node_name in yang_nodes: found = False for member in current._meta_info().meta_info_class_members: if member.name == yang_node_name: found = True current = eval('current.%s' % member.presentation_name) if current is None: return None if isinstance(current, YList): if len(current) == 0: return None if len(current) > 2: return current if len(current) == 1: current = current[0] break if not found: self.crud_logger.error( 'Error determing what needs to be returned') raise YPYError('Error determining what needs to be returned') return current
def _entity_exists(self, provider, entity): read_entity = self.read(provider, self._create_update_entity_filter(entity)) if not read_entity._has_data(): self.service_logger.error( 'Entity does not exist on remote server. Cannot perform update operations.' ) raise YPYError( 'Entity does not exist on remote server. Cannot perform update operation.' ) return True
def _handle_commit(self, session_manager, payload, reply_str): self.netconf_sp_logger.debug( '\n<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">\n <commit/>\n</rpc>' ) commit = session_manager.commit() if 'ok' not in commit.xml: self.netconf_sp_logger.error('%s\n%s\n%s\ncommit-reply\n%s\n%s', self.separator, payload, reply_str.xml, commit.xml, self.separator) raise YPYError(YPYErrorCode.SERVER_COMMIT_ERR, reply_str) else: self.netconf_sp_logger.debug('\n%s\n%s', reply_str, self.separator)
def _raise_non_rpc_error(self): self.netconf_sp_logger.error(YPYErrorCode.INVALID_RPC) raise YPYError(YPYErrorCode.INVALID_RPC)
def _raise_read_only_edit_error(self): self.netconf_sp_logger.error(YPYErrorCode.INVALID_MODIFY) raise YPYError(YPYErrorCode.INVALID_MODIFY)
def _raise_key_missing_error(self): self.netconf_sp_logger.error(YPYErrorCode.INVALID_HIERARCHY_KEY) raise YPYError(YPYErrorCode.INVALID_HIERARCHY_KEY)
def _raise_parent_hierarchy_error(self): self.netconf_sp_logger.error(YPYErrorCode.INVALID_HIERARCHY_PARENT) raise YPYError(YPYErrorCode.INVALID_HIERARCHY_PARENT)
def _handle_rpc_error(self, payload, reply_str, pathlist): self.netconf_sp_logger.error('%s\n%s\n%s\n%s', self.separator, payload, reply_str.xml, self.separator) raise YPYError(YPYErrorCode.SERVER_REJ, reply_str)