Class: Netconf::JunosConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/net/netconf/jnpr/junos_config.rb

Constant Summary collapse

DELETE =
{ :delete => 'delete' }
REPLACE =
{ :replace => 'replace' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ JunosConfig

Returns a new instance of JunosConfig


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/net/netconf/jnpr/junos_config.rb', line 17

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection


15
16
17
# File 'lib/net/netconf/jnpr/junos_config.rb', line 15

def collection
  @collection
end

#docObject (readonly)

Returns the value of attribute doc


14
15
16
# File 'lib/net/netconf/jnpr/junos_config.rb', line 14

def doc
  @doc
end

Instance Method Details

#<<(obj) ⇒ Object


40
41
42
43
44
45
46
47
48
# File 'lib/net/netconf/jnpr/junos_config.rb', line 40

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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/net/netconf/jnpr/junos_config.rb', line 50

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


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
# File 'lib/net/netconf/jnpr/junos_config.rb', line 65

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? or 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
  return dot
end

#with(ng_xml, &block) ⇒ Object


98
99
100
# File 'lib/net/netconf/jnpr/junos_config.rb', line 98

def with( ng_xml, &block )
  Nokogiri::XML::Builder.with( ng_xml, &block )
end