Class: Rbeapi::Api::Ospf

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/ospf.rb

Overview

The Ospf class is a global class that provides an instance for working with the node’s OSPF configuration.

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, #initialize, instance

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#add_network(pid, net, area) ⇒ Boolean

add_network adds network settings for router ospf and network area.



324
325
326
# File 'lib/rbeapi/api/ospf.rb', line 324

def add_network(pid, net, area)
  configure ["router ospf #{pid}", "network #{net} area #{area}"]
end

#create(pid) ⇒ Boolean

create will create a router ospf with the specified pid.



154
155
156
# File 'lib/rbeapi/api/ospf.rb', line 154

def create(pid)
  configure "router ospf #{pid}"
end

#delete(pid) ⇒ Boolean

delete will remove the specified router ospf.



164
165
166
# File 'lib/rbeapi/api/ospf.rb', line 164

def delete(pid)
  configure "no router ospf #{pid}"
end

#get(inst) ⇒ Hash

Returns the global OSPF configuration from the node.

rubocop:disable Metrics/MethodLength

Examples:

{
  router_id: <string>,
  max_lsa: <integer>,
  maximum_paths: <integer>,
  passive_interface_default <boolean>,
  passive_interfaces: array<string>,
  active_interfaces: array<string>,
  areas: {
    <string>: array<string>
  },
  redistribute: {
    <string>: {route_map: <string>}
  }
}


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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbeapi/api/ospf.rb', line 69

def get(inst)
  config = get_block("router ospf #{inst}")
  return nil unless config

  response = {}
  mdata = /(?<=^\s{3}router-id\s)(.+)$/.match(config)
  response[:router_id] = mdata.nil? ? '' : mdata[0]

  mdata = /(?<=^\s{3}max-lsa\s)(\d+)(?=.*$)/.match(config)
  response[:max_lsa] = mdata.nil? ? '' : mdata[0].to_i

  mdata = /(?<=^\s{3}maximum-paths\s)(\d+)$/.match(config)
  response[:maximum_paths] = mdata.nil? ? '' : mdata[0].to_i

  mdata = /^\s{3}passive-interface default$/ =~ config
  response[:passive_interface_default] = !mdata.nil?

  response[:passive_interfaces] =
    config.scan(/(?<=^\s{3}passive-interface\s)(?!default)(.*)$/)
          .flatten!.to_a

  response[:active_interfaces] =
    config.scan(/(?<=^\s{3}no passive-interface\s)(.*)$/).flatten!.to_a

  networks = config.scan(/^\s{3}network\s(.+)\sarea\s(.+)$/)
  areas = networks.each_with_object({}) do |cfg, hsh|
    net, area = cfg
    if hsh.include?(area)
      hsh[area] << net
    else
      hsh[area] = [net]
    end
  end
  response[:areas] = areas

  values = \
    config.scan(/(?<=^\s{3}redistribute\s)(\w+)[\s|$]*(route-map\s(.+))?/)

  response[:redistribute] = values.each_with_object({}) do |value, hsh|
    hsh[value[0]] = { route_map: value[2] }
  end
  response
end

#getallHash

Returns the OSPF configuration from the node as a Ruby hash.

Examples:

{
  <pid>: {
    router_id: <string>,
    max_lsa: <integer>,
    maximum_paths: <integer>,
    passive_interface_default <boolean>,
    passive_interfaces: array<string>,
    active_interfaces: array<string>,
    areas: {},
    redistribute: {}
  },
  interfaces: {}
}


133
134
135
136
137
138
139
140
# File 'lib/rbeapi/api/ospf.rb', line 133

def getall
  instances = config.scan(/(?<=^router\sospf\s)\d+$/)
  response = instances.each_with_object({}) do |inst, hsh|
    hsh[inst] = get inst
  end
  response[:interfaces] = interfaces.getall
  response
end

#interfacesObject



142
143
144
145
146
# File 'lib/rbeapi/api/ospf.rb', line 142

def interfaces
  @interfaces if @interfaces
  @interfaces = OspfInterfaces.new(node)
  @interfaces
end

#remove_network(pid, net, area) ⇒ Boolean

remove_network removes network settings for router ospf and network

area.


339
340
341
# File 'lib/rbeapi/api/ospf.rb', line 339

def remove_network(pid, net, area)
  configure ["router ospf #{pid}", "no network #{net} area #{area}"]
end

#set_active_interfaces(pid, opts = {}) ⇒ Boolean

set_active_interfaces sets router ospf no passive interface with pid and options, when passive interfaces default is configured.

default.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the active interface to



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rbeapi/api/ospf.rb', line 269

def set_active_interfaces(pid, opts = {})
  values = opts[:value]
  current = get(pid)[:active_interfaces]
  cmds = ["router ospf #{pid}"]
  current.each do |name|
    unless Array(values).include?(name)
      cmds << "passive-interface #{name}"
    end
  end
  Array(values).each do |name|
    cmds << "no passive-interface #{name}"
  end
  configure cmds
end

#set_max_lsa(pid, opts = {}) ⇒ Boolean

set_max_lsa sets router ospf max-lsa with pid and options.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the max-lsa to default.



200
201
202
203
204
# File 'lib/rbeapi/api/ospf.rb', line 200

def set_max_lsa(pid, opts = {})
  cmd = command_builder('max-lsa', opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end

#set_maximum_paths(pid, opts = {}) ⇒ Boolean

set_maximum_paths sets router ospf maximum-paths with pid and options.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the maximum-paths to default.



219
220
221
222
223
# File 'lib/rbeapi/api/ospf.rb', line 219

def set_maximum_paths(pid, opts = {})
  cmd = command_builder('maximum-paths', opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end

#set_passive_interface_default(pid, opts = {}) ⇒ Boolean

set_passive_interface_default sets router ospf passive-interface default with pid and options. If the passive-interface default keyword is false, then the passive-interface default is disabled. If the enable keyword is true, then the passive-interface default is enabled. If the default keyword is set to true, then the passive-interface default is configured using the default keyword. The default keyword takes precedence ver the enable keyword if both are provided.

to default.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the passive-interface default



246
247
248
249
250
251
252
# File 'lib/rbeapi/api/ospf.rb', line 246

def set_passive_interface_default(pid, opts = {})
  opts[:enable] = opts[:value] | false
  opts[:value] = nil
  cmd = command_builder('passive-interface default', opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end

#set_passive_interfaces(pid, opts = {}) ⇒ Boolean

set_passive_interfaces sets router ospf passive interface with pid and options.

default.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the passive interface to



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rbeapi/api/ospf.rb', line 299

def set_passive_interfaces(pid, opts = {})
  values = opts[:value]
  current = get(pid)[:passive_interfaces]
  cmds = ["router ospf #{pid}"]
  current.each do |name|
    unless Array(values).include?(name)
      cmds << "no passive-interface #{name}"
    end
  end
  Array(values).each do |name|
    cmds << "passive-interface #{name}"
  end
  configure cmds
end

#set_redistribute(pid, proto, opts = {}) ⇒ Boolean

set_redistribute sets router ospf router-id with pid and options.

Options Hash (opts):

  • routemap (String)

    The route-map value.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the router-id to default.



360
361
362
363
364
365
366
367
# File 'lib/rbeapi/api/ospf.rb', line 360

def set_redistribute(pid, proto, opts = {})
  routemap = opts[:route_map]
  redistribute = "redistribute #{proto}"
  redistribute << " route-map #{routemap}" if routemap
  cmd = command_builder(redistribute, opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end

#set_router_id(pid, opts = {}) ⇒ Boolean

set_router_id sets router ospf router-id with pid and options.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the router-id to default.



181
182
183
184
185
# File 'lib/rbeapi/api/ospf.rb', line 181

def set_router_id(pid, opts = {})
  cmd = command_builder('router-id', opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end