Class: CiscoAclIntp::AceProtoSpecBase

Inherits:
AceSpecBase show all
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

AceIpProtoSpec, AceTcpUdpProtoSpec

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

Instance Method Summary collapse

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

This method is abstract.
Note:

Variable ‘@protocol’ should be assigned in inherited class constructor.

Constructor

Parameters:

  • proto_id (String, Integer) (defaults to: nil)

    Protocol ID (No. or Name)

  • max_num (Integer) (defaults to: 255)

    Maximum protocol number.

Raises:



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_numInteger (readonly)

Returns Maximum protocol/port number.

Returns:

  • (Integer)

    Maximum protocol/port number



16
17
18
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 16

def max_num
  @max_num
end

#nameString (readonly)

Returns Protocol name.

Returns:

  • (String)

    Protocol name



10
11
12
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 10

def name
  @name
end

#numberInteger (readonly)

Returns Protocol/Port number.

Returns:

  • (Integer)

    Protocol/Port number



13
14
15
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 13

def number
  @number
end

#protocolSymbol (readonly)

Returns L3/L4 protocol type.

Returns:

  • (Symbol)

    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

Note:

Using “Comparable” module, ‘==’ operator is defined by ‘<=>’ operator. But ‘==’ is overriden to compare instance equivalence instead of port number comparison.

Compare by port number

Parameters:

Returns:

  • (Fixnum)

    Compare with protocol/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

Parameters:

Returns:

  • (Boolean)

    Compare with protocol/port number



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)

Parameters:

  • number (Integer)

    Protocol No.

Raises:



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)

Parameters:

  • name (String)

    Protocol name.

Raises:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


105
106
107
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 105

def min?
  @number == 0
end

#name_to_numberObject (private)

Convert protocol/port name to number

Raises:



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_nameString (private)

Convert protocol/port number to string (its name)

Returns:

  • (String)

    Name of protocol/port number. If does not match the number in IOS proto/port literal, return number.to_s string



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_tableHash

This method is abstract.

Protocol Table

Returns:

  • (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_iInteger

Return protocol/port number

Returns:

  • (Integer)

    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_sString

Generate string for Cisco IOS access list

Returns:

  • (String)


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.

Parameters:

  • name (String)

    IP/TCP/UDP port/protocol name

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


61
62
63
# File 'lib/cisco_acl_intp/acespec_proto_base.rb', line 61

def valid_range?
  (0..@max_num).cover?(@number)
end