Class: OSPFv2::LSDB::LinkStateDatabase

Inherits:
Object
  • Object
show all
Includes:
OSPFv2, Common, TO_S
Defined in:
lib/ls_db/link_state_database.rb,
lib/ls_db/link_state_database_build.rb,
lib/ls_db/link_state_database_links.rb

Constant Summary collapse

AreaId =
Class.new(OSPFv2::Id)

Constants included from OSPFv2

ASBR_SUMMMARY_LSA, AllDRouters, AllSPFRouters, AttachRouter, CheckAge, DefaultDestination, EXTERNAL_BASE_ADDRESS, EXTERNAL_LSA, IPPROTO_OSPF, InitialSequenceNumber, OSPFv2::LINK_BASE_ADDRESS, OSPFv2::LSA_HEADER_LEN, OSPFv2::LSInfinity, OSPFv2::LSRefreshTime, MaxAge, MaxAgeDiff, MaxSequenceNumber, MinLSArrival, MinLSInterval, N, NETWORK_BASE_ADDRESS, NETWORK_LSA, NSSA_LSA, Netmask, NetworkMask, PACKET_HEADER_LEN, ROUTER_LINK_P2P, ROUTER_LINK_STUB, ROUTER_LINK_TRANSIT, ROUTER_LINK_VL, ROUTER_LSA, RouterId, SUMMARY_BASE_ADDRESS, SUMMARY_LSA, VERSION

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 = {}) ⇒ LinkStateDatabase

Returns a new instance of LinkStateDatabase.



96
97
98
99
100
101
102
103
# File 'lib/ls_db/link_state_database.rb', line 96

def initialize(arg={})
  @ls_db = Hash.new
  @area_id = nil
  @advertised_routers= AdvertisedRouters.new
  @ls_refresh_interval=180
  @offset=0
  set arg
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



264
265
266
# File 'lib/ls_db/link_state_database.rb', line 264

def method_missing(method, *args, &block)
  super
end

Instance Attribute Details

#advertised_routersObject (readonly)

Returns the value of attribute advertised_routers.



93
94
95
# File 'lib/ls_db/link_state_database.rb', line 93

def advertised_routers
  @advertised_routers
end

#area_idObject (readonly)

Returns the value of attribute area_id.



90
91
92
# File 'lib/ls_db/link_state_database.rb', line 90

def area_id
  @area_id
end

#ls_refresh_intervalObject

Returns the value of attribute ls_refresh_interval.



94
95
96
# File 'lib/ls_db/link_state_database.rb', line 94

def ls_refresh_interval
  @ls_refresh_interval
end

#offsetObject

Returns the value of attribute offset.



94
95
96
# File 'lib/ls_db/link_state_database.rb', line 94

def offset
  @offset
end

Class Method Details

.create(arg = {}) ⇒ Object

lsdb = LinkStateDatabase.create 10, 10, :prefix => ‘192.168.0.0/24’ lsdb = LinkStateDatabase.create 10, 10, :prefix_base => ‘192.168.0.0/24’, :router_id_base =>



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ls_db/link_state_database_links.rb', line 58

def self.create(arg={})
  arg = {:area_id=> 0, :columns=>2, :rows=>2, :base_router_id=> 0, :base_prefix=>'172.21.0.0/30'}.merge(arg)
  ls_db = new arg
  rows=arg[:rows]
  cols=arg[:columns]
  @base_router_id = arg[:base_router_id] 
  if arg[:base_prefix]
    Link.reset_ip_addr
    Link.base_ip_addr arg[:base_prefix]
  end
  @base_prefix = arg[:base_prefix]
  router_id = lambda { |c,r|  (r<<16) + c + @base_router_id }
  1.upto(rows) do |r|
    1.upto(cols) do |c|
      ls_db.new_link(:router_id=> router_id.call(c,r-1), :neighbor_id=> router_id.call(c,r)) if r > 1
      ls_db.new_link(:router_id=> router_id.call(c-1,r), :neighbor_id=> router_id.call(c,r)) if c > 1
    end
  end
  ls_db
end

.router_id(row, col, base_rid = ROUTER_ID_BASE) ⇒ Object



48
49
50
# File 'lib/ls_db/link_state_database_links.rb', line 48

def self.router_id(row, col, base_rid=ROUTER_ID_BASE)
  (row << 16) + col + base_rid      
end

Instance Method Details

#<<(lsa) ⇒ Object



147
148
149
150
151
# File 'lib/ls_db/link_state_database.rb', line 147

def <<(lsa)
  lsa = OSPFv2::Lsa.factory(lsa) unless lsa.is_a?(Lsa)
  @ls_db.store(lsa.key,lsa)
  lsa
end

#[](*key) ⇒ Object



252
253
254
# File 'lib/ls_db/link_state_database.rb', line 252

def [](*key)
  lookup(*key)
end

#add_adjacency(*arg) ⇒ Object Also known as: add_p2p_adjacency

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ls_db/link_state_database_build.rb', line 36

def add_adjacency(*arg)
  
  if arg.size==1 and arg[0].is_a?(Hash)
    arg = arg[0]
    router_id = arg[:router_id]
    neighbor_id = arg[:neighbor_router_id]
    prefix = arg[:prefix]
    metric = arg[:metric] ||= 1
    link = arg[:link] if arg[:link] and arg[:link].is_a?(Link)
  elsif arg.size>2
    router_id, neighbor_id, prefix, metric = arg
    metric ||=1
  else
    raise ArgumentError
  end
  
  raise ArgumentError, "missing prefix" unless prefix
  raise ArgumentError, "missing neighbor router id" unless neighbor_id

  _, addr, plen, network, netmask = IPAddr.to_ary(prefix)

  # if not set assume router id is the interface address given to us in :prefix
  router_id ||= addr
  

  # router_id to list of advertised routers
  advertised_routers + router_id

  rlsa = find_router_lsa router_id

  if ! rlsa

    # We need to build a router lsa
     rlsa = OSPFv2::Lsa.factory \
     :advertising_router=> router_id, 
     :ls_id=> router_id,
     :ls_type=>:router_lsa, 
     :options=> 0x22
     
    advertised_routers + router_id

    # add to lsdb
    self << rlsa
    
  else

    # delete any exisiting p2p and stub network 
    rlsa.delete(1,neighbor_id)
    rlsa.delete(3,network)

  end

  # Add a new router-link and stub network that describes the adjacency
  rlsa << { :link_id=>neighbor_id, :link_data=>addr, :router_link_type=>:point_to_point, :metric=>metric, }      
  rlsa << { :link_id=>network, :link_data=>netmask, :router_link_type=>:stub_network, :metric=>metric }

  rlsa

end

:router_id=> 1, :link_id=> ‘192.168.0.1’, :link_data => ‘255.255.255.255’ :router_id=> 1, :network=> ‘192.168.0.1/24’, :metric => 10

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ls_db/link_state_database_build.rb', line 109

def add_link_to_stub_network(arg={})

  router_id = arg[:router_id]
  link_id = arg[:link_id]
  link_data = arg[:link_data]
  
  if arg[:network]
    addr = IPAddr.new arg[:network]
    link_id = addr.to_s
    link_data = addr.netmask
  end

  raise ArgumentError, "missing neighbor router id" unless router_id
  raise ArgumentError, "missing neighbor link_id" unless link_id
  raise ArgumentError, "missing neighbor link_data" unless link_data

  metric = arg[:metric] ||=0
  
  # link = arg[:link] if arg[:link] and arg[:link].is_a?(RouterLink)

  rlsa = find_router_lsa(router_id)

  link =  {:router_link_type=>:stub_network, :metric=>metric, :link_id=>link_id, :link_data=>link_data}
  if ! rlsa

    # build a router lsa with a stub network
     rlsa = OSPFv2::Lsa.factory \
      :advertising_router=> router_id,
      :ls_id=> router_id,
      :nwveb=>2, 
      :ls_type=>:router_lsa,
      :options=> 0x22
    
    advertised_routers + router_id
    
    # add to lsdb
    self << rlsa
    
  end
  
  # replace or add new stub router link
  rlsa = find_router_lsa(router_id)
  rlsa.delete(3,link_id)
  rlsa  << link
  rlsa
end

#add_loopback(arg = {}) ⇒ Object

Router *13.11.13.11 13.11.13.11 0x80000032 16 0x22 0xf55e 48

bits 0x0, link count 1
id 99.99.1.1, data 255.255.255.255, Type Stub (3)
  Topology count: 0, Default metric: 0


173
174
175
# File 'lib/ls_db/link_state_database_build.rb', line 173

def add_loopback(arg={})
  add_link_to_stub_network :router_id => arg[:router_id], :link_id=> arg[:address], :link_data=> '255.255.255.255', :metric=> arg[:metric]
end

#add_router_id(router_id) ⇒ Object

Router ID: 13.11.13.11

Router *13.11.13.11 13.11.13.11 0x80000022 458 0x22 0x58e9 36

bits 0x0, link count 1
id 192.168.1.200, data 192.168.1.200, Type Transit (2)
  Topology count: 0, Default metric: 10


164
165
166
# File 'lib/ls_db/link_state_database_build.rb', line 164

def add_router_id(router_id)
  add_link_to_stub_network :router_id => router_id, :link_id=> router_id, :link_data=> router_id
end

#allObject Also known as: lsas



117
118
119
# File 'lib/ls_db/link_state_database.rb', line 117

def all
  @ls_db.values
end

#all_not_ackedObject



260
261
262
# File 'lib/ls_db/link_state_database.rb', line 260

def all_not_acked
 all.find_all { |l| ! l.ack? }
end

#all_proxiedObject



129
130
131
# File 'lib/ls_db/link_state_database.rb', line 129

def all_proxied
  @ls_db.values.find_all { |lsa| advertised_routers.has? lsa.advertising_router }
end

#eachObject



133
134
135
136
137
# File 'lib/ls_db/link_state_database.rb', line 133

def each
  @ls_db.values.each do |lsa|
    yield lsa
  end
end

#find_asbr_sum(advertising_router) ⇒ Object



182
183
184
# File 'lib/ls_db/link_state_database.rb', line 182

def find_asbr_sum(advertising_router)
  lookup(4,advertising_router)
end

#find_router_lsa(router_id) ⇒ Object



179
180
181
# File 'lib/ls_db/link_state_database.rb', line 179

def find_router_lsa(router_id)
  lookup(1,router_id)
end

#has?(obj) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/ls_db/link_state_database.rb', line 297

def has?(obj)
  lookup(obj)
end

#keysObject



143
144
145
# File 'lib/ls_db/link_state_database.rb', line 143

def keys
  @ls_db.keys
end

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
# File 'lib/ls_db/link_state_database_links.rb', line 113

def link(link, action)
  raise ArgumentError, "invalid argument: #{link.class}" unless link.is_a?(Link)
  case action
  when :up   ; link_up(link)
  when :down ; link_down(link)
  end
end


133
134
135
136
137
138
# File 'lib/ls_db/link_state_database_links.rb', line 133

def link_down(link)      
  lsa = @ls_db[link.local_lsa]
  lsa.delete(1, link.neighbor_id.to_ip)
  lsa = @ls_db[link.remote_lsa]
  lsa.delete(1, link.router_id.to_ip)
end


144
145
146
# File 'lib/ls_db/link_state_database_links.rb', line 144

def link_maxage

end


140
141
142
# File 'lib/ls_db/link_state_database_links.rb', line 140

def link_refresh

end

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ls_db/link_state_database_links.rb', line 121

def link_up(link)
  raise ArgumentError, "invalid argument: #{link.class}" unless link.is_a?(Link)
  add_adjacency :router_id=> link.router_id.to_i, 
                :neighbor_router_id => link.neighbor_id.to_i, 
                :prefix=> link.local_prefix, 
                :metric => link.metric 
  add_adjacency :router_id=> link.neighbor_id.to_i, 
                :neighbor_router_id => link.router_id.to_i, 
                :prefix=> link.remote_prefix, 
                :metric => link.metric      
end

#lookup(*args) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ls_db/link_state_database.rb', line 186

def lookup(*args)
  if args.size==1
    if args[0].is_a?(Array) and args[0].size==3
      if args[0][0].is_a?(Symbol)
        args[0][0] = LsType.to_i(args[0][0])
      end
      args[0][1] = id2i(args[0][1])
      args[0][2] = id2i(args[0][2])
      # lsdb.lookup([type,lsid,advr])
      # self[args[0]]
      @ls_db[args[0]]
    elsif args[0].is_a?(Lsa)
      # ls_db.lookup(lsa)
      @ls_db[args[0].key]
    else
      raise ArgumentError, "Invalid argument, #{args.inspect}"
    end
  elsif args.size==3
    # lsdb.lookup(type, lsid, advr)
    lookup(args)
  elsif args.size==2
    # lsdb.lookup(type, lsid, lsid)
    lookup([args[0],args[1],args[1]])
  else
    raise ArgumentError, "*** Invalid argument, #{args.inspect}"
  end
end

#ls_ack(lsa) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ls_db/link_state_database.rb', line 153

def ls_ack(lsa)
  
  lsa = lookup(lsa)
  if lsa
    if lsa.maxaged?
      @ls_db.delete(lsa.key)
    else
      @ls_db[lsa.key].ack
    end
  end
  
end

#ls_db=(arg) ⇒ Object



139
140
141
# File 'lib/ls_db/link_state_database.rb', line 139

def ls_db=(arg)
  [arg].flatten.each { |l| self << l }
end

#ls_refresh?(ls) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
# File 'lib/ls_db/link_state_database.rb', line 272

def ls_refresh?(ls)
  rt = ls_refresh_time
  ls.instance_eval { refresh?(rt) }
end

#ls_refresh_timeObject



105
106
107
# File 'lib/ls_db/link_state_database.rb', line 105

def ls_refresh_time
  @ls_refresh_time ||= LSRefreshTime
end

#ls_refresh_time=(val) ⇒ Object



109
110
111
# File 'lib/ls_db/link_state_database.rb', line 109

def ls_refresh_time=(val)
  @ls_refresh_time=val
end

link, :dir => :local_only link_hash, :direction => :remote_only



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ls_db/link_state_database_links.rb', line 82

def new_link(*args)

  # get a link object
  local, remote = true, true
  if args.size==1 and args[0].is_a?(Hash)
    link = Link.new args[0]
  elsif args[0].is_a?(Link)
    link = args[0]
  else
    raise ArgumentError, "invalid argument: #{args.inspect}"
  end
  
  # local, remote or both
  dir = args[0][:direction] ||= :both
  local = false  if dir and dir == :remote_only
  remote = false if dir and dir == :local_only
  
  if local
    lsa = add_p2p_adjacency :router_id => link.router_id.to_i, :neighbor_router_id => link.neighbor_id.to_i, :prefix => link.local_prefix, :metric=>1
    lsa.lsdb_link_id = link.id
    link.local_lsa = lsa.key
  end

  if remote
    lsa = add_p2p_adjacency :router_id => link.neighbor_id.to_i, :neighbor_router_id => link.router_id.to_i, :prefix => link.remote_prefix, :metric=>1
    lsa.lsdb_link_id = link.id
    link.remote_lsa = lsa.key
  end
  
end

#proxied?(router_id) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/ls_db/link_state_database.rb', line 113

def proxied?(router_id)
  advertised_routers.has?(router_id)
end

#recv_dd(dd, ls_req_list) ⇒ Object

Raises:

  • (ArgumentError)


302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/ls_db/link_state_database.rb', line 302

def recv_dd(dd, ls_req_list)
  raise ArgumentError, "lss nil" unless ls_req_list
  dd.each { |dd_lsa|
    if advertised_routers.has?(dd_lsa.advertising_router)
      our_lsa = lookup(dd_lsa)
      if our_lsa and (our_lsa <=> dd_lsa)
        our_lsa.force_refresh(dd_lsa.sequence_number)
      end
    else 
      ls_req_list.store(dd_lsa.key,0)
    end
  }
  nil
end


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ls_db/link_state_database.rb', line 277

def recv_link_state_update(link_state_update)
  link_state_update.each do |lsa|
    if advertised_routers.has?(lsa.advertising_router)
      if @ls_db.key? lsa.key
        @ls_db[lsa.key].force_refresh(lsa.sequence_number)
      else
        @ls_db.store(lsa.key,lsa)
        lsa.maxage
      end
    else
      if lsa.maxaged?
        @ls_db.delete lsa.key
      else
        @ls_db.store(lsa.key,lsa)
        # TBD: remove lsa from lsr_list
      end
    end
  end 
end

#refreshObject



215
216
217
# File 'lib/ls_db/link_state_database.rb', line 215

def refresh
  all.find_all {|l| l.refresh(ls_refresh_time) }
end

#remove_adjacency(rid, neighbor_id, prefix) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/ls_db/link_state_database_build.rb', line 98

def remove_adjacency(rid, neighbor_id, prefix)
  if (rlsa = lookup(:router_lsa, rid))
    addr, source_address, plen, network, netmask = IPAddr.to_ary(prefix)
    rlsa.delete(:point_to_point,id2ip(neighbor_id))
    rlsa.delete(3,network)
  end
  rlsa
end

#resetObject



219
220
221
222
# File 'lib/ls_db/link_state_database.rb', line 219

def reset
  each {|lsa| lsa.ack }
  @offset=0
end

#router_id(*args) ⇒ Object



52
53
54
# File 'lib/ls_db/link_state_database_links.rb', line 52

def router_id(*args)
  LinkStateDatabase.router_id(*args)
end

#sizeObject



256
257
258
# File 'lib/ls_db/link_state_database.rb', line 256

def size
  @ls_db.size
end

#to_hashObject



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ls_db/link_state_database.rb', line 166

def to_hash
  h= { :area=> @area_id.to_ip }
  h.store :ls_db, @ls_db.sort.collect {|p| p[1].to_hash }
  h.store :advertised_routers, advertised_routers.routers
  h.store :ls_refresh_time, ls_refresh_time
  h.store :ls_refresh_interval, ls_refresh_interval
  
  # h.store(:retransmit,@retransmit)
  # h.store(:ls_rxmt_interval,@ls_rxmt_interval)
  # h.store(:aging,self.aging)
  h
end

#to_s_defaultObject



224
225
226
227
228
229
230
231
232
# File 'lib/ls_db/link_state_database.rb', line 224

def to_s_default
  s = []
  s << "    OSPF link state database, Area #{area_id.to_ip}"
  s << "Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length"
   LsType.all.each do |type|
     s << (__send__ "all_#{type}").collect { |l| l.to_s  }
   end
  s.join("\n")
end

#to_s_junos(verbose = false) ⇒ Object Also known as: to_j



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ls_db/link_state_database.rb', line 234

def to_s_junos(verbose=false)
  lsas = []
  lsas << "    OSPF link state database, Area #{area_id.to_ip}"
  lsas << " Type       ID               Adv Rtr           Seq      Age  Opt  Cksum  Len "
   LsType.all.each do |type|
     lsas << "    OSPF AS SCOPE link state database"  if type == :external
     lsas << (__send__ "all_#{type}").collect { |l| 
       if verbose
         l.to_s_junos_verbose 
       else 
         l.to_s_junos
       end
     }
   end
  lsas.join("\n")
end