Skip to content

Commit 3313334

Browse files
author
kkumara3
committed
Added ability to keep telemetry stream in gpb k/v byte format
1 parent 8907c56 commit 3313334

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

examples/telemetry_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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):
2222
recv_count += 1
2323
print json.dumps(segment, indent=4, separators=(',', ': '))
2424
print 'End Telemetry Segment'

lib/cisco_grpc_client.py

Lines changed: 59 additions & 33 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,13 @@ 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)
82+
responses = self._stub.GetConfig(message, self._timeout, metadata=self._metadata)
6083
objects = ''
6184
for response in responses:
6285
objects += response.yangjson
6386
return objects
6487

65-
def getsubscription(self, sub_id):
88+
def getsubscription(self, sub_id, unmarshal):
6689
"""Telemetry subscription function
6790
:param sub_id: Subscription ID
6891
:type: string
@@ -72,56 +95,63 @@ def getsubscription(self, sub_id):
7295
sub_args = ems_grpc_pb2.CreateSubsArgs(ReqId=1, encode=3, subidstr=sub_id)
7396
stream = self._stub.CreateSubs(sub_args, timeout=self._timeout, metadata=self._metadata)
7497
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)
98+
if not unmarshal:
99+
yield segment
100+
else:
101+
# Go straight for telemetry data
102+
telemetry_pb = telemetry_pb2.Telemetry()
103+
telemetry_pb.ParseFromString(segment.data)
104+
# Return in JSON format instead of protobuf.
105+
yield protobuf_json.pb2json(telemetry_pb)
80106

81107
def connectivityhandler(self, callback):
108+
"""Passing of a callback to monitor connectivety state updates.
109+
:param callback: A callback for monitoring
110+
:type: function
111+
"""
82112
self._channel.subscribe(callback, True)
83113

84-
def mergeconfig (self, yangjson):
114+
def mergeconfig(self, yangjson):
85115
"""Merge grpc call equivalent of PATCH RESTconf call
86116
:param data: JSON
87117
:type data: str
88118
:return: Return the response object
89119
:rtype: Response object
90120
"""
91-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
92-
response = self._stub.MergeConfig(message, self._timeout, metadata = self._metadata)
121+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
122+
response = self._stub.MergeConfig(message, self._timeout, metadata=self._metadata)
93123
return response
94124

95-
def deleteconfig (self, yangjson):
125+
def deleteconfig(self, yangjson):
96126
"""delete grpc call
97127
:param data: JSON
98128
:type data: str
99129
:return: Return the response object
100130
:rtype: Response object
101131
"""
102-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
103-
response = self._stub.DeleteConfig(message, self._timeout, metadata = self._metadata)
132+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
133+
response = self._stub.DeleteConfig(message, self._timeout, metadata=self._metadata)
104134
return response
105135

106-
def replaceconfig (self, yangjson):
136+
def replaceconfig(self, yangjson):
107137
"""Replace grpc call equivalent of PUT in restconf
108138
:param data: JSON
109139
:type data: str
110140
:return: Return the response object
111141
:rtype: Response object
112142
"""
113-
message = ems_grpc_pb2.ConfigArgs(yangjson= yangjson)
114-
response= self._stub.ReplaceConfig(message, self._timeout, metadata = self._metadata)
143+
message = ems_grpc_pb2.ConfigArgs(yangjson=yangjson)
144+
response = self._stub.ReplaceConfig(message, self._timeout, metadata=self._metadata)
115145
return response
116-
def getoper (self, path):
146+
def getoper(self, path):
117147
""" Get Oper call
118148
:param data: JSON
119149
:type data: str
120150
:return: Return the response object
121151
:rtype: Response stream object
122152
"""
123153
message = ems_grpc_pb2.GetOperArgs(yangpathjson=path)
124-
responses = self._stub.GetOper(message,self._timeout, metadata = self._metadata)
154+
responses = self._stub.GetOper(message, self._timeout, metadata=self._metadata)
125155
objects = ''
126156
for response in responses:
127157
objects += response.yangjson
@@ -134,40 +164,36 @@ def cliconfig(self, cli):
134164
:return: Return the response object
135165
:rtype: str
136166
"""
137-
message = ems_grpc_pb2.CliConfigArgs(cli = cli)
138-
response = self._stub.CliConfig(message, self._timeout, metadata = self._metadata)
167+
message = ems_grpc_pb2.CliConfigArgs(cli=cli)
168+
response = self._stub.CliConfig(message, self._timeout, metadata=self._metadata)
139169
return response
140170

141-
def showcmdtextoutput (self, cli):
171+
def showcmdtextoutput(self, cli):
142172
""" Get of CLI show commands in text
143173
:param data: cli show
144174
:type data: str
145175
:return: Return the response object
146176
:rtype: str
147177
"""
148178
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)
179+
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
180+
responses = stub.ShowCmdTextOutput(message, self._timeout, metadata=self._metadata)
151181
objects = ''
152182
for response in responses:
153183
objects += response.output
154184
return objects
155185

156-
def showcmdjsonoutput (self, cli):
186+
def showcmdjsonoutput(self, cli):
157187
""" Get of CLI show commands in json
158188
:param data: cli show
159189
:type data: str
160190
:return: Return the response object
161191
:rtype: str
162192
"""
163193
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)
194+
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
195+
responses = stub.ShowCmdJSONOutput(message, self._timeout, metadata=self._metadata)
166196
objects = ''
167197
for response in responses:
168198
objects += response.jsonoutput
169199
return objects
170-
171-
172-
173-

0 commit comments

Comments
 (0)