Пример #1
0
def _get_child_entity_from_top(top_entity, filter_entity):
    """Searches for 'filter_entity' in the hierarchy of given top-level entity.

    Args:
        top_entity (Entity or [Entity ..]): Top-level entity, usually returned from CRUD read operation.
        
        filter_entity (Entity or [Entity ..]): Top-level or non-top-level entity, which is expected to be in the 'top_entity' hierarchy.
        
        Argument type and list size must be matching.

    Returns:
        Top-level or non-top-level entity, which matches given filter under top-entity hierarchy.
        
    Raises: 
        YServiceError, if specified argument types are not matching or 'filter_entity' does not belong to 'top_entity' hierarchy. 
    """
    if filter_entity is None:
        return top_entity

    if isinstance(top_entity, list) and isinstance(
            filter_entity, list) and len(top_entity) == len(filter_entity):
        entities = []
        for i in range(0, len(top_entity)):
            entity = _get_child_entity_from_top(top_entity[i],
                                                filter_entity[i])
            entities.append(entity)
        return entities
    elif isinstance(top_entity, Entity) and isinstance(filter_entity, Entity):
        if filter_entity.is_top_level_class:
            if filter_entity.get_absolute_path(
            ) == top_entity.get_absolute_path():
                return top_entity
            else:
                raise YServiceError(
                    "_get_child_entity_from_top: The filter '%s' points to different top-entity"
                    % filter_entity.get_absolute_path())
        else:
            if not top_entity.is_top_level_class:
                raise YServiceError(
                    "_get_child_entity_from_top: The '%s' is not a top-level entity"
                    % top_entity.get_absolute_path())
            filter_abs_path = filter_entity.get_absolute_path()
            entity = _find_child_entity(top_entity, filter_abs_path)
            if entity is not None:
                entity.parent = None
            return entity
    elif top_entity is None:
        return None
    else:
        raise YServiceError(
            '_get_child_entity_from_top: Invalid arguments. Expected Entity or [Entity ...] for both arguments'
        )
Пример #2
0
def _read_entities(provider, get_config=True, source=Datastore.running):
    session = provider.get_session()
    root_schema = session.get_root_schema()

    if get_config:
        read_rpc = root_schema.create_rpc("ietf-netconf:get-config")
        source_str = "running"
        if source == Datastore.candidate:
            source_str = "candidate"
        elif source == Datastore.startup:
            source_str = "startup"
        elif source != Datastore.running:
            raise YServiceError("Wrong datastore source value '{}'".format(source))
        read_rpc.get_input_node().create_datanode("source/"+source_str);
    else:
        read_rpc = root_schema.create_rpc("ietf-netconf:get")

    data_nodes = read_rpc(session)

    config = Config()
    for node in data_nodes.get_children():
        try:
            config.append(_datanode_to_entity(node))
        except YModelError as err:
            log = logging.getLogger('ydk')
            log.error(err.message)
    return config
Пример #3
0
    def _encode(self, provider, entity, pretty, subtree):
        """Encode a YDK entity to string payload.

        Args:
            provider (ydk.providers.CodecServiceProvider): Codec provider.
            entity (ydk.types.Entity) : Encoding target.
            pretty (bool): Pretty formatting if True.
            subtree: (bool) flag, which directs encode to XML subtree; default= False

        Returns:
            Encoded payload if success.

        Raises:
            Instance of YServiceError if encoding fails.
        """
        bundle_name = _get_bundle_name(entity)
        provider.initialize(bundle_name, _get_yang_path(entity))
        root_schema = provider.get_root_schema(bundle_name)

        if subtree:
            if provider.encoding != EncodingFormat.XML:
                raise YServiceError('Subtree option can only be used with XML encoding')
            xml_codec = XmlSubtreeCodec()
            return xml_codec.encode(entity, root_schema)

        with _handle_error():
            data_node = _get_data_node_from_entity(entity, root_schema)
            codec_service = _Codec()
            result = codec_service.encode(data_node, provider.encoding, pretty)
            self.logger.debug("Performing encode operation, resulting in {}".format(result))
            return result
Пример #4
0
    def read_config(self, provider, read_filter=None):
        if provider is None:
            raise YServiceError("provider cannot be None")

        if read_filter is None:
            with _handle_error():
                return _read_entities(provider)

        filters = read_filter
        if isinstance(read_filter, EntityCollection):
            filters = read_filter.entities()
        with _handle_error():
            read_entity = self._crud.read_config(provider, filters)
        if isinstance(read_filter, EntityCollection):
            read_entity = Config(read_entity)
        return read_entity
Пример #5
0
    def read_config(self, provider, read_filter=None):
        if provider is None:
            raise YServiceError("provider cannot be None")

        if read_filter is None:
            with _handle_error():
                return _read_entities(provider)

        filters = read_filter
        if isinstance(read_filter, EntityCollection):
            filters = read_filter.entities()

        top_filters = _get_top_level_entity(filters, provider.get_session().get_root_schema())
        with _handle_error():
            read_top_entity = self._crud.read_config(provider, top_filters)
        read_entity = _get_child_entity_from_top(read_top_entity, filters)

        if isinstance(read_filter, EntityCollection):
            read_entity = Config(read_entity)
        return read_entity
Пример #6
0
    def _decode(self, provider, payload, subtree):
        """Decode payload to a YDK entity instance.

        Args:
            provider (ydk.providers.CodecServiceProvider): Codec provider.
            payload (str): Incoming payload, formatted in XML or JSON.

        Returns:
            A YDK entity (ydk.types.Entity) instance with children populated.

        Raises:
            YServiceProviderError with _PAYLOAD_ERROR_MSG, if payload
            contains more than one top level containers.
        """
        entity = _payload_to_top_entity(payload, provider.encoding)

        if subtree:
            if provider.encoding != EncodingFormat.XML:
                raise YServiceError('Subtree option can only be used with XML encoding')
            xml_codec = XmlSubtreeCodec()
            return xml_codec.decode(payload, entity)

        bundle_name = _get_bundle_name(entity)
        provider.initialize(bundle_name, _get_yang_path(entity))

        root_schema = provider.get_root_schema(bundle_name)

        self.logger.debug("Performing decode operation on payload:\n{}".format(payload))

        codec_service = _Codec()
        root_data_node = codec_service.decode(root_schema, payload, provider.encoding)
        data_nodes = root_data_node.get_children();
        if data_nodes is None or len(data_nodes) == 0:
            self.logger.debug(_PAYLOAD_ERROR_MSG)
            raise YServiceProviderError(_PAYLOAD_ERROR_MSG)
        else:
            data_node = data_nodes[0]
            _get_entity_from_data_node(data_node, entity)
            return entity
Пример #7
0
def _crud_read(provider, read_filter, is_config, crud_call):
    if provider is None:
        raise YServiceError("provider cannot be None")

    if read_filter is None:
        with _handle_error():
            return _read_entities(provider, get_config=is_config)

    filters = read_filter
    if isinstance(read_filter, EntityCollection):
        filters = read_filter.entities()

    _set_nontop_entity_filter(filters, YFilter.read)
    top_filters = _get_top_level_entity(
        filters,
        provider.get_session().get_root_schema())
    with _handle_error():
        read_top_entity = crud_call(provider, top_filters)
    read_entity = _get_child_entity_from_top(read_top_entity, filters)

    if isinstance(read_filter, EntityCollection):
        read_entity = Config(read_entity)
    return read_entity