gNMI Service API¶
Table of Contents
-
class
ydk.gnmi.services.
gNMIService
¶ Supports gNMI Set/Get/Subscribe operations on model API entities. It also allows to get gNMI server capabilities.
-
set
(provider, entity)¶ Create, update, or delete single entity or multiple entities in the server configuration.
Parameters: - provider – (
gNMIServiceProvider
) Provider instance. - entity –
(
Entity
) instance, which represents single container in device supported model. Each Entity instance must be annotated withYFilter
, which defines set operation:- YFilter.replace - add new configuration or replace the whole configuration tree
- YFilter.update - update or create configuration in existing tree
- YFilter.delete - delete part or entire configuration tree
For multiple containers the
Entity
instances must be encapsulated into Pythonlist
or YDKEntityCollection
Config
.
Returns: True
if successful,False
if not.Raises: YServiceProviderError
if an error has occurred.- provider – (
-
get
(provider, read_filter, read_mode='CONFIG')¶ Read the entity.
Parameters: - provider – (
gNMIServiceProvider
) Provider instance. - read_filter –
(
Entity
) instance, which represents single container in device supported model.For multiple containers the
Entity
instances must be encapsulated into Pythonlist
or YDKEntityCollection
Filter
. - read_mode – (
str
) One of the values:CONFIG
,STATE
,OPERATIONAL
, orALL
.
Returns: For single entity filter - an instance of
Entity
as identified by the read_filter orNone
, if operation fails.For multiple filters - collection of
Entity
instances encapsulated into Pythonlist
or YDKEntityCollection
Config
accordingly to the type of read_filter.Raises: YServiceProviderError
if an error has occurred.- provider – (
-
subscribe
(provider, subscription, qos=0, mode='ONCE, encoding='PROTO', callback_function=None)¶ Subscribe to telemetry updates.
Parameters: - provider – (
gNMIServiceProvider
) Provider instance. - subscription – (
gNMISubscription
) Single instance or Pythonlist
of instances of objects, which represent the subscription. - qos – (
long
) QOS indicating the packet marking. - mode – (
str
) Subscription mode: one ofSTREAM
,ONCE
orPOLL
. - encoding – (
str
) Encoding method for the output: one ofJSON
,BYTES
,PROTO
,ASCII
, orJSON_IETF
. - callback_function – (
func(str)
) Callback function, which is used to process the subscription data. The subscription data returned to the user as a string representation of protobuf SubscribeResponse message. If not specified, the response is printed to system stdout.
Raises: YServiceProviderError
if an error has occurred.- provider – (
-
capabilities
(provider)¶ Get gNMI server capabilities
Parameters: provider – ( gNMIServiceProvider
) Provider instance.Returns: ( str
) JSON encoded string, which represents gNMI server capabilities.
-
-
class
ydk.gnmi.services.
gNMISubscription
¶ Instance of this class defines subscription for a single entity. Members of the class are:
- entity: (
Entity
) Instance of the subscription entity. This parameter must be set by the user. - subscription_mode: (
str
) Expected one of the following string values:TARGET_DEFINED
,ON_CHANGE
, orSAMPLE
; default value isON_CHANGE
. - sample_interval: (
long
) Time interval in nanoseconds between samples inSTREAM
mode; default value is 60000000000 (1 minute). - suppress_redundant: (
bool
) Indicates whether values that not changed should be sent in aSTREAM
subscription; default value isFalse
- heartbeat_interval: (
long
) Specifies the maximum allowable silent period in nanoseconds when suppress_redundant is True. If not specified, the heartbeat_interval is set to 360000000000 (10 minutes) or sample_interval whatever is bigger.
- entity: (
gNMI Service Examples¶
To enable YDK logging include the following code:
1 2 3 4 5 6 7 | import logging
logger = logging.getLogger("ydk")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter(("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
handler.setFormatter(formatter)
logger.addHandler(handler)
|
To enable GRPC trace set environment variables as followed:
1 2 | export GRPC_VERBOSITY=debug
export GRPC_TRACE=transport_security
|
gNMI ‘set’ example¶
Example of instantiating and using objects of gNMIServiceProvider
with gNMIService
is shown below (assuming you have openconfig
bundle installed).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from ydk.models.openconfig import openconfig_bgp
from ydk.path import Repository
from ydk.gnmi.providers import gNMIServiceProvider
from ydk.gnmi.services import gNMIService
repository = Repository('/Users/test/yang_models_location')
provider = gNMIServiceProvider(repo=repository, address='10.0.0.1', port=57400, username='admin', password='admin')
gnmi_service = gNMIService()
# Create entire BGP configuration
bgp = openconfig_bgp.Bgp()
bgp.global_.config.as_ = 65172
neighbor = bgp.Neighbors.Neighbor()
neighbor.neighbor_address = '172.16.255.2'
neighbor.config.neighbor_address = '172.16.255.2'
neighbor.config.peer_as = 65172
bgp.neighbors.neighbor.append(neighbor)
bgp.yfilter = YFilter.replace # Define set/create operation
ok = gnmi_service.set(provider, bgp) # Perform create operation
# Delete one neighbor
bgp = openconfig_bgp.Bgp()
neighbor = bgp.Neighbors.Neighbor()
neighbor.neighbor_address = '172.16.255.2'
bgp.neighbors.neighbor.append(neighbor)
bgp.yfilter = YFilter.delete # Define set/delete operation
ok = gnmi_service.set(provider, bgp) # Perform delete operation
|
gNMI ‘get’ example¶
Example of instantiating and using objects of gNMIServiceProvider
with gNMIService
is shown below (assuming you have openconfig
bundle installed).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from ydk.models.openconfig import openconfig_bgp
from ydk.path import Repository
from ydk.gnmi.providers import gNMIServiceProvider
from ydk.gnmi.services import gNMIService
repository = Repository('/Users/test/yang_models_location')
provider = gNMIServiceProvider(repo=repository, address='10.0.0.1', port=57400, username='admin', password='admin')
gnmi_service = gNMIService()
capabilities = provider.get_capabilities() # Get list of capabilities
bgp = openconfig_bgp.Bgp()
bgp_read = gnmi_service.get(provider, bgp) # Perform get operation
|
gNMI ‘subscribe’ example¶
Example of subscribing to telemetry using gNMIServiceProvider
with gNMIService
is shown below (assuming you have openconfig
bundle installed).
NOTE: The gNMIService
class can be used with multiprocessing.Pool
for the subscribe
operation as shown below as the subcription is a long-lived connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | from ydk.models.openconfig import openconfig_interfaces
from ydk.path import Repository
from ydk.gnmi.providers import gNMIServiceProvider
from ydk.gnmi.services import gNMIService, gNMISubscription
import datetime
# Callback function to handle telemetry data
def print_telemetry_data(s):
if 'update' in s:
current_dt = datetime.datetime.now()
print("\n===> Received subscribe response at %s: \n%s" %
(current_dt.strftime("%Y-%m-%d %H:%M:%S"), s))
# Function to subscribe to telemetry data
def subscribe(func):
# Initialize gNMI Service and Provider
gnmi = gNMIService()
repository = Repository('/home/yan/ydk-workspace/ydk-gen/scripts/repository/10.30.110.84')
provider = gNMIServiceProvider(repo=repository, address='10.30.110.85', port=57400,
username='admin', password='admin')
# Create telemetry subscription entity
interfaces = openconfig_interfaces.Interfaces()
interface = openconfig_interfaces.Interfaces.Interface()
interface.name = '"MgmtEth0/RP0/CPU0/0"'
interfaces.interface.append(interface)
# Build subscription
subscription = gNMISubscription()
subscription.entity = interfaces
subscription.subscription_mode = 'SAMPLE'
subscription.sample_interval = 10 * 1000000000
subscription.suppress_redundant = False
subscription.heartbeat_interval = 100 * 1000000000
# Subscribe for updates in STREAM mode.
gnmi.subscribe(provider, subscription, 10, 'STREAM', "PROTO", func)
if __name__ == "__main__":
subscribe(print_telemetry_data)
|