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
+
1
23
import grpc
2
24
from grpc .beta import implementations
3
25
import ems_grpc_pb2
6
28
import telemetry_pb2
7
29
8
30
class CiscoGRPCClient (object ):
31
+ """This class creates grpc calls using python.
32
+ """
9
33
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
12
35
:param password: Password for device login
13
36
:param host: The ip address for the device
14
37
:param port: The port for the device
@@ -28,7 +51,7 @@ def __init__(self, host, port, timeout, user, password, creds=None, options=None
28
51
self ._creds = implementations .ssl_channel_credentials (creds )
29
52
self ._options = options
30
53
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 ,),))
32
55
self ._channel = implementations .Channel (channel )
33
56
else :
34
57
self ._host = host
@@ -56,13 +79,13 @@ def getconfig(self, path):
56
79
:rtype: Response stream object
57
80
"""
58
81
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 )
60
83
objects = ''
61
84
for response in responses :
62
85
objects += response .yangjson
63
86
return objects
64
87
65
- def getsubscription (self , sub_id ):
88
+ def getsubscription (self , sub_id , unmarshal ):
66
89
"""Telemetry subscription function
67
90
:param sub_id: Subscription ID
68
91
:type: string
@@ -72,56 +95,63 @@ def getsubscription(self, sub_id):
72
95
sub_args = ems_grpc_pb2 .CreateSubsArgs (ReqId = 1 , encode = 3 , subidstr = sub_id )
73
96
stream = self ._stub .CreateSubs (sub_args , timeout = self ._timeout , metadata = self ._metadata )
74
97
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 )
80
106
81
107
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
+ """
82
112
self ._channel .subscribe (callback , True )
83
113
84
- def mergeconfig (self , yangjson ):
114
+ def mergeconfig (self , yangjson ):
85
115
"""Merge grpc call equivalent of PATCH RESTconf call
86
116
:param data: JSON
87
117
:type data: str
88
118
:return: Return the response object
89
119
:rtype: Response object
90
120
"""
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 )
93
123
return response
94
124
95
- def deleteconfig (self , yangjson ):
125
+ def deleteconfig (self , yangjson ):
96
126
"""delete grpc call
97
127
:param data: JSON
98
128
:type data: str
99
129
:return: Return the response object
100
130
:rtype: Response object
101
131
"""
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 )
104
134
return response
105
135
106
- def replaceconfig (self , yangjson ):
136
+ def replaceconfig (self , yangjson ):
107
137
"""Replace grpc call equivalent of PUT in restconf
108
138
:param data: JSON
109
139
:type data: str
110
140
:return: Return the response object
111
141
:rtype: Response object
112
142
"""
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 )
115
145
return response
116
- def getoper (self , path ):
146
+ def getoper (self , path ):
117
147
""" Get Oper call
118
148
:param data: JSON
119
149
:type data: str
120
150
:return: Return the response object
121
151
:rtype: Response stream object
122
152
"""
123
153
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 )
125
155
objects = ''
126
156
for response in responses :
127
157
objects += response .yangjson
@@ -134,40 +164,36 @@ def cliconfig(self, cli):
134
164
:return: Return the response object
135
165
:rtype: str
136
166
"""
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 )
139
169
return response
140
170
141
- def showcmdtextoutput (self , cli ):
171
+ def showcmdtextoutput (self , cli ):
142
172
""" Get of CLI show commands in text
143
173
:param data: cli show
144
174
:type data: str
145
175
:return: Return the response object
146
176
:rtype: str
147
177
"""
148
178
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 )
151
181
objects = ''
152
182
for response in responses :
153
183
objects += response .output
154
184
return objects
155
185
156
- def showcmdjsonoutput (self , cli ):
186
+ def showcmdjsonoutput (self , cli ):
157
187
""" Get of CLI show commands in json
158
188
:param data: cli show
159
189
:type data: str
160
190
:return: Return the response object
161
191
:rtype: str
162
192
"""
163
193
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 )
166
196
objects = ''
167
197
for response in responses :
168
198
objects += response .jsonoutput
169
199
return objects
170
-
171
-
172
-
173
-
0 commit comments