gNMI Service Provider
- class ydk.gnmi.providers.gNMIServiceProvider(repo, address, port=57400, username, password, server_certificate="", private_key="")
A service provider, which allows YDK connect to a gNMI server. By default, the provider works in non-secure mode (tls is off). In order to enable secure mode connection the user must provide the gNMI server certificate of authorization (public key) and optionally the client (YDK application host) private key.
- Parameters:
repo – Instance of
Repository
with path to local directory containing the the ydk yang model along with all the yang models supported on the gNMI server.address – (
str
) Host address of the device supporting a gNMI interfaceport – (
int
)Port on which the gNMI interface can be accessed on the device. If not specified, the default value of57400
is assigned.username – (
str
) Username.password – (
str
) Password.server_certificate – (
str
) Full path to a file, which contains server certificate of authorization (public key). If not specified, it is assumed non-secure connection to gNMI server.private_key – (
str
) Full path to a file, which contains private key of the application host. If not specified and server_certificate is defined (secure connection), the GRPC internally defined private key is used.
Examples
To enable YDK logging include the following code:
1import logging
2logger = logging.getLogger("ydk")
3logger.setLevel(logging.INFO)
4handler = logging.StreamHandler()
5formatter = logging.Formatter(("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
6handler.setFormatter(formatter)
7logger.addHandler(handler)
To enable GRPC trace set environment variables as followed:
1export GRPC_VERBOSITY=debug
2export GRPC_TRACE=transport_security
To get server certificate, you need to retrieve the ems.pem file from the IOS XR device (after enabling gRPC/TLS) and put it to YDK application host in a folder, which is specific to the device. You can find the ems.pem file on the router in either /misc/config/grpc/ or /var/xr/config/grpc folder.
Example of instantiating and using objects of gNMIServiceProvider
is shown below (assuming you have openconfig
bundle installed).
1from ydk.models.openconfig import openconfig_bgp
2from ydk.path import Repository
3from ydk.gnmi.providers import gNMIServiceProvider
4from ydk.services import CRUDService
5
6# Create repository with location of directory where yang models from the server are downloaded
7repository = Repository('/Users/test/yang_models_location')
8ca_file_path = '/Users/test/device-ip/ems.pem'
9
10# Instantiate provider with non-secure connection to gNMI server
11provider = gNMIServiceProvider(repo=repository, address='10.0.0.1', port=57400,
12 username='admin', password='admin', server_certificate=ca_file_path)
13
14# Define filter
15bgp = openconfig_bgp.Bgp()
16
17# Run CRUD read opearaion
18crud = CRUDService()
19bgp_read = crud.read(provider, bgp) # Perform read operation