Class: Cisco::InterfaceOspf

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/interface_ospf.rb

Overview

InterfaceOspf - node utility class for per-interface OSPF config management

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(intf_name, ospf_name, area, create = true, show_name = nil) ⇒ InterfaceOspf

Returns a new instance of InterfaceOspf.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cisco_node_utils/interface_ospf.rb', line 28

def initialize(intf_name, ospf_name, area, create=true, show_name=nil)
  fail TypeError unless intf_name.is_a? String
  fail TypeError unless ospf_name.is_a? String
  fail TypeError unless area.is_a? String
  fail ArgumentError unless intf_name.length > 0
  fail ArgumentError unless ospf_name.length > 0
  fail ArgumentError unless area.length > 0

  # normalize
  @intf_name = intf_name.downcase
  @show_name = show_name.nil? ? '' : Utils.normalize_intf_pattern(show_name)
  fail "interface #{@intf_name} does not exist" if
    Interface.interfaces(nil, @intf_name)[@intf_name].nil?
  @ospf_name = ospf_name
  @area = area
  @get_args = { name: intf_name, show_name: @show_name }
  set_args_keys_default

  return unless create
  Feature.ospf_enable
  self.area = area
end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



26
27
28
# File 'lib/cisco_node_utils/interface_ospf.rb', line 26

def area
  @area
end

#intf_nameObject (readonly)

Returns the value of attribute intf_name.



26
27
28
# File 'lib/cisco_node_utils/interface_ospf.rb', line 26

def intf_name
  @intf_name
end

#ospf_nameObject (readonly)

Returns the value of attribute ospf_name.



26
27
28
# File 'lib/cisco_node_utils/interface_ospf.rb', line 26

def ospf_name
  @ospf_name
end

#show_nameObject (readonly)

Returns the value of attribute show_name.



26
27
28
# File 'lib/cisco_node_utils/interface_ospf.rb', line 26

def show_name
  @show_name
end

Class Method Details

.interfaces(ospf_name = nil, show_name = nil) ⇒ Object

can’t re-use Interface.interfaces because we need to filter based on “ip router ospf <name>”, which Interface doesn’t retrieve



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cisco_node_utils/interface_ospf.rb', line 57

def self.interfaces(ospf_name=nil, show_name=nil)
  fail TypeError unless ospf_name.is_a?(String) || ospf_name.nil?
  ints = {}
  show_name = Utils.normalize_intf_pattern(show_name)
  begin
    intf_list = config_get('interface_ospf', 'all_interfaces',
                           show_name: show_name)
  rescue CliError => e
    raise unless show_name
    # ignore logical interfaces that do not exist
    debug 'InterfaceOspf.interfaces ignoring CliError => ' + e.to_s
  end
  return ints if intf_list.nil?
  intf_list.each do |name|
    # Find interfaces with 'ip router ospf <name> area <area>'
    match = config_get('interface_ospf', 'area',
                       name: name, show_name: show_name)
    next if match.nil?
    ospf = match[0]
    area = match[1]
    next unless ospf_name.nil? || ospf == ospf_name
    int = name.downcase
    ints[int] = InterfaceOspf.new(int, ospf, area, false, show_name)
  end
  ints
end

Instance Method Details

#bfdObject

CLI can be either of the following or none ip ospf bfd ip ospf bfd disable



251
252
253
254
255
# File 'lib/cisco_node_utils/interface_ospf.rb', line 251

def bfd
  val = config_get('interface_ospf', 'bfd', @get_args)
  return if val.nil?
  val.include?('disable') ? false : true
end

#bfd=(val) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/cisco_node_utils/interface_ospf.rb', line 257

def bfd=(val)
  return if val == bfd
  Feature.bfd_enable
  state = (val == default_bfd) ? 'no' : ''
  disable = val ? '' : 'disable'
  config_set('interface_ospf', 'bfd',
             @set_args.merge!(state: state, disable: disable))
  set_args_keys_default
end

#costObject



193
194
195
# File 'lib/cisco_node_utils/interface_ospf.rb', line 193

def cost
  config_get('interface_ospf', 'cost', @get_args)
end

#cost=(c) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/cisco_node_utils/interface_ospf.rb', line 201

def cost=(c)
  if c == default_cost
    @set_args.merge!(state: 'no', cost: '')
  else
    @set_args.merge!(state: '', cost: c)
  end
  config_set('interface_ospf', 'cost', @set_args)
  set_args_keys_default
end

#dead_intervalObject



233
234
235
# File 'lib/cisco_node_utils/interface_ospf.rb', line 233

def dead_interval
  config_get('interface_ospf', 'dead_interval', @get_args)
end

#dead_interval=(interval) ⇒ Object



241
242
243
244
245
246
# File 'lib/cisco_node_utils/interface_ospf.rb', line 241

def dead_interval=(interval)
  # Previous behavior always sets interval and ignores 'no' cmd
  @set_args.merge!(state: '', interval: interval.to_i)
  config_set('interface_ospf', 'dead_interval', @set_args)
  set_args_keys_default
end

#default_bfdObject



267
268
269
# File 'lib/cisco_node_utils/interface_ospf.rb', line 267

def default_bfd
  config_get_default('interface_ospf', 'bfd')
end

#default_costObject



197
198
199
# File 'lib/cisco_node_utils/interface_ospf.rb', line 197

def default_cost
  config_get_default('interface_ospf', 'cost')
end

#default_dead_intervalObject



237
238
239
# File 'lib/cisco_node_utils/interface_ospf.rb', line 237

def default_dead_interval
  config_get_default('interface_ospf', 'dead_interval')
end

#default_hello_intervalObject



215
216
217
# File 'lib/cisco_node_utils/interface_ospf.rb', line 215

def default_hello_interval
  config_get_default('interface_ospf', 'hello_interval')
end

#default_message_digestObject



117
118
119
# File 'lib/cisco_node_utils/interface_ospf.rb', line 117

def default_message_digest
  config_get_default('interface_ospf', 'message_digest')
end

#default_message_digest_algorithm_typeObject



140
141
142
143
# File 'lib/cisco_node_utils/interface_ospf.rb', line 140

def default_message_digest_algorithm_type
  config_get_default('interface_ospf',
                     'message_digest_alg_type').to_sym
end

#default_message_digest_encryption_typeObject



151
152
153
154
# File 'lib/cisco_node_utils/interface_ospf.rb', line 151

def default_message_digest_encryption_type
  Encryption.cli_to_symbol(
    config_get_default('interface_ospf', 'message_digest_enc_type'))
end

#default_message_digest_key_idObject



132
133
134
# File 'lib/cisco_node_utils/interface_ospf.rb', line 132

def default_message_digest_key_id
  config_get_default('interface_ospf', 'message_digest_key_id')
end

#default_message_digest_passwordObject



166
167
168
# File 'lib/cisco_node_utils/interface_ospf.rb', line 166

def default_message_digest_password
  config_get_default('interface_ospf', 'message_digest_password')
end

#default_mtu_ignoreObject



291
292
293
# File 'lib/cisco_node_utils/interface_ospf.rb', line 291

def default_mtu_ignore
  config_get_default('interface_ospf', 'mtu_ignore')
end

#default_network_typeObject



271
272
273
274
275
276
277
278
279
# File 'lib/cisco_node_utils/interface_ospf.rb', line 271

def default_network_type
  case @intf_name
  when /loopback/i
    lookup = 'network_type_loopback_default'
  else
    lookup = 'network_type_default'
  end
  config_get_default('interface_ospf', lookup)
end

#default_passive_interfaceObject



313
314
315
# File 'lib/cisco_node_utils/interface_ospf.rb', line 313

def default_passive_interface
  config_get_default('interface_ospf', 'passive_interface')
end

#default_priorityObject



342
343
344
# File 'lib/cisco_node_utils/interface_ospf.rb', line 342

def default_priority
  config_get_default('interface_ospf', 'priority')
end

#default_shutdownObject



356
357
358
# File 'lib/cisco_node_utils/interface_ospf.rb', line 356

def default_shutdown
  config_get_default('interface_ospf', 'shutdown')
end

#default_transmit_delayObject



374
375
376
# File 'lib/cisco_node_utils/interface_ospf.rb', line 374

def default_transmit_delay
  config_get_default('interface_ospf', 'transmit_delay')
end

#destroyObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cisco_node_utils/interface_ospf.rb', line 99

def destroy
  config_set('interface_ospf', 'area', @set_args.merge!(state: 'no'))
  set_args_keys_default
  # Reset everything else back to default as well:
  self.message_digest = default_message_digest
  message_digest_key_set(default_message_digest_key_id, '', '', '')
  self.cost = default_cost
  destroy_interval('hello_interval')
  destroy_interval('dead_interval')
  self.bfd = default_bfd
  self.mtu_ignore = default_mtu_ignore
  self.priority = default_priority
  self.network_type = default_network_type
  self.passive_interface = default_passive_interface if passive_interface
  self.shutdown = default_shutdown
  self.transmit_delay = default_transmit_delay
end

#destroy_interval(prop) ⇒ Object



226
227
228
229
230
231
# File 'lib/cisco_node_utils/interface_ospf.rb', line 226

def destroy_interval(prop)
  # Helper to remove cli completely
  @set_args.merge!(state: 'no', interval: '')
  config_set('interface_ospf', prop, @set_args)
  set_args_keys_default
end

#hello_intervalObject



211
212
213
# File 'lib/cisco_node_utils/interface_ospf.rb', line 211

def hello_interval
  config_get('interface_ospf', 'hello_interval', @get_args)
end

#hello_interval=(interval) ⇒ Object



219
220
221
222
223
224
# File 'lib/cisco_node_utils/interface_ospf.rb', line 219

def hello_interval=(interval)
  # Previous behavior always sets interval and ignores 'no' cmd
  @set_args.merge!(state: '', interval: interval.to_i)
  config_set('interface_ospf', 'hello_interval', @set_args)
  set_args_keys_default
end

#message_digestObject



121
122
123
# File 'lib/cisco_node_utils/interface_ospf.rb', line 121

def message_digest
  config_get('interface_ospf', 'message_digest', @get_args)
end

#message_digest=(enable) ⇒ Object



125
126
127
128
129
130
# File 'lib/cisco_node_utils/interface_ospf.rb', line 125

def message_digest=(enable)
  return if enable == message_digest
  @set_args[:state] = (enable ? '' : 'no')
  config_set('interface_ospf', 'message_digest', @set_args)
  set_args_keys_default
end

#message_digest_algorithm_typeObject



145
146
147
148
149
# File 'lib/cisco_node_utils/interface_ospf.rb', line 145

def message_digest_algorithm_type
  match = config_get('interface_ospf', 'message_digest_alg_type',
                     @get_args)
  match.to_sym
end

#message_digest_encryption_typeObject



156
157
158
159
160
# File 'lib/cisco_node_utils/interface_ospf.rb', line 156

def message_digest_encryption_type
  match = config_get('interface_ospf', 'message_digest_enc_type',
                     @get_args)
  Encryption.cli_to_symbol(match)
end

#message_digest_key_idObject



136
137
138
# File 'lib/cisco_node_utils/interface_ospf.rb', line 136

def message_digest_key_id
  config_get('interface_ospf', 'message_digest_key_id', @get_args)
end

#message_digest_key_set(keyid, algtype, enctype, password) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cisco_node_utils/interface_ospf.rb', line 170

def message_digest_key_set(keyid, algtype, enctype, password)
  current_keyid = message_digest_key_id
  if keyid == default_message_digest_key_id && current_keyid != keyid
    @set_args.merge!(state:    'no',
                     keyid:    current_keyid,
                     algtype:  '',
                     enctype:  '',
                     password: '')
    config_set('interface_ospf', 'message_digest_key_set', @set_args)
  elsif keyid != default_message_digest_key_id
    fail TypeError unless password.is_a?(String)
    fail ArgumentError unless password.length > 0
    enctype = Encryption.symbol_to_cli(enctype)
    @set_args.merge!(state:    '',
                     keyid:    keyid,
                     algtype:  algtype,
                     enctype:  enctype,
                     password: password)
    config_set('interface_ospf', 'message_digest_key_set', @set_args)
  end
  set_args_keys_default
end

#message_digest_passwordObject



162
163
164
# File 'lib/cisco_node_utils/interface_ospf.rb', line 162

def message_digest_password
  config_get('interface_ospf', 'message_digest_password', @get_args)
end

#mtu_ignoreObject



281
282
283
# File 'lib/cisco_node_utils/interface_ospf.rb', line 281

def mtu_ignore
  config_get('interface_ospf', 'mtu_ignore', @get_args)
end

#mtu_ignore=(enable) ⇒ Object



285
286
287
288
289
# File 'lib/cisco_node_utils/interface_ospf.rb', line 285

def mtu_ignore=(enable)
  @set_args[:state] = enable ? '' : 'no'
  config_set('interface_ospf', 'mtu_ignore', @set_args)
  set_args_keys_default
end

#network_typeObject



295
296
297
298
299
300
# File 'lib/cisco_node_utils/interface_ospf.rb', line 295

def network_type
  type = config_get('interface_ospf', 'network_type', @get_args)
  return 'p2p' if type == 'point-to-point'
  return default_network_type if type.nil?
  type
end

#network_type=(type) ⇒ Object



302
303
304
305
306
307
308
309
310
311
# File 'lib/cisco_node_utils/interface_ospf.rb', line 302

def network_type=(type)
  if type == default_network_type
    @set_args.merge!(state: 'no', network_type: '')
  else
    network = 'point-to-point' if type.to_s == 'p2p'
    @set_args.merge!(state: '', network_type: network)
  end
  config_set('interface_ospf', 'network_type', @set_args)
  set_args_keys_default
end

#passive_interfaceObject



317
318
319
# File 'lib/cisco_node_utils/interface_ospf.rb', line 317

def passive_interface
  config_get('interface_ospf', 'passive_interface', @get_args)
end

#passive_interface=(enable) ⇒ Object



321
322
323
324
325
326
# File 'lib/cisco_node_utils/interface_ospf.rb', line 321

def passive_interface=(enable)
  fail TypeError unless enable == true || enable == false
  @set_args[:state] = enable ? '' : 'no'
  config_set('interface_ospf', 'passive_interface', @set_args)
  set_args_keys_default
end

#priorityObject



328
329
330
# File 'lib/cisco_node_utils/interface_ospf.rb', line 328

def priority
  config_get('interface_ospf', 'priority', @get_args)
end

#priority=(val) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/cisco_node_utils/interface_ospf.rb', line 332

def priority=(val)
  if val == default_priority
    @set_args.merge!(state: 'no', priority: '')
  else
    @set_args.merge!(state: '', priority: val)
  end
  config_set('interface_ospf', 'priority', @set_args)
  set_args_keys_default
end

#set_args_keys_defaultObject



51
52
53
# File 'lib/cisco_node_utils/interface_ospf.rb', line 51

def set_args_keys_default
  @set_args = { name: @intf_name, ospf_name: @ospf_name, area: @area }
end

#shutdownObject



346
347
348
# File 'lib/cisco_node_utils/interface_ospf.rb', line 346

def shutdown
  config_get('interface_ospf', 'shutdown', @get_args)
end

#shutdown=(enable) ⇒ Object



350
351
352
353
354
# File 'lib/cisco_node_utils/interface_ospf.rb', line 350

def shutdown=(enable)
  @set_args[:state] = enable ? '' : 'no'
  config_set('interface_ospf', 'shutdown', @set_args)
  set_args_keys_default
end

#transmit_delayObject



360
361
362
# File 'lib/cisco_node_utils/interface_ospf.rb', line 360

def transmit_delay
  config_get('interface_ospf', 'transmit_delay', @get_args)
end

#transmit_delay=(val) ⇒ Object



364
365
366
367
368
369
370
371
372
# File 'lib/cisco_node_utils/interface_ospf.rb', line 364

def transmit_delay=(val)
  if val == default_transmit_delay
    @set_args.merge!(state: 'no', delay: '')
  else
    @set_args.merge!(state: '', delay: val)
  end
  config_set('interface_ospf', 'transmit_delay', @set_args)
  set_args_keys_default
end