Changeset 218

Show
Ignore:
Timestamp:
08/07/08 22:57:34 (4 months ago)
Author:
pwh
Message:
  • Reworked the bridge model, adding plenty of validations.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nethorus/app/models/bridge.rb

    r183 r218  
    2929 
    3030  belongs_to :device 
    31   has_many :stp_ports, :dependent => :delete_all 
     31   
     32  has_many :stp_ports, 
     33           :dependent => :delete_all 
    3234 
    33   validates_presence_of :device_id, :dot1d_base_bridge_address 
     35  validates_presence_of :device_id, 
     36                        :message => "must be associated with a device" 
    3437 
    35   validates_format_of :dot1d_base_bridge_address, :with => /[0-9A-F]{12}/, :on => :create, :message => "Bridge base address must be a 12-character hexadecimal string" 
    36   validates_format_of :dot1d_base_bridge_address, :with => /[0-9A-F]{12}/, :on => :update, :message => "Bridge base address must be a 12-character hexadecimal string
     38  validates_presence_of :dot1d_base_bridge_address, 
     39                        :message => "must have a base bridge address set
    3740 
    38   validates_format_of :dot1d_stp_designated_root, :with => /\d{4}[0-9A-F]{12}/, :on => :create, :message => "STP designated root must be a priority and a 12-character hexadecimal string" 
    39   validates_format_of :dot1d_stp_designated_root, :with => /\d{4}[0-9A-F]{12}/, :on => :update, :message => "STP designated root must be a priority and a 12-character hexadecimal string" 
     41  validates_format_of :dot1d_base_bridge_address, 
     42                      :with => /[0-9A-F]{12}/, 
     43                      :message => "must be a 12-character hexadecimal string" 
    4044 
    41   validates_inclusion_of :dot1d_stp_priority, :in => 1..65535, :message => "must be in the range 1-65535" 
    42   validates_inclusion_of :dot1d_stp_root_cost, :in => 0..2147483647, :message => "must be in the range 0-2147483647" 
    43   validates_inclusion_of :dot1d_stp_max_age, :in => 1..2147483647, :message => "must be in the range 1-2147483647" 
    44   validates_inclusion_of :dot1d_stp_hello_time, :in => 1..2147483647, :message => "must be in the range 1-2147483647" 
    45   validates_inclusion_of :dot1d_stp_hold_time, :in => 1..2147483647, :message => "must be in the range 1-2147483647" 
    46   validates_inclusion_of :dot1d_stp_forward_delay, :in => 1..2147483647, :message => "must be in the range 1-2147483647" 
    47   validates_inclusion_of :dot1d_stp_bridge_max_age, :in => 600..4000, :message => "must be in the range 600-4000" 
    48   validates_inclusion_of :dot1d_stp_bridge_hello_time, :in => 100..1000, :message => "must be in the range 100-1000" 
     45  validates_numericality_of :dot1d_base_num_ports, 
     46                            :message => "must be a number" 
     47 
     48  validates_inclusion_of :dot1d_base_type, 
     49                         :in => %w( transparent-only ), 
     50                         :message => "is not a valid type" 
     51 
     52  validates_inclusion_of :dot1d_stp_protocol_specification, 
     53                         :in => %w( ieee8021d ), 
     54                         :message => "is not a valid protocol" 
     55 
     56  validates_inclusion_of :dot1d_stp_priority, 
     57                         :in => 1..65535, 
     58                         :message => "must be in the range 1-65535" 
     59 
     60  validates_format_of :dot1d_stp_designated_root, 
     61                      :with => /\d{4}[0-9A-F]{12}/, 
     62                      :message => "must be a priority and a 12-character hexadecimal string" 
     63                          
     64  validates_numericality_of :dot1d_stp_root_cost, 
     65                            :greater_than_or_equal_to => 0, 
     66                            :less_than_or_equal_to => 2147483647, 
     67                            :message => "must be in the range 0-2147483647" 
     68                          
     69  validates_numericality_of :dot1d_stp_max_age, 
     70                            :greater_than_or_equal_to => 1, 
     71                            :less_than_or_equal_to => 2147483647, 
     72                            :message => "must be in the range 1-2147483647" 
     73                          
     74  validates_numericality_of :dot1d_stp_hello_time, 
     75                            :greater_than_or_equal_to => 1, 
     76                            :less_than_or_equal_to => 2147483647, 
     77                            :message => "must be in the range 1-2147483647" 
     78 
     79  validates_numericality_of :dot1d_stp_hold_time, 
     80                            :greater_than_or_equal_to => 1, 
     81                            :less_than_or_equal_to => 2147483647, 
     82                            :message => "must be in the range 1-2147483647" 
     83                          
     84  validates_numericality_of :dot1d_stp_forward_delay, 
     85                            :greater_than_or_equal_to => 1, 
     86                            :less_than_or_equal_to => 2147483647, 
     87                            :message => "must be in the range 1-2147483647" 
     88                          
     89  validates_numericality_of :dot1d_stp_bridge_max_age, 
     90                            :greater_than_or_equal_to => 600, 
     91                            :less_than_or_equal_to => 4000, 
     92                            :message => "must be in the range 600-4000" 
     93                          
     94  validates_numericality_of :dot1d_stp_bridge_hello_time, 
     95                            :greater_than_or_equal_to => 100, 
     96                            :less_than_or_equal_to => 1000, 
     97                            :message => "must be in the range 100-1000" 
     98 
     99 
     100  # TODO: Some bridges set the timer values to seconds, not centi-seconds 
     101  # (1/100th of a second).  This is a hack to get around this problem. 
     102 
     103  def before_validation 
     104 
     105    if !self.dot1d_stp_max_age.nil? && self.dot1d_stp_max_age <= 99 
     106      logger.warn "dot1d_stp_max_age is #{self.dot1d_stp_max_age} - multiplying by 100" 
     107      self.dot1d_stp_max_age = self.dot1d_stp_max_age * 100 
     108    end       
     109 
     110    if !self.dot1d_stp_hello_time.nil? && self.dot1d_stp_hello_time <= 99 
     111      self.dot1d_stp_hello_time = self.dot1d_stp_hello_time * 100 
     112    end 
     113     
     114    if !self.dot1d_stp_hold_time.nil? && self.dot1d_stp_hold_time <= 99 
     115      self.dot1d_stp_hold_time = self.dot1d_stp_hold_time * 100 
     116    end 
     117     
     118    if !self.dot1d_stp_forward_delay.nil? && self.dot1d_stp_forward_delay <= 99 
     119      self.dot1d_stp_forward_delay = self.dot1d_stp_forward_delay * 100 
     120    end 
     121     
     122    if !self.dot1d_stp_bridge_max_age.nil? && self.dot1d_stp_bridge_max_age <= 99 
     123      self.dot1d_stp_bridge_max_age = self.dot1d_stp_bridge_max_age * 100 
     124    end 
     125     
     126    if !self.dot1d_stp_bridge_hello_time.nil? && self.dot1d_stp_bridge_hello_time <= 99 
     127      self.dot1d_stp_bridge_hello_time = self.dot1d_stp_bridge_hello_time * 100 
     128    end 
     129 
     130    if !self.dot1d_stp_bridge_forward_delay.nil? && self.dot1d_stp_bridge_forward_delay <= 99 
     131      self.dot1d_stp_bridge_forward_delay = self.dot1d_stp_bridge_forward_delay * 100 
     132    end 
     133 
     134  end 
    49135 
    50136 
     
    59145  end 
    60146 
     147 
     148  # Returns :true if this device is an STP root, or :false otherwise 
     149   
     150  def is_the_root_bridge? 
     151   
     152    return :true if self.dot1d_base_bridge_address == parse_stp_root_bridge_address(self.dot1d_stp_designated_root)[2] 
     153    return :false 
     154   
     155  end 
     156 
    61157end