Class: Cisco::Client::GRPC
Overview
Client implementation using gRPC API for IOS XR
Constant Summary
collapse
- DEFAULT_TIMEOUT =
Let commands in general take up to 2 minutes
120
Instance Attribute Summary collapse
#address, #host, #password, #platform, #port, #username
Class Method Summary
collapse
Instance Method Summary
collapse
-
#cli_req(stub, type, args) ⇒ Object
-
#diag ⇒ Object
-
#get(command: nil, **kwargs) ⇒ Object
-
#get_cli(command, regex = nil) ⇒ Object
-
#handle_errors(args, error_responses) ⇒ Object
-
#handle_json_error(args, msg) ⇒ Object
Generate a CliError from a failed CliConfigReply.
-
#handle_response(args, replies) ⇒ Object
-
#handle_text_error(args, msg) ⇒ Object
Generate an error from a failed request.
-
#handle_text_output(args, output) ⇒ Object
-
#host_name ⇒ Object
-
#initialize(**kwargs) ⇒ GRPC
constructor
-
#inventory ⇒ Object
-
#product_id ⇒ Object
-
#raise_cisco(e) ⇒ Object
-
#set(values: nil, **kwargs) ⇒ Object
Configure the given command(s) on the device.
-
#system ⇒ Object
-
#system_time ⇒ Object
-
#yang_req(stub, type, args) ⇒ Object
Send a YANG request via gRPC.
-
#yang_target(module_name, _namespace, container) ⇒ Object
clients, create, environment, environment_name, filter_cli, find_subconfig, handle_errors, #inspect, munge_to_array, #munge_to_array, register_client, silence_warnings, to_regexp, #to_s
Constructor Details
#initialize(**kwargs) ⇒ GRPC
Returns a new instance of GRPC.
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
|
# File 'lib/util/client/grpc/client.rb', line 37
def initialize(**kwargs)
kwargs[:host] ||= '127.0.0.1'
kwargs[:port] ||= 57_400
super(platform: :ios_xr,
**kwargs)
@config = GRPCConfigOper::Stub.new(@address, :this_channel_is_insecure, timeout: DEFAULT_TIMEOUT)
@exec = GRPCExec::Stub.new(@address, :this_channel_is_insecure, timeout: DEFAULT_TIMEOUT)
@timeout = 10
begin
base_msg = 'gRPC client creation failure: '
get(command: '{"Cisco-IOS-XR-shellutil-oper:system-time": "clock"}', mode: :get_oper)
rescue Cisco::ClientError => e
error 'initial connect failed: ' + e.to_s
if e.message[/deadline exceeded/i]
raise Cisco::ConnectionRefused, \
base_msg + 'timed out during initial connection: ' + e.message
end
raise e.class, base_msg + e.message
end
@timeout = DEFAULT_TIMEOUT
end
|
Instance Attribute Details
#timeout ⇒ Object
Returns the value of attribute timeout.
32
33
34
|
# File 'lib/util/client/grpc/client.rb', line 32
def timeout
@timeout
end
|
Class Method Details
.validate_args(**kwargs) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/util/client/grpc/client.rb', line 78
def self.validate_args(**kwargs)
super
base_msg = 'gRPC client creation failure: '
fail TypeError, base_msg + 'username must be specified' \
if kwargs[:username].nil?
fail TypeError, base_msg + 'password must be specified' \
if kwargs[:password].nil?
end
|
Instance Method Details
#cli_req(stub, type, args) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/util/client/grpc/client.rb', line 161
def cli_req(stub, type, args)
debug "Sending '#{type}' request:"
if args.is_a?(ShowCmdArgs) || args.is_a?(CliConfigArgs)
debug " with cli: '#{args.cli}'"
end
output = Cisco::Client.silence_warnings do
metadata = {
'timeout' => "#{@timeout}",
'username' => "#{@username}",
'password' => "#{@password}",
}
deadline = Time.new + @timeout
response = stub.send(type, args, deadline: deadline, metadata: metadata)
response = response.is_a?(Enumerator) ? response.to_a : [response]
debug "Got responses: #{response.map(&:class).join(', ')}"
handle_errors(args, response.select { |r| !r.errors.empty? })
handle_response(args, response)
end
return output
rescue ::GRPC::BadStatus => e
warn "gRPC error '#{e.code}' during '#{type}' request: "
if args.is_a?(ShowCmdArgs) || args.is_a?(CliConfigArgs)
warn " with cli: '#{args.cli}'"
end
warn " '#{e.details}'"
raise_cisco(e)
end
|
#diag ⇒ Object
363
364
365
366
367
368
369
370
371
372
373
374
|
# File 'lib/util/client/grpc/client.rb', line 363
def diag
unless defined? @diag
filter = '{"Cisco-IOS-XR-sdr-invmgr-diag-oper:diag":"racks"}'
reply = get(command: filter, mode: :get_oper)
begin
@diag = JSON.parse(reply)
rescue => e
puts "Failed to parse GRPC reply \"#{reply}\" from get_oper in \"diag\" with error #{e}"
end
end
@diag
end
|
#get(command: nil, **kwargs) ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/util/client/grpc/client.rb', line 101
def get(command: nil,
**kwargs)
super
fail ArgumentError if command.nil?
mode = kwargs[:mode] || :get_config
yang_req(@config, mode, ConfigGetArgs.new(yangpathjson: command))
end
|
#get_cli(command, regex = nil) ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/util/client/grpc/client.rb', line 152
def get_cli(command, regex=nil)
args = ShowCmdArgs.new(cli: command)
output = cli_req(@exec, 'show_cmd_text_output', args)
return regex.match(output)[1] if regex
output
end
|
#handle_errors(args, error_responses) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/util/client/grpc/client.rb', line 230
def handle_errors(args, error_responses)
return if error_responses.empty?
debug "#{error_responses.length} response(s) had errors:"
error_responses.each { |r| debug " error:\n#{r.errors}" }
first_error = error_responses.first.errors
begin
msg = JSON.parse(first_error)
handle_json_error(args, msg)
rescue JSON::ParserError
handle_text_error(args, first_error)
end
end
|
#handle_json_error(args, msg) ⇒ Object
Generate a CliError from a failed CliConfigReply
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/util/client/grpc/client.rb', line 290
def handle_json_error(args, msg)
msg = msg['cisco-grpc:errors']
msg = msg['error'] unless msg.is_a?(Array)
msg.each do |m|
type = m['error-type']
message = m['error-message'] || m['error-tag']
message += ': ' + m['error-path'] if m['error-path']
if type == 'protocol' && message == 'Failed authentication'
fail Cisco::AuthenticationFailed, message
elsif type == 'application'
if m['error-path']
fail Cisco::YangError.new( message
)
end
elsif type == 'protocol'
if args.is_a?(ConfigGetArgs)
input = args.yangpathjson
elsif args.is_a?(ConfigArgs)
input = args.yangjson
end
fail Cisco::YangError.new( rejected_input: input,
error: message,
)
else
fail Cisco::ClientError, message
end
end
end
|
#handle_response(args, replies) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/util/client/grpc/client.rb', line 195
def handle_response(args, replies)
klass = replies[0].class
unless replies.all? { |r| r.class == klass }
fail Cisco::ClientError, 'reply class inconsistent: ' +
replies.map(&:class).join(', ')
end
debug "Handling #{replies.length} '#{klass}' reply(s):"
case klass.to_s
when /ShowCmdJSONReply/
replies.each { |r| debug " jsonoutput:\n#{r.jsonoutput}" }
output = replies.map(&:jsonoutput).join("\n---\n")
when /ShowCmdTextReply/
replies.each { |r| debug " output:\n#{r.output}" }
output = replies.map(&:output).join('')
output = handle_text_output(args, output)
when /CliConfigReply/
output = ''
when /ConfigGetReply/
replies.each { |r| debug " yangjson:\n#{r.yangjson}" }
output = replies.map(&:yangjson).join('')
when /GetOperReply/
replies.each { |r| debug " yangjson:\n#{r.yangjson}" }
output = replies.map(&:yangjson).join('')
when /ConfigReply/
output = ''
else
fail Cisco::ClientError, "unsupported reply class #{klass}"
end
debug "Success with output:\n#{output}"
output
end
|
#handle_text_error(args, msg) ⇒ Object
Generate an error from a failed request
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/util/client/grpc/client.rb', line 273
def handle_text_error(args, msg)
if /^Disallowed commands:/ =~ msg
fail Cisco::RequestNotSupported, msg
elsif args.is_a?(ConfigGetArgs) || args.is_a?(ConfigArgs)
fail Cisco::YangError.new( rejected_input: args.yangpathjson,
error: msg,
)
else
fail Cisco::CliError.new( rejected_input: args.cli,
clierror: msg,
)
end
end
|
#handle_text_output(args, output) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/util/client/grpc/client.rb', line 245
def handle_text_output(args, output)
output = output.split("\n").drop(2)
return '' if output.nil? || output.empty?
if output[0].strip == args.cli.strip
fail Cisco::CliError.new( rejected_input: args.cli,
clierror: output.join("\n"),
)
end
output.join("\n")
end
|
#host_name ⇒ Object
385
386
387
388
|
# File 'lib/util/client/grpc/client.rb', line 385
def host_name
return '' unless system_time
system_time['Cisco-IOS-XR-shellutil-oper:system-time']['uptime']['host-name']
end
|
#inventory ⇒ Object
376
377
378
379
380
381
382
383
|
# File 'lib/util/client/grpc/client.rb', line 376
def inventory
unless defined? @inventory
filter = '{"Cisco-IOS-XR-invmgr-oper:inventory":"racks"}'
reply = get(command: filter, mode: :get_oper)
@inventory = JSON.parse(reply)
end
@inventory
end
|
#product_id ⇒ Object
390
391
392
393
394
395
|
# File 'lib/util/client/grpc/client.rb', line 390
def product_id
return diag['Cisco-IOS-XR-sdr-invmgr-diag-oper:diag']['racks']['rack'][0]['chassis']['pid']
rescue
puts "Unexpected diag value: #{diag}"
return ''
end
|
#raise_cisco(e) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/util/client/grpc/client.rb', line 67
def raise_cisco(e)
case e.code
when ::GRPC::Core::StatusCodes::UNAVAILABLE
fail Cisco::ConnectionRefused, "Connection refused: #{e.details}"
when ::GRPC::Core::StatusCodes::UNAUTHENTICATED
fail Cisco::AuthenticationFailed, e.details
else
fail Cisco::ClientError, e.details
end
end
|
#set(values: nil, **kwargs) ⇒ Object
Configure the given command(s) on the device.
93
94
95
96
97
98
99
|
# File 'lib/util/client/grpc/client.rb', line 93
def set(values: nil,
**kwargs)
super
mode = kwargs[:mode] || :merge_config
fail ArgumentError unless Cisco::Util::YANG_SET_MODE.include? mode
yang_req(@config, mode.to_s, ConfigArgs.new(yangjson: values))
end
|
#system ⇒ Object
397
398
399
|
# File 'lib/util/client/grpc/client.rb', line 397
def system
get_cli('sh install active', /^\s*(\S*)\s*version.*\[Boot image\]$/)
end
|
#system_time ⇒ Object
354
355
356
357
358
359
360
361
|
# File 'lib/util/client/grpc/client.rb', line 354
def system_time
unless defined? @system_time
filter = '{"Cisco-IOS-XR-shellutil-oper:system-time":"uptime"}'
reply = get(command: filter, mode: :get_oper)
@system_time = JSON.parse(reply)
end
@system_time
end
|
#yang_req(stub, type, args) ⇒ Object
Send a YANG request via gRPC
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
|
# File 'lib/util/client/grpc/client.rb', line 110
def yang_req(stub, type, args)
debug "Sending '#{type}' request:"
if args.is_a?(ConfigGetArgs)
debug " with yangpathjson: #{args.yangpathjson}"
elsif args.is_a?(ConfigArgs)
debug " with yangjson: #{args.yangjson}"
end
output = Cisco::Client.silence_warnings do
metadata = {
'timeout' => "#{@timeout}",
'username' => "#{@username}",
'password' => "#{@password}",
}
deadline = Time.new + @timeout
response = stub.send(type, args, deadline: deadline, metadata: metadata)
response = response.is_a?(Enumerator) ? response.to_a : [response]
debug "Got responses: #{response.map(&:class).join(', ')}"
debug "response: #{response}"
handle_errors(args, response.select { |r| !r.errors.empty? })
return handle_response(args, response)
end
return output
rescue ::GRPC::BadStatus => e
warn "gRPC error '#{e.code}' during '#{type}' request: "
if args.is_a?(ConfigGetArgs)
debug " with yangpathjson: #{args.yangpathjson}"
elsif args.is_a?(ConfigArgs)
debug " with yangjson: #{args.yangjson}"
end
warn " '#{e.details}'"
raise_cisco(e)
end
|
#yang_target(module_name, _namespace, container) ⇒ Object
401
402
403
|
# File 'lib/util/client/grpc/client.rb', line 401
def yang_target(module_name, _namespace, container)
"{\"#{module_name}:#{container}\": [null]}"
end
|