class Rbeapi::SwitchConfig::Section

Section class

A switch configuration section consists of the command line that enters into the configuration mode, an array of command strings that are executed in the current configuration mode, a reference to the parent section, and an array of refereces to all sub-sections contained within this section. A sub-section is a nested configuration mode.

Read Accessors for following class instance variables:

line: <string>,
parent: <Section>,
cmds: array<strings>,
children: array<Section>

Attributes

children[R]
cmds[R]
line[R]
parent[R]

Public Class Methods

new(line, parent) click to toggle source

The Section class contains a parsed section of switch config.

@param config [String] A string containing the switch configuration.

@return [Section] Returns an instance of Section

# File lib/rbeapi/switchconfig.rb, line 68
def initialize(line, parent)
  @line = line
  @parent = parent
  @cmds = []
  @children = []
end

Public Instance Methods

add_child(child) click to toggle source

Add a child to the end of the children array.

@param child [Section] A Section class instance.

# File lib/rbeapi/switchconfig.rb, line 79
def add_child(child)
  @children.push(child)
end
add_cmd(cmd) click to toggle source

Add a cmd to the end of the cmds array if it is not already in the cmd array.

@param cmd [String] A command string that is added to the cmds array.

# File lib/rbeapi/switchconfig.rb, line 88
def add_cmd(cmd)
  @cmds.push(cmd) unless @cmds.include?(cmd)
end
compare(section2) click to toggle source

Campare a Section class to the current section. The comparison will recurse through all the children in the Sections. The parent is ignored at the top level section.

@param section2 [Section] An instance of a Section class to compare.

@return [Array<Section>] Returns an array of 2 Section objects. The

first Section object contains the portion of self that is not
in section2. The second Section object returned is the portion of
section2 that is not in self.
# File lib/rbeapi/switchconfig.rb, line 182
def compare(section2)
  raise '@line does not equal section2.line' if @line != section2.line

  results = []
  # Compare self with section2
  results[0] = compare_r(section2)
  # Compare section2 with self
  results[1] = section2.compare_r(self)
  results
end
compare_r(section2) click to toggle source

Campare method to compare two Section classes. The comparison will recurse through all the children in the Sections. The parent is ignored at the top level section. Only call this method if self and section2 have the same line.

@param section2 [Section] An instance of a Section class to compare.

@return [Section] The Section object contains the portion of self

that is not in section2.
# File lib/rbeapi/switchconfig.rb, line 129
def compare_r(section2)
  raise '@line must equal section2.line' if @line != section2.line

  # XXX Need to have a list of exceptions of mode commands that
  # support default. If all the commands have been removed from
  # that section in the new config then the old config just wants
  # to default the mode command.
  # ex: spanning-tree mst configuration
  #       instance 1 vlan  1
  # Currently generates this error:
  # '   default instance 1 vlan  1' failed: invalid command

  results = Section.new(@line, nil)

  # Compare the commands
  diff_cmds = _compare_cmds(section2.cmds)
  diff_cmds.each do |cmd|
    results.add_cmd(cmd)
  end

  # Using a depth first search to recursively descend through the
  # children doing a comparison.
  @children.each do |s1_child|
    s2_child = section2.get_child(s1_child.line)
    if s2_child
      # Sections Match based on the line. Compare the children
      # and if there are differences add them to the results.
      res = s1_child.compare_r(s2_child)
      if !res.children.empty? || !res.cmds.empty?
        results.add_child(res)
        results.add_cmd(s1_child.line)
      end
    else
      # Section 1 has child, but section 2 does not, add to results
      results.add_child(s1_child.clone)
      results.add_cmd(s1_child.line)
    end
  end

  results
end
get_child(line) click to toggle source

Return the child that has the specified line (command mode).

@param line [String] The mode command for this section.

# File lib/rbeapi/switchconfig.rb, line 96
def get_child(line)
  @children.each do |child|
    return child if child.line == line
  end
  nil
end

Private Instance Methods

_compare_cmds(cmds2) click to toggle source

Private campare method to compare the commands between two Section classes.

@param cmds2 [Array<String>] An array of commands.

@return [Array<String>] The array of commands in @cmds that are not

in cmds2. The array is empty if @cmds equals cmds2.
# File lib/rbeapi/switchconfig.rb, line 111
def _compare_cmds(cmds2)
  c1 = Set.new(@cmds)
  c2 = Set.new(cmds2)
  # Compare the commands and return the difference as an array of strings
  c1.difference(c2).to_a
end