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(int_name, ospf_name, area, create = true) ⇒ 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
# File 'lib/cisco_node_utils/interface_ospf.rb', line 28

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

  # normalize
  int_name = int_name.downcase
  @interface = Interface.interfaces[int_name]
  fail "interface #{int_name} does not exist" if @interface.nil?

  @ospf_name = ospf_name

  return unless create
  Feature.ospf_enable

  config_set('interface_ospf', 'area', @interface.name,
             '', @ospf_name, area)
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface


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

def interface
  @interface
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

Class Method Details

.interfaces(ospf_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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cisco_node_utils/interface_ospf.rb', line 52

def self.interfaces(ospf_name=nil)
  fail TypeError unless ospf_name.is_a?(String) || ospf_name.nil?
  ints = {}

  intf_list = config_get('interface_ospf', 'all_interfaces')
  return ints if intf_list.nil?
  intf_list.each do |name|
    match = config_get('interface_ospf', 'area', name)
    next if match.nil?
    # ip router ospf <name> area <area>
    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)
  end
  ints
end

Instance Method Details

#areaObject


71
72
73
74
75
76
77
78
# File 'lib/cisco_node_utils/interface_ospf.rb', line 71

def area
  match = config_get('interface_ospf', 'area', @interface.name)
  return nil if match.nil?
  val = match[1]
  # Coerce numeric area to the expected dot-decimal format.
  val = IPAddr.new(val.to_i, Socket::AF_INET).to_s unless val.match(/\./)
  val
end

#area=(a) ⇒ Object


80
81
82
83
# File 'lib/cisco_node_utils/interface_ospf.rb', line 80

def area=(a)
  config_set('interface_ospf', 'area', @interface.name,
             '', @ospf_name, a)
end

#bfdObject

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


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

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

#bfd=(val) ⇒ Object

interface %s

%s ip ospf bfd %s

234
235
236
237
238
239
240
241
# File 'lib/cisco_node_utils/interface_ospf.rb', line 234

def bfd=(val)
  return if val == bfd
  Feature.bfd_enable
  state = (val == default_bfd) ? 'no' : ''
  disable = val ? '' : 'disable'
  config_set('interface_ospf', 'bfd', @interface.name,
             state, disable)
end

#costObject


175
176
177
# File 'lib/cisco_node_utils/interface_ospf.rb', line 175

def cost
  config_get('interface_ospf', 'cost', @interface.name)
end

#cost=(c) ⇒ Object

interface %s

ip ospf cost %d

185
186
187
188
189
190
191
# File 'lib/cisco_node_utils/interface_ospf.rb', line 185

def cost=(c)
  if c == default_cost
    config_set('interface_ospf', 'cost', @interface.name, 'no', '')
  else
    config_set('interface_ospf', 'cost', @interface.name, '', c)
  end
end

#dead_intervalObject


208
209
210
# File 'lib/cisco_node_utils/interface_ospf.rb', line 208

def dead_interval
  config_get('interface_ospf', 'dead_interval', @interface.name)
end

#dead_interval=(interval) ⇒ Object

interface %s

ip ospf dead-interval %d

218
219
220
221
# File 'lib/cisco_node_utils/interface_ospf.rb', line 218

def dead_interval=(interval)
  config_set('interface_ospf', 'dead_interval',
             @interface.name, '', interval.to_i)
end

#default_bfdObject


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

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

#default_costObject


179
180
181
# File 'lib/cisco_node_utils/interface_ospf.rb', line 179

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

#default_dead_intervalObject


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

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

#default_hello_intervalObject


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

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

#default_message_digestObject


104
105
106
# File 'lib/cisco_node_utils/interface_ospf.rb', line 104

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

#default_message_digest_algorithm_typeObject


128
129
130
131
# File 'lib/cisco_node_utils/interface_ospf.rb', line 128

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

#default_message_digest_encryption_typeObject


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

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


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

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

#default_message_digest_passwordObject


154
155
156
# File 'lib/cisco_node_utils/interface_ospf.rb', line 154

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

#default_mtu_ignoreObject


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

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

#default_network_typeObject


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

def default_network_type
  case @interface.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


289
290
291
# File 'lib/cisco_node_utils/interface_ospf.rb', line 289

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

#default_priorityObject


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

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

#default_shutdownObject


333
334
335
# File 'lib/cisco_node_utils/interface_ospf.rb', line 333

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

#default_transmit_delayObject


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

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

#destroyObject


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cisco_node_utils/interface_ospf.rb', line 85

def destroy
  config_set('interface_ospf', 'area', @interface.name,
             'no', @ospf_name, area)
  # 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
  self.hello_interval = default_hello_interval
  config_set('interface_ospf', 'dead_interval',
             @interface.name, 'no', '')
  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

#hello_intervalObject


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

def hello_interval
  config_get('interface_ospf', 'hello_interval', @interface.name)
end

#hello_interval=(interval) ⇒ Object

interface %s

ip ospf hello-interval %d

203
204
205
206
# File 'lib/cisco_node_utils/interface_ospf.rb', line 203

def hello_interval=(interval)
  config_set('interface_ospf', 'hello_interval',
             @interface.name, '', interval.to_i)
end

#message_digestObject


108
109
110
# File 'lib/cisco_node_utils/interface_ospf.rb', line 108

def message_digest
  config_get('interface_ospf', 'message_digest', @interface.name)
end

#message_digest=(enable) ⇒ Object

interface %s

%s ip ospf authentication message-digest

114
115
116
117
118
# File 'lib/cisco_node_utils/interface_ospf.rb', line 114

def message_digest=(enable)
  return if enable == message_digest
  config_set('interface_ospf', 'message_digest', @interface.name,
             enable ? '' : 'no')
end

#message_digest_algorithm_typeObject


133
134
135
136
137
# File 'lib/cisco_node_utils/interface_ospf.rb', line 133

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

#message_digest_encryption_typeObject


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

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

#message_digest_key_idObject


124
125
126
# File 'lib/cisco_node_utils/interface_ospf.rb', line 124

def message_digest_key_id
  config_get('interface_ospf', 'message_digest_key_id', @interface.name)
end

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

interface %s

%s ip ospf message-digest-key %d %s %d %s

160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cisco_node_utils/interface_ospf.rb', line 160

def message_digest_key_set(keyid, algtype, enctype, enc)
  current_keyid = message_digest_key_id
  if keyid == default_message_digest_key_id && current_keyid != keyid
    config_set('interface_ospf', 'message_digest_key_set',
               @interface.name, 'no', current_keyid,
               '', '', '')
  elsif keyid != default_message_digest_key_id
    fail TypeError unless enc.is_a?(String)
    fail ArgumentError unless enc.length > 0
    enctype = Encryption.symbol_to_cli(enctype)
    config_set('interface_ospf', 'message_digest_key_set',
               @interface.name, '', keyid, algtype, enctype, enc)
  end
end

#message_digest_passwordObject


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

def message_digest_password
  config_get('interface_ospf', 'message_digest_password', @interface.name)
end

#mtu_ignoreObject


257
258
259
# File 'lib/cisco_node_utils/interface_ospf.rb', line 257

def mtu_ignore
  config_get('interface_ospf', 'mtu_ignore', @interface.name)
end

#mtu_ignore=(enable) ⇒ Object

interface %s

%s ip ospf mtu-ignore

263
264
265
266
# File 'lib/cisco_node_utils/interface_ospf.rb', line 263

def mtu_ignore=(enable)
  config_set('interface_ospf', 'mtu_ignore', @interface.name,
             enable ? '' : 'no')
end

#network_typeObject


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

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

#network_type=(type) ⇒ Object

interface %s

%s ip ospf network %s

281
282
283
284
285
286
287
# File 'lib/cisco_node_utils/interface_ospf.rb', line 281

def network_type=(type)
  no_cmd = (type == default_network_type) ? 'no' : ''
  network = (type == default_network_type) ? '' : type
  network = 'point-to-point' if type.to_s == 'p2p'
  config_set('interface_ospf', 'network_type', @interface.name,
             no_cmd, network)
end

#passive_interfaceObject


293
294
295
# File 'lib/cisco_node_utils/interface_ospf.rb', line 293

def passive_interface
  config_get('interface_ospf', 'passive_interface', @interface.name)
end

#passive_interface=(enable) ⇒ Object

interface %s

%s ip ospf passive-interface

299
300
301
302
303
# File 'lib/cisco_node_utils/interface_ospf.rb', line 299

def passive_interface=(enable)
  fail TypeError unless enable == true || enable == false
  config_set('interface_ospf', 'passive_interface', @interface.name,
             enable ? '' : 'no')
end

#priorityObject


305
306
307
# File 'lib/cisco_node_utils/interface_ospf.rb', line 305

def priority
  config_get('interface_ospf', 'priority', @interface.name)
end

#priority=(val) ⇒ Object

interface %s

ip ospf priority %d

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

def priority=(val)
  no_cmd = (val == default_priority) ? 'no' : ''
  pri = (val == default_priority) ? '' : val
  config_set('interface_ospf', 'priority',
             @interface.name, no_cmd, pri)
end

#shutdownObject


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

def shutdown
  config_get('interface_ospf', 'shutdown', @interface.name)
end

#shutdown=(state) ⇒ Object

interface %s

%s ip ospf shutdown

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

def shutdown=(state)
  config_set('interface_ospf', 'shutdown', @interface.name,
             state ? '' : 'no')
end

#transmit_delayObject


337
338
339
# File 'lib/cisco_node_utils/interface_ospf.rb', line 337

def transmit_delay
  config_get('interface_ospf', 'transmit_delay', @interface.name)
end

#transmit_delay=(val) ⇒ Object

interface %s

ip ospf transmit-delay %d

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

def transmit_delay=(val)
  no_cmd = (val == default_transmit_delay) ? 'no' : ''
  delay = (val == default_transmit_delay) ? '' : val
  config_set('interface_ospf', 'transmit_delay',
             @interface.name, no_cmd, delay)
end