Class: OSPFv2::Lsa
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
        show all
      
    
 
  
  
  
  
  
  
  
      - Includes:
 
      - Comparable, Common, Constant, TO_S
 
  
  
  
  
  
  
    - Defined in:
 
    - lib/lsa/lsa.rb,
  lib/lsa/lsa_factory.rb,
 lib/ls_db/link_state_database_links.rb
 
  
  
 
  
    
      Constant Summary
      collapse
    
    
      
        - AdvertisingRouter =
          
        
 
        Class.new(Id)
 
      
        - LsId =
          
        
 
        Class.new(Id)
 
      
        - LsAge =
          
        
 
        Class.new(LsAge)
 
      
    
  
  
  
  Constants included
     from Constant
  Constant::DATABASE_DESCRIPTION, Constant::HELLO, Constant::LINK_STATE_ACKNOWLEDGEMENT, Constant::LINK_STATE_REQUEST, Constant::LINK_STATE_UPDATE, Constant::V2, Constant::V3
  Instance Attribute Summary collapse
  
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from TO_S
  #to_s
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  Methods included from Common
  #ivar_to_klassname, #ivars, #set
  
  Constructor Details
  
    
  
  
    #initialize(arg = {})  ⇒ Lsa 
  
  
  
  
    
Returns a new instance of Lsa.
   
 
  
  
    
      
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 
     | 
    
      # File 'lib/lsa/lsa.rb', line 162
def initialize(arg={})
  arg = arg.dup
  @ls_age = LsAge.new
  @sequence_number = SequenceNumber.new
  @options = Options.new
  @ls_type = LsType.new klass_to_ls_type
  @ls_id = LsId.new
  @advertising_router = AdvertisingRouter.new
  @_length = 0
  @_rxmt_ = false
  
  if arg.is_a?(Hash)
    set arg
  elsif arg.is_a?(String)
    parse arg
  elsif arg.is_a?(self.class)
    parse arg.encode
  else        
    raise ArgumentError, "Invalid Argument: #{arg.inspect}"
  end
  
end
     | 
  
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(method, *args, &block)  ⇒ Object 
  
  
  
  
    
      
348
349
350
351
352
353
354
355
356 
     | 
    
      # File 'lib/lsa/lsa.rb', line 348
def method_missing(method, *args, &block)
  puts "Method missing in #{self.class}: method: #{method}"
  if method == :to_s_junos
    :to_s_default
  else
    super
  end
end
     | 
  
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #advertising_router  ⇒ Object  
  
  
  
  
    
Returns the value of attribute advertising_router.
   
 
  
  
    
      
157
158
159 
     | 
    
      # File 'lib/lsa/lsa.rb', line 157
def advertising_router
  @advertising_router
end 
     | 
  
 
    
      
      
      
  
  
    
Returns the value of attribute ls_age.
   
 
  
  
    
      
155
156
157 
     | 
    
      # File 'lib/lsa/lsa.rb', line 155
def ls_age
  @ls_age
end 
     | 
  
 
    
      
      
      
  
  
    
Returns the value of attribute ls_id.
   
 
  
  
    
      
156
157
158 
     | 
    
      # File 'lib/lsa/lsa.rb', line 156
def ls_id
  @ls_id
end 
     | 
  
 
    
      
      
      
  
  
    
Returns the value of attribute ls_type.
   
 
  
  
    
      
155
156
157 
     | 
    
      # File 'lib/lsa/lsa.rb', line 155
def ls_type
  @ls_type
end 
     | 
  
 
    
      
      
      
  
  
    
Returns the value of attribute options.
   
 
  
  
    
      
155
156
157 
     | 
    
      # File 'lib/lsa/lsa.rb', line 155
def options
  @options
end 
     | 
  
 
    
      
      
      
  
  
    #sequence_number  ⇒ Object 
  
  
  
  
    
Returns the value of attribute sequence_number.
   
 
  
  
    
      
158
159
160 
     | 
    
      # File 'lib/lsa/lsa.rb', line 158
def sequence_number
  @sequence_number
end 
     | 
  
 
    
   
  
    Class Method Details
    
      
    
      
  
  
    .new_ntop(arg)  ⇒ Object 
  
  
  
  
    
      
126
127
128
129
130
131
132
133
134
135
136 
     | 
    
      # File 'lib/lsa/lsa.rb', line 126
def new_ntop(arg)
  lsa = new
  if arg.is_a?(String)
    lsa.parse(arg)
  elsif arg.is_a?(self)
    lsa.parse arg.encode
  else
    raise ArgumentError, "Invalid Argument: #{arg.inspect}"
  end
  lsa
end
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #<=>(other)  ⇒ Object 
  
  
  
  
    
-1  self older than other
0  self equivalent to other
+1  self newer than other FIXME: rename to ‘newer’ TODO: compare advr router id.
   
 
  
    
      
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 
     | 
    
      # File 'lib/lsa/lsa.rb', line 287
def <=>(other)
  raise RuntimeError unless self.key == other.key
  if self.sequence_number < other.sequence_number
        -1
  elsif self.sequence_number > other.sequence_number
        +1
  else
    if self.csum_to_i < other.csum_to_i
            -1
    elsif self.csum_to_i > other.csum_to_i
            +1
    else
      if (self.ls_age != other.ls_age and other.ls_age >= MaxAge) or
        ((other.ls_age - self.ls_age) > OSPFv2::MaxAgeDiff)
                +1
      else
                0
      end
    end
  end
end
     | 
  
 
    
      
  
  
    
FIXME: when adding LSA in LSDB should be acked when init, rxmt otherwise .…
   
 
  
  
    
      
143
144
145 
     | 
    
      # File 'lib/lsa/lsa.rb', line 143
def ack
  @_rxmt_=false
end 
     | 
  
 
    
      
  
  
    #encode(content = '')  ⇒ Object 
  
  
  
  
    
      
248
249
250
251
252
253
254
255
256 
     | 
    
      # File 'lib/lsa/lsa.rb', line 248
def encode(content='')
  lsa = []
  lsa << 
  lsa << content
  lsa = lsa.join
  lsa[18..19]= @_size = [lsa.size].pack('n')
  lsa[16..17]= self.csum = cheksum(lsa[2..-1], 15).pack('CC')
  lsa
end
     | 
  
 
    
      
  
  
    
def to_s(*args)
return to_s_default(*args) unless defined?($style)
case $style
when :junos ; to_s_junos(*args)
when :junos_verbose ; to_s_junos_verbose(*args)
else
  to_s_default(*args)
end
end
   
 
  
  
    
      
231
232
233
234
235
236
237
238
239
240
241 
     | 
    
      # File 'lib/lsa/lsa.rb', line 231
def 
   = []
   << ls_age.encode
   << [options.to_i].pack('C')
   << ls_type.encode
   << ls_id.encode
   << advertising_router.encode
   << sequence_number.encode
   << [''].pack('a4')
  .join
end
     | 
  
 
    
      
  
  
    #encode_request  ⇒ Object 
  
  
  
  
    
      
258
259
260
261
262
263
264 
     | 
    
      # File 'lib/lsa/lsa.rb', line 258
def encode_request
  req=[]
  req << ls_id.encode
  req << ls_type.encode
  req << @advertising_router.encode
  req.join
end 
     | 
  
 
    
      
  
  
    #find_lsa_from_link(id)  ⇒ Object 
  
  
  
  
    
      
37
38
39 
     | 
    
      # File 'lib/ls_db/link_state_database_links.rb', line 37
def find_lsa_from_link(id)
  all.find_all { |l| l.lsdb_link_id==id  }
end
     | 
  
 
    
      
  
  
    #force_refresh(seqn)  ⇒ Object 
  
  
  
  
    
      
325
326
327
328
329
330
331 
     | 
    
      # File 'lib/lsa/lsa.rb', line 325
def force_refresh(seqn)
  @sequence_number = SequenceNumber.new(seqn) if seqn
  @sequence_number + 1
  @ls_age = LsAge.new
  retransmit
  self
end 
     | 
  
 
    
      
  
  
    
      
244
245
246 
     | 
    
      # File 'lib/lsa/lsa.rb', line 244
def 
  self.class.new self
end 
     | 
  
 
    
      
  
  
    #is_acked?  ⇒ Boolean 
  
  
    Also known as:
    acked?
    
  
  
  
    
      
149
150
151 
     | 
    
      # File 'lib/lsa/lsa.rb', line 149
def is_acked?
  @_rxmt_ == false
end 
     | 
  
 
    
      
  
  
    
      
278
279
280 
     | 
    
      # File 'lib/lsa/lsa.rb', line 278
def key
  [ls_type.to_i, ls_id.to_i, advertising_router.to_i]
end 
     | 
  
 
    
      
  
  
    #lsdb_link_id  ⇒ Object 
  
  
  
  
    
      
31
32
33 
     | 
    
      # File 'lib/ls_db/link_state_database_links.rb', line 31
def lsdb_link_id
  @_lsdb_link_id
end 
     | 
  
 
    
      
  
  
    #lsdb_link_id=(val)  ⇒ Object 
  
  
  
  
    
      
34
35
36 
     | 
    
      # File 'lib/ls_db/link_state_database_links.rb', line 34
def lsdb_link_id=(val)
  @_lsdb_link_id= val
end 
     | 
  
 
    
      
  
  
    
      
333
334
335
336 
     | 
    
      # File 'lib/lsa/lsa.rb', line 333
def maxage
  ls_age.maxage and retransmit
  self
end 
     | 
  
 
    
      
  
  
    #maxaged?  ⇒ Boolean 
  
  
  
  
    
      
338
339
340 
     | 
    
      # File 'lib/lsa/lsa.rb', line 338
def maxaged?
  @ls_age.maxaged?
end 
     | 
  
 
    
      
  
  
    
      
266
267
268
269
270
271
272
273
274
275
276 
     | 
    
      # File 'lib/lsa/lsa.rb', line 266
def parse(s)
  validate_checksum(s)
  ls_age, options, ls_type, ls_id, advr, seqn, csum, length, lsa = s.unpack('nCCNNNnna*')
  @ls_type = LsType.new ls_type
  @options = Options.new options
  @ls_age = LsAge.new ls_age
  @sequence_number = SequenceNumber.new seqn
  @advertising_router = AdvertisingRouter.new advr
  @ls_id = LsId.new ls_id
  lsa
end
     | 
  
 
    
      
  
  
    #refresh(advertised_routers, refresh_time, seqn = nil)  ⇒ Object 
  
  
  
  
    
      
315
316
317
318
319
320
321
322
323 
     | 
    
      # File 'lib/lsa/lsa.rb', line 315
def refresh(advertised_routers, refresh_time, seqn=nil)
  return unless advertised_routers.has?(advertising_router)
  return unless refresh?(refresh_time)
  @sequence_number = SequenceNumber.new(seqn) if seqn
  @sequence_number + 1
  @ls_age = LsAge.new
  retransmit
  self
end 
     | 
  
 
    
      
  
  
    #retransmit  ⇒ Object 
  
  
  
  
    
      
146
147
148 
     | 
    
      # File 'lib/lsa/lsa.rb', line 146
def retransmit
  @_rxmt_=true
end 
     | 
  
 
    
      
  
  
    
      
342
343
344
345
346 
     | 
    
      # File 'lib/lsa/lsa.rb', line 342
def to_hash
  h = super
  h.delete(:time)
  h
end 
     | 
  
 
    
      
  
  
    #to_s_default  ⇒ Object 
  
  
    Also known as:
    to_s_dd
    
  
  
  
    
      
189
190
191
192
193
194 
     | 
    
      # File 'lib/lsa/lsa.rb', line 189
def to_s_default
  len = encode.size
  ls_type_to_s = ls_type.to_sym.to_s.chomp('_lsa')
  sprintf("%-4.0d  0x%2.2x  %-8s  %-15.15s %-15.15s 0x%8.8x  0x%4.4x   %-7d", 
  ls_age.to_i, options.to_i, ls_type.to_s_short, ls_id.to_ip, advertising_router.to_ip, seqn.to_I,csum_to_i,len)
end
     | 
  
 
    
      
  
  
    #to_s_junos  ⇒ Object 
  
  
    Also known as:
    to_s_junos_verbose
    
  
  
  
    
      
214
215
216
217 
     | 
    
      # File 'lib/lsa/lsa.rb', line 214
def to_s_junos
  len = encode.size
  sprintf("%-7s %-1.1s%-15.15s  %-15.15s  0x%8.8x  %4.0d  0x%2.2x 0x%4.4x %3d", LsType.to_junos(ls_type.to_i), '', ls_id.to_ip, advertising_router.to_ip, seqn.to_I, ls_age.to_i, options.to_i, csum_to_i, len)
end
     | 
  
 
    
      
  
  
    #to_s_verbose  ⇒ Object 
  
  
  
  
    
      
197
198
199
200
201
202
203
204
205
206
207
208
209
210 
     | 
    
      # File 'lib/lsa/lsa.rb', line 197
def to_s_verbose
  len = encode.size
  s=[]
  s << self.class.to_s.split('::').last + ":"
  s << ls_age.to_s
  s << options.to_s
  s << ls_type.to_s
  s << advertising_router.to_s
  s << ls_id.to_s
  s << "SequenceNumber: " + sequence_number.to_s
  s << "LS checksum: #{format "%4x", csum_to_i}" if @_csum
  s << "length: #{@_size.unpack('n')}" if @_size
  s.join("\n   ")
end
     |