Class: OSPFv2::Link_Tlv

Inherits:
Object show all
Includes:
Common, Tlv, Tlv::Common
Defined in:
lib/lsa/tlv/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#ivar_to_klassname, #ivars, #set, #to_hash

Methods included from Tlv::Common

#stlv_len, #tlv_len, #to_hash

Methods included from Tlv

factory

Constructor Details

#initialize(arg = {}) ⇒ Link_Tlv

Returns a new instance of Link_Tlv.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lsa/tlv/link.rb', line 22

def initialize(arg={})
  @tlv_type = 2
  @tlvs = []
  if arg.is_a?(Hash) then
    if arg.has_key?(:tlvs)
      @tlvs = arg[:tlvs].collect { |h| SubTlv.factory(h) }
    end
  elsif arg.is_a?(String)
    __parse(arg)
  else
    raise ArgumentError, "Invalid argument", caller
  end
end

Instance Attribute Details

#_lengthObject (readonly)

Returns the value of attribute _length.



20
21
22
# File 'lib/lsa/tlv/link.rb', line 20

def _length
  @_length
end

#tlv_typeObject (readonly)

Returns the value of attribute tlv_type.



20
21
22
# File 'lib/lsa/tlv/link.rb', line 20

def tlv_type
  @tlv_type
end

#tlvsObject (readonly)

Returns the value of attribute tlvs.



20
21
22
# File 'lib/lsa/tlv/link.rb', line 20

def tlvs
  @tlvs
end

Instance Method Details

#<<(obj) ⇒ Object



84
85
86
# File 'lib/lsa/tlv/link.rb', line 84

def <<(obj)
  add(obj)
end

#[](klass) ⇒ Object



71
72
73
# File 'lib/lsa/tlv/link.rb', line 71

def [](klass)
  find(klass)
end

#__parse(s) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/lsa/tlv/link.rb', line 93

def __parse(s)
  @tlv_type, @_length, tlvs = s.unpack('nna*')
  while tlvs.size>0
    _, len = tlvs.unpack('nn')
    @tlvs << SubTlv.factory(tlvs.slice!(0,stlv_len(len)))
  end
end

#add(obj) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/lsa/tlv/link.rb', line 75

def add(obj)
  if obj.is_a?(OSPFv2::SubTlv)
    @tlvs << obj
  else
    raise
  end
  self
end

#encodeObject



88
89
90
91
# File 'lib/lsa/tlv/link.rb', line 88

def encode
  tlvs = encoded_tlvs
  [@tlv_type, tlvs.size, tlvs].pack('nna*')
end

#encoded_tlvsObject



101
102
103
# File 'lib/lsa/tlv/link.rb', line 101

def encoded_tlvs
  tlvs.collect { |tlv| tlv.encode }.join
end

#find(klass) ⇒ Object



44
45
46
# File 'lib/lsa/tlv/link.rb', line 44

def find(klass)
  tlvs.find { |a| a.is_a?(klass) }
end

#has?(klass = nil) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/lsa/tlv/link.rb', line 36

def has?(klass=nil)
  if klass.nil?
    return tlvs.collect { |tlv| tlv.class }
  else
    return tlvs.find { |tlv| tlv.is_a?(klass) }.nil? ? false : true
  end
end

#remove(klass) ⇒ Object



67
68
69
# File 'lib/lsa/tlv/link.rb', line 67

def remove(klass) 
  tlvs.delete_if { |a| a.is_a?(klass) }
end

#replace(*objs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lsa/tlv/link.rb', line 55

def replace(*objs)
  objs.each do |obj|  
    if has?(obj.class)
      index = __index(obj.class)
      tlvs[index]=obj
    else
      add(obj)
    end
  end
  self
end

#to_s(args = {}) ⇒ Object



114
115
116
117
118
# File 'lib/lsa/tlv/link.rb', line 114

def to_s(args={})
  ident = [' ']* (args[:ident] || 0)
  "Link TLV (#{tlv_type}), length: #{encoded_tlvs.size}" +
  ['',tlvs].flatten.collect { |tlv| tlv.to_s }.join("\n#{ident}")
end

#to_s_iosObject



109
110
111
112
# File 'lib/lsa/tlv/link.rb', line 109

def to_s_ios
  #TODO
  tlvs.collect { |tlv| tlv.to_s }.join("\n  ")
end