-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgnmi_lib.py
165 lines (133 loc) · 5.21 KB
/
gnmi_lib.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Python3 library used for interacting with network elements using gNMI.
This library used for Get and SetRequests using gNMI.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import re
import ssl
import sys
sys.path.insert(0, './gnmi/proto/gnmi_ext/')
import gnmi_pb2
import gnmi_pb2_grpc
_RE_PATH_COMPONENT = re.compile(r'''
^
(?P<pname>[^[]+) # gNMI path name
(\[(?P<key>\w+) # gNMI path key
=
(?P<value>.*) # gNMI path value
\])?$
''', re.VERBOSE)
class Error(Exception):
"""Module-level Exception class."""
class XpathError(Error):
"""Error parsing xpath provided."""
def PathNames(xpath):
"""Parses the xpath names.
This takes an input string and converts it to a list of gNMI Path names. Those
are later turned into a gNMI Path Class object for use in the Get/SetRequests.
Args:
xpath: (str) xpath formatted path.
Returns:
list of gNMI path names.
"""
if not xpath or xpath == '/': # A blank xpath was provided at CLI.
return []
return xpath.strip().strip('/').split('/') # Remove leading and trailing '/'.
def ParsePath(p_names):
"""Parses a list of path names for path keys.
Args:
p_names: (list) of path elements, which may include keys.
Returns:
a gnmi_pb2.Path object representing gNMI path elements.
Raises:
XpathError: Unabled to parse the xpath provided.
"""
gnmi_elems = []
for word in p_names:
word_search = _RE_PATH_COMPONENT.search(word)
if not word_search: # Invalid path specified.
raise XpathError('xpath component parse error: %s' % word)
if word_search.group('key') is not None: # A path key was provided.
gnmi_elems.append(gnmi_pb2.PathElem(name=word_search.group(
'pname'), key={word_search.group('key'): word_search.group('value')}))
else:
gnmi_elems.append(gnmi_pb2.PathElem(name=word, key={}))
return gnmi_pb2.Path(elem=gnmi_elems)
def CreateCreds(target, port, get_cert, root_cert, cert_chain, private_key):
"""Define credentials used in gNMI Requests.
Args:
target: (str) gNMI Target.
port: (str) gNMI Target IP port.
get_cert: (str) Certificate should be obtained from Target for gRPC channel.
root_cert: (str) Root certificate to use in the gRPC channel.
cert_chain: (str) Certificate chain to use in the gRPC channel.
private_key: (str) Private key to use in the gRPC channel.
Returns:
a gRPC.ssl_channel_credentials object.
"""
if get_cert:
print('Obtaining certificate from Target')
rcert = ssl.get_server_certificate((target, port)).encode('utf-8')
return gnmi_pb2_grpc.grpc.ssl_channel_credentials(
root_certificates=rcert, private_key=private_key,
certificate_chain=cert_chain)
return gnmi_pb2_grpc.grpc.ssl_channel_credentials(
root_certificates=root_cert, private_key=private_key,
certificate_chain=cert_chain)
def CreateStub(creds, target, port, host_override):
"""Creates a gNMI Stub.
Args:
creds: (object) of gNMI Credentials class used to build the secure channel.
target: (str) gNMI Target.
port: (str) gNMI Target IP port.
host_override: (str) Hostname being overridden for Cert check.
Returns:
a gnmi_pb2_grpc object representing a gNMI Stub.
"""
if host_override:
channel = gnmi_pb2_grpc.grpc.secure_channel(target + ':' + port, creds, ((
'grpc.ssl_target_name_override', host_override,),))
else:
channel = gnmi_pb2_grpc.grpc.secure_channel(target + ':' + port, creds)
return gnmi_pb2_grpc.gNMIStub(channel)
def Get(stub, paths, username, password):
"""Create a gNMI GetRequest.
Args:
stub: (class) gNMI Stub used to build the secure channel.
paths: gNMI Path
username: (str) Username used when building the channel.
password: (str) Password used when building the channel.
Returns:
a gnmi_pb2.GetResponse object representing a gNMI GetResponse.
"""
if username and password: # User/pass supplied for Authentication.
return stub.Get(
gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'),
metadata=[('username', username), ('password', password)])
return stub.Get(gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'))
def Set(stub, paths, username, password, json_value, set_type):
"""Create a gNMI SetRequest.
Args:
stub: (class) gNMI Stub used to build the secure channel.
paths: gNMI Path.
username: (str) Username used when building the channel.
password: (str) Password used when building the channel.
json_value: (str) JSON_IETF Value or file.
set_type: (str) Type of gNMI SetRequest to build.
Returns:
set_request: (class) gNMI SetRequest.
"""
val = gnmi_pb2.TypedValue()
val.json_ietf_val = json.dumps(json_value)
path_val = gnmi_pb2.Update(path=paths, val=val,)
if set_type == 'update':
return stub.Set(gnmi_pb2.SetRequest(update=[path_val]), metadata=[
('username', username), ('password', password)])
elif set_type == 'replace':
return stub.Set(gnmi_pb2.SetRequest(replace=[path_val]), metadata=[
('username', username), ('password', password)])
elif set_type == 'delete':
return stub.Set(gnmi_pb2.SetRequest(prefix=paths), metadata=[
('username', username), ('password', password)])