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+
123import grpc
224from grpc .beta import implementations
325import ems_grpc_pb2
628import telemetry_pb2
729
830class 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