Module: Netconf::RPC::Junos

Defined in:
lib/net/netconf/jnpr/rpc.rb

Instance Method Summary collapse

Instance Method Details

#check_configurationObject


22
23
24
# File 'lib/net/netconf/jnpr/rpc.rb', line 22

def check_configuration
  validate('candidate')
end

#command(cmd_str, attrs = nil) ⇒ Object

load_configuration


129
130
131
132
133
# File 'lib/net/netconf/jnpr/rpc.rb', line 129

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) ⇒ Object


26
27
28
29
30
# File 'lib/net/netconf/jnpr/rpc.rb', line 26

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) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/net/netconf/jnpr/rpc.rb', line 32

def get_configuration(*args)
  filter = nil

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      filter = case arg
               when Nokogiri::XML::Builder  then arg.doc.root
               when Nokogiri::XML::Document then arg.root
               else arg
        end
    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>
    rpc.first_element_child << filter.dup # *MUST* use the .dup so we don't disrupt the original filter
  end

  @trans.rpc_exec(rpc)
end

#load_configuration(*args) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/net/netconf/jnpr/rpc.rb', line 63

def load_configuration(*args)
  config = nil

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

  while arg = args.shift
    case arg.class.to_s
    when /^Nokogiri/
      config = case arg
               when Nokogiri::XML::Builder  then arg.doc.root
               when Nokogiri::XML::Document then arg.root
               else arg
        end
    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_configurationObject


18
19
20
# File 'lib/net/netconf/jnpr/rpc.rb', line 18

def lock_configuration
  lock('candidate')
end

#request_pfe_execute(params = nil) ⇒ Object

contributed by 'dgjnpr'

Raises:

  • (ArgumentError)

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/net/netconf/jnpr/rpc.rb', line 136

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