Skip to content

Commit b9d938a

Browse files
author
kkumara3
committed
Merge branch 'streaming_telemetry'
2 parents 234ee89 + fbee2e9 commit b9d938a

File tree

4 files changed

+126
-56
lines changed

4 files changed

+126
-56
lines changed

examples/grpc_cfg.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,65 @@
33
This is an example to show replace and merge configs work with a get command.
44
The example is using XRdocs vagrant topology for all the configurations
55
'''
6-
6+
from grpc.framework.interfaces.face.face import AbortionError
7+
import json
8+
from time import sleep
79
import sys
810
sys.path.insert(0, '../')
911
from lib.cisco_grpc_client import CiscoGRPCClient
10-
import json
11-
from time import sleep
1212

13-
class Example:
13+
14+
class Example(object):
1415
def __init__(self):
15-
self.client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant')
16+
self.client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant')
1617
def get(self):
1718
path = '{"Cisco-IOS-XR-ipv4-bgp-cfg:bgp": [null]}'
1819
result = self.client.getconfig(path)
19-
print result
20+
try:
21+
err, result = self.client.getconfig(path)
22+
if err:
23+
print err
24+
print json.dumps(json.loads(result))
25+
except AbortionError:
26+
print(
27+
'Unable to connect to local box, check your gRPC destination.'
28+
)
2029

2130
def replace(self):
2231
path = open('snips/bgp_start.json').read()
23-
result = self.client.replaceconfig(path)
24-
print result # If this is sucessful, then there should be no errors.
32+
try:
33+
response = self.client.replaceconfig(path)
34+
if response.errors:
35+
err = json.loads(response.errors)
36+
print err
37+
except AbortionError:
38+
print(
39+
'Unable to connect to local box, check your gRPC destination.'
40+
)
2541

2642
def merge(self):
2743
path = open('snips/bgp_merge.json').read()
28-
result = self.client.mergeconfig(path)
29-
print result # If this is sucessful, then there should be no errors.
44+
try:
45+
response = self.client.mergeconfig(path)
46+
if response.errors:
47+
err = json.loads(response.errors)
48+
print err
49+
except AbortionError:
50+
print(
51+
'Unable to connect to local box, check your gRPC destination.'
52+
)
3053

3154
def delete(self):
3255
path = open('snips/bgp_start.json').read()
33-
result = self.client.deleteconfig(path)
34-
print result # If this is sucessful, then there should be no errors.
56+
try:
57+
response = self.client.deleteconfig(path)
58+
if response.errors:
59+
err = json.loads(response.errors)
60+
print err
61+
except AbortionError:
62+
print(
63+
'Unable to connect to local box, check your gRPC destination.'
64+
)
3565

3666
def main():
3767
'''

examples/grpc_example.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
""" Basic gRPC getcall configuration. Shows how to set up the vairables
22
for tls and how to put into the class and get information from the box.
33
"""
4+
import json
5+
from grpc.framework.interfaces.face.face import AbortionError
46
import sys
57
sys.path.insert(0, '../')
68
from lib.cisco_grpc_client import CiscoGRPCClient
7-
import json
89

910
def main():
1011
'''
@@ -14,12 +15,19 @@ def main():
1415
ex: client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant')
1516
'''
1617
creds = open('ems.pem').read()
17-
options='ems.cisco.com'
18-
client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant', creds, options)
18+
options = 'ems.cisco.com'
19+
client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant', creds, options)
1920
#Test 1: Test Get config json requests
2021
path = '{"Cisco-IOS-XR-ip-static-cfg:router-static": [null]}'
21-
result = client.getconfig(path)
22-
print json.dumps(json.loads(result))
22+
try:
23+
err, result = client.getconfig(path)
24+
if err:
25+
print err
26+
print json.dumps(json.loads(result))
27+
except AbortionError:
28+
print(
29+
'Unable to connect to local box, check your gRPC destination.'
30+
)
2331

2432
if __name__ == '__main__':
2533
main()

examples/telemetry_collector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def print_connectivity(connectivity):
1818
# Handle connectivity changes.
1919
client.connectivityhandler(print_connectivity)
2020
# Iterate over subscription recvs.
21-
for segment in client.getsubscription(subscription_id):
21+
for segment in client.getsubscription(subscription_id, unmarshal=True):
22+
# unmarshal is an optional argument, default is unmarshal = True
23+
# If unmarshal is false, out is in gpb k/v
2224
recv_count += 1
2325
print json.dumps(segment, indent=4, separators=(',', ': '))
2426
print 'End Telemetry Segment'

lib/cisco_grpc_client.py

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Copyright 2016 Cisco Systems All rights reserved.
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are
5+
# met:
6+
#
7+
# * Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
#
10+
# The contents of this file are licensed under the Apache License, Version 2.0
11+
# (the "License"); you may not use this file except in compliance with the
12+
# License. You may obtain a copy of the License at
13+
#
14+
# http://www.apache.org/licenses/LICENSE-2.0
15+
#
16+
# Unless required by applicable law or agreed to in writing, software
17+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19+
# License for the specific language governing permissions and limitations under
20+
# the License.
21+
22+
123
import grpc
224
from grpc.beta import implementations
325
import ems_grpc_pb2
@@ -6,9 +28,10 @@
628
import telemetry_pb2
729

830
class CiscoGRPCClient(object):
31+
"""This class creates grpc calls using python.
32+
"""
933
def __init__(self, host, port, timeout, user, password, creds=None, options=None):
10-
"""This class creates grpc calls using python.
11-
:param user: Username for device login
34+
""":param user: Username for device login
1235
:param password: Password for device login
1336
:param host: The ip address for the device
1437
:param port: The port for the device
@@ -28,7 +51,7 @@ def __init__(self, host, port, timeout, user, password, creds=None, options=None
2851
self._creds = implementations.ssl_channel_credentials(creds)
2952
self._options = options
3053
channel = grpc.secure_channel(
31-
self._target, self._creds, (('grpc.ssl_target_name_override', self._options,),))
54+
self._target, self._creds, (('grpc.ssl_target_name_override', self._options,),))
3255
self._channel = implementations.Channel(channel)
3356
else:
3457
self._host = host
@@ -56,13 +79,14 @@ def getconfig(self, path):
5679
:rtype: Response stream object
5780
"""
5881
message = ems_grpc_pb2.ConfigGetArgs(yangpathjson=path)
59-
responses = self._stub.GetConfig(message,self._timeout, metadata = self._metadata)
60-
objects = ''
82+
responses = self._stub.GetConfig(message, self._timeout, metadata=self._metadata)
83+
objects, err = '', ''
6184
for response in responses:
6285
objects += response.yangjson
63-
return objects
86+
err += response.errors
87+
return err, objects
6488

65-
def getsubscription(self, sub_id):
89+
def getsubscription(self, sub_id, unmarshal=True):
6690
"""Telemetry subscription function
6791
:param sub_id: Subscription ID
6892
:type: string
@@ -72,59 +96,67 @@ def getsubscription(self, sub_id):
7296
sub_args = ems_grpc_pb2.CreateSubsArgs(ReqId=1, encode=3, subidstr=sub_id)
7397
stream = self._stub.CreateSubs(sub_args, timeout=self._timeout, metadata=self._metadata)
7498
for segment in stream:
75-
# Go straight for telemetry data
76-
telemetry_pb = telemetry_pb2.Telemetry()
77-
telemetry_pb.ParseFromString(segment.data)
78-
# Return in JSON format instead of protobuf.
79-
yield protobuf_json.pb2json(telemetry_pb)
99+
if not unmarshal:
100+
yield segment
101+
else:
102+
# Go straight for telemetry data
103+
telemetry_pb = telemetry_pb2.Telemetry()
104+
telemetry_pb.ParseFromString(segment.data)
105+
# Return in JSON format instead of protobuf.
106+
yield protobuf_json.pb2json(telemetry_pb)
80107

81108
def connectivityhandler(self, callback):
109+
"""Passing of a callback to monitor connectivety state updates.
110+
:param callback: A callback for monitoring
111+
:type: function
112+
"""
82113
self._channel.subscribe(callback, True)
83114

84-
def mergeconfig (self, yangjson):
115+
def mergeconfig(self, yangjson):
85116
"""Merge grpc call equivalent of PATCH RESTconf call
86117
:param data: JSON
87118
:type data: str
88119
:return: Return the response object
89120
:rtype: Response object
90121
"""
91-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
92-
response = self._stub.MergeConfig(message, self._timeout, metadata = self._metadata)
122+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
123+
response = self._stub.MergeConfig(message, self._timeout, metadata=self._metadata)
93124
return response
94125

95-
def deleteconfig (self, yangjson):
126+
def deleteconfig(self, yangjson):
96127
"""delete grpc call
97128
:param data: JSON
98129
:type data: str
99130
:return: Return the response object
100131
:rtype: Response object
101132
"""
102-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
103-
response = self._stub.DeleteConfig(message, self._timeout, metadata = self._metadata)
133+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
134+
response = self._stub.DeleteConfig(message, self._timeout, metadata=self._metadata)
104135
return response
105136

106-
def replaceconfig (self, yangjson):
137+
def replaceconfig(self, yangjson):
107138
"""Replace grpc call equivalent of PUT in restconf
108139
:param data: JSON
109140
:type data: str
110141
:return: Return the response object
111142
:rtype: Response object
112143
"""
113-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
114-
response= self._stub.ReplaceConfig(message, self._timeout, metadata = self._metadata)
144+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
145+
response = self._stub.ReplaceConfig(message, self._timeout, metadata=self._metadata)
115146
return response
116-
def getoper (self, path):
147+
def getoper(self, path):
117148
""" Get Oper call
118149
:param data: JSON
119150
:type data: str
120151
:return: Return the response object
121152
:rtype: Response stream object
122153
"""
123154
message = ems_grpc_pb2.GetOperArgs(yangpathjson=path)
124-
responses = self._stub.GetOper(message,self._timeout, metadata = self._metadata)
125-
objects = ''
155+
responses = self._stub.GetOper(message, self._timeout, metadata=self._metadata)
156+
objects, err = '', ''
126157
for response in responses:
127158
objects += response.yangjson
159+
err += response.errors
128160
return objects
129161

130162
def cliconfig(self, cli):
@@ -134,40 +166,38 @@ def cliconfig(self, cli):
134166
:return: Return the response object
135167
:rtype: str
136168
"""
137-
message = ems_grpc_pb2.CliConfigArgs(cli = cli)
138-
response = self._stub.CliConfig(message, self._timeout, metadata = self._metadata)
169+
message = ems_grpc_pb2.CliConfigArgs(cli=cli)
170+
response = self._stub.CliConfig(message, self._timeout, metadata=self._metadata)
139171
return response
140172

141-
def showcmdtextoutput (self, cli):
173+
def showcmdtextoutput(self, cli):
142174
""" Get of CLI show commands in text
143175
:param data: cli show
144176
:type data: str
145177
:return: Return the response object
146178
:rtype: str
147179
"""
148180
stub = ems_grpc_pb2.beta_create_gRPCExec_stub(self._channel)
149-
message = ems_grpc_pb2.ShowCmdArgs(cli = cli)
150-
responses = stub.ShowCmdTextOutput(message, self._timeout, metadata = self._metadata)
151-
objects = ''
181+
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
182+
responses = stub.ShowCmdTextOutput(message, self._timeout, metadata=self._metadata)
183+
objects, err = '', ''
152184
for response in responses:
153185
objects += response.output
186+
err += response.errors
154187
return objects
155188

156-
def showcmdjsonoutput (self, cli):
189+
def showcmdjsonoutput(self, cli):
157190
""" Get of CLI show commands in json
158191
:param data: cli show
159192
:type data: str
160193
:return: Return the response object
161194
:rtype: str
162195
"""
163196
stub = ems_grpc_pb2.beta_create_gRPCExec_stub(self._channel)
164-
message = ems_grpc_pb2.ShowCmdArgs(cli = cli)
165-
responses = stub.ShowCmdJSONOutput(message, self._timeout, metadata = self._metadata)
166-
objects = ''
197+
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
198+
responses = stub.ShowCmdJSONOutput(message, self._timeout, metadata=self._metadata)
199+
objects, err = '', ''
167200
for response in responses:
168201
objects += response.jsonoutput
202+
err += response.errors
169203
return objects
170-
171-
172-
173-

0 commit comments

Comments
 (0)