Class: CiscoAclIntp::AceProtoSpecBase
- Inherits:
-
AceSpecBase
- Object
- AccessControlContainer
- AceSpecBase
- CiscoAclIntp::AceProtoSpecBase
- Includes:
- Comparable
- Defined in:
- lib/cisco_acl_intp/acespec_proto_base.rb
Overview
IP/TCP/UDP protocol number and protocol name container base
Direct Known Subclasses
Constant Summary collapse
- DUMMY_PROTO_TABLE =
Note:
Keys(protocol names) are String not as Symbol, because there are keys exists including ‘-’.
Convert table of protocol number/name
{ 'any' => -1 # dummy }.freeze
Constants inherited from AccessControlContainer
CiscoAclIntp::AccessControlContainer::TERM_COLOR_TABLE
Instance Attribute Summary collapse
-
#max_num ⇒ Integer
readonly
Maximum protocol/port number.
-
#name ⇒ String
readonly
Protocol name.
-
#number ⇒ Integer
readonly
Protocol/Port number.
-
#protocol ⇒ Symbol
readonly
L3/L4 protocol type.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Compare by port number.
-
#==(other) ⇒ Boolean
Check equality.
- #define_param_by_integer(number) ⇒ Object private
- #define_param_by_string(name) ⇒ Object private
-
#initialize(proto_id = nil, max_num = 255) ⇒ AceProtoSpecBase
constructor
abstract
Constructor.
-
#max? ⇒ Boolean
Check if port/protocol number is maximum.
-
#min? ⇒ Boolean
Check if port/protocol number is minimum.
-
#name_to_number ⇒ Object
private
Convert protocol/port name to number.
-
#number_to_name ⇒ String
private
Convert protocol/port number to string (its name).
-
#proto_table ⇒ Hash
abstract
Protocol Table.
-
#to_i ⇒ Integer
Return protocol/port number.
-
#to_s ⇒ String
Generate string for Cisco IOS access list.
-
#valid_name? ⇒ Boolean
Check the port name is known or not.
-
#valid_range? ⇒ Boolean
Check the port number in valid range of port number.
Methods inherited from AccessControlContainer
#clean_acl_string, disable_color, #generate_tag_footer, #generate_tag_header, #generate_tagged_str, #method_missing
Constructor Details
#initialize(proto_id = nil, max_num = 255) ⇒ AceProtoSpecBase
Variable ‘@protocol’ should be assigned in inherited class constructor.
Constructor
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 43 def initialize(proto_id = nil, max_num = 255) @protocol = nil # must be defined in inherited class. @max_num = max_num case proto_id when /\d+/ # Integer-String, MUST check before 'String' define_param_by_integer(proto_id.to_i) when String # Not Integer-String define_param_by_string(proto_id) when Integer define_param_by_integer(proto_id) else raise AclArgumentError, "invalid protocol id #{proto_id}" end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class CiscoAclIntp::AccessControlContainer
Instance Attribute Details
#max_num ⇒ Integer (readonly)
Returns Maximum protocol/port number.
16 17 18 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 16 def max_num @max_num end |
#name ⇒ String (readonly)
Returns Protocol name.
10 11 12 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 10 def name @name end |
#number ⇒ Integer (readonly)
Returns Protocol/Port number.
13 14 15 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 13 def number @number end |
#protocol ⇒ Symbol (readonly)
Returns L3/L4 protocol type.
19 20 21 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 19 def protocol @protocol end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Using “Comparable” module, ‘==’ operator is defined by ‘<=>’ operator. But ‘==’ is overriden to compare instance equivalence instead of port number comparison.
Compare by port number
90 91 92 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 90 def <=>(other) @number <=> other.to_i end |
#==(other) ⇒ Boolean
Check equality
97 98 99 100 101 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 97 def ==(other) @protocol == other.protocol && @name == other.name && @number == other.number end |
#define_param_by_integer(number) ⇒ Object (private)
148 149 150 151 152 153 154 155 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 148 def define_param_by_integer(number) @number = number if valid_range? @name = number_to_name else raise AclArgumentError, "Invalid protocol number: #{@number}" end end |
#define_param_by_string(name) ⇒ Object (private)
137 138 139 140 141 142 143 144 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 137 def define_param_by_string(name) @name = name if valid_name? @number = name_to_number else raise AclArgumentError, "Unknown protocol name: #{@name}" end end |
#max? ⇒ Boolean
Check if port/protocol number is maximum.
111 112 113 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 111 def max? @number == @max_num end |
#min? ⇒ Boolean
Check if port/protocol number is minimum.
105 106 107 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 105 def min? @number == 0 end |
#name_to_number ⇒ Object (private)
Convert protocol/port name to number
127 128 129 130 131 132 133 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 127 def name_to_number if proto_table.key?(@name) proto_table[@name] else raise AclArgumentError, "Unknown protocol name: #{@name}" end end |
#number_to_name ⇒ String (private)
Convert protocol/port number to string (its name)
121 122 123 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 121 def number_to_name proto_table.invert[@number] || @number.to_s end |
#proto_table ⇒ Hash
Protocol Table
31 32 33 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 31 def proto_table DUMMY_PROTO_TABLE end |
#to_i ⇒ Integer
Return protocol/port number
80 81 82 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 80 def to_i @number.to_i end |
#to_s ⇒ String
Generate string for Cisco IOS access list
74 75 76 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 74 def to_s @name end |
#valid_name? ⇒ Boolean
Check the port name is known or not.
68 69 70 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 68 def valid_name? proto_table.key?(@name) end |
#valid_range? ⇒ Boolean
Check the port number in valid range of port number
61 62 63 |
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 61 def valid_range? (0..@max_num).cover?(@number) end |