Ejemplo n.º 1
0
    def set(self, provider, entity):
        if provider is None:
            raise _YServiceError("Provider cannot be None")

        if entity is None:
            raise _YServiceError("Set entiy cannot be None")

        entities = entity
        if isinstance(entity, EntityCollection):
            entities = entity.entities()

        with _handle_error():
            result = self._gs.set(provider, entities)
        return result
Ejemplo n.º 2
0
    def set(self, provider, entity):
        if provider is None:
            raise _YServiceError("Provider cannot be None")

        if entity is None:
            raise _YServiceError("Set entiy cannot be None")

        entities = entity
        if isinstance(entity, EntityCollection):
            entities = entity.entities()

        with _handle_error():
            result = self._gs.set(provider, entities)
        return result
Ejemplo n.º 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 YError is 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
Ejemplo n.º 4
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 YError is 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
Ejemplo n.º 5
0
    def get(self, provider, read_filter, operation='CONFIG'):
        if provider is None:
            raise _YServiceError("Service Provider cannot be None")

        if read_filter is None:
            raise _YServiceError("Get filter cannot be None")

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

        with _handle_error():
            result = self._gs.get(provider, filters, operation)
        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 6
0
    def cancel_commit(self, provider, persist_id=None):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        if persist_id is None:
            persist_id = -1

        with _handle_error():
            return self._ns.cancel_commit(provider, persist_id)
Ejemplo n.º 7
0
    def subscribe(self, provider, subscription, qos=0, mode='ONCE', encoding='PROTO', gnmi_subscribe_callback=None):
        if provider is None:
            raise _YServiceError("Provider cannot be None")

#         if (subscription is None or (not isinstance(subscription, gNMISubscription)) or
#              (not (isinstance(subscription, list) and isinstance(subscription[0], gNMISubscription)))):
#             raise _YServiceError("Subscription is not properly defined")

        self._gs.subscribe(provider, subscription, qos, mode, encoding, gnmi_subscribe_callback)
Ejemplo n.º 8
0
    def cancel_commit(self, provider, persist_id=None):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        if persist_id is None:
            persist_id = -1

        with _handle_error():
            return self._ns.cancel_commit(provider, persist_id)
Ejemplo n.º 9
0
    def get(self, provider, read_filter, operation='CONFIG'):
        if provider is None:
            raise _YServiceError("Service Provider cannot be None")

        if read_filter is None:
            raise _YServiceError("Get filter cannot be None")

        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():
            top_result = self._gs.get(provider, top_filters, operation)
        result = _get_child_entity_from_top(top_result, filters)

        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 10
0
    def validate(self, provider, source=None, url="", source_config=None):
        if provider is None or (source is None and source_config is None):
            raise _YServiceError("provider and source/source_config cannot be None")

        with _handle_error():
            if isinstance(source, Datastore):
                return self._ns.validate(provider, source, url)
            elif source_config is not None:
                return self._ns.validate(provider, source_config)
            else:
                return self._ns.validate(provider, source)
Ejemplo n.º 11
0
    def validate(self, provider, source=None, url="", source_config=None):
        if provider is None or (source is None and source_config is None):
            raise _YServiceError("provider and source/source_config cannot be None")

        with _handle_error():
            if isinstance(source, Datastore):
                return self._ns.validate(provider, source, url)
            elif source_config is not None:
                return self._ns.validate(provider, source_config)
            else:
                return self._ns.validate(provider, source)
Ejemplo n.º 12
0
    def edit_config(self, provider, target, config,
        default_operation="", test_option="", error_option=""):

        if None in (provider, target, config):
            raise _YServiceError("provider, target, and config cannot be None")

        with _handle_error():
            if isinstance(config, Config):
                config = config.entities()
            return self._ns.edit_config(provider, target, config,
                default_operation, test_option, error_option)
Ejemplo n.º 13
0
    def edit_config(self, provider, target, config,
                    default_operation="", test_option="", error_option=""):

        if None in (provider, target, config):
            raise _YServiceError("provider, target, and config cannot be None")

        with _handle_error():
            if isinstance(config, Config):
                config = config.entities()
            return self._ns.edit_config(provider, target, config,
                                        default_operation, test_option, error_option)
Ejemplo n.º 14
0
    def get(self, provider, read_filter, operation='CONFIG'):
        if provider is None:
            raise _YServiceError("Service Provider cannot be None")

        if read_filter is None:
            raise _YServiceError("Get filter cannot be None")

        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():
            top_result = self._gs.get(provider, top_filters, operation)
        result = _get_child_entity_from_top(top_result, filters)

        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 15
0
    def copy_config(self, provider, target, source=None, url="", source_config=None):
        if None in (provider, target) or (source is None and source_config is None):
            raise _YServiceError("provider, target, and source/source_config cannot be None")

        with _handle_error():
            if isinstance(source, Datastore):
                return self._ns.copy_config(provider, target, source, url)
            elif source_config is not None:
                if isinstance(source_config, EntityCollection):
                    source_config = source_config.entities()
                return self._ns.copy_config(provider, target, source_config)
            else:
                return self._ns.copy_config(provider, target, source)
Ejemplo n.º 16
0
    def copy_config(self, provider, target, source=None, url="", source_config=None):
        if None in (provider, target) or (source is None and source_config is None):
            raise _YServiceError("provider, target, and source/source_config cannot be None")

        with _handle_error():
            if isinstance(source, Datastore):
                return self._ns.copy_config(provider, target, source, url)
            elif source_config is not None:
                if isinstance(source_config, EntityCollection):
                    source_config = source_config.entities()
                return self._ns.copy_config(provider, target, source_config)
            else:
                return self._ns.copy_config(provider, target, source)
Ejemplo n.º 17
0
    def commit(self, provider, confirmed=False, confirm_timeout=None, persist=None, persist_id=None):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        if confirm_timeout is None:
            confirm_timeout = -1

        if persist is None:
            persist = -1

        if persist_id is None:
            persist_id = -1

        with _handle_error():
            return self._ns.commit(provider, confirmed, confirm_timeout, persist, persist_id)
Ejemplo n.º 18
0
    def commit(self, provider, confirmed=False, confirm_timeout=None, persist=None, persist_id=None):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        if confirm_timeout is None:
            confirm_timeout = -1

        if persist is None:
            persist = -1

        if persist_id is None:
            persist_id = -1

        with _handle_error():
            return self._ns.commit(provider, confirmed, confirm_timeout, persist, persist_id)
Ejemplo n.º 19
0
    def subscribe(self,
                  provider,
                  subscription,
                  qos=0,
                  mode='ONCE',
                  encoding='PROTO',
                  gnmi_subscribe_callback=None):
        if provider is None:
            raise _YServiceError("Provider cannot be None")

#         if (subscription is None or (not isinstance(subscription, gNMISubscription)) or
#              (not (isinstance(subscription, list) and isinstance(subscription[0], gNMISubscription)))):
#             raise _YServiceError("Subscription is not properly defined")

        self._gs.subscribe(provider, subscription, qos, mode, encoding,
                           gnmi_subscribe_callback)
Ejemplo n.º 20
0
    def get(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, get_config=False)

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

        with _handle_error():
            result = self._ns.get(provider, filters)
        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 21
0
    def get_config(self, provider, source=Datastore.running, read_filter=None):
        if None in (provider, source):
            raise _YServiceError("provider and source cannot be None")

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

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

        with _handle_error():
            result = self._ns.get_config(provider, source, filters)
        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 22
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.
            - YServiceProviderError with _ENTITY_ERROR_MSG if no such entity
              could be found in locally installed YDK model packages.
        """
        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
Ejemplo n.º 23
0
    def get_config(self, provider, source=Datastore.running, read_filter=None):
        if None in (provider, source):
            raise _YServiceError("provider and source cannot be None")

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

        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():
            top_result = self._ns.get_config(provider, source, top_filters)
        result = _get_child_entity_from_top(top_result, filters)

        if isinstance(read_filter, EntityCollection):
            result = Config(result)
        return result
Ejemplo n.º 24
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.
            - YServiceProviderError with _ENTITY_ERROR_MSG if no such entity
              could be found in locally installed YDK model packages.
        """
        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
Ejemplo n.º 25
0
def handle_runtime_error():
    _exc = None
    try:
        yield
    except RuntimeError as err:
        msg = str(err)
        if ":" in msg:
            etype_str, msg = msg.split(":", 1)
            etype = _ERRORS.get(etype_str, _YError)
        else:
            etype = _YError
            msg = msg
        _exc = etype(msg)
    except TypeError as err:
        msg = str(err)
        if ':' in msg:
            etype_str, msg = msg.split(':', 1)
            _exc = _YServiceError(msg)
        else:
            _exc = _YError(msg)
    finally:
        if _exc:
            _raise(_exc)
Ejemplo n.º 26
0
 def helper(self, provider, entity, *args, **kwargs):
     _, pname, ename = inspect.getargspec(func).args[:3]
     if provider is None or entity is None:
         err_msg = "'{0}' and '{1}' cannot be None".format(pname, ename)
         raise _YServiceError(error_msg=err_msg)
     return func(self, provider, entity, *args, **kwargs)
Ejemplo n.º 27
0
    def unlock(self, provider, target):
        if None in (provider, target):
            raise _YServiceError("provider and target cannot be None")

        with _handle_error():
            return self._ns.unlock(provider, target)
Ejemplo n.º 28
0
    def close_session(self, provider):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        with _handle_error():
            return self._ns.close_session(provider)
Ejemplo n.º 29
0
    def delete_config(self, provider, target, url=""):
        if None in (provider, target):
            raise _YServiceError("provider and target cannot be None")

        with _handle_error():
            return self._ns.delete_config(provider, target, url)
Ejemplo n.º 30
0
    def discard_changes(self, provider):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        with _handle_error():
            return self._ns.discard_changes(provider)
Ejemplo n.º 31
0
    def delete_config(self, provider, target, url=""):
        if None in (provider, target):
            raise _YServiceError("provider and target cannot be None")

        with _handle_error():
            return self._ns.delete_config(provider, target, url)
Ejemplo n.º 32
0
    def discard_changes(self, provider):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        with _handle_error():
            return self._ns.discard_changes(provider)
Ejemplo n.º 33
0
    def close_session(self, provider):
        if provider is None:
            raise _YServiceError("provider cannot be None")

        with _handle_error():
            return self._ns.close_session(provider)
Ejemplo n.º 34
0
 def capabilities(self, provider):
     if provider is None:
         raise _YServiceError("Service Provider cannot be None")
     return self._gs.capabilities(provider)
Ejemplo n.º 35
0
    def unlock(self, provider, target):
        if None in (provider, target):
            raise _YServiceError("provider and target cannot be None")

        with _handle_error():
            return self._ns.unlock(provider, target)
Ejemplo n.º 36
0
    def kill_session(self, provider, session_id):
        if None in (provider, session_id):
            raise _YServiceError("provider and session_id cannot be None")

        with _handle_error():
            return self._ns.kill_session(provider, session_id)
Ejemplo n.º 37
0
 def get(self, provider, read_filter=None):
     if provider is None:
         raise _YServiceError("provider cannot be None")
     return _ns_get(provider, None, read_filter, self._ns.get)
Ejemplo n.º 38
0
 def get_config(self, provider, source=Datastore.running, read_filter=None):
     if None in (provider, source):
         raise _YServiceError("provider and source cannot be None")
     return _ns_get(provider, source, read_filter, self._ns.get_config)
Ejemplo n.º 39
0
    def kill_session(self, provider, session_id):
        if None in (provider, session_id):
            raise _YServiceError("provider and session_id cannot be None")

        with _handle_error():
            return self._ns.kill_session(provider, session_id)
Ejemplo n.º 40
0
    def execute_rpc(self, provider, entity, top_entity=None):
        if None in (provider, entity):
            raise _YServiceError("provider and entity cannot be None")

        with _handle_error():
            return self._es.execute_rpc(provider, entity, top_entity)
Ejemplo n.º 41
0
 def capabilities(self, provider):
     if provider is None:
         raise _YServiceError("Service Provider cannot be None")
     return self._gs.capabilities(provider)