Class: Cisco::Client::NETCONF

Inherits:
Cisco::Client show all
Defined in:
lib/util/client/netconf.rb,
lib/util/client/netconf/client.rb

Overview

Client implementation using Netconf API for IOS XR

Instance Attribute Summary collapse

Attributes inherited from Cisco::Client

#address, #host, #password, #platform, #port, #username

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cisco::Client

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) ⇒ NETCONF

Returns a new instance of NETCONF.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/util/client/netconf/client.rb', line 27

def initialize(**kwargs)
  super(:data_formats => [:xml],
        :platform => :ios_xr,
        **kwargs)

  @login = { target:   kwargs[:host],
             username: kwargs[:username],
             password: kwargs[:password] }

  @netconf_client = Netconf::NetconfClient.new(@login)
  begin
    @netconf_client.connect
  rescue => e
    puts "Attempted to connect and got class:#{e.class}/error:#{e}"
    raise_cisco(e)
  end
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



25
26
27
# File 'lib/util/client/netconf/client.rb', line 25

def timeout
  @timeout
end

Class Method Details

.validate_args(**kwargs) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/util/client/netconf/client.rb', line 65

def self.validate_args(**kwargs)
  super
  base_msg = 'Netconf client creation failure: '
  # Connection to remote system - username and password are required
  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

#bootObject



326
327
328
329
330
331
332
333
# File 'lib/util/client/netconf/client.rb', line 326

def boot
  return '' unless software_install
  boot_image = ''
  software_install.elements.each('rpc-reply/data/software-install/committed/committed-package-info/committed-packages') do |e|
    boot_image = /^\s*(.*)version=.*\[Boot image\]$/.match(e.text)[1]
  end
  boot_image
end

#chas_inventoryObject



144
145
146
147
148
149
150
151
# File 'lib/util/client/netconf/client.rb', line 144

def chas_inventory
  unless defined? @chas_inventory
    filter = '<platform-inventory xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-plat-chas-invmgr-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @chas_inventory = reply.response unless reply.errors?
  end
  @chas_inventory
end

#diagObject



180
181
182
183
184
185
186
187
# File 'lib/util/client/netconf/client.rb', line 180

def diag
  unless defined? @diag
    filter = '<diag xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-sdr-invmgr-diag-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @diag = reply.response unless reply.errors?
  end
  @diag
end

#domain_nameObject



270
271
272
273
274
275
276
277
# File 'lib/util/client/netconf/client.rb', line 270

def domain_name
  return '' unless ip_domain
  domain_name = ''
  ip_domain.elements.each('rpc-reply/data/ip-domain/vrfs/vrf/server/domain-name') do |e|
    domain_name = e.text
  end
  domain_name
end

#get(command: nil, **kwargs) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/util/client/netconf/client.rb', line 98

def get(command: nil,
        **kwargs)
  begin
    doc = REXML::Document.new(command)
  rescue => e
    raise Cisco::YangError.new(rejected_input: command, # rubocop:disable Style/RaiseArgs
                               error:          e.message)
  end

  return nil if doc.root.nil?

  begin
    mode = kwargs[:mode] || :get_config

    if mode == :get_oper
      reply = @netconf_client.get_oper(command)
    else
      reply = @netconf_client.get_config(command)
    end

    if reply.errors?
      fail Cisco::YangError.new( # rubocop:disable Style/RaiseArgs
        rejected_input: command,
        error:          reply.errors_as_string)
    else
      reply.data_as_string
    end

  rescue => e
    raise_cisco(e)
  end
end

#host_nameObject



261
262
263
264
265
266
267
268
# File 'lib/util/client/netconf/client.rb', line 261

def host_name
  return '' unless system_time
  host_name = ''
  system_time.elements.each('rpc-reply/data/system-time/uptime/host-name') do |e|
    host_name = e.text
  end
  host_name
end

#inventoryObject



135
136
137
138
139
140
141
142
# File 'lib/util/client/netconf/client.rb', line 135

def inventory
  unless defined? @inventory
    filter = '<inventory xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-invmgr-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @inventory = reply.response unless reply.errors?
  end
  @inventory
end

#ip_domainObject



153
154
155
156
157
158
159
160
# File 'lib/util/client/netconf/client.rb', line 153

def ip_domain
  unless defined? @ip_domain
    filter = '<ip-domain xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ip-domain-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @ip_domain = reply.response unless reply.errors?
  end
  @ip_domain
end

#last_reset_reasonObject



311
312
313
314
315
316
317
318
# File 'lib/util/client/netconf/client.rb', line 311

def last_reset_reason
  return '' unless redundancy
  reason = ''
  redundancy.elements.each('rpc-reply/data/redundancy/nodes/node/active-reboot-reason') do |e|
    reason = e.text
  end
  reason
end

#last_reset_timeObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/util/client/netconf/client.rb', line 295

def last_reset_time
  return '' unless redundancy
  last_reset_time = ''
  redundancy.elements.each('rpc-reply/data/redundancy/nodes/node/log') do |e|
    output = e.text
    t = /^Active.*:\s*(?:(\d+) day)?s?,?\s?(?:(\d+) hour)?s?,?\s?(?:(\d+) minute)?s?,?\s?(?:(\d+) second)?s?,?\s?/.match(output).to_a
    fail 'failed to retrieve system uptime' if t.nil?
    t.shift
    # time units: t = ["0", "23", "15", "49"]
    t.map!(&:to_i)
    d, h, m, s = t
    last_reset_time = (s + 60 * (m + 60 * (h + 24 * (d))))
  end
  last_reset_time
end

#osObject



207
208
209
210
211
212
213
214
# File 'lib/util/client/netconf/client.rb', line 207

def os
  return '' unless software_install
  os = ''
  software_install.elements.each('rpc-reply/data/software-install/version/log') do |e|
    os = /Cisco.*Software/.match(e.text).to_s
  end
  os
end

#os_versionObject



216
217
218
219
220
221
222
223
# File 'lib/util/client/netconf/client.rb', line 216

def os_version
  return '' unless inventory
  sr = ''
  inventory.elements.each('rpc-reply/data/inventory/racks/rack/attributes/inv-basic-bag/software-revision') do |e|
    sr = e.text
  end
  sr
end

#product_descriptionObject



225
226
227
228
229
230
231
232
# File 'lib/util/client/netconf/client.rb', line 225

def product_description
  return '' unless diag
  product_description = ''
  diag.elements.each('rpc-reply/data/diag/racks/rack/chassis/udi-description') do |e|
    product_description = e.text
  end
  product_description
end

#product_idObject



234
235
236
237
238
239
240
241
# File 'lib/util/client/netconf/client.rb', line 234

def product_id
  return '' unless diag
  product_id = ''
  diag.elements.each('rpc-reply/data/diag/racks/rack/chassis/pid') do |e|
    product_id = e.text
  end
  product_id
end

#product_serial_numberObject



252
253
254
255
256
257
258
259
# File 'lib/util/client/netconf/client.rb', line 252

def product_serial_number
  return '' unless chas_inventory
  product_id = ''
  chas_inventory.elements.each('rpc-reply/data/platform-inventory/racks/rack/attributes/basic-info/serial-number') do |e|
    product_id = e.text
  end
  product_id
end

#product_version_idObject



243
244
245
246
247
248
249
250
# File 'lib/util/client/netconf/client.rb', line 243

def product_version_id
  return '' unless inventory
  vid = ''
  inventory.elements.each('rpc-reply/data/inventory/racks/rack/entity/slot/tsi1s/tsi1/attributes/inv-eeprom-info/eeprom/vid') do |e|
    vid = e.text
  end
  vid
end

#raise_cisco(e) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/util/client/netconf/client.rb', line 45

def raise_cisco(e)
  # Net::SSH::Disconnect
  # Net::SSH::AuthenticationFailed
  # Errno::EHOSTUNREACH
  # Errno::ECONNREFUSED

  fail e
rescue Net::SSH::AuthenticationFailed => e
  raise Cisco::AuthenticationFailed, \
        'Netconf client creation failure: ' + e.message
rescue Net::SSH::Disconnect
  raise Cisco::YangError.new(error: e.message) # rubocop:disable Style/RaiseArgs
rescue Errno::EHOSTUNREACH
  raise Cisco::YangError.new(error: e.message) # rubocop:disable Style/RaiseArgs
rescue Errno::ECONNREFUSED
  raise Cisco::YangError.new(error: e.message) # rubocop:disable Style/RaiseArgs
rescue Errno::ECONNRESET
  raise Cisco::YangError.new(error: e.message) # rubocop:disable Style/RaiseArgs
end

#redundancyObject



189
190
191
192
193
194
195
196
# File 'lib/util/client/netconf/client.rb', line 189

def redundancy
  unless defined? @redundancy
    filter = '<redundancy xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-infra-rmf-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @redundancy = reply.response unless reply.errors?
  end
  @redundancy
end

#set(values: nil, **kwargs) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/util/client/netconf/client.rb', line 75

def set(values: nil,
        **kwargs)
  return if values.nil? || values.empty?
  begin
    mode = kwargs[:mode] || :merge
    fail ArgumentError unless Cisco::Util::NETCONF_SET_MODE.include? mode
    reply = @netconf_client.edit_config('candidate', mode.to_s, values)
    if reply.errors?
      fail Cisco::YangError.new( # rubocop:disable Style/RaiseArgs
        rejected_input: "apply of #{values}",
        error:          reply.errors_as_string)
    end
    reply = @netconf_client.commit_changes
    if reply.errors?
      fail Cisco::YangError.new( # rubocop:disable Style/RaiseArgs
        rejected_input: "commit of #{values}",
        error:          reply.errors_as_string)
    end
  rescue => e
    raise_cisco(e)
  end
end

#software_installObject



171
172
173
174
175
176
177
178
# File 'lib/util/client/netconf/client.rb', line 171

def software_install
  unless defined? @software_install
    filter = '<software-install xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-spirit-install-instmgr-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @software_install = reply.response unless reply.errors?
  end
  @software_install
end

#software_revisionObject



198
199
200
201
202
203
204
205
# File 'lib/util/client/netconf/client.rb', line 198

def software_revision
  return '' unless inventory
  sr = ''
  inventory.elements.each('rpc-reply/data/inventory/racks/rack/attributes/inv-basic-bag/software-revision') do |e|
    sr = e.text
  end
  sr
end

#systemObject



335
336
337
338
339
340
341
342
# File 'lib/util/client/netconf/client.rb', line 335

def system
  return '' unless inventory
  sr = ''
  inventory.elements.each('rpc-reply/data/inventory/racks/rack/attributes/inv-basic-bag/software-revision') do |e|
    sr = e.text
  end
  sr
end

#system_cpu_utilizationObject



320
321
322
323
324
# File 'lib/util/client/netconf/client.rb', line 320

def system_cpu_utilization
  'foo'
  # <system-monitoring xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-wdsysmon-fd-oper"/>
  # rpc-reply/data/system-monitoring/cpu-utilization/total-cpu-fifteen-minute
end

#system_timeObject



162
163
164
165
166
167
168
169
# File 'lib/util/client/netconf/client.rb', line 162

def system_time
  unless defined? @system_time
    filter = '<system-time xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-shellutil-oper"/>'
    reply = @netconf_client.get_oper(filter)
    @system_time = reply.response unless reply.errors?
  end
  @system_time
end

#system_uptimeObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/util/client/netconf/client.rb', line 279

def system_uptime
  return '' unless software_install
  uptime = ''
  software_install.elements.each('rpc-reply/data/software-install/version/log') do |e|
    output = /Cisco.*Software/.match(e.text).to_s
    t = /.*System uptime is (?:(\d+) day)?s?,?\s?(?:(\d+) hour)?s?,?\s?(?:(\d+) minute)?s?,?\s?(?:(\d+) second)?s?/.match(output).to_a
    fail 'failed to retrieve system uptime' if t.nil?
    t.shift
    # time units: t = ["0", "23", "15", "49"]
    t.map!(&:to_i)
    d, h, m, s = t
    uptime = (s + 60 * (m + 60 * (h + 24 * (d))))
  end
  uptime
end

#wants_cmd_refObject



131
132
133
# File 'lib/util/client/netconf/client.rb', line 131

def wants_cmd_ref
  false
end

#yang_target(_module_name, namespace, container) ⇒ Object



344
345
346
# File 'lib/util/client/netconf/client.rb', line 344

def yang_target(_module_name, namespace, container)
  "<#{container} xmlns=\"#{namespace}\" />"
end