module Netconf::RPC::Junos

Public Instance Methods

check_configuration() click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 23
def check_configuration
  validate('candidate')
end
command(cmd_str, attrs = nil) click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 132
def command(cmd_str, attrs = nil)
  rpc = Nokogiri::XML("<rpc><command>#{cmd_str}</command></rpc>").root
  Netconf::RPC.add_attributes(rpc.at('command'), attrs) if attrs
  @trans.rpc_exec(rpc)
end
commit_configuration(params = nil, attrs = nil) click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 27
def commit_configuration(params = nil, attrs = nil)
  rpc = Netconf::RPC::Builder.commit_configuration(params, attrs)
  Netconf::RPC.set_exception(rpc, Netconf::CommitError)
  @trans.rpc_exec(rpc)
end
get_configuration(*args) { |xml| ... } click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 40
def get_configuration(*args)
  filter = nil

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      nokogiri_case(arg)
    when 'Hash' then attrs = arg
    end
  end

  rpc = Nokogiri::XML('<rpc><get-configuration/></rpc>').root
  Netconf::RPC.add_attributes(rpc.first_element_child, attrs) if attrs

  if block_given?
    Nokogiri::XML::Builder.with(rpc.at('get-configuration')) do |xml|
      xml.configuration do
        yield(xml)
      end
    end
  elsif filter
    # filter must have toplevel = <configuration>
    # *MUST* use the .dup so we don't disrupt the original filter
    rpc.first_element_child << filter.dup
  end

  @trans.rpc_exec(rpc)
end
load_configuration(*args) { |xml| ... } click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 69
def load_configuration(*args)
  config = nil

  # default format is XML
  attrs = { format: 'xml' }

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      nokogiri_case(arg)
    when 'Hash' then attrs.merge! arg
    when 'Array' then config = arg.join("\n")
    when 'String' then config = arg
    end
  end

  case attrs[:format]
  when 'set'
    toplevel = 'configuration-set'
    attrs[:format] = 'text'
    attrs[:action] = 'set'
  when 'text'
    toplevel = 'configuration-text'
  when 'xml'
    toplevel = 'configuration'
  end

  rpc = Nokogiri::XML('<rpc><load-configuration/></rpc>').root
  ld_cfg = rpc.first_element_child
  Netconf::RPC.add_attributes(ld_cfg, attrs) if attrs

  if block_given?
    if attrs[:format] == 'xml'
      Nokogiri::XML::Builder.with(ld_cfg) do |xml|
        xml.send(toplevel) do
          yield(xml)
        end
      end
    else
      config = yield  # returns String | Array(of stringable)
      config = config.join("\n") if config.class == Array
    end
  end

  if config
    if attrs[:format] == 'xml'
      # config assumes toplevel = <configuration> given
      ld_cfg << config.dup # duplicate the config so as to not distrupt it
    else
      # config is stringy, so just add it as the text node
      c_node = Nokogiri::XML::Node.new(toplevel, rpc)
      c_node.content = config
      ld_cfg << c_node
    end
  end

  # set a specific exception class on this RPC so it can be
  # properlly handled by the calling enviornment

  Netconf::RPC.set_exception(rpc, Netconf::EditError)
  @trans.rpc_exec(rpc)
end
lock_configuration() click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 19
def lock_configuration
  lock('candidate')
end
nokogiri_case(arg) click to toggle source
# File lib/net/netconf/jnpr/rpc.rb, line 33
def nokogiri_case(arg)
  filter = case arg
           when Nokogiri::XML::Builder  then arg.doc.root
           when Nokogiri::XML::Document then arg.root
           else arg
end

def get_configuration(*args)
  filter = nil

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      nokogiri_case(arg)
    when 'Hash' then attrs = arg
    end
  end

  rpc = Nokogiri::XML('<rpc><get-configuration/></rpc>').root
  Netconf::RPC.add_attributes(rpc.first_element_child, attrs) if attrs

  if block_given?
    Nokogiri::XML::Builder.with(rpc.at('get-configuration')) do |xml|
      xml.configuration do
        yield(xml)
      end
    end
  elsif filter
    # filter must have toplevel = <configuration>
    # *MUST* use the .dup so we don't disrupt the original filter
    rpc.first_element_child << filter.dup
  end

  @trans.rpc_exec(rpc)
end

def load_configuration(*args)
  config = nil

  # default format is XML
  attrs = { format: 'xml' }

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      nokogiri_case(arg)
    when 'Hash' then attrs.merge! arg
    when 'Array' then config = arg.join("\n")
    when 'String' then config = arg
    end
  end

  case attrs[:format]
  when 'set'
    toplevel = 'configuration-set'
    attrs[:format] = 'text'
    attrs[:action] = 'set'
  when 'text'
    toplevel = 'configuration-text'
  when 'xml'
    toplevel = 'configuration'
  end

  rpc = Nokogiri::XML('<rpc><load-configuration/></rpc>').root
  ld_cfg = rpc.first_element_child
  Netconf::RPC.add_attributes(ld_cfg, attrs) if attrs

  if block_given?
    if attrs[:format] == 'xml'
      Nokogiri::XML::Builder.with(ld_cfg) do |xml|
        xml.send(toplevel) do
          yield(xml)
        end
      end
    else
      config = yield  # returns String | Array(of stringable)
      config = config.join("\n") if config.class == Array
    end
  end

  if config
    if attrs[:format] == 'xml'
      # config assumes toplevel = <configuration> given
      ld_cfg << config.dup # duplicate the config so as to not distrupt it
    else
      # config is stringy, so just add it as the text node
      c_node = Nokogiri::XML::Node.new(toplevel, rpc)
      c_node.content = config
      ld_cfg << c_node
    end
  end

  # set a specific exception class on this RPC so it can be
  # properlly handled by the calling enviornment

  Netconf::RPC.set_exception(rpc, Netconf::EditError)
  @trans.rpc_exec(rpc)
end # load_configuration

def command(cmd_str, attrs = nil)
  rpc = Nokogiri::XML("<rpc><command>#{cmd_str}</command></rpc>").root
  Netconf::RPC.add_attributes(rpc.at('command'), attrs) if attrs
  @trans.rpc_exec(rpc)
end

## contributed by 'dgjnpr'
def request_pfe_execute(params = nil)
  raise ArgumentError, 'Manditorary argument :target missing' unless params[:target]
  raise ArgumentError, 'Manditorary argument :command missing' unless params[:command]

  rpc_nx = Nokogiri::XML::Builder.new do |xml|
    xml.rpc do
      xml.send('request-pfe-execute') do
        xml.send('target', params[:target])
        if params[:command].class.to_s =~ /^Array/
          params[:command].each do |cmd|
            xml.send('command', cmd)
          end
        elsif params[:command].class.to_s =~ /^String/
          xml.send('command', params[:command])
        end
      end
    end
  end
  @trans.rpc_exec(rpc_nx)
end
end
request_pfe_execute(params = nil) click to toggle source

contributed by 'dgjnpr'

# File lib/net/netconf/jnpr/rpc.rb, line 139
def request_pfe_execute(params = nil)
  raise ArgumentError, 'Manditorary argument :target missing' unless params[:target]
  raise ArgumentError, 'Manditorary argument :command missing' unless params[:command]

  rpc_nx = Nokogiri::XML::Builder.new do |xml|
    xml.rpc do
      xml.send('request-pfe-execute') do
        xml.send('target', params[:target])
        if params[:command].class.to_s =~ /^Array/
          params[:command].each do |cmd|
            xml.send('command', cmd)
          end
        elsif params[:command].class.to_s =~ /^String/
          xml.send('command', params[:command])
        end
      end
    end
  end
  @trans.rpc_exec(rpc_nx)
end