netconf 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
- require 'net/netconf'
2
- require 'net/netconf/jnpr/rpc'
3
- require 'net/netconf/jnpr/junos_config'
4
-
5
- module Netconf::Junos
6
- NETCONF_CLI = "junoscript netconf need-trailer"
7
- NETCONF_SHELL = "exec xml-mode netconf need-trailer"
8
- end
9
-
1
+ require 'net/netconf'
2
+ require 'net/netconf/jnpr/rpc'
3
+ require 'net/netconf/jnpr/junos_config'
4
+
5
+ module Netconf::Junos
6
+ NETCONF_CLI = "junoscript netconf need-trailer"
7
+ NETCONF_SHELL = "exec xml-mode netconf need-trailer"
8
+ end
9
+
@@ -1,14 +1,14 @@
1
- require 'net/netconf'
2
- require 'net/netconf/ioproc'
3
- require 'net/netconf/jnpr'
4
-
5
- module Netconf
6
- module Junos
7
- module IOProc
8
- def trans_open
9
- @trans = IO.popen( "xml-mode netconf need-trailer", "r+")
10
- self
11
- end
12
- end
13
- end
14
- end
1
+ require 'net/netconf'
2
+ require 'net/netconf/ioproc'
3
+ require 'net/netconf/jnpr'
4
+
5
+ module Netconf
6
+ module Junos
7
+ module IOProc
8
+ def trans_open
9
+ @trans = IO.popen( "xml-mode netconf need-trailer", "r+")
10
+ self
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,140 +1,162 @@
1
- ## -----------------------------------------------------------------------
2
- ## This file contains the Junos specific RPC methods that are generated
3
- ## specifically and different as generated by the Netconf::RPC::Builder
4
- ## module. These are specifically the following:
5
- ##
6
- ## get_configuration - alternative NETCONF: 'get-config'
7
- ## load_configuration - alternative NETCONF: 'edit-config'
8
- ## lock_configuration - alternative NETCONF: 'lock'
9
- ## commit_configuration - alternative NETCONF: 'commit'
10
- ##
11
- ## note: unlock_configuration is not included in this file since
12
- ## the Netconf::RPC::Builder works "as-is" in this case
13
- ## -----------------------------------------------------------------------
14
-
15
- module Netconf
16
- module RPC
17
- module Junos
18
-
19
- def lock_configuration
20
- lock( 'candidate' )
21
- end
22
-
23
- def check_configuration
24
- validate( 'candidate' )
25
- end
26
-
27
- def commit_configuration( params = nil, attrs = nil )
28
- rpc = Netconf::RPC::Builder.commit_configuration( params, attrs )
29
- Netconf::RPC.set_exception( rpc, Netconf::CommitError )
30
- @trans.rpc_exec( rpc )
31
- end
32
-
33
- def get_configuration( *args )
34
-
35
- filter = nil
36
-
37
- while arg = args.shift
38
- case arg.class.to_s
39
- when /^Nokogiri/
40
- filter = case arg
41
- when Nokogiri::XML::Builder then arg.doc.root
42
- when Nokogiri::XML::Document then arg.root
43
- else arg
44
- end
45
- when 'Hash' then attrs = arg
46
- end
47
- end
48
-
49
- rpc = Nokogiri::XML('<rpc><get-configuration/></rpc>').root
50
- Netconf::RPC.add_attributes( rpc.first_element_child, attrs ) if attrs
51
-
52
- if block_given?
53
- Nokogiri::XML::Builder.with(rpc.at( 'get-configuration' )){ |xml|
54
- xml.configuration {
55
- yield( xml )
56
- }}
57
- elsif filter
58
- # filter must have toplevel = <configuration>
59
- rpc.first_element_child << filter.dup # *MUST* use the .dup so we don't disrupt the original filter
60
- end
61
-
62
- @trans.rpc_exec( rpc )
63
- end
64
-
65
- def load_configuration( *args )
66
-
67
- config = nil
68
-
69
- # default format is XML
70
- attrs = { :format => 'xml' }
71
-
72
- while arg = args.shift
73
- case arg.class.to_s
74
- when /^Nokogiri/
75
- config = case arg
76
- when Nokogiri::XML::Builder then arg.doc.root
77
- when Nokogiri::XML::Document then arg.root
78
- else arg
79
- end
80
- when 'Hash' then attrs.merge! arg
81
- when 'Array' then config = arg.join("\n")
82
- when 'String' then config = arg
83
- end
84
- end
85
-
86
- case attrs[:format]
87
- when 'set'
88
- toplevel = 'configuration-set'
89
- attrs[:format] = 'text'
90
- attrs[:action] = 'set'
91
- when 'text'
92
- toplevel = 'configuration-text'
93
- when 'xml'
94
- toplevel = 'configuration'
95
- end
96
-
97
- rpc = Nokogiri::XML('<rpc><load-configuration/></rpc>').root
98
- ld_cfg = rpc.first_element_child
99
- Netconf::RPC.add_attributes( ld_cfg, attrs ) if attrs
100
-
101
- if block_given?
102
- if attrs[:format] == 'xml'
103
- Nokogiri::XML::Builder.with( ld_cfg ){ |xml|
104
- xml.send( toplevel ) {
105
- yield( xml )
106
- }}
107
- else
108
- config = yield # returns String | Array(of stringable)
109
- config = config.join("\n") if config.class == Array
110
- end
111
- end
112
-
113
- if config
114
- if attrs[:format] == 'xml'
115
- # config assumes toplevel = <configuration> given
116
- ld_cfg << config.dup # duplicate the config so as to not distrupt it
117
- else
118
- # config is stringy, so just add it as the text node
119
- c_node = Nokogiri::XML::Node.new( toplevel, rpc )
120
- c_node.content = config
121
- ld_cfg << c_node
122
- end
123
- end
124
-
125
- # set a specific exception class on this RPC so it can be
126
- # properlly handled by the calling enviornment
127
-
128
- Netconf::RPC::set_exception( rpc, Netconf::EditError )
129
- @trans.rpc_exec( rpc )
130
- end # load_configuration
131
-
132
- def command( cmd_str, attrs = nil )
133
- rpc = Nokogiri::XML("<rpc><command>#{cmd_str}</command></rpc>").root
134
- Netconf::RPC.add_attributes( rpc.at('command'), attrs ) if attrs
135
- @trans.rpc_exec( rpc )
136
- end
137
-
138
- end # module: JUNOS
139
- end # module: RPC
140
- end # module: Netconf
1
+ ## -----------------------------------------------------------------------
2
+ ## This file contains the Junos specific RPC methods that are generated
3
+ ## specifically and different as generated by the Netconf::RPC::Builder
4
+ ## module. These are specifically the following:
5
+ ##
6
+ ## get_configuration - alternative NETCONF: 'get-config'
7
+ ## load_configuration - alternative NETCONF: 'edit-config'
8
+ ## lock_configuration - alternative NETCONF: 'lock'
9
+ ## commit_configuration - alternative NETCONF: 'commit'
10
+ ##
11
+ ## note: unlock_configuration is not included in this file since
12
+ ## the Netconf::RPC::Builder works "as-is" in this case
13
+ ## -----------------------------------------------------------------------
14
+
15
+ module Netconf
16
+ module RPC
17
+ module Junos
18
+
19
+ def lock_configuration
20
+ lock( 'candidate' )
21
+ end
22
+
23
+ def check_configuration
24
+ validate( 'candidate' )
25
+ end
26
+
27
+ def commit_configuration( params = nil, attrs = nil )
28
+ rpc = Netconf::RPC::Builder.commit_configuration( params, attrs )
29
+ Netconf::RPC.set_exception( rpc, Netconf::CommitError )
30
+ @trans.rpc_exec( rpc )
31
+ end
32
+
33
+ def get_configuration( *args )
34
+
35
+ filter = nil
36
+
37
+ while arg = args.shift
38
+ case arg.class.to_s
39
+ when /^Nokogiri/
40
+ filter = case arg
41
+ when Nokogiri::XML::Builder then arg.doc.root
42
+ when Nokogiri::XML::Document then arg.root
43
+ else arg
44
+ end
45
+ when 'Hash' then attrs = arg
46
+ end
47
+ end
48
+
49
+ rpc = Nokogiri::XML('<rpc><get-configuration/></rpc>').root
50
+ Netconf::RPC.add_attributes( rpc.first_element_child, attrs ) if attrs
51
+
52
+ if block_given?
53
+ Nokogiri::XML::Builder.with(rpc.at( 'get-configuration' )){ |xml|
54
+ xml.configuration {
55
+ yield( xml )
56
+ }}
57
+ elsif filter
58
+ # filter must have toplevel = <configuration>
59
+ rpc.first_element_child << filter.dup # *MUST* use the .dup so we don't disrupt the original filter
60
+ end
61
+
62
+ @trans.rpc_exec( rpc )
63
+ end
64
+
65
+ def load_configuration( *args )
66
+
67
+ config = nil
68
+
69
+ # default format is XML
70
+ attrs = { :format => 'xml' }
71
+
72
+ while arg = args.shift
73
+ case arg.class.to_s
74
+ when /^Nokogiri/
75
+ config = case arg
76
+ when Nokogiri::XML::Builder then arg.doc.root
77
+ when Nokogiri::XML::Document then arg.root
78
+ else arg
79
+ end
80
+ when 'Hash' then attrs.merge! arg
81
+ when 'Array' then config = arg.join("\n")
82
+ when 'String' then config = arg
83
+ end
84
+ end
85
+
86
+ case attrs[:format]
87
+ when 'set'
88
+ toplevel = 'configuration-set'
89
+ attrs[:format] = 'text'
90
+ attrs[:action] = 'set'
91
+ when 'text'
92
+ toplevel = 'configuration-text'
93
+ when 'xml'
94
+ toplevel = 'configuration'
95
+ end
96
+
97
+ rpc = Nokogiri::XML('<rpc><load-configuration/></rpc>').root
98
+ ld_cfg = rpc.first_element_child
99
+ Netconf::RPC.add_attributes( ld_cfg, attrs ) if attrs
100
+
101
+ if block_given?
102
+ if attrs[:format] == 'xml'
103
+ Nokogiri::XML::Builder.with( ld_cfg ){ |xml|
104
+ xml.send( toplevel ) {
105
+ yield( xml )
106
+ }}
107
+ else
108
+ config = yield # returns String | Array(of stringable)
109
+ config = config.join("\n") if config.class == Array
110
+ end
111
+ end
112
+
113
+ if config
114
+ if attrs[:format] == 'xml'
115
+ # config assumes toplevel = <configuration> given
116
+ ld_cfg << config.dup # duplicate the config so as to not distrupt it
117
+ else
118
+ # config is stringy, so just add it as the text node
119
+ c_node = Nokogiri::XML::Node.new( toplevel, rpc )
120
+ c_node.content = config
121
+ ld_cfg << c_node
122
+ end
123
+ end
124
+
125
+ # set a specific exception class on this RPC so it can be
126
+ # properlly handled by the calling enviornment
127
+
128
+ Netconf::RPC::set_exception( rpc, Netconf::EditError )
129
+ @trans.rpc_exec( rpc )
130
+ end # load_configuration
131
+
132
+ def command( cmd_str, attrs = nil )
133
+ rpc = Nokogiri::XML("<rpc><command>#{cmd_str}</command></rpc>").root
134
+ Netconf::RPC.add_attributes( rpc.at('command'), attrs ) if attrs
135
+ @trans.rpc_exec( rpc )
136
+ end
137
+
138
+ ## contributed by 'dgjnpr'
139
+ def request_pfe_execute( params = nil )
140
+ raise ArgumentError, 'Manditorary argument :target missing' unless params[:target]
141
+ raise ArgumentError, 'Manditorary argument :command missing' unless params[:command]
142
+
143
+ rpc_nx = Nokogiri::XML::Builder.new { |xml|
144
+ xml.rpc {
145
+ xml.send( 'request-pfe-execute' ) {
146
+ xml.send( 'target', params[:target] )
147
+ if params[:command].class.to_s =~ /^Array/
148
+ params[:command].each { |cmd|
149
+ xml.send( 'command', cmd )
150
+ }
151
+ elsif params[:command].class.to_s =~ /^String/
152
+ xml.send( 'command', params[:command] )
153
+ end
154
+ }
155
+ }
156
+ }
157
+ @trans.rpc_exec( rpc_nx )
158
+ end
159
+
160
+ end # module: JUNOS
161
+ end # module: RPC
162
+ end # module: Netconf
@@ -1,15 +1,15 @@
1
- require 'net/netconf'
2
- require 'net/netconf/serial'
3
- require 'net/netconf/jnpr'
4
-
5
- module Netconf
6
- module Junos
7
- module TransSerial
8
- def trans_start_netconf( last_console )
9
- last_console.match(/[^%]\s+$/)
10
- netconf_cmd = ($1 == '%') ? Netconf::Junos::NETCONF_SHELL : Netconf::Junos::NETCONF_CLI
11
- puts netconf_cmd
12
- end
13
- end
14
- end
15
- end
1
+ require 'net/netconf'
2
+ require 'net/netconf/serial'
3
+ require 'net/netconf/jnpr'
4
+
5
+ module Netconf
6
+ module Junos
7
+ module TransSerial
8
+ def trans_start_netconf( last_console )
9
+ last_console.match(/[^%]\s+$/)
10
+ netconf_cmd = ($1 == '%') ? Netconf::Junos::NETCONF_SHELL : Netconf::Junos::NETCONF_CLI
11
+ puts netconf_cmd
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,23 +1,23 @@
1
- require 'net/netconf'
2
- require 'net/netconf/telnet'
3
- require 'net/netconf/jnpr'
4
-
5
- module Netconf
6
- module Junos
7
- module TransTelnet
8
-
9
- def trans_login
10
- l_rsp = @trans.login( @args[:username], @args[:password] )
11
- # @@@/JLS: need to rescue the timeout ... ???
12
- l_rsp.match("([>%])\s+$")
13
- @exec_netconf = ($1 == '%') ? Netconf::Junos::NETCONF_SHELL : Netconf::Junos::NETCONF_CLI
14
- end
15
-
16
- def trans_start_netconf
17
- @trans.puts @exec_netconf
18
- end
19
-
20
- end
21
- end
22
- end
23
-
1
+ require 'net/netconf'
2
+ require 'net/netconf/telnet'
3
+ require 'net/netconf/jnpr'
4
+
5
+ module Netconf
6
+ module Junos
7
+ module TransTelnet
8
+
9
+ def trans_login
10
+ l_rsp = @trans.login( @args[:username], @args[:password] )
11
+ # @@@/JLS: need to rescue the timeout ... ???
12
+ l_rsp.match("([>%])\s+$")
13
+ @exec_netconf = ($1 == '%') ? Netconf::Junos::NETCONF_SHELL : Netconf::Junos::NETCONF_CLI
14
+ end
15
+
16
+ def trans_start_netconf
17
+ @trans.puts @exec_netconf
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+
@@ -1,71 +1,71 @@
1
- require 'net/netconf/rpc_std'
2
-
3
- module Netconf
4
- module RPC
5
-
6
- def RPC.add_attributes( ele_nx, attr_h )
7
- attr_h.each{ |k,v| ele_nx[k] = v }
8
- end
9
-
10
- def RPC.set_exception( rpc_nx, exception )
11
- rpc_nx.instance_variable_set(:@netconf_exception, exception )
12
- end
13
-
14
- def RPC.get_exception( rpc_nx )
15
- rpc_nx.instance_variable_get(:@netconf_exception) || Netconf::RpcError
16
- end
17
-
18
- module Builder
19
- # autogenerate an <rpc>, converting underscores (_)
20
- # to hyphens (-) along the way ...
21
-
22
- def Builder.method_missing( method, params = nil, attrs = nil )
23
-
24
- rpc_name = method.to_s.tr('_','-').to_sym
25
-
26
- if params
27
- # build the XML starting at <rpc>, envelope the <method> toplevel element,
28
- # and then create name/value elements for each of the additional params. An element
29
- # without a value should simply be set to true
30
- rpc_nx = Nokogiri::XML::Builder.new { |xml|
31
- xml.rpc { xml.send( rpc_name ) {
32
- params.each{ |k,v|
33
- sym = k.to_s.tr('_','-').to_sym
34
- xml.send(sym, (v==true) ? nil : v )
35
- }
36
- }}
37
- }.doc.root
38
- else
39
- # -- no params
40
- rpc_nx = Nokogiri::XML("<rpc><#{rpc_name}/></rpc>").root
41
- end
42
-
43
- # if a block is given it is used to set the attributes of the toplevel element
44
- RPC.add_attributes( rpc_nx.at( rpc_name ), attrs ) if attrs
45
-
46
- # return the rpc command
47
- rpc_nx
48
- end # def: method-missing?
49
-
50
- end # module: Builder
51
-
52
- class Executor
53
- include Netconf::RPC::Standard
54
-
55
- def initialize( trans, os_type )
56
- @trans = trans
57
- begin
58
- extend Netconf::RPC::const_get( os_type )
59
- rescue NameError
60
- # no extensions available ...
61
- end
62
- end
63
-
64
- def method_missing( method, params = nil, attrs = nil )
65
- @trans.rpc_exec( Netconf::RPC::Builder.send( method, params, attrs ))
66
- end
67
- end # class: Executor
68
-
69
- end # module: RPC
70
- end # module: Netconf
71
-
1
+ require 'net/netconf/rpc_std'
2
+
3
+ module Netconf
4
+ module RPC
5
+
6
+ def RPC.add_attributes( ele_nx, attr_h )
7
+ attr_h.each{ |k,v| ele_nx[k] = v }
8
+ end
9
+
10
+ def RPC.set_exception( rpc_nx, exception )
11
+ rpc_nx.instance_variable_set(:@netconf_exception, exception )
12
+ end
13
+
14
+ def RPC.get_exception( rpc_nx )
15
+ rpc_nx.instance_variable_get(:@netconf_exception) || Netconf::RpcError
16
+ end
17
+
18
+ module Builder
19
+ # autogenerate an <rpc>, converting underscores (_)
20
+ # to hyphens (-) along the way ...
21
+
22
+ def Builder.method_missing( method, params = nil, attrs = nil )
23
+
24
+ rpc_name = method.to_s.tr('_','-').to_sym
25
+
26
+ if params
27
+ # build the XML starting at <rpc>, envelope the <method> toplevel element,
28
+ # and then create name/value elements for each of the additional params. An element
29
+ # without a value should simply be set to true
30
+ rpc_nx = Nokogiri::XML::Builder.new { |xml|
31
+ xml.rpc { xml.send( rpc_name ) {
32
+ params.each{ |k,v|
33
+ sym = k.to_s.tr('_','-').to_sym
34
+ xml.send(sym, (v==true) ? nil : v )
35
+ }
36
+ }}
37
+ }.doc.root
38
+ else
39
+ # -- no params
40
+ rpc_nx = Nokogiri::XML("<rpc><#{rpc_name}/></rpc>").root
41
+ end
42
+
43
+ # if a block is given it is used to set the attributes of the toplevel element
44
+ RPC.add_attributes( rpc_nx.at( rpc_name ), attrs ) if attrs
45
+
46
+ # return the rpc command
47
+ rpc_nx
48
+ end # def: method-missing?
49
+
50
+ end # module: Builder
51
+
52
+ class Executor
53
+ include Netconf::RPC::Standard
54
+
55
+ def initialize( trans, os_type )
56
+ @trans = trans
57
+ begin
58
+ extend Netconf::RPC::const_get( os_type )
59
+ rescue NameError
60
+ # no extensions available ...
61
+ end
62
+ end
63
+
64
+ def method_missing( method, params = nil, attrs = nil )
65
+ @trans.rpc_exec( Netconf::RPC::Builder.send( method, params, attrs ))
66
+ end
67
+ end # class: Executor
68
+
69
+ end # module: RPC
70
+ end # module: Netconf
71
+