Class: Cisco::Ace

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/ace.rb

Overview

Ace - node utility class for Ace Configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(afi, acl_name, seqno) ⇒ Ace

Returns a new instance of Ace.



22
23
24
25
26
27
# File 'lib/cisco_node_utils/ace.rb', line 22

def initialize(afi, acl_name, seqno)
  @afi = Acl.afi_cli(afi)
  @acl_name = acl_name.to_s
  @seqno = seqno.to_s
  set_args_keys_default
end

Instance Attribute Details

#acl_nameObject (readonly)

Returns the value of attribute acl_name.



20
21
22
# File 'lib/cisco_node_utils/ace.rb', line 20

def acl_name
  @acl_name
end

#afiObject (readonly)

Returns the value of attribute afi.



20
21
22
# File 'lib/cisco_node_utils/ace.rb', line 20

def afi
  @afi
end

Class Method Details

.acesObject

Create a hash of all aces under a given acl_name.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cisco_node_utils/ace.rb', line 30

def self.aces
  afis = %w(ipv4 ipv6)
  hash = {}
  afis.each do |afi|
    hash[afi] = {}
    acls = config_get('acl', 'all_acls', afi: Acl.afi_cli(afi))
    next if acls.nil?

    acls.each do |acl_name|
      hash[afi][acl_name] = {}
      aces = config_get('acl', 'all_aces',
                        afi: Acl.afi_cli(afi), acl_name: acl_name)
      next if aces.nil?

      aces.each do |seqno|
        hash[afi][acl_name][seqno] = Ace.new(afi, acl_name, seqno)
      end
    end
  end
  hash
end

Instance Method Details

#ace_getObject

common ace getter



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/cisco_node_utils/ace.rb', line 69

def ace_get
  str = config_get('acl', 'ace', @get_args)
  return nil if str.nil?

  remark = Regexp.new('(?<seqno>\d+) remark (?<remark>.*)').match(str)
  return remark unless remark.nil?

  # rubocop:disable Metrics/LineLength
  regexp = Regexp.new('(?<seqno>\d+) (?<action>\S+)'\
             ' *(?<proto>\d+|\S+)'\
             ' *(?<src_addr>any|host \S+|[:\.0-9a-fA-F]+ [:\.0-9a-fA-F]+|[:\.0-9a-fA-F]+\/\d+|addrgroup \S+)'\
             ' *(?<src_port>range \S+ \S+|(lt|eq|gt|neq|portgroup) \S+)?'\
             ' *(?<dst_addr>any|host \S+|[:\.0-9a-fA-F]+ [:\.0-9a-fA-F]+|[:\.0-9a-fA-F]+\/\d+|addrgroup \S+)'\
             ' *(?<dst_port>range \S+ \S+|(lt|eq|gt|neq|portgroup) \S+)?'\
             ' *(?<tcp_flags>(ack *|fin *|urg *|syn *|psh *|rst *)*)?'\
             ' *(?<established>established)?'\
             ' *(?<precedence>precedence \S+)?'\
             ' *(?<dscp>dscp \S+)?'\
             ' *(?<time_range>time-range \S+)?'\
             ' *(?<packet_length>packet-length (range \d+ \d+|(lt|eq|gt|neq) \d+))?'\
             ' *(?<ttl>ttl \d+)?'\
             ' *(?<http_method>http-method (\d+|connect|delete|get|head|post|put|trace))?'\
             ' *(?<tcp_option_length>tcp-option-length \d+)?'\
             ' *(?<redirect>redirect \S+)?'\
             ' *(?<log>log)?')
  # rubocop:enable Metrics/LineLength
  regexp.match(str)
end

#ace_set(attrs) ⇒ Object

common ace setter. Put the values you need in a hash and pass it in. attrs = :proto=>‘tcp’, :src =>‘host 1.1.1.1’



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cisco_node_utils/ace.rb', line 100

def ace_set(attrs)
  if attrs.empty?
    attrs[:state] = 'no'
  else
    # remove existing ace first
    destroy if seqno
    attrs[:state] = ''
  end

  if attrs[:remark]
    cmd = 'ace_remark'
    set_args_keys(attrs)
  else
    cmd = 'ace'
    set_args_keys_default
    set_args_keys(attrs)
    [:action,
     :proto,
     :src_addr,
     :src_port,
     :dst_addr,
     :dst_port,
     :tcp_flags,
     :established,
     :precedence,
     :dscp,
     :time_range,
     :packet_length,
     :ttl,
     :http_method,
     :tcp_option_length,
     :redirect,
     :log,
    ].each do |p|
      attrs[p] = '' if attrs[p].nil?
      send(p.to_s + '=', attrs[p])
    end
    @get_args = @set_args
  end
  config_set('acl', cmd, @set_args)
end

#actionObject



177
178
179
180
181
# File 'lib/cisco_node_utils/ace.rb', line 177

def action
  match = ace_get
  return nil if match.nil?
  match.names.include?('action') ? match[:action] : nil
end

#action=(action) ⇒ Object



183
184
185
# File 'lib/cisco_node_utils/ace.rb', line 183

def action=(action)
  @set_args[:action] = action
end

#attach_prefix(val, prop, prefix = nil) ⇒ Object

prepend property name prefix/keyword to value



164
165
166
167
# File 'lib/cisco_node_utils/ace.rb', line 164

def attach_prefix(val, prop, prefix=nil)
  prefix = prop.to_s if prefix.nil?
  @set_args[prop] = val.to_s.empty? ? val : "#{prefix} #{val}"
end

#destroyObject



52
53
54
55
# File 'lib/cisco_node_utils/ace.rb', line 52

def destroy
  set_args_keys(state: 'no')
  config_set('acl', 'ace_destroy', @set_args)
end

#dscpObject



282
283
284
# File 'lib/cisco_node_utils/ace.rb', line 282

def dscp
  extract_value('dscp')
end

#dscp=(dscp) ⇒ Object



286
287
288
# File 'lib/cisco_node_utils/ace.rb', line 286

def dscp=(dscp)
  attach_prefix(dscp, :dscp)
end

#dst_addrObject



230
231
232
233
234
235
236
237
# File 'lib/cisco_node_utils/ace.rb', line 230

def dst_addr
  match = ace_get
  return nil if match.nil? || !match.names.include?('dst_addr')
  addr = match[:dst_addr]
  # Normalize addr. Some platforms zero_pad ipv6 addrs.
  addr.gsub!(/^0*/, '').gsub!(/:0*/, ':')
  addr
end

#dst_addr=(dst_addr) ⇒ Object



239
240
241
# File 'lib/cisco_node_utils/ace.rb', line 239

def dst_addr=(dst_addr)
  @set_args[:dst_addr] = dst_addr
end

#dst_portObject



243
244
245
246
247
# File 'lib/cisco_node_utils/ace.rb', line 243

def dst_port
  match = ace_get
  return nil if match.nil?
  match.names.include?('dst_port') ? match[:dst_port] : nil
end

#dst_port=(src_port) ⇒ Object



249
250
251
# File 'lib/cisco_node_utils/ace.rb', line 249

def dst_port=(src_port)
  @set_args[:dst_port] = src_port
end

#establishedObject



263
264
265
266
267
268
# File 'lib/cisco_node_utils/ace.rb', line 263

def established
  match = ace_get
  return false if match.nil?
  return false unless match.names.include?('established')
  match[:established] == 'established' ? true : false
end

#established=(established) ⇒ Object



270
271
272
# File 'lib/cisco_node_utils/ace.rb', line 270

def established=(established)
  @set_args[:established] = established.to_s == 'true' ? 'established' : ''
end

#extract_value(prop, prefix = nil) ⇒ Object

extract value of property from ace



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cisco_node_utils/ace.rb', line 146

def extract_value(prop, prefix=nil)
  prefix = prop if prefix.nil?
  ace_match = ace_get

  # matching ace not found
  return nil if ace_match.nil? # no matching ace found

  # property not defined for matching ace
  return nil unless ace_match.names.include?(prop)

  # extract and return value that follows prefix + <space>
  regexp = Regexp.new("#{Regexp.escape(prefix)} (?<extracted>.*)")
  value_match = regexp.match(ace_match[prop])
  return nil if value_match.nil?
  value_match[:extracted]
end

#http_methodObject



314
315
316
# File 'lib/cisco_node_utils/ace.rb', line 314

def http_method
  extract_value('http_method', 'http-method')
end

#http_method=(http_method) ⇒ Object



318
319
320
# File 'lib/cisco_node_utils/ace.rb', line 318

def http_method=(http_method)
  attach_prefix(http_method, :http_method, 'http-method')
end

#logObject



338
339
340
341
342
343
# File 'lib/cisco_node_utils/ace.rb', line 338

def log
  match = ace_get
  return false if match.nil?
  return false unless match.names.include?('log')
  match[:log] == 'log' ? true : false
end

#log=(log) ⇒ Object



345
346
347
# File 'lib/cisco_node_utils/ace.rb', line 345

def log=(log)
  @set_args[:log] = log.to_s == 'true' ? 'log' : ''
end

#packet_lengthObject



298
299
300
# File 'lib/cisco_node_utils/ace.rb', line 298

def packet_length
  extract_value('packet_length', 'packet-length')
end

#packet_length=(packet_length) ⇒ Object



302
303
304
# File 'lib/cisco_node_utils/ace.rb', line 302

def packet_length=(packet_length)
  attach_prefix(packet_length, :packet_length, 'packet-length')
end

#precedenceObject



274
275
276
# File 'lib/cisco_node_utils/ace.rb', line 274

def precedence
  extract_value('precedence')
end

#precedence=(precedence) ⇒ Object



278
279
280
# File 'lib/cisco_node_utils/ace.rb', line 278

def precedence=(precedence)
  attach_prefix(precedence, :precedence)
end

#protoObject



197
198
199
200
201
# File 'lib/cisco_node_utils/ace.rb', line 197

def proto
  match = ace_get
  return nil if match.nil?
  match.names.include?('proto') ? match[:proto] : nil
end

#proto=(proto) ⇒ Object



203
204
205
# File 'lib/cisco_node_utils/ace.rb', line 203

def proto=(proto)
  @set_args[:proto] = proto # TBD ip vs ipv4
end

#redirectObject



330
331
332
# File 'lib/cisco_node_utils/ace.rb', line 330

def redirect
  extract_value('redirect')
end

#redirect=(redirect) ⇒ Object



334
335
336
# File 'lib/cisco_node_utils/ace.rb', line 334

def redirect=(redirect)
  attach_prefix(redirect, :redirect)
end

#remarkObject



187
188
189
190
191
# File 'lib/cisco_node_utils/ace.rb', line 187

def remark
  match = ace_get
  return nil if match.nil?
  match.names.include?('remark') ? match[:remark] : nil
end

#remark=(remark) ⇒ Object



193
194
195
# File 'lib/cisco_node_utils/ace.rb', line 193

def remark=(remark)
  @set_args[:remark] = remark
end

#seqnoObject

PROPERTIES




171
172
173
174
175
# File 'lib/cisco_node_utils/ace.rb', line 171

def seqno
  match = ace_get
  return nil if match.nil?
  match.names.include?('seqno') ? match[:seqno] : nil
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName



63
64
65
66
# File 'lib/cisco_node_utils/ace.rb', line 63

def set_args_keys(hash={})
  set_args_keys_default
  @set_args = @get_args.merge!(hash) unless hash.empty?
end

#set_args_keys_defaultObject



57
58
59
60
# File 'lib/cisco_node_utils/ace.rb', line 57

def set_args_keys_default
  keys = { afi: @afi, acl_name: @acl_name, seqno: @seqno }
  @get_args = @set_args = keys
end

#src_addrObject



207
208
209
210
211
212
213
214
# File 'lib/cisco_node_utils/ace.rb', line 207

def src_addr
  match = ace_get
  return nil if match.nil? || !match.names.include?('src_addr')
  addr = match[:src_addr]
  # Normalize addr. Some platforms zero_pad ipv6 addrs.
  addr.gsub!(/^0*/, '').gsub!(/:0*/, ':')
  addr
end

#src_addr=(src_addr) ⇒ Object



216
217
218
# File 'lib/cisco_node_utils/ace.rb', line 216

def src_addr=(src_addr)
  @set_args[:src_addr] = src_addr
end

#src_portObject



220
221
222
223
224
# File 'lib/cisco_node_utils/ace.rb', line 220

def src_port
  match = ace_get
  return nil if match.nil?
  match.names.include?('src_port') ? match[:src_port] : nil
end

#src_port=(src_port) ⇒ Object



226
227
228
# File 'lib/cisco_node_utils/ace.rb', line 226

def src_port=(src_port)
  @set_args[:src_port] = src_port
end

#tcp_flagsObject



253
254
255
256
257
# File 'lib/cisco_node_utils/ace.rb', line 253

def tcp_flags
  match = ace_get
  return nil if match.nil?
  match.names.include?('tcp_flags') ? match[:tcp_flags].strip : nil
end

#tcp_flags=(tcp_flags) ⇒ Object



259
260
261
# File 'lib/cisco_node_utils/ace.rb', line 259

def tcp_flags=(tcp_flags)
  @set_args[:tcp_flags] = tcp_flags.strip
end

#tcp_option_lengthObject



322
323
324
# File 'lib/cisco_node_utils/ace.rb', line 322

def tcp_option_length
  extract_value('tcp_option_length', 'tcp-option-length')
end

#tcp_option_length=(tcp_option_length) ⇒ Object



326
327
328
# File 'lib/cisco_node_utils/ace.rb', line 326

def tcp_option_length=(tcp_option_length)
  attach_prefix(tcp_option_length, :tcp_option_length, 'tcp-option-length')
end

#time_rangeObject



290
291
292
# File 'lib/cisco_node_utils/ace.rb', line 290

def time_range
  extract_value('time_range', 'time-range')
end

#time_range=(time_range) ⇒ Object



294
295
296
# File 'lib/cisco_node_utils/ace.rb', line 294

def time_range=(time_range)
  attach_prefix(time_range, :time_range, 'time-range')
end

#ttlObject



306
307
308
# File 'lib/cisco_node_utils/ace.rb', line 306

def ttl
  extract_value('ttl')
end

#ttl=(ttl) ⇒ Object



310
311
312
# File 'lib/cisco_node_utils/ace.rb', line 310

def ttl=(ttl)
  attach_prefix(ttl, :ttl)
end