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



150
151
152
153
154
# File 'lib/cisco_node_utils/ace.rb', line 150

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

#action=(action) ⇒ Object



156
157
158
# File 'lib/cisco_node_utils/ace.rb', line 156

def action=(action)
  @set_args[:action] = action
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



256
257
258
# File 'lib/cisco_node_utils/ace.rb', line 256

def dscp
  Utils.extract_value(ace_get, 'dscp')
end

#dscp=(dscp) ⇒ Object



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

def dscp=(dscp)
  @set_args[:dscp] = Utils.attach_prefix(dscp, :dscp)
end

#dst_addrObject



203
204
205
206
207
208
209
210
# File 'lib/cisco_node_utils/ace.rb', line 203

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



212
213
214
# File 'lib/cisco_node_utils/ace.rb', line 212

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

#dst_portObject



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

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



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

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

#establishedObject



236
237
238
239
240
241
242
# File 'lib/cisco_node_utils/ace.rb', line 236

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

#established=(established) ⇒ Object



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

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

#http_methodObject



292
293
294
# File 'lib/cisco_node_utils/ace.rb', line 292

def http_method
  Utils.extract_value(ace_get, 'http_method', 'http-method')
end

#http_method=(http_method) ⇒ Object



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

def http_method=(http_method)
  @set_args[:http_method] = Utils.attach_prefix(http_method,
                                                :http_method,
                                                'http-method')
end

#logObject



320
321
322
323
324
325
326
# File 'lib/cisco_node_utils/ace.rb', line 320

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

#log=(log) ⇒ Object



328
329
330
# File 'lib/cisco_node_utils/ace.rb', line 328

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

#packet_lengthObject



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

def packet_length
  Utils.extract_value(ace_get, 'packet_length', 'packet-length')
end

#packet_length=(packet_length) ⇒ Object



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

def packet_length=(packet_length)
  @set_args[:packet_length] = Utils.attach_prefix(packet_length,
                                                  :packet_length,
                                                  'packet-length')
end

#precedenceObject



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

def precedence
  Utils.extract_value(ace_get, 'precedence')
end

#precedence=(precedence) ⇒ Object



252
253
254
# File 'lib/cisco_node_utils/ace.rb', line 252

def precedence=(precedence)
  @set_args[:precedence] = Utils.attach_prefix(precedence, :precedence)
end

#protoObject



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

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

#proto=(proto) ⇒ Object



176
177
178
# File 'lib/cisco_node_utils/ace.rb', line 176

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

#redirectObject



312
313
314
# File 'lib/cisco_node_utils/ace.rb', line 312

def redirect
  Utils.extract_value(ace_get, 'redirect')
end

#redirect=(redirect) ⇒ Object



316
317
318
# File 'lib/cisco_node_utils/ace.rb', line 316

def redirect=(redirect)
  @set_args[:redirect] = Utils.attach_prefix(redirect, :redirect)
end

#remarkObject



160
161
162
163
164
# File 'lib/cisco_node_utils/ace.rb', line 160

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

#remark=(remark) ⇒ Object



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

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

#seqnoObject

PROPERTIES




144
145
146
147
148
# File 'lib/cisco_node_utils/ace.rb', line 144

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



180
181
182
183
184
185
186
187
# File 'lib/cisco_node_utils/ace.rb', line 180

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



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

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

#src_portObject



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

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



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

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

#tcp_flagsObject



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

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



232
233
234
# File 'lib/cisco_node_utils/ace.rb', line 232

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

#tcp_option_lengthObject



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

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

#tcp_option_length=(tcp_option_length) ⇒ Object



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

def tcp_option_length=(tcp_option_length)
  @set_args[:tcp_option_length] = Utils.attach_prefix(tcp_option_length,
                                                      :tcp_option_length,
                                                      'tcp-option-length')
end

#time_rangeObject



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

def time_range
  Utils.extract_value(ace_get, 'time_range', 'time-range')
end

#time_range=(time_range) ⇒ Object



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

def time_range=(time_range)
  @set_args[:time_range] = Utils.attach_prefix(time_range,
                                               :time_range,
                                               'time-range')
end

#ttlObject



284
285
286
# File 'lib/cisco_node_utils/ace.rb', line 284

def ttl
  Utils.extract_value(ace_get, 'ttl')
end

#ttl=(ttl) ⇒ Object



288
289
290
# File 'lib/cisco_node_utils/ace.rb', line 288

def ttl=(ttl)
  @set_args[:ttl] = Utils.attach_prefix(ttl, :ttl)
end