Module: Netconf::RPC::Junos

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

Instance Method Summary collapse

Instance Method Details

#check_configurationObject



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

def check_configuration
  validate('candidate')
end

#command(cmd_str, attrs = nil) ⇒ Object

load_configuration



132
133
134
135
136
# 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) ⇒ Object



27
28
29
30
31
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



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
128
129
130
# 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_configurationObject



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

def lock_configuration
  lock('candidate')
end

#nokogiri_case(arg) ⇒ Object



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
62
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
128
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
# 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) ⇒ Object

contributed by ‘dgjnpr’

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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