Class: Netconf::JunosConfig

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

Constant Summary collapse

DELETE =
{ delete: 'delete' }.freeze
REPLACE =
{ replace: 'replace' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ JunosConfig


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

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 = {}
    @to_xml = options[:build]
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection


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

def collection
  @collection
end

#docObject (readonly)

Returns the value of attribute doc


12
13
14
# File 'lib/net/netconf/jnpr/junos_config.rb', line 12

def doc
  @doc
end

Instance Method Details

#<<(obj) ⇒ Object


38
39
40
41
42
43
44
# File 'lib/net/netconf/jnpr/junos_config.rb', line 38

def <<(obj)
  if defined? @collection
    @collection[obj[:name]] = obj
  elsif defined? @doc
    obj.build_xml(@doc)
  end
end

#build_xml(ng_xml, &block) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/net/netconf/jnpr/junos_config.rb', line 46

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


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

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

#with(ng_xml, &block) ⇒ Object


94
95
96
# File 'lib/net/netconf/jnpr/junos_config.rb', line 94

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