class Netconf::JunosConfig

Constants

DELETE
REPLACE

Attributes

collection[R]
doc[R]

Public Class Methods

new(options) click to toggle source
# File lib/net/netconf/jnpr/junos_config.rb, line 15
def initialize(options)
  @doc_ele = 'configuration'

  if options == :TOP
    @doc = Nokogiri::XML('<#{@doc_ele}/>')
    return
  end

  unless options[:TOP].nil?
    @doc_ele = options[:TOP]
    @doc = Nokogiri::XML('<#{@doc_ele}/>')
    return
  end

  unless defined? @collection
    edit = "#{@doc_ele}/#{options[:edit].strip}"
    @at_name = edit[edit.rindex('/') + 1, edit.length]
    @edit_path = edit
    @collection = Hash.new
    @to_xml = options[:build]
  end
end

Public Instance Methods

<<(obj) click to toggle source
# File lib/net/netconf/jnpr/junos_config.rb, line 38
def <<(obj)
  if defined? @collection
    @collection[obj[:name]] = obj
  elsif defined? @doc
    obj.build_xml(@doc)
  else
    # TBD:error
  end
end
build_xml(ng_xml, &block) click to toggle source
# File lib/net/netconf/jnpr/junos_config.rb, line 48
def build_xml(ng_xml, &block)
  at_ele = ng_xml.at(@edit_path)
  if at_ele.nil?
    # no xpath anchor point, so we need to create it
    at_ele = edit_path(ng_xml, @edit_path)
  end
  build_proc = (block_given?) ? block : @to_xml

  @collection.each do |_k, v|
    with(at_ele) do |e|
      build_proc.call(e, v)
    end
  end
end
edit_path( ng_xml, xpath ) click to toggle source
# File lib/net/netconf/jnpr/junos_config.rb, line 63
def edit_path( ng_xml, xpath )
  # junos configuration always begins with
  # the 'configuration' element, so don't make
  # the user enter it all the time

  cfg_xpath = xpath
  dot = ng_xml.at(cfg_xpath)
  return dot if dot

  # we need to determine how much of the xpath
  # we need to create.  walk down the xpath
  # children to determine what exists and
  # what needs to be added

  xpath_a = cfg_xpath.split('/')
  need_a = []
  until xpath_a.empty? || dot
    need_a.unshift xpath_a.pop
    check_xpath = xpath_a.join('/')
    dot = ng_xml.at( check_xpath )
  end

  # start at the deepest level of what
  # actually exists and then start adding
  # the children that were missing

  dot = ng_xml.at(xpath_a.join('/'))
  need_a.each do |ele|
    dot = dot.add_child(Nokogiri::XML::Node.new(ele, ng_xml))
  end

  dot
end
with(ng_xml, &block) click to toggle source
# File lib/net/netconf/jnpr/junos_config.rb, line 97
def with(ng_xml, &block)
  Nokogiri::XML::Builder.with(ng_xml, &block)
end