Class: Cisco::Utils

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

Overview

General utility class

Class Method Summary collapse

Class Method Details

.array_to_str(array, sort = true) ⇒ Object

This method converts an array to string form for ex: if the array has 1, 2 to 10, 83 to 2014, 3022 and the string will be “1,2-10,83-2014,3022”



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 202

def self.array_to_str(array, sort=true)
  farray = sort ? array.compact.uniq.sort : array.compact
  lranges = []
  unless farray.empty?
    l = array.first
    r = nil
    farray.each do |aelem|
      if r && aelem != r.succ
        if l == r
          lranges << l
        else
          lranges << Range.new(l, r)
        end
        l = aelem
      end
      r = aelem
    end
    if l == r
      lranges << l
    else
      lranges << Range.new(l, r)
    end
  end
  lranges.to_s.gsub('..', '-').delete('[').delete(']').delete(' ')
end

.bitmask_to_length(bitmask) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 154

def self.bitmask_to_length(bitmask)
  # Convert bitmask to a 32-bit integer,
  # convert that to binary, and count the 1s
  IPAddr.new(bitmask).to_i.to_s(2).count('1')
rescue IPAddr::InvalidAddressError => e
  raise ArgumentError, "bitmask '#{bitmask}' is not valid: #{e}"
end

.delta_add_remove(should, current = [], opt = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 126

def self.delta_add_remove(should, current=[], opt=nil)
  current = [] if current.nil?
  should = [] if should.nil?

  # Remove nil entries from array
  should.each(&:compact!) if depth(should) > 1
  delta = { add: should - current, remove: current - should }

  # Some cli properties cannot be updated, thus must be removed first
  return delta if opt == :updates_not_allowed

  # Delete entries from :remove if f1 is an update to an existing command
  delta[:add].each do |id, _|
    # Differentiate between comparing nested and unnested arrays by
    # checking the depth of the array.
    if depth(should) == 1
      delta[:remove].delete_if { |f1| [f1] if f1.to_s == id.to_s }
    else
      delta[:remove].delete_if { |f1, f2| [f1, f2] if f1.to_s == id.to_s }
    end
  end
  delta
end

.depth(a) ⇒ Object

Helper to build a hash of add/remove commands for a nested array. Useful for network, redistribute, etc.

 should: an array of expected cmds (manifest/recipe)
current: an array of existing cmds on the device


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

def self.depth(a)
  return 0 unless a.is_a?(Array)
  1 + depth(a[0])
end

.get_reset_range(total_range, remove_ranges) ⇒ Object

For spanning tree range based parameters, the range is very dynamic and so before the parameters are set, the rest of the range needs to be reset For ex: if the ranges 2-42 and 83-200 are getting set, and the total range of the given parameter is 1-4000 then 1,43-82,201-4000 needs to be reset. This method takes the set ranges and gives back the range to be reset



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 176

def self.get_reset_range(total_range, remove_ranges)
  fail 'invalid range' unless total_range.include?('-')
  return total_range if remove_ranges.empty?

  trs = total_range.gsub('-', '..')
  tra = trs.split('..').map { |d| Integer(d) }
  tr = tra[0]..tra[1]
  tarray = tr.to_a
  remove_ranges.each do |rr, _val|
    rarray = rr.gsub('-', '..').split(',')
    rarray.each do |elem|
      if elem.include?('..')
        elema = elem.split('..').map { |d| Integer(d) }
        ele = elema[0]..elema[1]
        tarray -= ele.to_a
      else
        tarray.delete(elem.to_i)
      end
    end
  end
  Utils.array_to_str(tarray)
end

.length_to_bitmask(length) ⇒ Object

delta_add_remove



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

def self.length_to_bitmask(length)
  IPAddr.new('255.255.255.255').mask(length).to_s
end

.nexus_i2_imageObject

Helper utility to check for older Nexus I2 images



101
102
103
104
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 101

def self.nexus_i2_image
  require_relative 'platform'
  true if Platform.image_version[/7.0.3.I2/]
end

.process_network_mask(network) ⇒ Object

Helper utility method for ip/prefix format networks. For ip/prefix format ‘1.1.1.1/24’ or ‘2000:123:38::34/64’, we need to mask the address using the prefix length so that they are converted to ‘1.1.1.0/24’ or ‘2000:123:38::/64’



110
111
112
113
114
115
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 110

def self.process_network_mask(network)
  mask = network.split('/')[1]
  address = IPAddr.new(network).to_s
  network = address + '/' + mask unless mask.nil?
  network
end

.zero_pad_macaddr(mac) ⇒ Object

Helper to 0-pad a mac address.



163
164
165
166
167
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 163

def self.zero_pad_macaddr(mac)
  return nil if mac.nil? || mac.empty?
  o1, o2, o3 = mac.split('.').map { |o| o.to_i(16).to_s(10) }
  sprintf('%04x.%04x.%04x', o1, o2, o3)
end