Module: Cisco::Netconf

Defined in:
lib/util/client/netconf/netconf.rb

Overview

Cisco module

Constant Summary collapse

NC_BASE_1_0_NS =

Netconf module performs conversion of REXML documents into a format with types that are comparable to the output of JSON.parse

'urn:ietf:params:xml:ns:netconf:base:1.0'

Class Method Summary collapse

Class Method Details

.convert_rexml_from_string(input) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/util/client/netconf/netconf.rb', line 56

def self.convert_rexml_from_string(input)
  if empty?(input)
    out = {}
  else
    unless defined? @iw
      @iw = {}
      @iw[:ignore_whitespace_nodes] = :all
    end
    out = convert_xml(REXML::Document.new(input, @iw))
  end
  out
end

.convert_xml(xml) ⇒ Object



32
33
34
35
# File 'lib/util/client/netconf/netconf.rb', line 32

def self.convert_xml(xml)
  fail "#{xml} is not an XML document" unless xml.is_a?(REXML::Document)
  convert_xml_node(xml.root)
end

.convert_xml_node(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/util/client/netconf/netconf.rb', line 37

def self.convert_xml_node(node)
  fail "#{node} is not an XML node" unless node.is_a?(REXML::Element)
  out_hash = {}
  children = node.to_a
  if children.length == 1 && node.has_text?
    out_hash[node.name] = [children[0].value.strip]
  elsif !node.has_elements?
    out_hash[node.name] = []
  else
    out_hash[node.name] = children.map { |child| convert_xml_node(child) }
  end
  # Looking for operation=delete in the netconf:base:1.0 namespace
  if node.attributes.get_attribute_ns(NC_BASE_1_0_NS,
                                      'operation').to_s == 'delete'
    out_hash[:delete] = :delete
  end
  out_hash
end

.empty?(nc) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/util/client/netconf/netconf.rb', line 28

def self.empty?(nc)
  !nc || nc.empty?
end

.insync_for_merge(target, current) ⇒ Object



69
70
71
72
73
# File 'lib/util/client/netconf/netconf.rb', line 69

def self.insync_for_merge(target, current)
  !Yang.needs_something?(:merge,
                         convert_rexml_from_string(target),
                         convert_rexml_from_string(current))
end

.insync_for_replace(target, current) ⇒ Object



75
76
77
78
79
# File 'lib/util/client/netconf/netconf.rb', line 75

def self.insync_for_replace(target, current)
  !Yang.needs_something?(:replace,
                         convert_rexml_from_string(target),
                         convert_rexml_from_string(current))
end