Class: OSPFv2::Lsa_Base

Inherits:
Object show all
Includes:
Comparable, Common, Constant, TO_S
Defined in:
lib/lsa/lsa_base.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_Base

Returns a new instance of Lsa_Base.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lsa/lsa_base.rb', line 163

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



339
340
341
342
343
344
345
346
347
# File 'lib/lsa/lsa_base.rb', line 339

def method_missing(method, *args, &block)
  # puts "&&&&&  #{self.class}: method: #{method}"
  # p caller[0]
  if method == :to_s_junos
    :to_s_default
  else
    super
  end
end

Instance Attribute Details

#advertising_routerObject (readonly)

Returns the value of attribute advertising_router.



158
159
160
# File 'lib/lsa/lsa_base.rb', line 158

def advertising_router
  @advertising_router
end

#ls_ageObject (readonly)

Returns the value of attribute ls_age.



156
157
158
# File 'lib/lsa/lsa_base.rb', line 156

def ls_age
  @ls_age
end

#ls_idObject (readonly)

Returns the value of attribute ls_id.



157
158
159
# File 'lib/lsa/lsa_base.rb', line 157

def ls_id
  @ls_id
end

#ls_typeObject (readonly)

Returns the value of attribute ls_type.



156
157
158
# File 'lib/lsa/lsa_base.rb', line 156

def ls_type
  @ls_type
end

#optionsObject (readonly)

Returns the value of attribute options.



156
157
158
# File 'lib/lsa/lsa_base.rb', line 156

def options
  @options
end

#sequence_numberObject

Returns the value of attribute sequence_number.



159
160
161
# File 'lib/lsa/lsa_base.rb', line 159

def sequence_number
  @sequence_number
end

Class Method Details

.new_ntop(arg) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lsa/lsa_base.rb', line 127

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.

Raises:

  • (RuntimeError)


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/lsa/lsa_base.rb', line 278

def <=>(other)
  raise RuntimeError unless self.key == other.key
  if self.sequence_number < other.sequence_number
    # puts "*** jme: our lsa older than other: our seq less than other seq ***"        
    -1
  elsif self.sequence_number > other.sequence_number
    # puts "*** jme: our lsa newer than other: our seq greater than other seq ***"        
    +1
  else
    if self.csum_to_i < other.csum_to_i
      # puts "*** jme: our lsa older than other: our csum less than other csum ***"        
      -1
    elsif self.csum_to_i > other.csum_to_i
      # puts "*** jme: our lsa newer than other: our csum greater than other csum ***"        
      +1
    else
      if (self.ls_age != other.ls_age and other.ls_age >= MaxAge) or
        ((other.ls_age - self.ls_age) > OSPFv2::MaxAgeDiff)
        # puts "*** jme: our lsa newer than other: age diff < maxage diff: #{(other.ls_age - self.ls_age)} ***"
        +1
      else
        # puts "*** jme: same lsa: age diff < maxage diff: #{(other.ls_age - self.ls_age)} ***"
        0
      end
    end
  end
end

#ackObject

FIXME: when adding LSA in LSDB should be acked when init, rxmt otherwise .…



144
145
146
# File 'lib/lsa/lsa_base.rb', line 144

def ack
  @_rxmt_=false
end

#encode(content = '') ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/lsa/lsa_base.rb', line 239

def encode(content='')
  lsa = []
  lsa << encode_header
  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

#encode_headerObject Also known as: header_encode



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/lsa/lsa_base.rb', line 222

def encode_header
  header = []
  header << ls_age.encode
  header << [options.to_i].pack('C')
  header << ls_type.encode
  header << ls_id.encode
  header << advertising_router.encode
  header << sequence_number.encode
  header << [''].pack('a4')
  header.join
end

#encode_requestObject



249
250
251
252
253
254
255
# File 'lib/lsa/lsa_base.rb', line 249

def encode_request
  req=[]
  req << ls_id.encode
  req << ls_type.encode
  req << @advertising_router.encode
  req.join
end

#force_refresh(seqn) ⇒ Object



316
317
318
319
320
321
322
# File 'lib/lsa/lsa_base.rb', line 316

def force_refresh(seqn)
  @sequence_number = SequenceNumber.new(seqn) if seqn
  @sequence_number + 1
  @ls_age = LsAge.new
  retransmit
  self
end

#header_lsaObject



235
236
237
# File 'lib/lsa/lsa_base.rb', line 235

def header_lsa
  self.class.new self
end

#is_acked?Boolean Also known as: acked?

Returns:

  • (Boolean)


150
151
152
# File 'lib/lsa/lsa_base.rb', line 150

def is_acked?
  @_rxmt_ == false
end

#keyObject



269
270
271
# File 'lib/lsa/lsa_base.rb', line 269

def key
  [ls_type.to_i, ls_id.to_i, advertising_router.to_i]
end

#maxageObject



324
325
326
327
# File 'lib/lsa/lsa_base.rb', line 324

def maxage
  ls_age.maxage and retransmit
  self
end

#maxaged?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/lsa/lsa_base.rb', line 329

def maxaged?
  @ls_age.maxaged?
end

#parse(s) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/lsa/lsa_base.rb', line 257

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



306
307
308
309
310
311
312
313
314
# File 'lib/lsa/lsa_base.rb', line 306

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

#retransmitObject



147
148
149
# File 'lib/lsa/lsa_base.rb', line 147

def retransmit
  @_rxmt_=true
end

#to_hashObject



333
334
335
336
337
# File 'lib/lsa/lsa_base.rb', line 333

def to_hash
  h = super
  h.delete(:time)
  h
end

#to_s_defaultObject Also known as: to_s_dd



190
191
192
193
194
195
# File 'lib/lsa/lsa_base.rb', line 190

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_junosObject Also known as: to_s_junos_verbose



215
216
217
218
# File 'lib/lsa/lsa_base.rb', line 215

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_verboseObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/lsa/lsa_base.rb', line 198

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