Class: Cisco::BridgeDomainVNI

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

Overview

node_utils class for bridge_domain_vni

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(bds, instantiate = true) ⇒ BridgeDomainVNI


27
28
29
30
31
32
33
34
35
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 27

def initialize(bds, instantiate=true)
  # Spaces are removed as bridge-domain cli doesn't accept value with
  # space
  @bd_ids = bds.to_s.gsub(/\s+/, '')
  fail 'bridge-domain value is empty' if @bd_ids.empty? # no empty strings

  @bd_ids_list = BridgeDomainVNI.string_to_array(@bd_ids)
  create if instantiate
end

Instance Attribute Details

#bd_idsObject (readonly)

Returns the value of attribute bd_ids


25
26
27
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 25

def bd_ids
  @bd_ids
end

#bd_ids_listObject (readonly)

Returns the value of attribute bd_ids_list


25
26
27
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 25

def bd_ids_list
  @bd_ids_list
end

Class Method Details

.range_bdsObject


41
42
43
44
45
46
47
48
49
50
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 41

def self.range_bds
  hash = {}
  bd_list = config_get('bridge_domain_vni', 'range_bds')
  return hash if bd_list.nil?

  bd_list.each do |id|
    hash[id] = BridgeDomainVNI.new(id, false)
  end
  hash
end

.string_to_array(string) ⇒ Object

This will expand the string to a list of bds as integers


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

def self.string_to_array(string)
  list = []
  narray = string.split(',')
  narray.each do |elem|
    if elem.include?('-')
      es = elem.gsub('-', '..')
      ea = es.split('..').map { |d| Integer(d) }
      er = ea[0]..ea[1]
      list << er.to_a
    else
      list << elem.to_i
    end
  end
  list.flatten
end

Instance Method Details

#createObject

This function will first add bds to the system bridge-domain and then create the bds. If bds already existing then just create. Else add the non added bds to system range first then create all. This is to avoid the idempotency issue as system add command throws error if a bd is already present in the system range.


109
110
111
112
113
114
115
116
117
118
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 109

def create
  sys_bds_array = BridgeDomainVNI.string_to_array(system_bridge_domain)
  if (@bd_ids_list - sys_bds_array).any?
    add_bds = Utils
              .array_to_str((@bd_ids_list - sys_bds_array), false)
    config_set('bridge_domain_vni', 'system_bridge_domain', oper: 'add',
                                                            bd:   add_bds)
  end
  config_set('bridge_domain_vni', 'create', bd: @bd_ids)
end

#curr_bd_vni_hashObject

Example clis; system bridge-domain 101-200 bridge-domain 101-200 bridge-domain 101-110,120,141-145,180

member vni 6001-6011,5041-5044,8000,9000

config_get('bridge_domain_vni', 'member_vni') will get the current member vni in this case 6001-6011,5041-5044,8000,9000

config_get('bridge_domain_vni', 'member_vni_bd') will get the current bd's mapped to member vni in this case 101-110,120,141-145,180

The @bd_ids_list which is created when the BridgeDomainVNI object is initialized which could be 101-110 bd range. hash_map will have 101=>6001,102=>6002…120=>6011,141=>5041…180=>9000 And the final_bd_vni hash will be based of the initialized list 101=>6001,102=>6002,103=>6003.…110=>6010 only.


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

def curr_bd_vni_hash
  final_bd_vni = {}
  curr_vni = config_get('bridge_domain_vni', 'member_vni')
  curr_bd_vni = config_get('bridge_domain_vni', 'member_vni_bd')
  return final_bd_vni if curr_vni.empty? || curr_bd_vni.empty?

  curr_vni_list = BridgeDomainVNI.string_to_array(curr_vni)
  curr_bd_vni_list = BridgeDomainVNI.string_to_array(curr_bd_vni)

  hash_map = Hash[curr_bd_vni_list.zip(curr_vni_list.map)]
  @bd_ids_list.each do |bd|
    final_bd_vni[bd.to_i] = hash_map[bd.to_i] if hash_map.key?(bd.to_i)
  end
  final_bd_vni
end

#default_member_vniObject


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

def default_member_vni
  config_get_default('bridge_domain_vni', 'member_vni')
end

#destroyObject


120
121
122
123
124
125
126
127
128
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 120

def destroy
  bd_vni = curr_bd_vni_hash
  bd_val = bd_vni.keys.join(',')
  vni_val = bd_vni.values.join(',')
  return '' if vni_val.empty?
  config_set('bridge_domain_vni', 'member_vni', bd: bd_val,
             membstate: 'no', membvni: vni_val)
  config_set('bridge_domain_vni', 'vni', state: 'no', vni: vni_val)
end

#member_vniObject

Bridge-Domain member vni assigning case Not all the bds created or initialized in this class context can have the member vni's mapped. So will get the vnis of the ones which are mapped to the bds. Eg: Suppose the bd mapping is as below on the switch bridge-domain 100,105,107-109,150

member vni 5000, 8000, 5007-5008, 7000, 5050

If puppet layer tries to get values of 100,105,107 bds as defined in manifest then the final_bd_vni map which is returned will contain only these mappings as below; '5000,8000,5007' This is also to handle the idempotence case, and if any mismatch then set the member_vni


147
148
149
150
151
152
153
154
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 147

def member_vni
  bd_vni_hash = curr_bd_vni_hash
  ret_list = []
  @bd_ids_list.each do |bd|
    ret_list << bd_vni_hash[bd]
  end
  Utils.array_to_str(ret_list, false)
end

#member_vni=(val) ⇒ Object

This member_vni mapping will be executed only when the val is not empty else it will be treated as a 'no' cli and executed as required. If the mappings do not match in any fashion then cli normally returns a failure which will be handled. During set of member vni we first see if any bd is mapped to some other vni, if yes then remove that one and apply the new vni. Eg: bridge-domain 100-110

member vni 5100-5110

Now the manifest is changed to 5100-5105,6106-6110, so hence first 5106-5110 mapping is removed and then 6106-6110 is applied.


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
194
195
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 166

def member_vni=(val)
  val = val.to_s
  Feature.vni_enable
  bd_vni = curr_bd_vni_hash
  if val.empty?
    bd_val = bd_vni.keys.join(',')
    vni_val = bd_vni.values.join(',')
    return '' if vni_val.empty?
    config_set('bridge_domain_vni', 'member_vni', bd: bd_val,
               membstate: 'no', membvni: vni_val)
    config_set('bridge_domain_vni', 'vni', state: 'no', vni: vni_val)
  else
    unless bd_vni.empty?
      inp_vni_list = BridgeDomainVNI.string_to_array(val.to_s)
      inp_bd_vni_hash = Hash[@bd_ids_list.zip(inp_vni_list)]

      temp_hash = bd_vni.to_a.keep_if { |k, _v| inp_bd_vni_hash.key? k }
      rem_hash = (temp_hash.to_a - inp_bd_vni_hash.to_a).to_h

      rm_bd = rem_hash.keys.join(',')
      rm_vni = rem_hash.values.join(',')
      config_set('bridge_domain_vni', 'member_vni', bd: rm_bd,
                 membstate: 'no', membvni: rm_vni)
      config_set('bridge_domain_vni', 'vni', state: 'no', vni: rm_vni)
    end
    config_set('bridge_domain_vni', 'vni', state: '', vni: val)
    config_set('bridge_domain_vni', 'member_vni', bd: @bd_ids,
               membstate: '', membvni: val)
  end
end

#system_bridge_domainObject

getter for system bridge-domain


202
203
204
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 202

def system_bridge_domain
  config_get('bridge_domain_vni', 'system_bridge_domain')
end

#to_sObject


37
38
39
# File 'lib/cisco_node_utils/bridge_domain_vni.rb', line 37

def to_s
  "Bridge Domain #{bd_ids}"
end