Class: Rbeapi::SwitchConfig::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/rbeapi/switchconfig.rb

Overview

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>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, parent) ⇒ Section

The Section class contains a parsed section of switch config.

Parameters:

  • config (String)

    A string containing the switch configuration.



68
69
70
71
72
73
# File 'lib/rbeapi/switchconfig.rb', line 68

def initialize(line, parent)
  @line = line
  @parent = parent
  @cmds = []
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



59
60
61
# File 'lib/rbeapi/switchconfig.rb', line 59

def children
  @children
end

#cmdsObject (readonly)

Returns the value of attribute cmds.



58
59
60
# File 'lib/rbeapi/switchconfig.rb', line 58

def cmds
  @cmds
end

#lineObject (readonly)

Returns the value of attribute line.



56
57
58
# File 'lib/rbeapi/switchconfig.rb', line 56

def line
  @line
end

#parentObject (readonly)

Returns the value of attribute parent.



57
58
59
# File 'lib/rbeapi/switchconfig.rb', line 57

def parent
  @parent
end

Instance Method Details

#add_child(child) ⇒ Object

Add a child to the end of the children array.

Parameters:

  • child (Section)

    A Section class instance.



79
80
81
# File 'lib/rbeapi/switchconfig.rb', line 79

def add_child(child)
  @children.push(child)
end

#add_cmd(cmd) ⇒ Object

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

Parameters:

  • cmd (String)

    A command string that is added to the cmds array.



88
89
90
# File 'lib/rbeapi/switchconfig.rb', line 88

def add_cmd(cmd)
  @cmds.push(cmd) unless @cmds.include?(cmd)
end

#compare(section2) ⇒ Array<Section>

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.

Parameters:

  • section2 (Section)

    An instance of a Section class to compare.

Returns:

  • (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.



182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Section

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.

Parameters:

  • section2 (Section)

    An instance of a Section class to compare.

Returns:

  • (Section)

    The Section object contains the portion of self that is not in section2.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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) ⇒ Object

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

Parameters:

  • line (String)

    The mode command for this section.



96
97
98
99
100
101
# File 'lib/rbeapi/switchconfig.rb', line 96

def get_child(line)
  @children.each do |child|
    return child if child.line == line
  end
  nil
end