Class: Cisco::YangAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/util/yang_accessor.rb

Overview

Utility class to output the current state of an XR configuration.

Instance Method Summary collapse

Instance Method Details

#clientObject



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/util/yang_accessor.rb', line 207

def client
  unless @client
    @client = Cisco::Client.create(@options[:client_class])

    puts "[ Connected to client: #{@client} ]"\
        if @options[:verbose]
  end
  @client
rescue Cisco::AuthenticationFailed
  abort 'Unauthorized to connect'
rescue Cisco::ClientError, TypeError, ArgumentError => e
  abort "Error in establishing connection: #{e}"
end

#output_data(yang_target, data) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/util/yang_accessor.rb', line 188

def output_data(yang_target, data)
  if @options[:manifest]
    if @options[:client_class] == Cisco::Client::GRPC
      puppet_type = 'cisco_yang'
      ensure_prop = "    ensure => present,\n"
    else
      puppet_type = 'cisco_yang_netconf'
      ensure_prop = ''
    end

    puts "  #{puppet_type} { '#{yang_target}':\n#{ensure_prop}"\
        "    source => '#{data.chomp.gsub(/\n/, "\n    ")}'\n"\
        '  }'
  else
    puts data
  end
  puts # spacer
end

#process(options) ⇒ Object



26
27
28
29
30
31
32
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
# File 'lib/util/yang_accessor.rb', line 26

def process(options)
  @options = options

  # suppress stdout
  old_stdout = $stdout
  $stdout = StringIO.new if @options[:quiet]

  client # initialize the client

  dir_or_file = options[:path] || '/pkg/yang'

  file = nil
  dir = nil

  if File.exist?(dir_or_file)
    if File.directory?(dir_or_file)
      dir = dir_or_file
    else
      file = dir_or_file
    end
  else
    puts "Directory or file not found: #{dir_or_file}"
    exit(-1)
  end

  puts "File found: #{file}" if file
  puts "Directory found: #{dir}" if dir
  puts 'Searching for configuration data...' unless @options[:oper]
  puts 'Searching for operational data...' if @options[:oper]

  t1 = Time.now

  @files = 0
  @cnrs = 0
  @errors = 0

  targets = []

  if file
    targets.concat(process_file(file))
    @files += 1
  else
    Dir.glob(dir + '/*.yang').sort.each do |item|
      targets.concat(process_file(item))
      @files += 1
    end
  end

  delta = Time.now - t1
  puts '---------------------------------------------'
  puts "Files Processed: #{@files}"
  puts "Containers Processed: #{@cnrs}"
  puts "Errors: #{@errors}"
  puts "Time: #{delta.round(2)} seconds"
  puts # spacer

  $stdout = old_stdout

  targets
end

#process_file(file) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/util/yang_accessor.rb', line 94

def process_file(file)
  @module = nil
  @namespace = nil
  @containers = {}

  targets = []
  puts "[ Processing file #{file} ]" if @options[:verbose]

  File.open(file) do |f|
    loop do
      break if (line = f.gets).nil?
      target = process_line(line, f)
      targets << target if target
    end
  end
  targets
end

#process_line(line, file) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/util/yang_accessor.rb', line 112

def process_line(line, file)
  if @module.nil?
    @module = Regexp.last_match(1) if line =~ /^module (.+) {/
  elsif @namespace.nil?
    # handle split lines (move this to the process_file line
    # loop if more general handling is needed)
    until (m = line.match(/(.*)"\+$/)).nil?
      line2 = file.gets
      break if line2.nil?
      line2.match(/^\s*"(.*)/) do |m2|
        line = m[1] + m2[1]
      end
    end

    @namespace = Regexp.last_match(1) if line =~ /^  namespace "(.+)"/
  elsif line =~ /^  container (.+) {/
    return process_root_container(@module, @namespace, Regexp.last_match(1), file)
  end
  nil
end

#process_root_container(module_name, namespace, container, file) ⇒ Object



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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/util/yang_accessor.rb', line 133

def process_root_container(module_name, namespace, container, file)
  operation = :get_config
  loop do
    line = file.gets
    break if !line || line.strip == ''
    if line =~ /^    config false;/ # abort cnr if not config
      operation = :get_oper
      break
    end
  end

  # only output config or operational data, depending on options
  if @options[:oper]
    return if operation == :get_config
  else
    return unless operation == :get_config
  end

  # guard against duplicate containers
  if @containers.key?(container)
    puts "[   Duplicate container #{container} ]" if @options[:verbose]
    return
  end

  yang_target = client.yang_target(module_name, namespace, container)

  @containers[container] = true
  @cnrs += 1

  unless @options[:parse_only]
    begin
      puts "[   Processing container #{container}... ]"\
          if @options[:verbose]
      data = client.get(data_format: :yang_json,
                        command:     yang_target,
                        mode:        operation)
      if data && data.strip.length > 0
        puts '[     Data returned ]'\
            if @options[:verbose]
        output_data(yang_target, data)
      else
        puts '[     No data returned ]'\
            if @options[:verbose]
      end
    rescue Cisco::ClientError, Cisco::YangError => e
      @errors += 1
      puts "!!Error on '#{yang_target}': #{e}"
      debug e.backtrace
      puts # spacer
    end
  end

  yang_target
end

#targets(options) ⇒ Object



87
88
89
90
91
92
# File 'lib/util/yang_accessor.rb', line 87

def targets(options)
  options[:parse_only] = true
  options[:quiet] = true

  process(options)
end