Class: OSPFv2::LSDB::LinkStateDatabase

Inherits:
Object
  • Object
show all
Includes:
OSPFv2, Common, Ios
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, 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, PACKET_HEADER_LEN, ROUTER_LINK_P2P, ROUTER_LINK_STUB, ROUTER_LINK_TRANSIT, ROUTER_LINK_VL, ROUTER_LSA, SUMMARY_BASE_ADDRESS, SUMMARY_LSA, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ios

#_to_s_hdr_area_ios, #_to_s_hdr_as_external_ios, #_to_s_hdr_asbr_summary_ios, #_to_s_hdr_ios, #_to_s_hdr_network_ios, #_to_s_hdr_router_ios, #_to_s_hdr_summary_ios

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 = AreaId.new
  @advertised_routers= AdvertisedRouters.new
  @ls_refresh_interval=180
  @offset=0
  set arg
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

FIXME: should be moved to base 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 =>



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ls_db/link_state_database_links.rb', line 63

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



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

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

Instance Method Details

#<<(lsa) ⇒ Object



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

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

#[](*key) ⇒ Object



233
234
235
# File 'lib/ls_db/link_state_database.rb', line 233

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_arr(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, 
     :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,
      :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



241
242
243
# File 'lib/ls_db/link_state_database.rb', line 241

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

#all_proxiedObject



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

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

#eachObject



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

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

#find_asbr_sum(advertising_router) ⇒ Object



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

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

#find_router_lsa(router_id) ⇒ Object



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

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

#has?(obj) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/ls_db/link_state_database.rb', line 280

def has?(obj)
  lookup(obj)
end

#keysObject



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

def keys
  @ls_db.keys
end

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
# File 'lib/ls_db/link_state_database_links.rb', line 118

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


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

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


149
150
151
# File 'lib/ls_db/link_state_database_links.rb', line 149

def link_maxage
  #TODO implement link_maxage
end


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

def link_refresh
  #TODO implement link_refresh
end

Raises:

  • (ArgumentError)


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ls_db/link_state_database_links.rb', line 126

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



185
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
# File 'lib/ls_db/link_state_database.rb', line 185

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



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

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



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

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

#ls_refresh?(ls) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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
112
113
114
115
116
# File 'lib/ls_db/link_state_database_links.rb', line 87

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)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ls_db/link_state_database.rb', line 284

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


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ls_db/link_state_database.rb', line 260

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

FIXME: don’t use find_all ?????



246
247
248
# File 'lib/ls_db/link_state_database.rb', line 246

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

#refresh2(age) ⇒ Object



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

def refresh2(age)
  all.each {|l| l.refresh2(advertised_routers, age) }
  ''
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, rid))
    addr, source_address, plen, network, netmask = IPAddr.to_arr(prefix)
    rlsa.delete(:point_to_point,id2ip(neighbor_id))
    rlsa.delete(3,network)
  end
  rlsa
end

#resetObject



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

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

#router_id(*args) ⇒ Object

FIXME: 9/24/2014 ??????? check where this is used? should create an infinite loop?



56
57
58
# File 'lib/ls_db/link_state_database_links.rb', line 56

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

#sizeObject



237
238
239
# File 'lib/ls_db/link_state_database.rb', line 237

def size
  @ls_db.size
end

#to_hashObject



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

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(verbose = false) ⇒ Object



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

def to_s verbose=false
  _to_s '', verbose
end