Class: Cisco::RouterBgpNeighbor

Inherits:
NodeUtil
  • Object
show all
Defined in:
lib/cisco_node_utils/bgp_neighbor.rb

Overview

RouterBgpNeighbor - node utility class for BGP neighbor configs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

config_get, #config_get, config_get_default, #config_get_default, #config_set, config_set, #node, node, #show

Constructor Details

#initialize(asn, vrf, nbr, instantiate = true) ⇒ RouterBgpNeighbor

Returns a new instance of RouterBgpNeighbor.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 30

def initialize(asn, vrf, nbr, instantiate=true)
  fail TypeError unless nbr.is_a?(String)
  # for IP/prefix format, such as "1.1.1.1/24" or "2000:123:38::34/64",
  # we need to mask the address using prefix length, so that it becomes
  # something like "1.1.1.0/24" or "2000:123:38::/64"
  @nbr = Utils.process_network_mask(nbr)
  @asn = RouterBgp.validate_asnum(asn)
  @vrf = vrf
  @get_args = @set_args = { asnum: @asn, nbr: @nbr }
  @get_args[:vrf] = @set_args[:vrf] = vrf if vrf != 'default'

  create if instantiate
end

Instance Attribute Details

#asnObject (readonly)

Returns the value of attribute asn.



28
29
30
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 28

def asn
  @asn
end

#nbrObject (readonly)

Returns the value of attribute nbr.



28
29
30
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 28

def nbr
  @nbr
end

#vrfObject (readonly)

Returns the value of attribute vrf.



28
29
30
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 28

def vrf
  @vrf
end

Class Method Details

.neighborsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 44

def self.neighbors
  hash = {}
  RouterBgp.routers.each do |asn, vrf|
    hash[asn] = {}
    vrf.each_key do |vrf_id|
      get_args = { asnum: asn }
      get_args[:vrf] = vrf_id unless vrf_id == 'default'
      neighbor_list = config_get('bgp_neighbor', 'all_neighbors', get_args)
      next if neighbor_list.nil?

      hash[asn][vrf_id] = {}
      neighbor_list.each do |nbr|
        hash[asn][vrf_id][nbr] = RouterBgpNeighbor.new(asn, vrf_id,
                                                       nbr, false)
      end
    end
  end
  hash
rescue Cisco::CliError => e
  # Raise the error unless the error message contains "Syntax error", which
  # means the error was caused by feature is not enabled.
  raise unless e.clierror =~ /Syntax error/
  return {}
end

Instance Method Details

#capability_negotiationObject



130
131
132
133
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 130

def capability_negotiation
  result = config_get('bgp_neighbor', 'capability_negotiation', @get_args)
  result ? false : true
end

#capability_negotiation=(val) ⇒ Object



123
124
125
126
127
128
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 123

def capability_negotiation=(val)
  # the cli is "dont-capability-negotiate". Therefore when val is true, we
  # need to set state to "no"
  set_args_keys(state: (val) ? 'no' : '')
  config_set('bgp_neighbor', 'capability_negotiation', @set_args)
end

#connected_checkObject



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

def connected_check
  result = config_get('bgp_neighbor', 'connected_check', @get_args)
  result ? false : true
end

#connected_check=(val) ⇒ Object



107
108
109
110
111
112
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 107

def connected_check=(val)
  # the cli is "disable-connected-check", therefore when val is true, we
  # need to set set state to "no"
  set_args_keys(state: (val) ? 'no' : '')
  config_set('bgp_neighbor', 'connected_check', @set_args)
end

#createObject



69
70
71
72
73
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 69

def create
  Feature.bgp_enable
  set_args_keys(state: '')
  config_set('bgp', 'create_destroy_neighbor', @set_args)
end

#default_capability_negotiationObject



135
136
137
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 135

def default_capability_negotiation
  config_get_default('bgp_neighbor', 'capability_negotiation')
end

#default_connected_checkObject



119
120
121
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 119

def default_connected_check
  config_get_default('bgp_neighbor', 'connected_check')
end

#default_descriptionObject



103
104
105
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 103

def default_description
  config_get_default('bgp_neighbor', 'description')
end

#default_dynamic_capabilityObject



149
150
151
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 149

def default_dynamic_capability
  config_get_default('bgp_neighbor', 'dynamic_capability')
end

#default_ebgp_multihopObject



164
165
166
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 164

def default_ebgp_multihop
  config_get_default('bgp_neighbor', 'ebgp_multihop')
end

#default_local_asObject



181
182
183
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 181

def default_local_as
  config_get_default('bgp_neighbor', 'local_as').to_s
end

#default_log_neighbor_changesObject



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

def default_log_neighbor_changes
  result = config_get_default('bgp_neighbor', 'log_neighbor_changes')
  result.to_sym unless result.nil?
end

#default_low_memory_exemptObject



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

def default_low_memory_exempt
  config_get_default('bgp_neighbor', 'low_memory_exempt')
end

#default_maximum_peersObject



232
233
234
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 232

def default_maximum_peers
  config_get_default('bgp_neighbor', 'maximum_peers')
end

#default_passwordObject



256
257
258
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 256

def default_password
  config_get_default('bgp_neighbor', 'password')
end

#default_password_typeObject



265
266
267
268
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 265

def default_password_type
  result = config_get_default('bgp_neighbor', 'password_type')
  Encryption.cli_to_symbol(result)
end

#default_remote_asObject



283
284
285
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 283

def default_remote_as
  config_get_default('bgp_neighbor', 'remote_as').to_s
end

#default_remove_private_asObject



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

def default_remove_private_as
  result = config_get_default('bgp_neighbor', 'remove_private_as')
  result.to_sym
end

#default_shutdownObject



319
320
321
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 319

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

#default_suppress_4_byte_asObject



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

def default_suppress_4_byte_as
  config_get_default('bgp_neighbor', 'suppress_4_byte_as')
end

#default_timers_holdtimeObject



370
371
372
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 370

def default_timers_holdtime
  config_get_default('bgp_neighbor', 'timers_holdtime')
end

#default_timers_keepaliveObject



366
367
368
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 366

def default_timers_keepalive
  config_get_default('bgp_neighbor', 'timers_keepalive')
end

#default_timers_keepalive_holdObject



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

def default_timers_keepalive_hold
  ["#{default_timers_keepalive}", "#{default_timers_holdtime}"]
end

#default_transport_passive_onlyObject



388
389
390
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 388

def default_transport_passive_only
  config_get_default('bgp_neighbor', 'transport_passive_only')
end

#default_update_sourceObject



406
407
408
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 406

def default_update_source
  config_get_default('bgp_neighbor', 'update_source')
end

#descriptionObject



99
100
101
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 99

def description
  config_get('bgp_neighbor', 'description', @get_args)
end

#description=(desc) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 91

def description=(desc)
  fail TypeError unless desc.is_a?(String)
  desc.strip!
  set_args_keys(state: desc.empty? ? 'no' : '',
                desc:  desc)
  config_set('bgp_neighbor', 'description', @set_args)
end

#destroyObject



75
76
77
78
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 75

def destroy
  set_args_keys(state: 'no')
  config_set('bgp', 'create_destroy_neighbor', @set_args)
end

#dynamic_capabilityObject



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

def dynamic_capability
  result = config_get('bgp_neighbor', 'dynamic_capability', @get_args)
  result ? true : false
end

#dynamic_capability=(val) ⇒ Object



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

def dynamic_capability=(val)
  set_args_keys(state: (val) ? '' : 'no')
  config_set('bgp_neighbor', 'dynamic_capability', @set_args)
end

#ebgp_multihopObject



159
160
161
162
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 159

def ebgp_multihop
  result = config_get('bgp_neighbor', 'ebgp_multihop', @get_args)
  result.nil? ? default_ebgp_multihop : result.to_i
end

#ebgp_multihop=(ttl) ⇒ Object



153
154
155
156
157
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 153

def ebgp_multihop=(ttl)
  set_args_keys(state: (ttl == default_ebgp_multihop) ? 'no' : '',
                ttl:   (ttl == default_ebgp_multihop) ? '' : ttl)
  config_set('bgp_neighbor', 'ebgp_multihop', @set_args)
end

#local_asObject



177
178
179
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 177

def local_as
  config_get('bgp_neighbor', 'local_as', @get_args).to_s
end

#local_as=(val) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 168

def local_as=(val)
  if val == default_local_as
    set_args_keys(state: 'no', local_as: '')
  else
    set_args_keys(state: '', local_as: val)
  end
  config_set('bgp_neighbor', 'local_as', @set_args)
end

#log_neighbor_changesObject



196
197
198
199
200
201
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 196

def log_neighbor_changes
  result = config_get('bgp_neighbor', 'log_neighbor_changes', @get_args)
  return default_log_neighbor_changes if result.nil?
  return :disable if /disable/.match(result.first)
  :enable
end

#log_neighbor_changes=(val) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 185

def log_neighbor_changes=(val)
  val = val.to_sym
  if val == default_log_neighbor_changes
    set_args_keys(state: 'no', disable: '')
  else
    set_args_keys(state:   '',
                  disable: (val == :enable) ? '' : 'disable')
  end
  config_set('bgp_neighbor', 'log_neighbor_changes', @set_args)
end

#low_memory_exemptObject



213
214
215
216
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 213

def low_memory_exempt
  result = config_get('bgp_neighbor', 'low_memory_exempt', @get_args)
  result ? true : false
end

#low_memory_exempt=(val) ⇒ Object



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

def low_memory_exempt=(val)
  set_args_keys(state: (val) ? '' : 'no')
  config_set('bgp_neighbor', 'low_memory_exempt', @set_args)
end

#maximum_peersObject



228
229
230
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 228

def maximum_peers
  config_get('bgp_neighbor', 'maximum_peers', @get_args)
end

#maximum_peers=(val) ⇒ Object



222
223
224
225
226
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 222

def maximum_peers=(val)
  set_args_keys(state: (val == default_maximum_peers) ? 'no' : '',
                num:   (val == default_maximum_peers) ? '' : val)
  config_set('bgp_neighbor', 'maximum_peers', @set_args)
end

#passwordObject



252
253
254
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 252

def password
  config_get('bgp_neighbor', 'password', @get_args)
end

#password_set(val, type = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 236

def password_set(val, type=nil)
  val = val.to_s
  if val.strip.empty?
    set_args_keys(state: 'no', type: '', passwd: '')
  elsif type.nil?
    set_args_keys(state:  '',
                  type:   Encryption.symbol_to_cli(default_password_type),
                  passwd: val.to_s)
  else
    set_args_keys(state:  '',
                  type:   Encryption.symbol_to_cli(type),
                  passwd: val.to_s)
  end
  config_set('bgp_neighbor', 'password', @set_args)
end

#password_typeObject



260
261
262
263
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 260

def password_type
  result = config_get('bgp_neighbor', 'password_type', @get_args)
  Encryption.cli_to_symbol(result.to_i)
end

#remote_asObject



279
280
281
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 279

def remote_as
  config_get('bgp_neighbor', 'remote_as', @get_args).to_s
end

#remote_as=(val) ⇒ Object



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

def remote_as=(val)
  if val == default_remote_as
    set_args_keys(state: 'no', remote_as: '')
  else
    set_args_keys(state: '', remote_as: val)
  end
  config_set('bgp_neighbor', 'remote_as', @set_args)
end

#remove_private_asObject



298
299
300
301
302
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 298

def remove_private_as
  result = config_get('bgp_neighbor', 'remove_private_as', @get_args)
  return default_remove_private_as if result.nil?
  result.first.nil? ? :enable : result.first.to_sym
end

#remove_private_as=(val) ⇒ Object



287
288
289
290
291
292
293
294
295
296
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 287

def remove_private_as=(val)
  val = val.to_sym
  if val == default_remove_private_as
    set_args_keys(state: 'no', option: '')
  else
    set_args_keys(state:  '',
                  option: (val == :enable) ? '' : val.to_s)
  end
  config_set('bgp_neighbor', 'remove_private_as', @set_args)
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName



86
87
88
89
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 86

def set_args_keys(hash={}) # rubocop:disable Style/AccessorMethodName
  set_args_keys_default
  @set_args = @set_args.merge!(hash) unless hash.empty?
end

#set_args_keys_defaultObject



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

def set_args_keys_default
  keys = { asnum: @asn, nbr: @nbr }
  keys[:vrf] = @vrf unless @vrf == 'default'
  @set_args = keys
end

#shutdownObject



314
315
316
317
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 314

def shutdown
  result = config_get('bgp_neighbor', 'shutdown', @get_args)
  result ? true : false
end

#shutdown=(val) ⇒ Object



309
310
311
312
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 309

def shutdown=(val)
  set_args_keys(state: (val) ? '' : 'no')
  config_set('bgp_neighbor', 'shutdown', @set_args)
end

#suppress_4_byte_asObject



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

def suppress_4_byte_as
  result = config_get('bgp_neighbor', 'suppress_4_byte_as', @get_args)
  result ? true : false
end

#suppress_4_byte_as=(val) ⇒ Object



323
324
325
326
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 323

def suppress_4_byte_as=(val)
  set_args_keys(state: (val) ? '' : 'no')
  config_set('bgp_neighbor', 'suppress_4_byte_as', @set_args)
end

#timers_holdtimeObject



360
361
362
363
364
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 360

def timers_holdtime
  _keepalive, hold = timers_keepalive_hold
  return default_timers_holdtime if hold.nil?
  hold.to_i
end

#timers_keepaliveObject



354
355
356
357
358
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 354

def timers_keepalive
  keepalive, _hold = timers_keepalive_hold
  return default_timers_keepalive if keepalive.nil?
  keepalive.to_i
end

#timers_keepalive_holdObject



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

def timers_keepalive_hold
  match = config_get('bgp_neighbor', 'timers_keepalive_hold', @get_args)
  match.nil? ? default_timers_keepalive_hold : match
end

#timers_set(keepalive, hold) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 337

def timers_set(keepalive, hold)
  if keepalive == default_timers_keepalive &&
     hold == default_timers_holdtime
    set_args_keys(state: 'no', keepalive: timers_keepalive,
                  hold: timers_holdtime)
  else
    set_args_keys(state: '', keepalive: keepalive,
                  hold: hold)
  end
  config_set('bgp_neighbor', 'timers_keepalive_hold', @set_args)
end

#transport_passive_onlyObject



383
384
385
386
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 383

def transport_passive_only
  result = config_get('bgp_neighbor', 'transport_passive_only', @get_args)
  result ? true : false
end

#transport_passive_only=(val) ⇒ Object



378
379
380
381
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 378

def transport_passive_only=(val)
  set_args_keys(state: (val) ? '' : 'no')
  config_set('bgp_neighbor', 'transport_passive_only', @set_args)
end

#update_sourceObject



401
402
403
404
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 401

def update_source
  result = config_get('bgp_neighbor', 'update_source', @get_args)
  result.downcase.strip
end

#update_source=(val) ⇒ Object



392
393
394
395
396
397
398
399
# File 'lib/cisco_node_utils/bgp_neighbor.rb', line 392

def update_source=(val)
  if val.strip == default_update_source
    set_args_keys(state: 'no', interface: update_source)
  else
    set_args_keys(state: '', interface: val)
  end
  config_set('bgp_neighbor', 'update_source', @set_args)
end