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

config_get, #config_get, config_get_default, #config_get_default, #config_set, config_set, #node, node, #show

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

#seqnoObject (readonly)

PROPERTIES




120
121
122
# File 'lib/cisco_node_utils/ace.rb', line 120

def seqno
  @seqno
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
# 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 is a description field, needs a separate regex
  # Example: <MatchData "20 remark foo bar" seqno:"20" remark:"foo bar">
  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+|\S+\/\d+|\S+ [:\.0-9a-fA-F]+|addrgroup \S+)*'\
             ' *(?<src_port>eq \S+|neq \S+|lt \S+|''gt \S+|range \S+ \S+|portgroup \S+)?'\
             ' *(?<dst_addr>any|host \S+|\S+\/\d+|\S+ [:\.0-9a-fA-F]+|addrgroup \S+)'\
             ' *(?<dst_port>eq \S+|neq \S+|lt \S+|gt \S+|range \S+ \S+|portgroup \S+)?')
  # 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’



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cisco_node_utils/ace.rb', line 91

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'
  else
    cmd = 'ace'
    [:action,
     :proto,
     :src_addr,
     :src_port,
     :dst_addr,
     :dst_port,
    ].each do |p|
      attrs[p] = '' if attrs[p].nil?
    end
  end
  set_args_keys(attrs)
  config_set('acl', cmd, @set_args)
end

#actionObject



126
127
128
129
130
# File 'lib/cisco_node_utils/ace.rb', line 126

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

#action=(action) ⇒ Object



132
133
134
# File 'lib/cisco_node_utils/ace.rb', line 132

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

#dst_addrObject



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

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

#dst_addr=(dst_addr) ⇒ Object



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

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

#dst_portObject



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

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



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

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

#protoObject



146
147
148
149
150
# File 'lib/cisco_node_utils/ace.rb', line 146

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

#proto=(proto) ⇒ Object



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

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

#remarkObject



136
137
138
139
140
# File 'lib/cisco_node_utils/ace.rb', line 136

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

#remark=(remark) ⇒ Object



142
143
144
# File 'lib/cisco_node_utils/ace.rb', line 142

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



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

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

#src_addr=(src_addr) ⇒ Object



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

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

#src_portObject



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

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



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

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